From b729afa09cea8541fe0a459de6b82b37ebdd070f Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Wed, 14 Sep 2016 13:44:55 +0100 Subject: [PATCH] Adjusted required notifier callback signature - a value can now be passed through to listener functions. --- fsl/data/image.py | 10 +++++----- fsl/data/melodicimage.py | 2 +- fsl/utils/notifier.py | 32 +++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/fsl/data/image.py b/fsl/data/image.py index 17a4d5ab6..ba2ae2d78 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -598,7 +598,7 @@ class Image(Nifti, notifier.Notifier): :class:`.Notifier` interface) on the ``'dataRange'`` topic. """ if not self.__suppressDataRange: - self.notify(notifier_topic='dataRange') + self.notify(topic='dataRange') def calcRange(self, sizethres=None): @@ -693,7 +693,7 @@ class Image(Nifti, notifier.Notifier): self.__dataSource = filename self.__saveState = True - self.notify(notifier_topic='saveState') + self.notify(topic='saveState') def __getitem__(self, sliceobj): @@ -739,14 +739,14 @@ class Image(Nifti, notifier.Notifier): if values.size > 0: - self.notify(notifier_topic='data') + self.notify(topic='data') if self.__saveState: self.__saveState = False - self.notify(notifier_topic='saveState') + self.notify(topic='saveState') if not np.all(np.isclose(oldRange, newRange)): - self.notify(notifier_topic='dataRange') + self.notify(topic='dataRange') ALLOWED_EXTENSIONS = ['.nii.gz', '.nii', '.img', '.hdr', '.img.gz', '.hdr.gz'] diff --git a/fsl/data/melodicimage.py b/fsl/data/melodicimage.py index 5229d8bc2..0dada8dc9 100644 --- a/fsl/data/melodicimage.py +++ b/fsl/data/melodicimage.py @@ -106,7 +106,7 @@ class MelodicImage(fslimage.Image): self.__tr = val if oldval != val: - self.notify(notifier_topic='tr') + self.notify(topic='tr') def getComponentTimeSeries(self, component): diff --git a/fsl/utils/notifier.py b/fsl/utils/notifier.py index b05aebc3d..106161cdb 100644 --- a/fsl/utils/notifier.py +++ b/fsl/utils/notifier.py @@ -39,6 +39,7 @@ class Notifier(object): class, provided by the :mod:`props` package. """ + def __new__(cls, *args, **kwargs): """Initialises a dictionary of listeners on a new ``Notifier`` instance. @@ -57,13 +58,21 @@ class Notifier(object): """Register a listener with this ``Notifier``. :arg name: A unique name for the listener. - :arg callback: The function to call - must accept this ``Notifier`` - instance as its sole argument. + + :arg callback: The function to call - must accept two positional + arguments: + + - this ``Notifier`` instance. + + - A value, which may be ``None`` - see + :meth:`notify`. + :arg topic: Optional topic on which to listen for notifications. + :arg runOnIdle: If ``True``, this listener will be called on the main thread, via the :func:`.async.idle` function. Otherwise this function will be called directly by the - :meth:`notify` method. + :meth:`notify` method. Defaults to ``False``. """ if topic is None: @@ -132,9 +141,13 @@ class Notifier(object): The documented arguments must be passed as keyword arguments. - :args notifier_topic: The topic on which to notify. Default - listeners are always notified, regardless - of the specified topic. + :arg topic: The topic on which to notify. Default + listeners are always notified, regardless + of the specified topic. + + :arg 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. @@ -143,7 +156,8 @@ class Notifier(object): See :meth:`register`. """ - topic = kwargs.get('notifier_topic', DEFAULT_TOPIC) + topic = kwargs.get('topic', DEFAULT_TOPIC) + value = kwargs.get('value', None) listeners = [self.__listeners[topic]] if topic != DEFAULT_TOPIC: @@ -179,5 +193,5 @@ class Notifier(object): 'removing from list'.format(name)) ldict.pop(name) - elif runOnIdle: async.idle(callback, self) - else: callback(self) + elif runOnIdle: async.idle(callback, self, value) + else: callback( self, value) -- GitLab