find method in config
This commit is contained in:
parent
d3dc40033b
commit
aa67ae31aa
|
@ -137,6 +137,14 @@ def test_delattr():
|
|||
c.int = 45
|
||||
assert c.int == 45
|
||||
|
||||
def test_find_in_config():
|
||||
"finds option in config"
|
||||
descr = make_description()
|
||||
conf = Config(descr)
|
||||
assert conf.find(byname='dummy')[0] == conf.unwrap_from_name('dummy')
|
||||
assert conf.find(bytype=ChoiceOption)[0] == conf.unwrap_from_name('name')
|
||||
assert conf.find(byvalue='ref')[0] == conf.unwrap_from_name('name')
|
||||
|
||||
#def test_validator():
|
||||
# "validates the integrity of an option towards a whole configuration"
|
||||
# def my_validator_1(config):
|
||||
|
|
|
@ -359,7 +359,6 @@ class Config(object):
|
|||
and sets the found option if the match is not ambiguous.
|
||||
:param kwargs: dict of name strings to values.
|
||||
"""
|
||||
|
||||
all_paths = [p.split(".") for p in self.getpaths(allpaths=True)]
|
||||
for key, value in kwargs.iteritems():
|
||||
key_p = key.split('.')
|
||||
|
@ -384,10 +383,15 @@ class Config(object):
|
|||
|
||||
def get(self, name):
|
||||
"""
|
||||
same as a find_first() method in a config that has identical names
|
||||
that is : Returns the first item of an option named 'name'
|
||||
|
||||
much like the attribute access way, except that
|
||||
the search for the option is performed recursively in the whole
|
||||
configuration tree.
|
||||
**carefull**: very slow !
|
||||
|
||||
:returns: option value.
|
||||
"""
|
||||
paths = self.getpaths(allpaths=True)
|
||||
pathsvalues = []
|
||||
|
@ -560,6 +564,11 @@ class Config(object):
|
|||
def getpaths(self, include_groups=False, allpaths=False, mandatory=False):
|
||||
"""returns a list of all paths in self, recursively, taking care of
|
||||
the context of properties (hidden/disabled)
|
||||
|
||||
:param include_groups: if true, OptionDescription are included
|
||||
:param allpaths: all the options (event the properties protected ones)
|
||||
:param mandatory: includes the mandatory options
|
||||
:returns: list of all paths
|
||||
"""
|
||||
paths = []
|
||||
for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
|
||||
|
@ -576,6 +585,45 @@ class Config(object):
|
|||
paths.append(path)
|
||||
return paths
|
||||
|
||||
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
|
||||
"""
|
||||
finds an option recursively in the config
|
||||
|
||||
:param bytype: Option class (BoolOption, StrOption, ...)
|
||||
:param byname: filter by Option._name
|
||||
:param byvalue: filter by the option's value
|
||||
:param byattrs: dict of option attributes (default, callback...)
|
||||
:returns: list of matching Option objects
|
||||
"""
|
||||
def _filter_by_name():
|
||||
if byname is not None:
|
||||
pathname = path.split('.')[-1]
|
||||
if pathname == byname:
|
||||
yield option
|
||||
def _filter_by_value():
|
||||
if byvalue is not None:
|
||||
try:
|
||||
value = getattr(self, path)
|
||||
if value == byvalue:
|
||||
yield option
|
||||
except Exception, e: # a property restricts the acces to value
|
||||
pass
|
||||
def _filter_by_type():
|
||||
if bytype is not None:
|
||||
if isinstance(option, bytype):
|
||||
find_results.append(self.unwrap_from_path(path))
|
||||
find_results = []
|
||||
paths = self.getpaths(allpaths=True)
|
||||
for path in paths:
|
||||
option = self.unwrap_from_path(path)
|
||||
if _filter_by_name() is not None:
|
||||
find_results.extend(list( _filter_by_name() ))
|
||||
if _filter_by_value() is not None:
|
||||
find_results.extend(list( _filter_by_value() ))
|
||||
if _filter_by_type() is not None:
|
||||
find_results.extend(list( _filter_by_type() ))
|
||||
return find_results
|
||||
|
||||
def make_dict(config, flatten=False):
|
||||
"""export the whole config into a `dict`
|
||||
:returns: dict of Option's name (or path) and values"""
|
||||
|
|
Loading…
Reference in New Issue