From 520ee612065a95f881aa451700d7f707b4601926 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauld.mccarthy@gmail.com> Date: Sat, 3 Sep 2016 15:31:30 +0100 Subject: [PATCH] Using new props.suppress context manager. Also, Notifier.__new__ prints a warning if the sub-class is also a HasProperties, because both classes define a 'notify' method, which could be super confusing. --- fsl/data/melodiclabels.py | 44 +++++++++++++-------------------------- fsl/utils/notifier.py | 4 ++++ 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/fsl/data/melodiclabels.py b/fsl/data/melodiclabels.py index 49e9192d9..9ca546cae 100644 --- a/fsl/data/melodiclabels.py +++ b/fsl/data/melodiclabels.py @@ -88,14 +88,10 @@ class MelodicClassification(props.HasProperties): def clear(self): """Removes all labels from all components. """ - notifState = self.getNotificationState('labels') - self.disableNotification('labels') - - self.__components = {} - self.labels = [[] for i in range(self.__ncomps)] - - self.setNotificationState('labels', notifState) - self.notify('labels') + with props.suppress(self, 'labels', notify=True): + + self.__components = {} + self.labels = [[] for i in range(self.__ncomps)] def load(self, filename): @@ -130,15 +126,11 @@ class MelodicClassification(props.HasProperties): allLabels.append(['Unknown']) # Add the labels to this melclass object - notifState = self.getNotificationState('labels') - self.disableNotification('labels') + with props.suppress(self, 'labels', notify=True): - for i, labels in enumerate(allLabels): - for label in labels: - self.addLabel(i, label) - - self.setNotificationState('labels', notifState) - self.notify('labels') + for i, labels in enumerate(allLabels): + for label in labels: + self.addLabel(i, label) def save(self, filename): @@ -223,13 +215,9 @@ class MelodicClassification(props.HasProperties): labels = self.getLabels(component) - self.disableNotification('labels') - - for l in labels: - self.removeLabel(component, l) - - self.enableNotification('labels') - self.notify('labels') + with props.suppress(self, 'labels', notify=True): + for l in labels: + self.removeLabel(component, l) log.debug('Labels cleared from component: {}'.format(component)) @@ -262,13 +250,9 @@ class MelodicClassification(props.HasProperties): components = self.getComponents(label) - self.disableNotification('labels') - - for c in components: - self.removeComponent(label, c) - - self.enableNotification('labels') - self.notify('labels') + with props.suppress(self, 'labels', notify=True): + for c in components: + self.removeComponent(label, c) def loadLabelFile(filename, includeLabel=None, excludeLabel=None): diff --git a/fsl/utils/notifier.py b/fsl/utils/notifier.py index 5aef630df..b05aebc3d 100644 --- a/fsl/utils/notifier.py +++ b/fsl/utils/notifier.py @@ -46,6 +46,10 @@ class Notifier(object): new = object.__new__(cls) new.__listeners = collections.defaultdict(collections.OrderedDict) + if isinstance(new, props.HasProperties): + log.warning('Warning: {} is a sub-class of both ' + 'Notifier and props.HasProperties!') + return new -- GitLab