ENH: Add RequiredAttr to raise warnings when attr not set.

This commit is contained in:
Tony S Yu
2012-08-03 20:45:01 -04:00
parent ce017cc035
commit 54af4176dd
2 changed files with 22 additions and 13 deletions
+4 -12
View File
@@ -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:
+18 -1
View File
@@ -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):