From 54af4176dd3de0adc2cdd1e819910e4897831598 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Fri, 3 Aug 2012 20:45:01 -0400 Subject: [PATCH] ENH: Add RequiredAttr to raise warnings when attr not set. --- skimage/viewer/plugins/base.py | 16 ++++------------ skimage/viewer/utils/core.py | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/skimage/viewer/plugins/base.py b/skimage/viewer/plugins/base.py index ee2cbb5d..562237b2 100644 --- a/skimage/viewer/plugins/base.py +++ b/skimage/viewer/plugins/base.py @@ -3,9 +3,10 @@ Base class for Plugins that interact with ImageViewer. """ from PyQt4 import QtGui from PyQt4.QtCore import Qt - import matplotlib as mpl +from ..utils import RequiredAttr + class Plugin(QtGui.QDialog): """Base class for plugins that interact with an ImageViewer. @@ -68,6 +69,7 @@ class Plugin(QtGui.QDialog): """ name = 'Plugin' + image_viewer = RequiredAttr("%s is not attached to ImageViewer" % name) draws_on_image = False def __init__(self, image_filter=None, height=0, width=400, useblit=None): @@ -92,18 +94,8 @@ class Plugin(QtGui.QDialog): self.cids = [] self.artists = [] - @property - def image_viewer(self): - if self._image_viewer is None: - raise RuntimeError("Plugin is not attached to ImageViewer") - return self._image_viewer - - @image_viewer.setter - def image_viewer(self, image_viewer): - self._image_viewer = image_viewer - def attach(self, image_viewer): - """Attach the plugin to an ImageViewer. + """Attach the plugin to an ImageViewer. Note that the ImageViewer will automatically call this method when the plugin is added to the ImageViewer. For example: diff --git a/skimage/viewer/utils/core.py b/skimage/viewer/utils/core.py index 81d66183..36476df8 100644 --- a/skimage/viewer/utils/core.py +++ b/skimage/viewer/utils/core.py @@ -5,7 +5,24 @@ from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg from PyQt4 import QtGui -__all__ = ['figimage', 'LinearColormap', 'ClearColormap', 'MatplotlibCanvas'] +__all__ = ['figimage', 'LinearColormap', 'ClearColormap', 'MatplotlibCanvas', + 'RequiredAttr'] + + +class RequiredAttr(object): + """A class attribute that must be set before use.""" + + def __init__(self, msg): + self.msg = msg + self.val = None + + def __get__(self, obj, objtype): + if self.val is None: + raise RuntimeError(self.msg) + return self.val + + def __set__(self, obj, val): + self.val = val def figimage(image, scale=1, dpi=None, **kwargs):