Skip to content

Commit

Permalink
Updated Call, Invocation, Publisher and Publication to use kwargs-sty…
Browse files Browse the repository at this point in the history
…le options syntax
  • Loading branch information
Adam Jorgensen committed Nov 26, 2017
1 parent 007b618 commit 11b19b1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 120 deletions.
97 changes: 30 additions & 67 deletions opendna/autobahn/repl/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ def __getattr__(self, item) -> 'AbstractCall':


class AbstractCall(object):
def __init__(self, manager: AbstractCallManager,
procedure: str, on_progress: Callable=None,
timeout: Union[int, float, None]=None):
def __init__(self,
manager: AbstractCallManager,
procedure: str,
on_progress: Callable=None,
call_options_kwargs: dict=None):
assert isinstance(manager, AbstractCallManager)
self._manager = manager
self._procedure = procedure
self._on_progress = on_progress
self._timeout = timeout
self._call_options_kwargs = call_options_kwargs

@property
def manager(self) -> AbstractCallManager:
Expand All @@ -224,8 +226,8 @@ def on_progress(self) -> Optional[Callable]:
return self._on_progress

@property
def timeout(self) -> Union[int, float, None]:
return self._timeout
def call_options_kwargs(self) -> Optional[dict]:
return self._call_options_kwargs

def __call__(self, *args, **kwargs) -> 'AbstractInvocation':
raise NotImplementedError
Expand All @@ -238,12 +240,13 @@ def __getattr__(self, item) -> 'AbstractInvocation':


class AbstractInvocation(object):
def __init__(self, call: 'AbstractCall', args: Iterable,
def __init__(self,
call: 'AbstractCall',
args: Iterable,
kwargs: Dict[str, Any]):
self._call = call
self._args = args
self._kwargs = kwargs
self._progress = []
self._result = None
self._exception = None

Expand All @@ -260,7 +263,7 @@ def exception(self) -> Optional[Exception]:
return self._exception

def _default_on_progress(self, value):
self._progress.append(value)
raise NotImplementedError

async def _invoke(self):
raise NotImplementedError
Expand All @@ -285,8 +288,11 @@ def __getattr__(self, item) -> 'AbstractRegistration':


class AbstractRegistration(object):
def __init__(self, manager: AbstractRegistrationManager, procedure: str,
endpoint: Callable=None, prefix: str=None,
def __init__(self,
manager: AbstractRegistrationManager,
procedure: str,
endpoint: Callable=None,
prefix: str=None,
register_options_kwargs: dict=None):
assert isinstance(manager, AbstractRegistrationManager)
self._manager = manager
Expand Down Expand Up @@ -360,27 +366,11 @@ class AbstractPublisher(object):
def __init__(self,
manager: AbstractPublisherManager,
topic: str,
acknowledge: bool=None,
exclude_me: bool=None,
exclude: Union[int, List[int]]=None,
exclude_authid: Union[str, List[str]]=None,
exclude_authrole: Union[str, List[str]]=None,
eligible: Union[int, List[int]]=None,
eligible_authid: Union[str, List[str]]=None,
eligible_authrole: Union[str, List[str]]=None,
retain: bool=None):
publish_options_kwargs: dict=None):
assert isinstance(manager, AbstractPublisherManager)
self._manager = manager
self._topic = topic
self._acknowledge = acknowledge
self._exclude_me = exclude_me
self._exclude = exclude
self._exclude_authid = exclude_authid
self._exclude_authrole = exclude_authrole
self._eligible = eligible
self._eligible_authid = eligible_authid
self._eligible_authrole = eligible_authrole
self._retain = retain
self._publish_options_kwargs = publish_options_kwargs

@property
def manager(self) -> AbstractPublisherManager:
Expand All @@ -391,40 +381,8 @@ def topic(self) -> str:
return self._topic

@property
def acknowledge(self):
return self._acknowledge

@property
def exclude_me(self) -> bool:
return self._exclude_me

@property
def exclude(self) -> Union[int, List[int]]:
return self._exclude

@property
def exclude_authid(self) -> Union[str, List[str]]:
return self._exclude_authid

@property
def exclude_authrole(self) -> Union[str, List[str]]:
return self._exclude_authrole

@property
def eligible(self) -> Union[int, List[int]]:
return self._eligible

@property
def eligible_authid(self) -> Union[str, List[str]]:
return self._eligible_authid

@property
def eligible_authrole(self) -> Union[str, List[str]]:
return self._eligible_authrole

@property
def retain(self) -> bool:
return self._retain
def publish_options_kwargs(self) -> Optional[dict]:
return self._publish_options_kwargs

def __call__(self, *args, **kwargs) -> 'AbstractPublication':
raise NotImplementedError
Expand All @@ -437,8 +395,10 @@ def __getattr__(self, item) -> 'AbstractPublication':


class AbstractPublication(object):
def __init__(self, publisher: AbstractPublisher,
args: Iterable, kwargs: Dict[str, Any]):
def __init__(self,
publisher: AbstractPublisher,
args: Iterable,
kwargs: Dict[str, Any]):
assert isinstance(publisher, AbstractPublisher)
self._publisher = publisher
self._args = args
Expand Down Expand Up @@ -477,8 +437,11 @@ def __getattr__(self, item) -> 'AbstractSubscription':


class AbstractSubscription(object):
def __init__(self, manager: AbstractSubscriptionManager, topic: str,
handler: Callable=None, subscribe_options_kwargs: dict=None):
def __init__(self,
manager: AbstractSubscriptionManager,
topic: str,
handler: Callable=None,
subscribe_options_kwargs: dict=None):
self._manager = manager
self._topic = topic
self._handler = handler
Expand Down
56 changes: 14 additions & 42 deletions opendna/autobahn/repl/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,7 @@ def invoke(future: asyncio.Future):
async def _invoke(self):
topic = self._publisher.topic
try:
options = PublishOptions(
acknowledge=self._publisher.acknowledge,
exclude_me=self._publisher.exclude_me,
exclude=self._publisher.exclude,
exclude_authid=self._publisher.exclude_authid,
exclude_authrole=self._publisher.exclude_authrole,
eligible=self._publisher.eligible,
eligible_authid=self._publisher.eligible_authid,
eligible_authrole=self._publisher.eligible_authrole,
retain=self._publisher.retain
)
options = PublishOptions(**self._publisher.publish_options_kwargs)
session = self._publisher.manager.session.application_session
print(f'Publication to {topic} with name {self.name} starting')
self._result = session.publish(
Expand Down Expand Up @@ -114,21 +104,11 @@ def __call__(self, *new_args, **new_kwargs) -> AbstractPublication:
class Publisher(HasName, ManagesNames, AbstractPublisher):
def __init__(self, manager: Union[ManagesNames, AbstractPublisherManager],
topic: str,
acknowledge: bool=None,
exclude_me: bool=None,
exclude: Union[int, List[int]]=None,
exclude_authid: Union[str, List[str]]=None,
exclude_authrole: Union[str, List[str]]=None,
eligible: Union[int, List[int]]=None,
eligible_authid: Union[str, List[str]]=None,
eligible_authrole: Union[str, List[str]]=None,
retain: bool=None):
publish_options_kwargs: dict=None):
super().__init__(
manager=manager, topic=topic, acknowledge=acknowledge,
exclude_me=exclude_me, exclude=exclude,
exclude_authid=exclude_authid, exclude_authrole=exclude_authrole,
eligible=eligible, eligible_authid=eligible_authid,
eligible_authrole=eligible_authrole, retain=retain
manager=manager,
topic=topic,
publish_options_kwargs=publish_options_kwargs
)
self.__init_has_name__(manager)
self.__init_manages_names__()
Expand Down Expand Up @@ -168,24 +148,15 @@ def name_for(self, item):
@ManagesNames.with_name
def __call__(self,
topic: str,
acknowledge: bool=None,
exclude_me: bool=None,
exclude: Union[int, List[int]]=None,
exclude_authid: Union[str, List[str]]=None,
exclude_authrole: Union[str, List[str]]=None,
eligible: Union[int, List[int]]=None,
eligible_authid: Union[str, List[str]]=None,
eligible_authrole: Union[str, List[str]]=None,
retain: bool=None, *,
name: str=None) -> AbstractPublisher:
*,
name: str=None,
**publish_options_kwargs) -> AbstractPublisher:
print(f'Generating publish to {topic} with name {name}')
publisher_class = get_class(environ['publisher'])
publisher_class: AbstractPublisher = get_class(environ['publisher'])
publisher = publisher_class(
manager=self, topic=topic, acknowledge=acknowledge,
exclude_me=exclude_me, exclude=exclude,
exclude_authid=exclude_authid, exclude_authrole=exclude_authrole,
eligible=eligible, eligible_authid=eligible_authid,
eligible_authrole=eligible_authrole, retain=retain
manager=self,
topic=topic,
publish_options_kwargs=publish_options_kwargs
)
publisher_id = id(publisher)
self._items[publisher_id] = publisher
Expand Down Expand Up @@ -287,7 +258,8 @@ def name_for(self, item):
def __call__(self,
topic: str,
handler: Callable=None,
*, name: str=None,
*,
name: str=None,
**subscribe_options_kwargs) -> AbstractSubscription:
print(f'Generating subscription for {topic} with name {name}')
subscription_class = get_class(environ['subscription'])
Expand Down
34 changes: 23 additions & 11 deletions opendna/autobahn/repl/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(self,
super(Invocation, self).__init__(call=call, args=args, kwargs=kwargs)
self.__init_has_name__(call)
self.__init_has_future__()
self._progress = []

def invoke(future: asyncio.Future):
loop = call.manager.session.connection.manager.loop
Expand All @@ -71,9 +72,13 @@ def invoke(future: asyncio.Future):
# TODO: Fix this type confusion
call.manager.session.future.add_done_callback(invoke)

@property
def progress(self) -> list:
return self._progress

def _default_on_progress(self, value):
print(f'Invocation of {self._call.procedure} with name {self.name} has progress')
super()._default_on_progress(value)
self._progress.append(value)
if callable(self._call.on_progress):
self._call.on_progress(value)

Expand All @@ -82,7 +87,7 @@ async def _invoke(self):
try:
options = CallOptions(
on_progress=self._default_on_progress,
timeout=self._call.timeout
**self._call.call_options_kwargs
)
session = self._call.manager.session.application_session
print(f'Invocation of {procedure} with name {self.name} starting')
Expand Down Expand Up @@ -115,13 +120,17 @@ def __call__(self, *new_args, **new_kwargs) -> AbstractInvocation:


class Call(ManagesNames, AbstractCall):
def __init__(self, manager: AbstractCallManager,
procedure: str, on_progress: Callable=None,
timeout: Union[int, float, None]=None):
def __init__(self,
manager: AbstractCallManager,
procedure: str,
on_progress: Callable=None,
call_options_kwargs: dict=None):
self.__init_manages_names__()
super().__init__(
manager=manager, procedure=procedure, on_progress=on_progress,
timeout=timeout
manager=manager,
procedure=procedure,
on_progress=on_progress,
call_options_kwargs=call_options_kwargs
)
self._proxy = ManagesNamesProxy(self)

Expand Down Expand Up @@ -160,8 +169,9 @@ def name_for(self, item):
def __call__(self,
procedure: str,
on_progress: Callable=None,
timeout: Union[int, float]=None, *,
name: str=None) -> AbstractCall:
*,
name: str=None,
**call_options_kwargs) -> AbstractCall:
"""
Generates a Callable which can be called to initiate an asynchronous
request to the WAMP router this Session is connected to
Expand All @@ -177,8 +187,10 @@ def __call__(self,
print(f'Generating call to {procedure} with name {name}')
call_class = get_class(environ['call'])
call = call_class(
manager=self, procedure=procedure, on_progress=on_progress,
timeout=timeout
manager=self,
procedure=procedure,
on_progress=on_progress,
call_options_kwargs=call_options_kwargs
)
call_id = id(call)
self._items[call_id] = call
Expand Down

0 comments on commit 11b19b1

Please sign in to comment.