fsl.utils.notifier
¶
This module provides the Notifier
class, intended to be used
as a mixin for providing a simple notification API.
-
fsl.utils.notifier.
DEFAULT_TOPIC
= 'default'¶ Topic used when the caller does not specify one when registering, deregistering, or notifying listeners.
-
exception
fsl.utils.notifier.
Registered
[source]¶ Bases:
Exception
Exception
raised byNotifier.register()
when an attempt is made to register a listener with a name that is already registered.-
__module__
= 'fsl.utils.notifier'¶
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
fsl.utils.notifier.
_Listener
(name, callback, topic, runOnIdle)[source]¶ Bases:
object
This class is used internally by the
Notifier
class to store references to callback functions.-
__init__
(name, callback, topic, runOnIdle)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
property
callback
¶ Returns the callback function, or
None
if it has been garbage-collected.
-
__dict__
= mappingproxy({'__module__': 'fsl.utils.notifier', '__doc__': 'This class is used internally by the :class:`.Notifier` class to\n store references to callback functions.\n ', '__init__': <function _Listener.__init__>, 'callback': <property object>, '__str__': <function _Listener.__str__>, '__repr__': <function _Listener.__repr__>, '__dict__': <attribute '__dict__' of '_Listener' objects>, '__weakref__': <attribute '__weakref__' of '_Listener' objects>, '__annotations__': {}})¶
-
__module__
= 'fsl.utils.notifier'¶
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
fsl.utils.notifier.
Notifier
(*args, **kwargs)[source]¶ Bases:
object
The
Notifier
class is a mixin which provides simple notification capability. Listeners can be registered/deregistered to listen via theregister()
andderegister()
methods, and notified via thenotify()
method. Listeners can optionally listen on a specific topic, or be notified for all topics.Note
The
Notifier
class storesweakref
references to registered callback functions, using theWeakFunctionRef
class.-
static
__new__
(cls, *args, **kwargs)[source]¶ Initialises a dictionary of listeners on a new
Notifier
instance.
-
register
(name, callback, topic=None, runOnIdle=False)[source]¶ Register a listener with this
Notifier
.- Parameters
name – A unique name for the listener.
callback –
The function to call - must accept two positional arguments:
topic – Optional topic on which to listen for notifications.
runOnIdle – If
True
, this listener will be called on the main thread, via theidle.idle()
function. Otherwise this function will be called directly by thenotify()
method. Defaults toFalse
.
- Raises
A
Registered
error if a listener with the givenname
is already registered on the giventopic
.
-
deregister
(name, topic=None)[source]¶ De-register a listener that has been previously registered with this
Notifier
.- Parameters
name – Name of the listener to de-register.
topic – Topic on which the listener was registered.
-
isEnabled
(name, topic=None)[source]¶ Returns
True
if the specified listener is enabled,False
otherwise.
-
enableAll
(topic=None, state=True)[source]¶ Enable/disable all listeners for the specified topic.
- Parameters
topic – Topic to enable/disable listeners on. If
None
, all listeners are enabled/disabled.state – State to set listeners to.
-
disableAll
(topic=None)[source]¶ Disable all listeners for the specified topic (or
None
to disable all listeners).
-
isAllEnabled
(topic=None)[source]¶ Returns
True
if all listeners for the specified topic (or all listeners iftopic=None
) are enabled,False
otherwise.
-
skipAll
(topic=None)[source]¶ Context manager which disables all listeners for the specified topic, and restores their state before returning.
- Parameters
topic – Topic to skip listeners on. If
None
, notification is disabled for all topics.
-
skip
(name, topic=None)[source]¶ Context manager which disables the speciifed listener, and restores its state before returning.
You can use this method if you have some code which triggers a notification, but you do not your own listener to be notified. For example:
def __myListener(*a): pass notifier.register('myListener', __myListener) with notifier.skip('myListener'): # if a notification is triggered # by the code here, the __myListener # function will not be called.
- Parameters
name – Name of the listener to skip
topic – Topic or topics that the listener is registered on.
-
notify
(*args, **kwargs)[source]¶ Notify all registered listeners of this
Notifier
.The documented arguments must be passed as keyword arguments.
- Parameters
topic – The topic on which to notify. Default listeners are always notified, regardless of the specified topic.
value – A value passed through to the registered listener functions. If not provided, listeners will be passed a value of
None
.
All other arguments passed to this method are ignored.
Note
Listeners registered with
runOnIdle=True
are called viaidle.idle()
. Other listeners are called directly. Seeregister()
.
-
__getListeners
(topic)¶ Called by
notify()
. Returns all listeners which should be notified for the specifiedtopic
.
-
__dict__
= mappingproxy({'__module__': 'fsl.utils.notifier', '__doc__': 'The ``Notifier`` class is a mixin which provides simple notification\n capability. Listeners can be registered/deregistered to listen via the\n :meth:`register` and :meth:`deregister` methods, and notified via the\n :meth:`notify` method. Listeners can optionally listen on a specific\n *topic*, or be notified for all topics.\n\n .. note:: The ``Notifier`` class stores ``weakref`` references to\n registered callback functions, using the\n :class:`.WeakFunctionRef` class.\n ', '__new__': <staticmethod object>, 'register': <function Notifier.register>, 'deregister': <function Notifier.deregister>, 'enable': <function Notifier.enable>, 'disable': <function Notifier.disable>, 'isEnabled': <function Notifier.isEnabled>, 'enableAll': <function Notifier.enableAll>, 'disableAll': <function Notifier.disableAll>, 'isAllEnabled': <function Notifier.isAllEnabled>, 'skipAll': <function Notifier.skipAll>, 'skip': <function Notifier.skip>, 'notify': <function Notifier.notify>, '_Notifier__getListeners': <function Notifier.__getListeners>, '__dict__': <attribute '__dict__' of 'Notifier' objects>, '__weakref__': <attribute '__weakref__' of 'Notifier' objects>, '__annotations__': {}})¶
-
__module__
= 'fsl.utils.notifier'¶
-
__weakref__
¶ list of weak references to the object (if defined)
-
static