find in api return an api object

This commit is contained in:
2018-04-10 12:33:51 +02:00
parent 9ea373efdf
commit 5eb2f04202
13 changed files with 280 additions and 267 deletions

View File

@ -628,6 +628,8 @@ class TiramisuOption(CommonTiramisu):
self.config_bag)
elif subfunc == 'make_dict' and self._get_option().impl_is_optiondescription():
return self._make_dict
elif subfunc == 'find' and self._get_option().impl_is_optiondescription():
return self._find
elif subfunc == 'list' and self._get_option().impl_is_optiondescription():
return self._list
elif subfunc == 'group_type' and self._get_option().impl_is_optiondescription():
@ -650,6 +652,32 @@ class TiramisuOption(CommonTiramisu):
withoption=withoption,
withvalue=withvalue)
def _find(self,
name,
value=undefined,
first=False):
"""find an option by name (only for optiondescription)"""
if not first:
ret = []
for path in self.config_bag.config.find(byname=name,
byvalue=value,
bytype=None,
type_='path',
_subpath=self.path,
config_bag=self.config_bag):
config_bag = self.config_bag.copy('nooption')
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
config_bag)
if first:
return t_option
ret.append(t_option)
return ret
@count
def _group_type(self):
"""get type for an optiondescription (only for optiondescription)"""
@ -864,17 +892,29 @@ class TiramisuContextOption(TiramisuContext):
@count
def find(self,
name,
type='option',
value=undefined,
first=False):
"""find an option by name"""
if first:
return self.config_bag.config.find_first(byname=name,
type_=type,
config_bag=self.config_bag)
else:
return self.config_bag.config.find(byname=name,
type_=type,
config_bag=self.config_bag)
if not first:
ret = []
for path in self.config_bag.config.find(byname=name,
byvalue=value,
bytype=None,
type_='path',
#_subpath=self.path,
config_bag=self.config_bag):
config_bag = self.config_bag.copy('nooption')
subconfig, name = config_bag.config.cfgimpl_get_home_by_path(path,
config_bag)
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
config_bag)
if first:
return t_option
ret.append(t_option)
return ret
#@count
#def get(self, path):
@ -928,12 +968,12 @@ class TiramisuContextConfig(TiramisuContext):
"""configuration methods"""
def find(self,
name,
byvalue=undefined,
value=undefined,
first=False):
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first:
return self.config_bag.config.find_firsts(byname=name,
byvalue=byvalue,
byvalue=value,
config_bag=self.config_bag)
else:
raise APIError('not implemented yet')

View File

@ -381,62 +381,15 @@ class SubConfig(object):
return value
def find(self,
bytype,
byname,
byvalue,
config_bag,
bytype=None,
byname=None,
byvalue=undefined,
type_='option'):
"""
finds a list of options recursively in the config
:param bytype: Option class (BoolOption, StrOption, ...)
:param byname: filter by Option.impl_getname()
:param byvalue: filter by the option's value
:returns: list of matching Option objects
"""
return self.cfgimpl_get_context()._find(bytype,
byname,
byvalue,
config_bag,
first=False,
type_=type_,
_subpath=self.cfgimpl_get_path(False))
def find_first(self,
config_bag,
bytype=None,
byname=None,
byvalue=undefined,
type_='option',
raise_if_not_found=True):
"""
finds an option recursively in the config
:param bytype: Option class (BoolOption, StrOption, ...)
:param byname: filter by Option.impl_getname()
:param byvalue: filter by the option's value
:returns: list of matching Option objects
"""
return self.cfgimpl_get_context()._find(bytype,
byname,
byvalue,
config_bag,
first=True,
type_=type_,
_subpath=self.cfgimpl_get_path(False),
raise_if_not_found=raise_if_not_found)
def _find(self,
bytype,
byname,
byvalue,
config_bag,
first,
type_='option',
_subpath=None,
raise_if_not_found=True,
only_path=undefined,
only_option=undefined):
type_='option',
_subpath=None,
raise_if_not_found=True,
only_path=undefined,
only_option=undefined):
"""
convenience method for finding an option that lives only in the subtree
@ -459,20 +412,14 @@ class SubConfig(object):
if type_ not in ('option', 'path', 'value'): # pragma: optional cover
raise ValueError(_('unknown type_ type {0}'
'for _find').format(type_))
find_results = []
# if value and/or validate_properties are set, need all avalaible option
# If first one has no good value or not good property check second one
# and so on
only_first = first is True and byvalue is undefined and \
config_bag.validate_properties is False
'for find').format(type_))
found = False
if only_path is not undefined:
options = [(only_path, only_option)]
else:
options = self.cfgimpl_get_description().impl_get_options_paths(bytype,
byname,
_subpath,
only_first,
config_bag)
for path, option in options:
sconfig_bag = config_bag.copy('nooption')
@ -480,39 +427,34 @@ class SubConfig(object):
if not _filter_by_value(sconfig_bag):
continue
#remove option with propertyerror, ...
if config_bag.validate_properties:
if sconfig_bag.validate_properties:
try:
self.unwrap_from_path(path,
config_bag)
sconfig_bag)
self.cfgimpl_get_settings().validate_properties(path,
None,
config_bag)
sconfig_bag)
except PropertiesOptionError:
continue
if type_ == 'value':
retval = self.getattr(path,
None,
config_bag)
sconfig_bag)
elif type_ == 'path':
retval = path
elif type_ == 'option':
retval = option
if first:
return retval
else:
find_results.append(retval)
return self._find_return_results(find_results,
found = True
yield retval
return self._find_return_results(found,
raise_if_not_found)
def _find_return_results(self,
find_results,
found,
raise_if_not_found):
if find_results == []: # pragma: optional cover
if raise_if_not_found:
raise AttributeError(_("no option found in config"
" with these criteria"))
else:
return find_results
if not found and raise_if_not_found:
raise AttributeError(_("no option found in config"
" with these criteria"))
def make_dict(self,
config_bag,
@ -565,13 +507,12 @@ class SubConfig(object):
"option"))
context = self.cfgimpl_get_context()
if withoption is not None:
for path in context._find(bytype=None,
byname=withoption,
byvalue=withvalue,
first=False,
type_='path',
_subpath=self.cfgimpl_get_path(False),
config_bag=config_bag):
for path in context.find(bytype=None,
byname=withoption,
byvalue=withvalue,
type_='path',
_subpath=self.cfgimpl_get_path(False),
config_bag=config_bag):
path = '.'.join(path.split('.')[:-1])
opt = context.unwrap_from_path(path,
config_bag)
@ -946,23 +887,21 @@ class GroupConfig(_CommonConfig):
_sub=False):
"""Find first not in current GroupConfig, but in each children
"""
ret = []
#if MetaConfig, all children have same OptionDescription in context
#so search only one time the option for all children
if bypath is undefined and byname is not None and \
isinstance(self,
MetaConfig):
bypath = self._find(bytype=None,
byvalue=undefined,
byname=byname,
first=True,
config_bag=config_bag,
type_='path',
raise_if_not_found=raise_if_not_found)
bypath = next(self.find(bytype=None,
byvalue=undefined,
byname=byname,
config_bag=config_bag,
type_='path',
raise_if_not_found=raise_if_not_found))
byname = None
byoption = self.cfgimpl_get_description().impl_get_opt_by_path(bypath)
ret = []
for child in self._impl_children:
nconfig_bag = config_bag.copy('nooption')
nconfig_bag.option = child
@ -974,21 +913,25 @@ class GroupConfig(_CommonConfig):
config_bag=config_bag,
raise_if_not_found=False,
_sub=True))
elif child._find(None,
byname,
byvalue,
first=True,
type_='path',
config_bag=config_bag,
raise_if_not_found=False,
only_path=bypath,
only_option=byoption):
ret.append(child)
else:
try:
next(child.find(None,
byname,
byvalue,
type_='path',
config_bag=config_bag,
raise_if_not_found=False,
only_path=bypath,
only_option=byoption))
ret.append(child)
except StopIteration:
pass
if _sub:
return ret
else:
return GroupConfig(self._find_return_results(ret,
raise_if_not_found))
self._find_return_results(ret != [],
raise_if_not_found)
return GroupConfig(ret)
def impl_getname(self):
return self._impl_name

View File

@ -81,9 +81,8 @@ class DynOptionDescription(OptionDescription):
None,
config_bag)
if not isinstance(values, list):
raise ValueError(_('invalid suffix "{}" for option "{}", must be a list'
'').format(values,
self.impl_get_display_name()))
raise ValueError(_('DynOptionDescription callback for option "{}", is not a list ({})'
'').format(self.impl_get_display_name(), values))
if len(values) > len(set(values)):
raise ValueError(_('DynOptionDescription callback return not unique value'))
for val in values:

View File

@ -19,6 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from copy import copy
from itertools import chain
from ..i18n import _
@ -254,74 +255,61 @@ class OptionDescriptionWalk(CacheOptionDescription):
bytype,
byname,
_subpath,
only_first,
config_bag):
find_results = []
def _filter_by_type(path,
option):
if isinstance(option,
bytype):
if byname is None:
if option.issubdyn():
for doption in self.build_dynoptions(option, config_bag):
dpath = doption.impl_getname(config_bag.config)
yield (dpath, doption)
else:
yield (path, option)
def _filter_by_name(path,
option):
name = option.impl_getname()
if option.issubdyn():
found = False
if byname.startswith(name):
for doption in self.build_dynoptions(option, config_bag):
if byname == doption.impl_getname():
dpath = doption.impl_getpath(config_bag.config)
find_results.append((dpath, doption))
found = True
yield (dpath, doption)
break
if not found:
return False
else:
if not byname == name:
return False
find_results.append((path, option))
return True
def _filter_by_type(path,
option):
if isinstance(option,
bytype):
#if byname is not None, check option byname in _filter_by_name
#not here
if byname is None:
if option.issubdyn():
for doption in self.build_dynoptions(option, config_bag):
dpath = doption.impl_getname(config_bag.config)
find_results.append((dpath, doption))
else:
find_results.append((path, option))
return True
return False
if byname == name:
yield (path, option)
def _filter(path, option):
generators = []
if bytype is not None:
retval = _filter_by_type(path, option)
if byname is None:
return retval
generators.append(_filter_by_type(path, option))
if byname is not None:
return _filter_by_name(path, option)
generators.append(_filter_by_name(path, option))
if len(generators) == 1:
return generators[0]
else:
return chain(*generators)
opts, paths = self._cache_paths
for index, path in enumerate(paths):
option = opts[index]
for index, option in enumerate(opts):
if option.impl_is_optiondescription():
continue
path = paths[index]
if _subpath is not None and not path.startswith(_subpath + '.'):
continue
if bytype == byname is None:
if option.issubdyn():
for doption in self.build_dynoptions(option, config_bag):
dpath = doption.impl_getpath(config_bag.config)
find_results.append((dpath, doption))
yield (dpath, doption)
else:
find_results.append((dpath, option))
yield (dpath, option)
else:
if _filter(path, option) is False:
continue
if only_first:
return find_results
return find_results
for ret in _filter(path, option):
yield ret
def impl_getchild(self,
name,

View File

@ -34,23 +34,23 @@ class Properties(Cache):
# properties
def setproperties(self, path, properties):
if DEBUG:
if DEBUG: # pragma: no cover
print('setproperties', path, properties)
self._properties[path] = properties
def getproperties(self, path, default_properties):
ret = self._properties.get(path, frozenset(default_properties))
if DEBUG:
if DEBUG: # pragma: no cover
print('getproperties', path, ret)
return ret
def reset_all_properties(self):
if DEBUG:
if DEBUG: # pragma: no cover
print('reset_all_properties')
self._properties.clear()
def delproperties(self, path):
if DEBUG:
if DEBUG: # pragma: no cover
print('delproperties', path)
if path in self._properties:
del(self._properties[path])
@ -74,7 +74,7 @@ class Permissives(Cache):
super(Permissives, self).__init__(storage)
def setpermissive(self, path, permissive):
if DEBUG:
if DEBUG: # pragma: no cover
print('setpermissive', path, permissive)
if not permissive:
if path in self._permissives:
@ -84,7 +84,7 @@ class Permissives(Cache):
def getpermissive(self, path=None):
ret = self._permissives.get(path, frozenset())
if DEBUG:
if DEBUG: # pragma: no cover
print('getpermissive', path, ret)
return ret

View File

@ -75,7 +75,7 @@ class Values(Cache):
"""set value for a path
a specified value must be associated to an owner
"""
if DEBUG:
if DEBUG: # pragma: no cover
print('setvalue', path, value, owner, index, id(self))
values = []
vidx = None
@ -97,9 +97,7 @@ class Values(Cache):
return: boolean
"""
has_path = path in self._values[0]
if path.startswith('subod.subodval'):
raise Exception('arf ...')
if DEBUG:
if DEBUG: # pragma: no cover
print('hasvalue', path, index, has_path, id(self))
if index is None:
return has_path
@ -113,7 +111,7 @@ class Values(Cache):
"""
_values == ((path1, path2), ((idx1_1, idx1_2), None), ((value1_1, value1_2), value2), ((owner1_1, owner1_2), owner2))
"""
if DEBUG:
if DEBUG: # pragma: no cover
print('reduce_index', path, index, id(self))
path_idx = self._values[0].index(path)
indexes = self._values[1][path_idx]
@ -128,7 +126,7 @@ class Values(Cache):
self._values = tuple(values)
def resetvalue_index(self, path, index):
if DEBUG:
if DEBUG: # pragma: no cover
print('resetvalue_index', path, index, id(self))
def _resetvalue(nb):
values_idx = list(values[nb])
@ -161,7 +159,7 @@ class Values(Cache):
def resetvalue(self, path, commit):
"""remove value means delete value in storage
"""
if DEBUG:
if DEBUG: # pragma: no cover
print('resetvalue', path, id(self))
def _resetvalue(nb):
lst = list(self._values[nb])
@ -211,7 +209,7 @@ class Values(Cache):
with_value)
if owner is undefined:
owner = default
if DEBUG:
if DEBUG: # pragma: no cover
print('getvalue', path, index, value, owner, id(self))
if with_value:
return owner, value