Use InheritedConfig for plugin_store

This commit is contained in:
Tony S Yu
2013-12-05 23:19:46 -06:00
parent f1dce4be9d
commit 860c6b5cb5
3 changed files with 22 additions and 4 deletions
+9 -2
View File
@@ -1,3 +1,4 @@
import os
from io import BytesIO
import numpy as np
@@ -5,7 +6,6 @@ import six
from skimage.io._plugins import call_plugin
from skimage.color import rgb2grey
from skimage._shared import six
from .util import file_or_url_context
@@ -95,7 +95,14 @@ def imread(fname, as_grey=False, plugin=None, flatten=None,
as_grey = flatten
with file_or_url_context(fname) as fname:
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
function = 'imread'
# TODO: This should probably use imghdr to get the image format.
try:
_, extension = os.path.splitext(fname)
function = function + extension
except AttributeError: # Buffers don't work with splitext
pass
img = call_plugin(function, fname, plugin=plugin, **plugin_args)
if as_grey and getattr(img, 'ndim', 0) >= 3:
img = rgb2grey(img)
+7 -2
View File
@@ -32,7 +32,10 @@ class InheritedConfig(dict):
super(InheritedConfig, self).__init__(config_values, **kwargs)
def get(self, key, default=None, _prev=None):
def __getitem__(self, key):
return self.get(key, _raise=True)
def get(self, key, default=None, _raise=False):
"""Return best matching config value for `key`.
Get value from configuration. The search for `key` is in the following
@@ -94,11 +97,13 @@ class InheritedConfig(dict):
1
"""
if key in self.keys():
return self[key]
return super(InheritedConfig, self).__getitem__(key)
elif default is not None:
return default
elif self._separator in key:
return self.get(self._parent(key))
elif _raise:
raise KeyError('%r not in %s' % (key, self.__class__.__name__))
else:
return None
@@ -36,6 +36,12 @@ def test_contains():
assert 'imread.jpg' in config
def test_getitem():
config = InheritedConfig({'imread': 'a'})
assert config['imread'] == 'a'
assert config['imread.png'] == 'a'
if __name__ == '__main__':
from numpy import testing
testing.run_module_suite()