Factor out function injection

This commit is contained in:
Tony S Yu
2013-12-08 10:14:00 -06:00
parent 19102510ce
commit 07f2656975
2 changed files with 19 additions and 6 deletions
+10
View File
@@ -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)
+9 -6
View File
@@ -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))