-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
ProcessListener
instances persistable (#277)
The `ProcessListener` is made persistable by deriving it, as well as the `EventHelper` class from `persistence.Savable`. The class `EventHelper` is moved to a new file because of a circular import that would result between the `utils` and `persistence` modules. There was a circular reference issue in the test listener that was storing a reference to the process inside it, making its serialization impossible. To fix the tests an ugly hack was used: storing the reference to the process outside the class in a global dict using id as keys. Some more ugly hacks were needed to correctly check the equality of two processes. Instances having different listeners should be ignored. Cherry-pick: 98a375f
- Loading branch information
Showing
8 changed files
with
206 additions
and
63 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
import logging | ||
from typing import TYPE_CHECKING, Any, Callable | ||
|
||
from . import persistence | ||
|
||
if TYPE_CHECKING: | ||
from typing import Set, Type | ||
|
||
from .process_listener import ProcessListener # pylint: disable=cyclic-import | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@persistence.auto_persist('_listeners', '_listener_type') | ||
class EventHelper(persistence.Savable): | ||
|
||
def __init__(self, listener_type: 'Type[ProcessListener]'): | ||
assert listener_type is not None, 'Must provide valid listener type' | ||
|
||
self._listener_type = listener_type | ||
self._listeners: 'Set[ProcessListener]' = set() | ||
|
||
def add_listener(self, listener: 'ProcessListener') -> None: | ||
assert isinstance(listener, self._listener_type), 'Listener is not of right type' | ||
self._listeners.add(listener) | ||
|
||
def remove_listener(self, listener: 'ProcessListener') -> None: | ||
self._listeners.discard(listener) | ||
|
||
def remove_all_listeners(self) -> None: | ||
self._listeners.clear() | ||
|
||
@property | ||
def listeners(self) -> 'Set[ProcessListener]': | ||
return self._listeners | ||
|
||
def fire_event(self, event_function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: | ||
"""Call an event method on all listeners. | ||
:param event_function: the method of the ProcessListener | ||
:param args: arguments to pass to the method | ||
:param kwargs: keyword arguments to pass to the method | ||
""" | ||
if event_function is None: | ||
raise ValueError('Must provide valid event method') | ||
|
||
# Make a copy of the list for iteration just in case it changes in a callback | ||
for listener in list(self.listeners): | ||
try: | ||
getattr(listener, event_function.__name__)(*args, **kwargs) | ||
except Exception as exception: # pylint: disable=broad-except | ||
_LOGGER.error("Listener '%s' produced an exception:\n%s", listener, exception) |
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
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
Oops, something went wrong.