diff --git a/skimage/__init__.py b/skimage/__init__.py index daac238c..23e5d3a7 100644 --- a/skimage/__init__.py +++ b/skimage/__init__.py @@ -38,8 +38,6 @@ util Utility Functions ----------------- -get_log - Returns the ``skimage`` log. Use this to print debug output. img_as_float Convert an image to floating point format, with values in [0, 1]. img_as_uint @@ -54,6 +52,7 @@ img_as_ubyte import os.path as _osp import imp as _imp import functools as _functools +from skimage._shared.utils import deprecated as _deprecated pkg_dir = _osp.abspath(_osp.dirname(__file__)) data_dir = _osp.join(pkg_dir, 'data') @@ -62,6 +61,7 @@ try: from .version import version as __version__ except ImportError: __version__ = "unbuilt-dev" +del version try: @@ -88,6 +88,57 @@ test_verbose = _functools.partial(test, verbose=True) test_verbose.__doc__ = test.__doc__ +from exceptions import Warning as _Warning + +class _Log(_Warning): + pass + +class _FakeLog(object): + def __init__(self, name): + """ + Parameters + ---------- + name : str + Name of the log. + repeat : bool + Whether to print repeating messages more than once (False by + default). + """ + self._name = name + + import warnings + warnings.simplefilter("always", _Log) + + self._warnings = warnings + + def _warn(self, msg, wtype): + self._warnings.warn('%s: %s' % (wtype, msg), _Log) + + def debug(self, msg): + self._warn(msg, 'DEBUG') + + def info(self, msg): + self._warn(msg, 'INFO') + + def warning(self, msg): + self._warn(msg, 'WARNING') + + warn = warning + + def error(self, msg): + self._warn(msg, 'ERROR') + + def critical(self, msg): + self._warn(msg, 'CRITICAL') + + def addHandler(*args): + pass + + def setLevel(*args): + pass + + +@_deprecated() def get_log(name=None): """Return a console logger. @@ -105,39 +156,12 @@ def get_log(name=None): http://docs.python.org/library/logging.html """ - import logging - if name is None: name = 'skimage' else: name = 'skimage.' + name - log = logging.getLogger(name) - return log + return _FakeLog(name) -def _setup_log(): - """Configure root logger. - - """ - import logging - import sys - - formatter = logging.Formatter( - '%(name)s: %(levelname)s: %(message)s' - ) - - try: - handler = logging.StreamHandler(stream=sys.stdout) - except TypeError: - handler = logging.StreamHandler(strm=sys.stdout) - handler.setFormatter(formatter) - - log = get_log() - log.addHandler(handler) - log.setLevel(logging.WARNING) - log.propagate = False - -_setup_log() - from .util.dtype import * diff --git a/skimage/_shared/utils.py b/skimage/_shared/utils.py index ea631716..18cd25c9 100644 --- a/skimage/_shared/utils.py +++ b/skimage/_shared/utils.py @@ -1,6 +1,7 @@ import warnings import functools import sys +from exceptions import Warning from . import six @@ -8,6 +9,14 @@ from . import six __all__ = ['deprecated', 'get_bound_method_class'] +class skimage_deprecation(Warning): + """Create our own deprecation class, since Python >= 2.7 + silences deprecations by default. + + """ + pass + + class deprecated(object): """Decorator to mark deprecated functions with warning. @@ -39,12 +48,14 @@ class deprecated(object): def wrapped(*args, **kwargs): if self.behavior == 'warn': func_code = six.get_function_code(func) + warnings.simplefilter('always', skimage_deprecation) + warnings.warn(msg, category=skimage_deprecation) warnings.warn_explicit(msg, - category=DeprecationWarning, + category=skimage_deprecation, filename=func_code.co_filename, lineno=func_code.co_firstlineno + 1) elif self.behavior == 'raise': - raise DeprecationWarning(msg) + raise skimage_deprecation(msg) return func(*args, **kwargs) # modify doc string to display deprecation warning diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 8daea421..3f85cc2b 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -1,12 +1,10 @@ from __future__ import division import numpy as np +from warnings import warn __all__ = ['img_as_float', 'img_as_int', 'img_as_uint', 'img_as_ubyte', 'img_as_bool', 'dtype_limits'] -from .. import get_log -log = get_log('dtype_converter') - dtype_range = {np.bool_: (False, True), np.bool8: (False, True), np.uint8: (0, 255), @@ -101,12 +99,12 @@ def convert(image, dtype, force_copy=False, uniform=False): raise ValueError("can not convert %s to %s." % (dtypeobj_in, dtypeobj)) def sign_loss(): - log.warn("Possible sign loss when converting negative image of type " - "%s to positive image of type %s." % (dtypeobj_in, dtypeobj)) + warn("Possible sign loss when converting negative image of type " + "%s to positive image of type %s." % (dtypeobj_in, dtypeobj)) def prec_loss(): - log.warn("Possible precision loss when converting from " - "%s to %s" % (dtypeobj_in, dtypeobj)) + warn("Possible precision loss when converting from " + "%s to %s" % (dtypeobj_in, dtypeobj)) def _dtype(itemsize, *dtypes): # Return first of `dtypes` with itemsize greater than `itemsize`