Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Added Default coroutine behaviour for wait_for, which by default ad…
Browse files Browse the repository at this point in the history
…ds a new empty coro, so that the args and kwargs can be accessed as returns.
  • Loading branch information
Luna-Klatzer committed Sep 3, 2021
1 parent 2405d5a commit a2c2b5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased

### Added
- Added parsers for events `on_message_create` and `on_message_delete`
- Added new property `room_ids` to `HivenClient`
- Parsers for events `on_message_create` and `on_message_delete`
- New property `room_ids` to `HivenClient`
- Default coroutine behaviour for `wait_for`, which by default adds a new empty
coro, so that the args and kwargs can be accessed as returns.
### Changed
- Moved cleanup from `HivenClient.close()` to `HivenClient.connect()` to
clean up even when the Client closes unexpectedly or due to an issue.
Expand All @@ -26,7 +28,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fixed `rooms` and `entities` property in `House`
- Fixed `mentions`, `author`, `house` and `attachment` property in `Message`
### Removed
- unnecessary reset of Client-User and the call of `init_client_user_obj` in
- Unnecessary reset of Client-User and the call of `init_client_user_obj` in
`ClientCache.closing_cleanup()`
- `log_websocket` from the `ClientCache`

Expand Down
26 changes: 18 additions & 8 deletions openhivenpy/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,13 @@ def set_awaitable(self, awaitable: Union[Awaitable, Callable]) -> None:
or standard asyncio awaitable functions are defined using the
`async def` syntax
"""
if inspect.iscoroutine(awaitable):
raise RuntimeError(
"Coroutine are not allowed as event_listener awaitable")
elif inspect.isawaitable(awaitable) or inspect.iscoroutinefunction(
awaitable):
if inspect.isawaitable(awaitable) \
or inspect.iscoroutinefunction(awaitable):
self._awaitable = awaitable
else:
raise RuntimeError(f"Expected awaitable, but got {type(awaitable)}")
raise RuntimeError(
f"Expected awaitable, but got {type(awaitable)}"
)

async def dispatch(self) -> None:
...
Expand All @@ -176,7 +175,9 @@ def __repr__(self):
('dispatched', self.dispatched),
('awaitable', getattr(self, 'awaitable', None))
]
return '<SingleDispatchEventListener {}>'.format(' '.join('%s=%s' % t for t in info))
return '<SingleDispatchEventListener {}>'.format(
' '.join('%s=%s' % t for t in info)
)

@property
def dispatched(self) -> bool:
Expand Down Expand Up @@ -208,7 +209,10 @@ async def dispatch(self, *args, **kwargs) -> None:
brief=f"[EVENTS] Ignoring exception in {repr(self)}:",
exc_info=sys.exc_info()
)
raise RuntimeError(f"Failed to execute assigned coroutine '{self.awaitable.__name__}'") from e
raise RuntimeError(
f"Failed to execute assigned coroutine "
f"'{self.awaitable.__name__}'"
) from e
self._dispatched = True
self.client.remove_listener(self)

Expand Down Expand Up @@ -384,6 +388,12 @@ async def wait_for(
"The passed event type is invalid/does not exist"
)

if awaitable is None:
async def _empty(*args, **kwargs):
...

awaitable = _empty

listener = self.add_single_listener(event_name, awaitable)
while not listener.dispatched:
await asyncio.sleep(.05)
Expand Down
2 changes: 1 addition & 1 deletion pytest/test_core/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def on_ready():

client.run(token)

def test_wait_for(self):
def test_wait_for_simple_coroutine(self):
async def on_ready():
print("\non_ready was called!")
return
Expand Down

0 comments on commit a2c2b5e

Please sign in to comment.