-
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple support for delegates with `colour.utilities.Delegat…
…e` class.
- Loading branch information
Showing
6 changed files
with
256 additions
and
1 deletion.
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,73 @@ | ||
""" | ||
Delegate - Event Notifications | ||
============================== | ||
Define a delegate class for event notifications: | ||
- :class:`colour.utilities.Delegate` | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from colour.hints import Any, Callable, List | ||
|
||
__author__ = "Colour Developers" | ||
__copyright__ = "Copyright 2013 Colour Developers" | ||
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" | ||
__maintainer__ = "Colour Developers" | ||
__email__ = "[email protected]" | ||
__status__ = "Production" | ||
|
||
__all__ = ["Delegate"] | ||
|
||
|
||
class Delegate: | ||
""" | ||
Define a delegate allowing listeners to register and be notified of events. | ||
Methods | ||
------- | ||
- :meth:`~colour.utilities.Delegate.add_listener` | ||
- :meth:`~colour.utilities.Delegate.remove_listener` | ||
- :meth:`~colour.utilities.Delegate.notify` | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._listeners: List = [] | ||
|
||
def add_listener(self, listener: Callable) -> None: | ||
""" | ||
Add the given listener to the delegate. | ||
Parameters | ||
---------- | ||
listener | ||
Callable listening to the delegate notifications. | ||
""" | ||
|
||
if listener not in self._listeners: | ||
self._listeners.append(listener) | ||
|
||
def remove_listener(self, listener: Callable) -> None: | ||
""" | ||
Remove the given listener from the delegate. | ||
Parameters | ||
---------- | ||
listener | ||
Callable listening to the delegate notifications. | ||
""" | ||
|
||
if listener in self._listeners: | ||
self._listeners.remove(listener) | ||
|
||
def notify(self, *args: Any, **kwargs: Any) -> None: | ||
""" | ||
Notify the delegate listeners. | ||
""" | ||
|
||
for listener in self._listeners: | ||
listener(*args, **kwargs) |
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,57 @@ | ||
"""Define the unit tests for the :mod:`colour.utilities.delegate` module.""" | ||
|
||
from __future__ import annotations | ||
|
||
from colour.utilities import ( | ||
Delegate, | ||
) | ||
|
||
__author__ = "Colour Developers" | ||
__copyright__ = "Copyright 2013 Colour Developers" | ||
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause" | ||
__maintainer__ = "Colour Developers" | ||
__email__ = "[email protected]" | ||
__status__ = "Production" | ||
|
||
__all__ = [ | ||
"TestDelegate", | ||
] | ||
|
||
|
||
class TestDelegate: | ||
""" | ||
Define :class:`colour.utilities.structures.Delegate` class unit tests | ||
methods. | ||
""" | ||
|
||
def test_required_methods(self) -> None: | ||
"""Test the presence of required methods.""" | ||
|
||
required_methods = ("add_listener", "remove_listener", "notify") | ||
|
||
for method in required_methods: | ||
assert method in dir(Delegate) | ||
|
||
def test_Delegate(self) -> None: | ||
"""Test the :class:`colour.utilities.structures.Delegate` class.""" | ||
|
||
delegate = Delegate() | ||
|
||
data = [] | ||
|
||
def _listener(a: int) -> None: | ||
"""Define a unit tests listener.""" | ||
|
||
data.append(a) | ||
|
||
delegate.add_listener(_listener) | ||
|
||
delegate.notify("Foo") | ||
|
||
assert data == ["Foo"] | ||
|
||
delegate.remove_listener(_listener) | ||
|
||
delegate.notify("Bar") | ||
|
||
assert data == ["Foo"] |
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.