-
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.
Merge branch 'support/0.21.x' into async-run
- Loading branch information
Showing
16 changed files
with
280 additions
and
75 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
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,7 +1,11 @@ | ||
version: 2 | ||
|
||
build: | ||
os: ubuntu-22.04 | ||
tools: | ||
python: '3.8' | ||
|
||
python: | ||
version: 3.8 | ||
install: | ||
- method: pip | ||
path: . | ||
|
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
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.