Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: simplify ModuleRouter.route
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Oct 11, 2021
1 parent d641cda commit 7e8078c
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions aries_staticagent/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def route(
"""

if not func_or_name:
# Empty @route case
return lambda f: cast(
RouteFunc,
self.route(
Expand All @@ -111,6 +112,7 @@ def route(
),
)
if isinstance(func_or_name, str):
# @route("msg_name") case
name = func_or_name
return lambda f: cast(
RouteFunc,
Expand All @@ -123,30 +125,35 @@ def route(
msg_type=msg_type,
),
)

# After the previous checks, the first positional argument must now be
# the method to decorate.
if not isinstance(func_or_name, Callable):
raise TypeError("func is not a callable")

# Func and name (if present) in expected parameters
func: Callable = func_or_name
if msg_type:
if isinstance(msg_type, str):
msg_type = MsgType(msg_type)
self._routes[MsgType(msg_type)] = func
return func

type_to_route = MsgType.unparse(
doc_uri=doc_uri or self.protocol.doc_uri or "",
protocol=protocol or self.protocol.protocol or "",
version=version or self.protocol.version or "",
name=name or func.__name__ or "",
)
self._routes[MsgType(type_to_route)] = func
type_to_route = msg_type
else:
type_to_route = MsgType.unparse(
doc_uri=doc_uri or self.protocol.doc_uri or "",
protocol=protocol or self.protocol.protocol or "",
version=version or self.protocol.version or "",
name=name or func.__name__ or "",
)

self._routes[type_to_route] = func
return func

def __call__(self, *args, **kwargs):
"""Route definition decorator."""
return self.route(*args, **kwargs)

def contextualize(self, context: object) -> Dict[MsgType, Callable]:
"""Return routes with handlers wrapped as partials to include 'self'."""
return {
msg_type: partial(handler, context) for msg_type, handler in self.items()
}
Expand All @@ -164,6 +171,7 @@ def __init__(self):

@property
def protocol_identifier(self) -> ProtocolIdentifier:
"""Parsed protocol identifier."""
return self._protocol_identifier

@property
Expand All @@ -173,14 +181,17 @@ def router(self) -> ModuleRouter:

@property
def doc_uri(self) -> str:
"""Protocol doc URI."""
return self.protocol_identifier.doc_uri

@property
def protocol_name(self) -> str:
"""Protocol name."""
return self.protocol_identifier.protocol

@property
def version(self) -> str:
"""Protocol version."""
return self.protocol_identifier.version

def type(
Expand Down

0 comments on commit 7e8078c

Please sign in to comment.