From 5c1e14658cc3f96b6ae311e15fab2b1f98fa8545 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 14 Mar 2021 09:54:24 +0200 Subject: [PATCH] Change where to condition --- sanic/app.py | 4 ++-- sanic/blueprints.py | 8 ++++---- sanic/mixins/signals.py | 8 ++++---- sanic/models/futures.py | 2 +- sanic/signals.py | 20 ++++++++++---------- tests/test_signals.py | 6 +++--- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index c984d324fd..338e22ceab 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -324,13 +324,13 @@ def dispatch( self, event: str, *, - where: Optional[Dict[str, str]] = None, + condition: Optional[Dict[str, str]] = None, context: Optional[Dict[str, Any]] = None, ) -> Coroutine[Any, Any, Awaitable[Any]]: return self.signal_router.dispatch( event, context=context, - where=where, + condition=condition, ) def event(self, event: str, timeout: Optional[Union[int, float]] = None): diff --git a/sanic/blueprints.py b/sanic/blueprints.py index d48860324a..27bdfd6f5e 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -225,7 +225,7 @@ def register(self, app, options): listeners[listener.event].append(app._apply_listener(listener)) for signal in self._future_signals: - signal.where.update({"blueprint": self.name}) + signal.condition.update({"blueprint": self.name}) app._apply_signal(signal) self.routes = [route for route in routes if isinstance(route, Route)] @@ -239,9 +239,9 @@ def register(self, app, options): self.listeners = dict(listeners) async def dispatch(self, *args, **kwargs): - where = kwargs.pop("where", {}) - where.update({"blueprint": self.name}) - kwargs["where"] = where + condition = kwargs.pop("condition", {}) + condition.update({"blueprint": self.name}) + kwargs["condition"] = condition await asyncio.gather( *[app.dispatch(*args, **kwargs) for app in self.apps] ) diff --git a/sanic/mixins/signals.py b/sanic/mixins/signals.py index 5756c990bb..2d34f838a3 100644 --- a/sanic/mixins/signals.py +++ b/sanic/mixins/signals.py @@ -22,7 +22,7 @@ def signal( event: str, *, apply: bool = True, - where: Dict[str, Any] = None, + condition: Dict[str, Any] = None, ) -> Callable[[SignalHandler], FutureSignal]: """ For creating a signal handler, used similar to a route handler: @@ -37,9 +37,9 @@ async def signal_handler(thing, **kwargs): :type event: str :param apply: For lazy evaluation, defaults to True :type apply: bool, optional - :param where: For use with the ``where`` argument in dispatch + :param condition: For use with the ``condition`` argument in dispatch filtering, defaults to None - :type where: Dict[str, Any], optional + :type condition: Dict[str, Any], optional """ def decorator(handler: SignalHandler): @@ -47,7 +47,7 @@ def decorator(handler: SignalHandler): nonlocal apply future_signal = FutureSignal( - handler, event, HashableDict(where or {}) + handler, event, HashableDict(condition or {}) ) self._future_signals.add(future_signal) diff --git a/sanic/models/futures.py b/sanic/models/futures.py index 592e727af0..f6371b4d15 100644 --- a/sanic/models/futures.py +++ b/sanic/models/futures.py @@ -56,4 +56,4 @@ class FutureStatic(NamedTuple): class FutureSignal(NamedTuple): handler: SignalHandler event: str - where: Optional[Dict[str, str]] + condition: Optional[Dict[str, str]] diff --git a/sanic/signals.py b/sanic/signals.py index 7e9a6a75be..27f6bfa443 100644 --- a/sanic/signals.py +++ b/sanic/signals.py @@ -39,9 +39,9 @@ def __init__(self) -> None: def get( # type: ignore self, event: str, - where: Optional[Dict[str, str]] = None, + condition: Optional[Dict[str, str]] = None, ): - extra = where or {} + extra = condition or {} try: return self.resolve(f".{event}", extra=extra) except NotFound: @@ -56,9 +56,9 @@ async def _dispatch( self, event: str, context: Optional[Dict[str, Any]] = None, - where: Optional[Dict[str, str]] = None, + condition: Optional[Dict[str, str]] = None, ) -> None: - signal, handlers, params = self.get(event, where=where) + signal, handlers, params = self.get(event, condition=condition) signal_event = signal.ctx.event signal_event.set() @@ -67,7 +67,7 @@ async def _dispatch( try: for handler in handlers: - if where is None or where == handler.__requirements__: + if condition is None or condition == handler.__requirements__: maybe_coroutine = handler(**params) if isawaitable(maybe_coroutine): await maybe_coroutine @@ -79,13 +79,13 @@ async def dispatch( event: str, *, context: Optional[Dict[str, Any]] = None, - where: Optional[Dict[str, str]] = None, + condition: Optional[Dict[str, str]] = None, ) -> asyncio.Task: task = self.ctx.loop.create_task( self._dispatch( event, context=context, - where=where, + condition=condition, ) ) await asyncio.sleep(0) @@ -95,7 +95,7 @@ def add( # type: ignore self, handler: SignalHandler, event: str, - where: Optional[Dict[str, Any]] = None, + condition: Optional[Dict[str, Any]] = None, ) -> Signal: parts = path_to_parts(event, self.delimiter) @@ -111,12 +111,12 @@ def add( # type: ignore else: name = event - handler.__requirements__ = where # type: ignore + handler.__requirements__ = condition # type: ignore return super().add( event, handler, - requirements=where, + requirements=condition, name=name, overwrite=True, ) # type: ignore diff --git a/tests/test_signals.py b/tests/test_signals.py index b877577b1b..a2e8669576 100644 --- a/tests/test_signals.py +++ b/tests/test_signals.py @@ -101,7 +101,7 @@ def sync_signal(baz): async def test_dispatch_signal_triggers_with_requirements(app): counter = 0 - @app.signal("foo.bar.baz", where={"one": "two"}) + @app.signal("foo.bar.baz", condition={"one": "two"}) def sync_signal(*_): nonlocal counter counter += 1 @@ -110,7 +110,7 @@ def sync_signal(*_): await app.dispatch("foo.bar.baz") assert counter == 0 - await app.dispatch("foo.bar.baz", where={"one": "two"}) + await app.dispatch("foo.bar.baz", condition={"one": "two"}) assert counter == 1 @@ -216,7 +216,7 @@ async def do_wait(): app.blueprint(bp) app.signal_router.finalize() signal, *_ = app.signal_router.get( - "foo.bar.baz", where={"blueprint": "bp"} + "foo.bar.baz", condition={"blueprint": "bp"} ) await bp.dispatch("foo.bar.baz")