Compare commits

...

1 Commits

Author SHA1 Message Date
Adam Hopkins
dcea7da2e1
Allow for simple signals 2023-08-30 12:48:04 +03:00

View File

@ -39,6 +39,7 @@ class Event(Enum):
RESERVED_NAMESPACES = { RESERVED_NAMESPACES = {
"sanic": ("sanic.__signal__.__init__",),
"server": ( "server": (
Event.SERVER_EXCEPTION_REPORT.value, Event.SERVER_EXCEPTION_REPORT.value,
Event.SERVER_INIT_AFTER.value, Event.SERVER_INIT_AFTER.value,
@ -65,6 +66,8 @@ RESERVED_NAMESPACES = {
), ),
} }
GENERIC_SIGNAL_FORMAT = "__generic__.__signal__.%s"
def _blank(): def _blank():
... ...
@ -89,11 +92,26 @@ class SignalRouter(BaseRouter):
self.allow_fail_builtin = True self.allow_fail_builtin = True
self.ctx.loop = None self.ctx.loop = None
@staticmethod
def format_event(event: str) -> str:
"""Ensure event strings in proper format
Args:
event (str): event string
Returns:
str: formatted event string
"""
if "." not in event:
event = GENERIC_SIGNAL_FORMAT % event
return event
def get( # type: ignore def get( # type: ignore
self, self,
event: str, event: str,
condition: Optional[Dict[str, str]] = None, condition: Optional[Dict[str, str]] = None,
): ):
event = self.format_event(event)
extra = condition or {} extra = condition or {}
try: try:
group, param_basket = self.find_route( group, param_basket = self.find_route(
@ -133,6 +151,7 @@ class SignalRouter(BaseRouter):
fail_not_found: bool = True, fail_not_found: bool = True,
reverse: bool = False, reverse: bool = False,
) -> Any: ) -> Any:
event = self.format_event(event)
try: try:
group, handlers, params = self.get(event, condition=condition) group, handlers, params = self.get(event, condition=condition)
except NotFound as e: except NotFound as e:
@ -195,6 +214,7 @@ class SignalRouter(BaseRouter):
inline: bool = False, inline: bool = False,
reverse: bool = False, reverse: bool = False,
) -> Union[asyncio.Task, Any]: ) -> Union[asyncio.Task, Any]:
event = self.format_event(event)
dispatch = self._dispatch( dispatch = self._dispatch(
event, event,
context=context, context=context,
@ -218,6 +238,7 @@ class SignalRouter(BaseRouter):
condition: Optional[Dict[str, Any]] = None, condition: Optional[Dict[str, Any]] = None,
exclusive: bool = True, exclusive: bool = True,
) -> Signal: ) -> Signal:
event = self.format_event(event)
event_definition = event event_definition = event
parts = self._build_event_parts(event) parts = self._build_event_parts(event)
if parts[2].startswith("<"): if parts[2].startswith("<"):