From 14032f66ff29dae39bc574ba1987f4b63123b131 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Mon, 11 Jan 2021 09:44:25 +0100 Subject: [PATCH] Refactor to package specific directory --- example.py | 2 +- graphit/__init__.py | 0 graphit_event/__init__.py | 4 +++ graphit_event/interface.py | 27 +++++++++++++++++++ graphit/event.py => graphit_event/mixin.py | 31 +++------------------- 5 files changed, 35 insertions(+), 29 deletions(-) delete mode 100644 graphit/__init__.py create mode 100644 graphit_event/__init__.py create mode 100644 graphit_event/interface.py rename graphit/event.py => graphit_event/mixin.py (70%) diff --git a/example.py b/example.py index 203d1b1..e4839c6 100755 --- a/example.py +++ b/example.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from graphit.event import EventEmitterMixin +from graphit_event import EventEmitterMixin class ExampleEmitter(EventEmitterMixin): diff --git a/graphit/__init__.py b/graphit/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/graphit_event/__init__.py b/graphit_event/__init__.py new file mode 100644 index 0000000..b68f1b7 --- /dev/null +++ b/graphit_event/__init__.py @@ -0,0 +1,4 @@ +__all__ = ('EventEmitterInterface', 'EventEmitterMixin') + +from .interface import EventEmitterInterface +from .mixin import EventEmitterMixin diff --git a/graphit_event/interface.py b/graphit_event/interface.py new file mode 100644 index 0000000..8b18970 --- /dev/null +++ b/graphit_event/interface.py @@ -0,0 +1,27 @@ +__all__ = ('EventEmitterInterface') + +import abc +from typing import Hashable, Callable + + +class EventEmitterInterface(abc.ABC): + @abc.abstractmethod + def on(self, event: Hashable, callback: Callable) -> Hashable: + ''' + Registers the given callback for the given event. + Returns handle to unregister the given callback. + ''' + + @abc.abstractmethod + def off(self, handle: Hashable) -> bool: + ''' + Unregisters a previously registered callback by the given handle. + Returns True on success. + ''' + + @abc.abstractmethod + def _emit(self, event: Hashable, *args, **kwargs) -> None: + ''' + Emits the given event by calling all callbacks registered for this + event. + ''' diff --git a/graphit/event.py b/graphit_event/mixin.py similarity index 70% rename from graphit/event.py rename to graphit_event/mixin.py index 67c677e..971671c 100644 --- a/graphit/event.py +++ b/graphit_event/mixin.py @@ -1,39 +1,14 @@ -__all__ = ('EventEmitterInterface', 'EventEmitterMixin') +__all__ = ('EventEmitterMixin') - -import abc import uuid - from typing import Hashable, Callable, MutableMapping, Mapping +from .interface import EventEmitterInterface + EvMap = MutableMapping[Hashable, Hashable] CbMap = MutableMapping[Hashable, MutableMapping[Hashable, Callable]] -class EventEmitterInterface(abc.ABC): - - @abc.abstractmethod - def on(self, event: Hashable, callback: Callable) -> Hashable: - ''' - Registers the given callback for the given event. - Returns handle to unregister the given callback. - ''' - - @abc.abstractmethod - def off(self, handle: Hashable) -> bool: - ''' - Unregisters a previously registered callback by the given handle. - Returns True on success. - ''' - - @abc.abstractmethod - def _emit(self, event: Hashable, *args, **kwargs) -> None: - ''' - Emits the given event by calling all callbacks registered for this - event. - ''' - - class EventEmitterMixin(EventEmitterInterface): def on(self, event: Hashable, callback: Callable) -> Hashable: events: EvMap -- 2.34.1