From b14514e0181975064ea23219d60dca790cf15428 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sat, 25 Aug 2012 12:06:03 -0400 Subject: [PATCH] BUG: Fix nosetest and autodoc errors when PyQt4 not available nose and autodoc imports the viewer modules so all PyQt4 imports must be wrapped in a try-except block. In addition, any classes derived from PyQt4 must be proxied since the class definition are run on import. This is really hacky. --- skimage/viewer/plugins/base.py | 17 +++++++++++++---- skimage/viewer/utils/core.py | 18 ++++++++++++++---- skimage/viewer/viewers/core.py | 9 +++++++-- skimage/viewer/widgets/core.py | 13 +++++++++---- skimage/viewer/widgets/history.py | 5 ++++- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/skimage/viewer/plugins/base.py b/skimage/viewer/plugins/base.py index fc2951b2..ae240c20 100644 --- a/skimage/viewer/plugins/base.py +++ b/skimage/viewer/plugins/base.py @@ -1,14 +1,23 @@ """ Base class for Plugins that interact with ImageViewer. """ -from PyQt4 import QtGui -from PyQt4.QtCore import Qt -import matplotlib as mpl +try: + from PyQt4 import QtGui + from PyQt4.QtCore import Qt + from PyQt4.QtGui import QDialog +except ImportError: + QDialog = object # hack to prevent nosetest and autodoc errors + print("Could not import PyQt4 -- skimage.viewer not available.") + +try: + import matplotlib as mpl +except ImportError: + print("Could not import matplotlib -- skimage.viewer not available.") from ..utils import RequiredAttr -class Plugin(QtGui.QDialog): +class Plugin(QDialog): """Base class for plugins that interact with an ImageViewer. A plugin connects an image filter (or another function) to an image viewer. diff --git a/skimage/viewer/utils/core.py b/skimage/viewer/utils/core.py index 36476df8..c311a1a1 100644 --- a/skimage/viewer/utils/core.py +++ b/skimage/viewer/utils/core.py @@ -1,8 +1,18 @@ import numpy as np -import matplotlib.pyplot as plt -from matplotlib.colors import LinearSegmentedColormap -from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg -from PyQt4 import QtGui + +try: + import matplotlib.pyplot as plt + from matplotlib.colors import LinearSegmentedColormap + from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg +except ImportError: + FigureCanvasQTAgg = object # hack to prevent nosetest and autodoc errors + LinearSegmentedColormap = object + print("Could not import matplotlib -- skimage.viewer not available.") + +try: + from PyQt4 import QtGui +except ImportError: + print("Could not import PyQt4 -- skimage.viewer not available.") __all__ = ['figimage', 'LinearColormap', 'ClearColormap', 'MatplotlibCanvas', diff --git a/skimage/viewer/viewers/core.py b/skimage/viewer/viewers/core.py index 1d7d3c3d..b3d0d260 100644 --- a/skimage/viewer/viewers/core.py +++ b/skimage/viewer/viewers/core.py @@ -3,7 +3,12 @@ ImageViewer class for viewing and interacting with images. """ import sys -from PyQt4 import QtGui, QtCore +try: + from PyQt4 import QtGui, QtCore + from PyQt4.QtGui import QMainWindow +except ImportError: + QMainWindow = object # hack to prevent nosetest and autodoc errors + print("Could not import PyQt4 -- skimage.viewer not available.") from ..utils import figimage, MatplotlibCanvas @@ -18,7 +23,7 @@ class ImageCanvas(MatplotlibCanvas): super(ImageCanvas, self).__init__(parent, self.fig, **kwargs) -class ImageViewer(QtGui.QMainWindow): +class ImageViewer(QMainWindow): """Viewer for displaying images. This viewer is a simple container object that holds a Matplotlib axes diff --git a/skimage/viewer/widgets/core.py b/skimage/viewer/widgets/core.py index 169a4401..0ae2d1e8 100644 --- a/skimage/viewer/widgets/core.py +++ b/skimage/viewer/widgets/core.py @@ -15,9 +15,14 @@ parameter type specified by its `ptype` attribute, which can be: property of the same name that updates the display. """ -from PyQt4.QtCore import Qt -from PyQt4 import QtGui -from PyQt4 import QtCore +try: + from PyQt4.QtCore import Qt + from PyQt4 import QtGui + from PyQt4 import QtCore + from PyQt4.QtGui import QWidget +except ImportError: + QWidget = object # hack to prevent nosetest and autodoc errors + print("Could not import PyQt4 -- skimage.viewer not available.") from ..utils import RequiredAttr @@ -25,7 +30,7 @@ from ..utils import RequiredAttr __all__ = ['BaseWidget', 'Slider', 'ComboBox'] -class BaseWidget(QtGui.QWidget): +class BaseWidget(QWidget): plugin = RequiredAttr("Widget is not attached to a Plugin.") diff --git a/skimage/viewer/widgets/history.py b/skimage/viewer/widgets/history.py index bb65b7dd..efc8a21c 100644 --- a/skimage/viewer/widgets/history.py +++ b/skimage/viewer/widgets/history.py @@ -1,7 +1,10 @@ import os from textwrap import dedent -from PyQt4 import QtGui +try: + from PyQt4 import QtGui +except ImportError: + print("Could not import PyQt4 -- skimage.viewer not available.") from skimage import io from .core import BaseWidget