mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-29 01:11:56 +08:00
Move __init__ functions to submodules.
And rename functions so we don't have to alias imports.
This commit is contained in:
+5
-35
@@ -4,12 +4,7 @@ The following plug-ins are available:
|
||||
|
||||
"""
|
||||
|
||||
from ._plugins import use as use_plugin
|
||||
from ._plugins import available as plugins
|
||||
from ._plugins import info as plugin_info
|
||||
from ._plugins import configuration as plugin_order
|
||||
from ._plugins import reset_plugins as _reset_plugins
|
||||
|
||||
from ._plugins import *
|
||||
from .sift import *
|
||||
from .collection import *
|
||||
|
||||
@@ -18,34 +13,9 @@ from ._image_stack import *
|
||||
from .video import *
|
||||
|
||||
|
||||
available_plugins = plugins()
|
||||
reset_plugins()
|
||||
|
||||
|
||||
def _load_preferred_plugins():
|
||||
# Load preferred plugin for each io function.
|
||||
io_funcs = ['imsave', 'imshow', 'imread_collection', 'imread']
|
||||
preferred_plugins = ['matplotlib', 'pil', 'qt', 'freeimage', 'null']
|
||||
for func in io_funcs:
|
||||
for plugin in preferred_plugins:
|
||||
if plugin not in available_plugins:
|
||||
continue
|
||||
try:
|
||||
use_plugin(plugin, kind=func)
|
||||
break
|
||||
except (ImportError, RuntimeError, OSError):
|
||||
pass
|
||||
|
||||
# Use PIL as the default imread plugin, since matplotlib (1.2.x)
|
||||
# is buggy (flips PNGs around, returns bytes as floats, etc.)
|
||||
try:
|
||||
use_plugin('pil', 'imread')
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
def reset_plugins():
|
||||
_reset_plugins()
|
||||
_load_preferred_plugins()
|
||||
|
||||
def _update_doc(doc):
|
||||
"""Add a list of plugins to the module docstring, formatted as
|
||||
a ReStructuredText table.
|
||||
@@ -53,7 +23,9 @@ def _update_doc(doc):
|
||||
"""
|
||||
from textwrap import wrap
|
||||
|
||||
info = [(p, plugin_info(p)) for p in plugins() if not p == 'test']
|
||||
info = [(p, plugin_info(p)) for p in available_plugins if not p == 'test']
|
||||
print('test:', available_plugins, info)
|
||||
|
||||
col_1_len = max([len(n) for (n, _) in info])
|
||||
|
||||
wrap_len = 73
|
||||
@@ -75,5 +47,3 @@ def _update_doc(doc):
|
||||
return doc
|
||||
|
||||
__doc__ = _update_doc(__doc__)
|
||||
|
||||
reset_plugins()
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ from io import BytesIO
|
||||
import numpy as np
|
||||
import six
|
||||
|
||||
from skimage.io._plugins import call as call_plugin
|
||||
from skimage.io._plugins import call_plugin
|
||||
from skimage.color import rgb2grey
|
||||
|
||||
|
||||
|
||||
@@ -2,25 +2,26 @@
|
||||
|
||||
"""
|
||||
|
||||
__all__ = ['use', 'available', 'call', 'info', 'configuration', 'reset_plugins']
|
||||
|
||||
try:
|
||||
from configparser import ConfigParser
|
||||
from configparser import ConfigParser # Python 3
|
||||
except ImportError:
|
||||
from ConfigParser import ConfigParser
|
||||
from ConfigParser import ConfigParser # Python 2
|
||||
|
||||
import os.path
|
||||
from glob import glob
|
||||
|
||||
|
||||
plugin_store = None
|
||||
__all__ = ['use_plugin', 'call_plugin', 'plugin_info', 'plugin_order',
|
||||
'reset_plugins', 'find_available_plugins', 'available_plugins']
|
||||
|
||||
|
||||
plugin_store = None
|
||||
plugin_provides = {}
|
||||
plugin_module_name = {}
|
||||
plugin_meta_data = {}
|
||||
|
||||
|
||||
def reset_plugins():
|
||||
def _clear_plugins():
|
||||
"""Clear the plugin state to the default, i.e., where no plugins are loaded
|
||||
|
||||
"""
|
||||
@@ -30,8 +31,34 @@ def reset_plugins():
|
||||
'imshow': [],
|
||||
'imread_collection': [],
|
||||
'_app_show': []}
|
||||
_clear_plugins()
|
||||
|
||||
reset_plugins()
|
||||
|
||||
def _load_preferred_plugins():
|
||||
# Load preferred plugin for each io function.
|
||||
io_funcs = ['imsave', 'imshow', 'imread_collection', 'imread']
|
||||
preferred_plugins = ['matplotlib', 'pil', 'qt', 'freeimage', 'null']
|
||||
for func in io_funcs:
|
||||
for plugin in preferred_plugins:
|
||||
if plugin not in available_plugins:
|
||||
continue
|
||||
try:
|
||||
use_plugin(plugin, kind=func)
|
||||
break
|
||||
except (ImportError, RuntimeError, OSError):
|
||||
pass
|
||||
|
||||
# Use PIL as the default imread plugin, since matplotlib (1.2.x)
|
||||
# is buggy (flips PNGs around, returns bytes as floats, etc.)
|
||||
try:
|
||||
use_plugin('pil', 'imread')
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def reset_plugins():
|
||||
_clear_plugins()
|
||||
_load_preferred_plugins()
|
||||
|
||||
|
||||
def _scan_plugins():
|
||||
@@ -66,7 +93,40 @@ def _scan_plugins():
|
||||
_scan_plugins()
|
||||
|
||||
|
||||
def call(kind, *args, **kwargs):
|
||||
def find_available_plugins(loaded=False):
|
||||
"""List available plugins.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
loaded : bool
|
||||
If True, show only those plugins currently loaded. By default,
|
||||
all plugins are shown.
|
||||
|
||||
Returns
|
||||
-------
|
||||
p : dict
|
||||
Dictionary with plugin names as keys and exposed functions as
|
||||
values.
|
||||
|
||||
"""
|
||||
active_plugins = set()
|
||||
for plugin_func in plugin_store.values():
|
||||
for plugin, func in plugin_func:
|
||||
active_plugins.add(plugin)
|
||||
|
||||
d = {}
|
||||
for plugin in plugin_provides:
|
||||
if not loaded or plugin in active_plugins:
|
||||
d[plugin] = [f for f in plugin_provides[plugin] \
|
||||
if not f.startswith('_')]
|
||||
|
||||
return d
|
||||
|
||||
|
||||
available_plugins = find_available_plugins()
|
||||
|
||||
|
||||
def call_plugin(kind, *args, **kwargs):
|
||||
"""Find the appropriate plugin of 'kind' and execute it.
|
||||
|
||||
Parameters
|
||||
@@ -105,7 +165,7 @@ command. A list of all available plugins can be found using
|
||||
return func(*args, **kwargs)
|
||||
|
||||
|
||||
def use(name, kind=None):
|
||||
def use_plugin(name, kind=None):
|
||||
"""Set the default plugin for a specified operation. The plugin
|
||||
will be loaded if it hasn't been already.
|
||||
|
||||
@@ -158,35 +218,6 @@ def use(name, kind=None):
|
||||
plugin_store[k] = funcs
|
||||
|
||||
|
||||
def available(loaded=False):
|
||||
"""List available plugins.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
loaded : bool
|
||||
If True, show only those plugins currently loaded. By default,
|
||||
all plugins are shown.
|
||||
|
||||
Returns
|
||||
-------
|
||||
p : dict
|
||||
Dictionary with plugin names as keys and exposed functions as
|
||||
values.
|
||||
|
||||
"""
|
||||
active_plugins = set()
|
||||
for plugin_func in plugin_store.values():
|
||||
for plugin, func in plugin_func:
|
||||
active_plugins.add(plugin)
|
||||
|
||||
d = {}
|
||||
for plugin in plugin_provides:
|
||||
if not loaded or plugin in active_plugins:
|
||||
d[plugin] = [f for f in plugin_provides[plugin] \
|
||||
if not f.startswith('_')]
|
||||
|
||||
return d
|
||||
|
||||
|
||||
def _load(plugin):
|
||||
"""Load the given plugin.
|
||||
@@ -201,7 +232,7 @@ def _load(plugin):
|
||||
plugins : List of available plugins
|
||||
|
||||
"""
|
||||
if plugin in available(loaded=True):
|
||||
if plugin in find_available_plugins(loaded=True):
|
||||
return
|
||||
if not plugin in plugin_module_name:
|
||||
raise ValueError("Plugin %s not found." % plugin)
|
||||
@@ -222,7 +253,7 @@ def _load(plugin):
|
||||
store.append((plugin, func))
|
||||
|
||||
|
||||
def info(plugin):
|
||||
def plugin_info(plugin):
|
||||
"""Return plugin meta-data.
|
||||
|
||||
Parameters
|
||||
@@ -242,7 +273,7 @@ def info(plugin):
|
||||
raise ValueError('No information on plugin "%s"' % plugin)
|
||||
|
||||
|
||||
def configuration():
|
||||
def plugin_order():
|
||||
"""Return the currently preferred plugin order.
|
||||
|
||||
Returns
|
||||
|
||||
@@ -20,7 +20,7 @@ except RuntimeError:
|
||||
|
||||
|
||||
def setup_module(self):
|
||||
plugin.use('test') # see ../_plugins/test_plugin.py
|
||||
plugin.use_plugin('test') # see ../_plugins/test_plugin.py
|
||||
|
||||
|
||||
def teardown_module(self):
|
||||
@@ -41,37 +41,37 @@ class TestPlugin:
|
||||
io.imread_collection('*.png', conserve_memory=False, plugin='test')
|
||||
|
||||
def test_use(self):
|
||||
plugin.use('test')
|
||||
plugin.use('test', 'imshow')
|
||||
plugin.use_plugin('test')
|
||||
plugin.use_plugin('test', 'imshow')
|
||||
|
||||
@raises(ValueError)
|
||||
def test_failed_use(self):
|
||||
plugin.use('asd')
|
||||
plugin.use_plugin('asd')
|
||||
|
||||
@skipif(not PIL_available and not FI_available)
|
||||
def test_use_priority(self):
|
||||
plugin.use(priority_plugin)
|
||||
plugin.use_plugin(priority_plugin)
|
||||
plug, func = plugin.plugin_store['imread'][0]
|
||||
assert_equal(plug, priority_plugin)
|
||||
|
||||
plugin.use('test')
|
||||
plugin.use_plugin('test')
|
||||
plug, func = plugin.plugin_store['imread'][0]
|
||||
assert_equal(plug, 'test')
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_use_priority_with_func(self):
|
||||
plugin.use('pil')
|
||||
plugin.use_plugin('pil')
|
||||
plug, func = plugin.plugin_store['imread'][0]
|
||||
assert_equal(plug, 'pil')
|
||||
|
||||
plugin.use('test', 'imread')
|
||||
plugin.use_plugin('test', 'imread')
|
||||
plug, func = plugin.plugin_store['imread'][0]
|
||||
assert_equal(plug, 'test')
|
||||
|
||||
plug, func = plugin.plugin_store['imsave'][0]
|
||||
assert_equal(plug, 'pil')
|
||||
|
||||
plugin.use('test')
|
||||
plugin.use_plugin('test')
|
||||
plug, func = plugin.plugin_store['imsave'][0]
|
||||
assert_equal(plug, 'test')
|
||||
|
||||
@@ -81,8 +81,8 @@ class TestPlugin:
|
||||
assert 'test' in p['imread']
|
||||
|
||||
def test_available(self):
|
||||
assert 'qt' in io.plugins()
|
||||
assert 'test' in io.plugins(loaded=True)
|
||||
assert 'qt' in io.available_plugins
|
||||
assert 'test' in io.find_available_plugins(loaded=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user