-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Garulf/improve-event-handler
Improve event handler
- Loading branch information
Showing
4 changed files
with
55 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,56 @@ | ||
import asyncio | ||
from typing import Any, Callable, Iterable, Type | ||
from typing import Any, Callable, Iterable, Type, Union | ||
|
||
|
||
class EventNotFound(Exception): | ||
|
||
def __init__(self, event: str): | ||
self.event = event | ||
super().__init__(f"Event '{event}' not found.") | ||
|
||
|
||
class EventHandler: | ||
|
||
def __init__(self): | ||
self._methods = {} | ||
self._events = {} | ||
self._handlers = {} | ||
|
||
def _get_callable_name(self, method: Callable[..., Any]): | ||
def _get_callable_name(self, method: Union[Callable[..., Any], Exception]): | ||
return getattr(method, '__name__', method.__class__.__name__).lower() | ||
|
||
def add_method(self, method: Callable[..., Any], *, name=None) -> str: | ||
key = name or self._get_callable_name(method) | ||
self._methods[key] = method | ||
def add_event(self, event: Callable[..., Any], *, name=None) -> str: | ||
key = name or self._get_callable_name(event) | ||
self._events[key] = event | ||
return key | ||
|
||
def add_methods(self, methods: Iterable[Callable[..., Any]]): | ||
for method in methods: | ||
self.add_method(method) | ||
def add_events(self, events: Iterable[Callable[..., Any]]): | ||
for event in events: | ||
self.add_event(event) | ||
|
||
def add_exception_handler(self, exception: Type[Exception], handler: Callable[..., Any]): | ||
self._handlers[exception] = handler | ||
|
||
async def __call__(self, method: str, *args, **kwargs): | ||
def get_event(self, event: str) -> Callable[..., Any]: | ||
try: | ||
return self._events[event] | ||
except KeyError: | ||
raise EventNotFound(event) | ||
|
||
async def _await_maybe(self, result: Any) -> Any: | ||
if asyncio.iscoroutine(result): | ||
return await result | ||
return result | ||
|
||
async def trigger_exception_handler(self, exception: Exception) -> Any: | ||
try: | ||
handler = self._handlers[exception.__class__] | ||
return await self._await_maybe(handler(exception)) | ||
except KeyError: | ||
raise exception | ||
|
||
async def trigger_event(self, event: str, *args, **kwargs) -> Any: | ||
try: | ||
result = self._methods[method](*args, **kwargs) | ||
if asyncio.iscoroutine(result): | ||
return await result | ||
return result | ||
result = self.get_event(event)(*args, **kwargs) | ||
return await self._await_maybe(result) | ||
except Exception as e: | ||
handler = self._handlers.get(type(e), None) | ||
if handler: | ||
return handler(e) | ||
raise e | ||
return await self.trigger_exception_handler(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters