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

@ -517,11 +517,11 @@ class Blueprint(BaseSanic):
)
def event(
self,
event: str,
timeout: Optional[Union[int, float]] = None,
*,
condition: Optional[Dict[str, Any]] = None,
self,
event: str,
timeout: Optional[Union[int, float]] = None,
*,
condition: Optional[Dict[str, Any]] = None,
):
"""Wait for a signal event to be dispatched.
@ -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,9 +104,10 @@ class SignalWaiter:
self.signal.ctx.waiters.remove(self)
def matches(self, event, condition):
return ((condition is None and not self.exclusive)
or (condition is None and not self.requirements)
or condition == self.requirements
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)
@ -278,12 +279,14 @@ class SignalRouter(BaseRouter):
return task
def get_waiter(
self,
event: Union[str, Enum],
condition: Optional[Dict[str, Any]],
exclusive: bool,
self,
event: Union[str, Enum],
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
@ -92,7 +91,7 @@ async def test_dispatch_signal_triggers_event(app):
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -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"
@ -157,7 +155,7 @@ async def test_dispatch_signal_with_enum_event_to_event(app):
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -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
@ -200,13 +197,12 @@ async def test_dispatch_signal_triggers_multiple_events(app):
assert event_task1.done()
assert event_task2.done()
event_task1.result() # Will raise if there was an exception
event_task2.result() # Will raise if there was an exception
event_task1.result() # Will raise if there was an exception
event_task2.result() # Will raise if there was an exception
@pytest.mark.asyncio
async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
@app.signal("foo.bar.baz")
def sync_signal(*_):
pass
@ -222,7 +218,7 @@ async def test_dispatch_signal_with_multiple_handlers_triggers_event_once(app):
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -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
@ -255,12 +250,11 @@ async def test_dispatch_signal_triggers_parameterized_dynamic_route_event(app):
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@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
@ -272,7 +266,7 @@ async def test_dispatch_signal_triggers_starred_dynamic_route_event(app):
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -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()
@ -309,7 +304,7 @@ async def test_dispatch_signal_to_event_with_requirements(app):
await app.dispatch("foo.bar.baz", condition={"one": "two"})
await asyncio.sleep(0)
assert event_task.done()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -331,24 +326,27 @@ 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.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()
event_task.result() # Will raise if there was an exception
event_task.result() # Will raise if there was an exception
@pytest.mark.asyncio
@ -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):