Skip to content

Commit

Permalink
feat(class_manager): wrapper level setters & getters
Browse files Browse the repository at this point in the history
previously everything, except js_ref and core methods, went to the
underlying js_ref.
  • Loading branch information
wiwichips committed Jul 23, 2024
1 parent 69ae9f8 commit c2585ea
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
8 changes: 4 additions & 4 deletions dcp/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .compute_for import compute_for_maker
from .job import Job
from .result_handle import ResultHandle
from .job import job_maker
from .result_handle import result_handle_maker

sub_classes = {
'Job': Job,
'ResultHandle': ResultHandle,
'Job': job_maker,
'ResultHandle': result_handle_maker,
}

__all__ = ['compute_for_maker', 'sub_classes']
Expand Down
35 changes: 18 additions & 17 deletions dcp/api/job.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import pythonmonkey as pm

def job_maker(super_class):
class Job(super_class):
#def exec(self, *args, **kwargs):
# # TODO change behaviour to match spec
# #print("overidden by Job hook")

class Job():
#def exec(self, *args, **kwargs):
# # TODO change behaviour to match spec
# #print("overidden by Job hook")
def wait(self, *args, **kwargs):
pass

def wait(self, *args, **kwargs):
pass

def on(self, *args):
if len(args) > 1 and callable(args[1]):
event_name = args[0]
event_cb = args[1]
self.js_ref.on(event_name, event_cb)
else:
event_name = args[0]
def decorator(fn):
self.js_ref.on(event_name, fn)
return decorator
def on(self, *args):
if len(args) > 1 and callable(args[1]):
event_name = args[0]
event_cb = args[1]
self.js_ref.on(event_name, event_cb)
else:
event_name = args[0]
def decorator(fn):
self.js_ref.on(event_name, fn)
return decorator
return Job

12 changes: 7 additions & 5 deletions dcp/api/result_handle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class ResultHandle():
def __str__(self):
return str(self.values())
def result_handle_maker(super_class):
class ResultHandle(super_class):
def __str__(self):
return str(self.values())

def __repr__(self):
return self.__str__()
def __repr__(self):
return self.__str__()
return ResultHandle

12 changes: 11 additions & 1 deletion dcp/dry/class_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def wrapper(*args, **kwargs):
object.__setattr__(self, 'aio', AsyncAttrs(self))

def __getattr__(self, name):
js_attr = self.js_ref[name]
js_attr = object.__getattribute__(self, 'js_ref')[name]

if isinstance(js_attr, pm.null.__class__):
return None
Expand All @@ -52,6 +52,14 @@ def __setattr__(self, name, value):
else:
self.js_ref[name] = value

def _wrapper_set_attribute(self, name, value):
"""Allows for attributes to be set on the proxy itself, not the js_ref."""
object.__setattr__(self, name, value)

def _wrapper_get_attribute(self, name):
"""Allows for attributes to be get on the proxy itself, not the js_ref."""
object.__getattribute__(self, name)

def __str__(self):
# Workaround required since PythonMonkey will encounter errors while str values
try:
Expand All @@ -63,6 +71,8 @@ def __str__(self):
'__init__': __init__,
'__getattr__': __getattr__,
'__setattr__': __setattr__,
'_wrapper_set_attribute': _wrapper_set_attribute, #TODO this is doing too much, should just be a proxy, not also have its own api...
'_wrapper_get_attribute': _wrapper_get_attribute, #TODO same as above
'__str__': __str__,
'get_js_class': staticmethod(lambda: js_class),
}
Expand Down
4 changes: 2 additions & 2 deletions dcp/dry/class_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def _find(self, cmp):
# classes to the registry... instead it also does the inheritance stuff. ):
def add(self, bfclass):
"""Registers a new BF2 Wrapper class, replaces with api subclasses."""
if sub_class := api.sub_classes.get(bfclass.__name__):
bfclass = type(bfclass.__name__, (bfclass,), dict(sub_class.__dict__))
if sub_class_maker := api.sub_classes.get(bfclass.__name__):
bfclass = sub_class_maker(bfclass)
self._list.append(bfclass)
return bfclass

Expand Down

0 comments on commit c2585ea

Please sign in to comment.