Prettified

This commit is contained in:
Josh Bartlett 2023-09-25 11:02:04 +10:00
parent 4d0a0a3570
commit 5fb7eaaeab
4 changed files with 45 additions and 42 deletions

View File

@ -724,7 +724,9 @@ class Sanic(
if self.config.EVENT_AUTOREGISTER:
self.signal_router.reset()
self.add_signal(None, event)
waiter = self.signal_router.get_waiter(event, condition, exclusive)
waiter = self.signal_router.get_waiter(
event, condition, exclusive
)
self.signal_router.finalize()
else:
raise NotFound("Could not find signal %s" % event)

View File

@ -541,7 +541,9 @@ class Blueprint(BaseSanic):
waiters = []
for app in self.apps:
waiter = app.signal_router.get_waiter(event, condition, exclusive=False)
waiter = app.signal_router.get_waiter(
event, condition, exclusive=False
)
if not waiter:
raise NotFound("Could not find signal %s" % event)
waiters.append(waiter)
@ -558,7 +560,7 @@ class Blueprint(BaseSanic):
task.cancel()
if not done:
raise TimeoutError()
finished_task, = done
(finished_task,) = done
return finished_task.result()
@staticmethod

View File

@ -104,7 +104,8 @@ class SignalWaiter:
self.signal.ctx.waiters.remove(self)
def matches(self, event, condition):
return ((condition is None and not self.exclusive)
return (
(condition is None and not self.exclusive)
or (condition is None and not self.requirements)
or condition == self.requirements
) and (self.trigger or event == self.event_definition)
@ -283,7 +284,9 @@ class SignalRouter(BaseRouter):
condition: Optional[Dict[str, Any]],
exclusive: bool,
):
event_definition = str(event.value) if isinstance(event, Enum) else event
event_definition = (
str(event.value) if isinstance(event, Enum) else event
)
name, trigger, _ = self._get_event_parts(event_definition)
signal = cast(Signal, self.name_index.get(name))
if not signal:

View File

@ -80,7 +80,6 @@ def test_invalid_signal(app, signal):
@pytest.mark.asyncio
async def test_dispatch_signal_triggers_event(app):
@app.signal("foo.bar.baz")
def sync_signal(*args):
pass
@ -142,7 +141,6 @@ async def test_dispatch_signal_with_enum_event(app):
@pytest.mark.asyncio
async def test_dispatch_signal_with_enum_event_to_event(app):
class FooEnum(Enum):
FOO_BAR_BAZ = "foo.bar.baz"
@ -185,7 +183,6 @@ async def test_dispatch_signal_triggers_multiple_handlers(app):
@pytest.mark.asyncio
async def test_dispatch_signal_triggers_multiple_events(app):
@app.signal("foo.bar.baz")
def sync_signal(*_):
pass
@ -206,7 +203,6 @@ async def test_dispatch_signal_triggers_multiple_events(app):
@pytest.mark.asyncio
async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
@app.signal("foo.bar.baz")
def sync_signal(*_):
pass
@ -243,7 +239,6 @@ async def test_dispatch_signal_triggers_dynamic_route(app):
@pytest.mark.asyncio
async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
@app.signal("foo.bar.<baz:int>")
def sync_signal(baz):
pass
@ -260,7 +255,6 @@ async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
@pytest.mark.asyncio
async def test_dispatch_signal_triggers_starred_dynamic_route_event(app):
@app.signal("foo.bar.<baz:int>")
def sync_signal(baz):
pass
@ -294,14 +288,15 @@ async def test_dispatch_signal_triggers_with_requirements(app):
@pytest.mark.asyncio
async def test_dispatch_signal_to_event_with_requirements(app):
@app.signal("foo.bar.baz")
def sync_signal(*_):
pass
app.signal_router.finalize()
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}))
event_task = asyncio.create_task(
app.event("foo.bar.baz", condition={"one": "two"})
)
await app.dispatch("foo.bar.baz")
await asyncio.sleep(0)
assert not event_task.done()
@ -331,20 +326,23 @@ async def test_dispatch_signal_triggers_with_requirements_exclusive(app):
@pytest.mark.asyncio
async def test_dispatch_signal_to_event_with_requirements_exclusive(app):
@app.signal("foo.bar.baz")
def sync_signal(*_):
pass
app.signal_router.finalize()
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False))
event_task = asyncio.create_task(
app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False)
)
await app.dispatch("foo.bar.baz")
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task = asyncio.create_task(app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False))
event_task = asyncio.create_task(
app.event("foo.bar.baz", condition={"one": "two"}, exclusive=False)
)
await app.dispatch("foo.bar.baz", condition={"one": "two"})
await asyncio.sleep(0)
assert event_task.done()
@ -368,7 +366,6 @@ async def test_dispatch_signal_triggers_with_context(app):
@pytest.mark.asyncio
async def test_dispatch_signal_to_event_with_context(app):
@app.signal("foo.bar.baz")
def sync_signal(**context):
pass
@ -379,7 +376,7 @@ async def test_dispatch_signal_to_event_with_context(app):
await app.dispatch("foo.bar.baz", context={"amount": 9})
await asyncio.sleep(0)
assert event_task.done()
assert event_task.result()['amount'] == 9
assert event_task.result()["amount"] == 9
@pytest.mark.asyncio
@ -399,7 +396,6 @@ async def test_dispatch_signal_triggers_with_context_fail(app):
@pytest.mark.asyncio
async def test_dispatch_signal_to_dynamic_route_event(app):
@app.signal("foo.bar.<something>")
def sync_signal(**context):
pass
@ -410,7 +406,7 @@ async def test_dispatch_signal_to_dynamic_route_event(app):
await app.dispatch("foo.bar.baz")
await asyncio.sleep(0)
assert event_task.done()
assert event_task.result()['something'] == "baz"
assert event_task.result()["something"] == "baz"
@pytest.mark.asyncio
@ -521,7 +517,7 @@ async def test_dispatch_signal_triggers_event_on_bp_with_context(app):
for _ in range(5):
await asyncio.sleep(0)
assert event_task.done()
assert event_task.result()['amount'] == 9
assert event_task.result()["amount"] == 9
def test_bad_finalize(app):