Skip to content

Commit

Permalink
You can bind to specific protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayBlagoev committed Apr 11, 2024
1 parent a627062 commit 10d1915
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 12 additions & 8 deletions deccom/protocols/abstractprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class AbstractProtocol(object):
bindings = dict({"_lower_start": "start", "_lower_sendto": "sendto", "datagram_received": "callback"})


def check_if_have(submodule, attr)->bool:

def check_if_have(submodule, attr, name = None)->bool:
if name != None and submodule.__class__.__name__ != name:
return AbstractProtocol.check_if_have(submodule.submodule,attr,name)
if not hasattr(submodule,attr):
if hasattr(submodule,"offers"):
if isinstance(submodule.offers, dict):
Expand All @@ -30,7 +31,7 @@ def check_if_have(submodule, attr)->bool:
if not hasattr(submodule,"submodule") or submodule.submodule == None:
return False
else:
return AbstractProtocol.check_if_have(submodule.submodule,attr)
return AbstractProtocol.check_if_have(submodule.submodule,attr,name)

else:
if submodule._taken.get(attr) != None:
Expand All @@ -40,7 +41,9 @@ def check_if_have(submodule, attr)->bool:


return True
def get_if_have(submodule: Any, attr)->bool:
def get_if_have(submodule: Any, attr, name = None)->bool:
if name != None and submodule.__class__.__name__ != name:
return AbstractProtocol.get_if_have(submodule.submodule,attr,name)
if not hasattr(submodule,attr):
if hasattr(submodule,"offers"):
if isinstance(submodule.offers, dict):
Expand All @@ -49,7 +52,7 @@ def get_if_have(submodule: Any, attr)->bool:
if not hasattr(submodule,"submodule") or submodule.submodule == None:
return None
else:
return AbstractProtocol.get_if_have(submodule.submodule,attr)
return AbstractProtocol.get_if_have(submodule.submodule,attr,name)
else:
return getattr(submodule,attr)
return None
Expand All @@ -63,8 +66,9 @@ async def send_datagram(self, msg: bytes, addr: tuple[str,int]):
await self._lower_sendto(self.uniqueid + msg, addr)


def set_if_have(submodule,attr,val):

def set_if_have(submodule,attr,val,name = None):
if name != None and submodule.__class__.__name__ != name:
return AbstractProtocol.set_if_have(submodule.submodule,attr,val,name)
if not hasattr(submodule,attr):
if hasattr(submodule,"offers"):
if isinstance(submodule.offers, dict):
Expand All @@ -78,7 +82,7 @@ def set_if_have(submodule,attr,val):
if not hasattr(submodule,"submodule") or submodule.submodule == None:
raise Exception("Cannot find any method to bind to",attr,"asked to be bound to",val)
else:
AbstractProtocol.set_if_have(submodule.submodule,attr,val)
AbstractProtocol.set_if_have(submodule.submodule,attr,val,name)
return
else:
if submodule._taken.get(attr) != None:
Expand Down
8 changes: 6 additions & 2 deletions deccom/protocols/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# cuz i am slim shady, the slimmiest of shadies, all you other limidadies are just imitating... idk i am not a wrapper

def bindto(attr):
def bindto(attr, of_class = None, share = False):
def outer(fn):

def inner(*args, **kwargs):
return fn(*args, **kwargs)

inner.bindto = attr
inner.share = share
inner.of_class = of_class
return inner

return outer
def bindfrom(attr):
def bindfrom(attr, of_class = None, share = False):
def outer(fn):

def inner(*args, **kwargs):
return fn(*args, **kwargs)

inner.bindfrom = attr
inner.share = share
inner.of_class = of_class
return inner
return outer
def nobind(fn):
Expand Down

0 comments on commit 10d1915

Please sign in to comment.