From 07f2656975c660be30a92e09aa17fa5def0e770a Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 8 Dec 2013 10:14:00 -0600 Subject: [PATCH] Factor out function injection --- skimage/io/collection.py | 10 ++++++++++ skimage/io/manage_plugins.py | 15 +++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/skimage/io/collection.py b/skimage/io/collection.py index 96d29d4a..28a9632d 100644 --- a/skimage/io/collection.py +++ b/skimage/io/collection.py @@ -457,3 +457,13 @@ def imread_collection_wrapper(imread): return ImageCollection(load_pattern, conserve_memory=conserve_memory, load_func=imread) return imread_collection + + +def inject_imread_collection_if_needed(module): + """Add `imread_collection` to module if not already present.""" + add_plugin = (not hasattr(module, 'imread_collection') and + hasattr(module, 'imread')) + if add_plugin: + imread = getattr(module, 'imread') + func = imread_collection_wrapper(imread) + setattr(module, 'imread_collection', func) diff --git a/skimage/io/manage_plugins.py b/skimage/io/manage_plugins.py index f80b38c1..d31e6dbe 100644 --- a/skimage/io/manage_plugins.py +++ b/skimage/io/manage_plugins.py @@ -266,6 +266,14 @@ def use_plugin(name, kind=None): plugin_store[k] = funcs +def _inject_imread_collection_if_needed(module): + """Add `imread_collection` to module if not already present.""" + if not hasattr(module, 'imread_collection') and hasattr(module, 'imread'): + imread = getattr(module, 'imread') + func = imread_collection_wrapper(imread) + setattr(module, 'imread_collection', func) + + def _load(plugin): """Load the given plugin. @@ -291,12 +299,7 @@ def _load(plugin): provides = plugin_provides[plugin] for p in provides: if p == 'imread_collection': - add_plugin = (not hasattr(plugin_module, 'imread_collection') and - hasattr(plugin_module, 'imread')) - if add_plugin: - imread = getattr(plugin_module, 'imread') - func = imread_collection_wrapper(imread) - setattr(plugin_module, 'imread_collection', func) + _inject_imread_collection_if_needed(plugin_module) elif not hasattr(plugin_module, p): print("Plugin %s does not provide %s as advertised. Ignoring." % \ (plugin, p))