simplify optiondescription

This commit is contained in:
2018-09-30 20:32:00 +02:00
parent e29e11b939
commit fbff3d9ced
9 changed files with 124 additions and 182 deletions

View File

@ -219,71 +219,45 @@ class CacheOptionDescription(BaseOption):
class OptionDescriptionWalk(CacheOptionDescription):
__slots__ = ('_children',)
def build_dynoptions(self,
option_bag):
option = option_bag.option
dynopt = option.getsubdyn()
rootpath = dynopt.impl_getpath()
ori_index = len(rootpath) + 1
subpaths = [rootpath] + option.impl_getpath()[ori_index:].split('.')[:-1]
for suffix in dynopt.impl_get_suffixes(option_bag):
subpath = '.'.join([subp + suffix for subp in subpaths])
if isinstance(option, OnlyOption):
yield option.impl_get_dynoption(subpath,
suffix)
else:
yield SynDynOptionDescription(option,
subpath,
suffix)
def impl_get_options_paths(self,
bytype,
byname,
_subpath,
config_bag,
self_opt=None):
if self_opt is None:
self_opt = self
def _filter_by_name(option):
return byname is None or option.impl_getname() == byname
for option in self_opt.impl_getchildren(config_bag):
if _subpath is None:
path = option.impl_getname()
else:
path = _subpath + '.' + option.impl_getname()
if option.impl_is_optiondescription():
for subopt in option.impl_get_options_paths(bytype,
byname,
path,
config_bag):
yield subopt
else:
if bytype is not None:
if isinstance(option, bytype) and \
_filter_by_name(option):
yield (path, option)
elif _filter_by_name(option):
yield (path, option)
def impl_getchild(self,
name,
config_bag,
subpath):
if name in self._children[0]:
child = self._children[1][self._children[0].index(name)]
if not child.impl_is_dynoptiondescription():
return child
else:
child = self._impl_search_dynchild(name,
subpath,
config_bag)
if child:
return child
return self._children[1][self._children[0].index(name)]
for child in self._children[1]:
if child.impl_is_dynoptiondescription():
cname = child.impl_getname()
if name.startswith(cname):
for value in child.impl_get_suffixes(config_bag):
if name == cname + value:
return SynDynOptionDescription(child,
subpath,
value)
raise AttributeError(_('unknown option "{0}" '
'in optiondescription "{1}"'
'').format(name, self.impl_getname()))
def impl_getchildren(self,
config_bag,
dyn=True):
subpath = None
for child in self._children[1]:
if dyn and child.impl_is_dynoptiondescription():
if config_bag.context is None: # pragma: no cover
raise ConfigError(_('need context'))
if subpath is None:
if config_bag.context.cfgimpl_get_description() == self:
subpath = ''
else:
subpath = self.impl_getpath()
for suffix in child.impl_get_suffixes(config_bag):
yield SynDynOptionDescription(child,
subpath,
suffix)
else:
yield child
def impl_get_opt_by_path(self,
path,
config_bag):
@ -299,61 +273,47 @@ class OptionDescriptionWalk(CacheOptionDescription):
subpath += '.' + step
return opt
def impl_getchildren(self,
def impl_get_options(self,
bytype,
byname,
config_bag,
dyn=True):
subpath = None
for child in self._impl_st_getchildren():
if dyn and child.impl_is_dynoptiondescription():
if config_bag.context is None: # pragma: no cover
raise ConfigError(_('need context'))
if subpath is None:
if config_bag.context.cfgimpl_get_description() == self:
subpath = ''
else:
subpath = self.impl_getpath()
option_bag = OptionBag()
option_bag.set_option(child,
subpath,
None,
config_bag)
for suffix in child.impl_get_suffixes(option_bag):
yield SynDynOptionDescription(child,
subpath,
suffix)
self_opt=None):
if self_opt is None:
self_opt = self
def _filter_by_name(option):
return byname is None or option.impl_getname() == byname
for option in self_opt.impl_getchildren(config_bag):
if option.impl_is_optiondescription():
for subopt in option.impl_get_options(bytype,
byname,
config_bag):
yield subopt
else:
yield child
if bytype is not None:
if isinstance(option, bytype) and \
_filter_by_name(option):
yield option
elif _filter_by_name(option):
yield option
def _impl_st_getchildren(self,
only_dyn=False):
for child in self._children[1]:
if only_dyn is False or child.impl_is_dynoptiondescription():
yield child
def get_dynoptions(self,
option_bag):
option = option_bag.option
dynopt = option.getsubdyn()
rootpath = dynopt.impl_getpath()
ori_index = len(rootpath) + 1
subpaths = [rootpath] + option.impl_getpath()[ori_index:].split('.')[:-1]
for suffix in dynopt.impl_get_suffixes(option_bag.config_bag):
subpath = '.'.join([subp + suffix for subp in subpaths])
yield self.impl_get_dynchild(option,
suffix,
subpath)
def _impl_search_dynchild(self,
name,
subpath,
config_bag):
for child in self._impl_st_getchildren(only_dyn=True):
#sconfig_bag = config_bag.copy('nooption')
#sconfig_bag.option = child
cname = child.impl_getname()
if name.startswith(cname):
option_bag = OptionBag()
option_bag.set_option(child,
subpath,
None,
config_bag)
for value in child.impl_get_suffixes(option_bag):
if name == cname + value:
return SynDynOptionDescription(child,
subpath,
value)
def _impl_get_dynchild(self,
child,
suffix,
subpath):
def impl_get_dynchild(self,
child,
suffix,
subpath):
if isinstance(child, OptionDescription):
return SynDynOptionDescription(child,
subpath,