refactor
This commit is contained in:
@ -43,8 +43,12 @@ del(sys)
|
||||
class CacheOptionDescription(BaseOption):
|
||||
__slots__ = ('_cache_paths', '_cache_consistencies', '_cache_force_store_values')
|
||||
|
||||
def _build_cache(self, config, path='', _consistencies=None,
|
||||
cache_option=None, force_store_values=None,
|
||||
def _build_cache(self,
|
||||
config,
|
||||
path='',
|
||||
_consistencies=None,
|
||||
cache_option=None,
|
||||
force_store_values=None,
|
||||
_dependencies=None):
|
||||
"""validate duplicate option and set option has readonly option
|
||||
"""
|
||||
@ -69,13 +73,16 @@ class CacheOptionDescription(BaseOption):
|
||||
subpath = path + '.' + option.impl_getname()
|
||||
if isinstance(option, OptionDescription):
|
||||
option._set_readonly()
|
||||
option._build_cache(config, subpath, _consistencies,
|
||||
cache_option, force_store_values,
|
||||
_dependencies)
|
||||
option._build_cache(config,
|
||||
subpath,
|
||||
_consistencies,
|
||||
cache_option,
|
||||
force_store_values,
|
||||
_dependencies)
|
||||
else:
|
||||
option._set_readonly()
|
||||
is_multi = option.impl_is_multi()
|
||||
if not option._is_symlinkoption() and 'force_store_value' in option.impl_getproperties():
|
||||
if not option.impl_is_symlinkoption() and 'force_store_value' in option.impl_getproperties():
|
||||
force_store_values.append((subpath, option))
|
||||
for func, all_cons_opts, params in option._get_consistencies():
|
||||
option._valid_consistencies(all_cons_opts[1:], init=False)
|
||||
@ -150,7 +157,9 @@ class CacheOptionDescription(BaseOption):
|
||||
def impl_already_build_caches(self):
|
||||
return getattr(self, '_cache_paths', None) is not None
|
||||
|
||||
def impl_build_force_store_values(self, config, force_store_values):
|
||||
def impl_build_force_store_values(self,
|
||||
config,
|
||||
force_store_values):
|
||||
session = config._impl_values._p_.getsession()
|
||||
value_set = False
|
||||
for subpath, option in self._cache_force_store_values:
|
||||
@ -174,8 +183,10 @@ class CacheOptionDescription(BaseOption):
|
||||
if value_set:
|
||||
config._impl_values._p_.commit()
|
||||
|
||||
def _build_cache_option(self, _currpath=None, cache_path=None,
|
||||
cache_option=None):
|
||||
def _build_cache_option(self,
|
||||
_currpath=None,
|
||||
cache_path=None,
|
||||
cache_option=None):
|
||||
|
||||
if self.impl_is_readonly() or (_currpath is None and getattr(self, '_cache_paths', None) is not None):
|
||||
# cache already set
|
||||
@ -195,8 +206,9 @@ class CacheOptionDescription(BaseOption):
|
||||
cache_path.append(path)
|
||||
if option.impl_is_optiondescription():
|
||||
_currpath.append(attr)
|
||||
option._build_cache_option(_currpath, cache_path,
|
||||
cache_option)
|
||||
option._build_cache_option(_currpath,
|
||||
cache_path,
|
||||
cache_option)
|
||||
_currpath.pop()
|
||||
if save:
|
||||
_setattr = object.__setattr__
|
||||
@ -236,8 +248,8 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
option):
|
||||
name = option.impl_getname()
|
||||
if option._is_subdyn():
|
||||
found = False
|
||||
if byname.startswith(name):
|
||||
found = False
|
||||
subdyn = option._subdyn()
|
||||
for suffix in subdyn._impl_get_suffixes(
|
||||
context):
|
||||
@ -249,8 +261,8 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
option = option._impl_to_dyn(name + suffix,
|
||||
path)
|
||||
break
|
||||
if not found:
|
||||
return False
|
||||
if not found:
|
||||
return False
|
||||
else:
|
||||
if not byname == name:
|
||||
return False
|
||||
@ -312,12 +324,18 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
return find_results
|
||||
return find_results
|
||||
|
||||
def _impl_st_getchildren(self, context, only_dyn=False):
|
||||
def _impl_st_getchildren(self,
|
||||
context,
|
||||
only_dyn=False):
|
||||
for child in self._children[1]:
|
||||
if only_dyn is False or child.impl_is_dynoptiondescription():
|
||||
yield(child)
|
||||
|
||||
def _getattr(self, name, suffix=undefined, context=undefined, dyn=True):
|
||||
def _getattr(self,
|
||||
name,
|
||||
suffix=undefined,
|
||||
context=undefined,
|
||||
dyn=True):
|
||||
error = False
|
||||
if suffix is not undefined:
|
||||
if undefined in [suffix, context]: # pragma: no cover
|
||||
@ -337,7 +355,8 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
else:
|
||||
return child
|
||||
else:
|
||||
child = self._impl_search_dynchild(name, context=context)
|
||||
child = self._impl_search_dynchild(name,
|
||||
context=context)
|
||||
if child != []:
|
||||
return child
|
||||
error = True
|
||||
@ -346,27 +365,35 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
'in OptionDescription {1}'
|
||||
'').format(name, self.impl_getname()))
|
||||
|
||||
def impl_getpaths(self, include_groups=False, _currpath=None):
|
||||
def impl_getpaths(self,
|
||||
include_groups=False,
|
||||
_currpath=None):
|
||||
"""returns a list of all paths in self, recursively
|
||||
_currpath should not be provided (helps with recursion)
|
||||
"""
|
||||
return _impl_getpaths(self, include_groups, _currpath)
|
||||
return _impl_getpaths(self,
|
||||
include_groups,
|
||||
_currpath)
|
||||
|
||||
def impl_get_opt_by_path(self, path):
|
||||
def impl_get_opt_by_path(self,
|
||||
path):
|
||||
if getattr(self, '_cache_paths', None) is None:
|
||||
raise ConfigError(_('use impl_get_opt_by_path only with root OptionDescription'))
|
||||
if path not in self._cache_paths[1]:
|
||||
raise AttributeError(_('no option for path {0}').format(path))
|
||||
return self._cache_paths[0][self._cache_paths[1].index(path)]
|
||||
|
||||
def impl_get_path_by_opt(self, opt):
|
||||
def impl_get_path_by_opt(self,
|
||||
opt):
|
||||
if getattr(self, '_cache_paths', None) is None:
|
||||
raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription'))
|
||||
if opt not in self._cache_paths[0]:
|
||||
raise AttributeError(_('no option {0} found').format(opt))
|
||||
return self._cache_paths[1][self._cache_paths[0].index(opt)]
|
||||
|
||||
def _impl_getchildren(self, dyn=True, context=undefined):
|
||||
def _impl_getchildren(self,
|
||||
dyn=True,
|
||||
context=undefined):
|
||||
for child in self._impl_st_getchildren(context):
|
||||
cname = child.impl_getname()
|
||||
if dyn and child.impl_is_dynoptiondescription():
|
||||
@ -380,32 +407,47 @@ class OptionDescriptionWalk(CacheOptionDescription):
|
||||
def impl_getchildren(self):
|
||||
return list(self._impl_getchildren())
|
||||
|
||||
def __getattr__(self, name, context=undefined):
|
||||
if name.startswith('_'): # or name.startswith('impl_'):
|
||||
return object.__getattribute__(self, name)
|
||||
def __getattr__(self,
|
||||
name,
|
||||
context=undefined):
|
||||
if name.startswith('_'):
|
||||
return object.__getattribute__(self,
|
||||
name)
|
||||
if '.' in name:
|
||||
path = name.split('.')[0]
|
||||
subpath = '.'.join(name.split('.')[1:])
|
||||
return self.__getattr__(path, context=context).__getattr__(subpath, context=context)
|
||||
return self._getattr(name, context=context)
|
||||
return self.__getattr__(path, context=context).__getattr__(subpath,
|
||||
context=context)
|
||||
return self._getattr(name,
|
||||
context=context)
|
||||
|
||||
def _impl_search_dynchild(self, name, context):
|
||||
def _impl_search_dynchild(self,
|
||||
name,
|
||||
context):
|
||||
ret = []
|
||||
for child in self._impl_st_getchildren(context, only_dyn=True):
|
||||
for child in self._impl_st_getchildren(context,
|
||||
only_dyn=True):
|
||||
cname = child.impl_getname()
|
||||
if name.startswith(cname):
|
||||
for value in child._impl_get_suffixes(context):
|
||||
if name == cname + value:
|
||||
return SynDynOptionDescription(child, name, value)
|
||||
return SynDynOptionDescription(child,
|
||||
name,
|
||||
value)
|
||||
return ret
|
||||
|
||||
def _impl_get_dynchild(self, child, suffix):
|
||||
def _impl_get_dynchild(self,
|
||||
child,
|
||||
suffix):
|
||||
name = child.impl_getname() + suffix
|
||||
path = self.impl_getname() + suffix + '.' + name
|
||||
if isinstance(child, OptionDescription):
|
||||
return SynDynOptionDescription(child, name, suffix)
|
||||
return SynDynOptionDescription(child,
|
||||
name,
|
||||
suffix)
|
||||
else:
|
||||
return child._impl_to_dyn(name, path)
|
||||
return child._impl_to_dyn(name,
|
||||
path)
|
||||
|
||||
|
||||
class OptionDescription(OptionDescriptionWalk):
|
||||
@ -414,12 +456,18 @@ class OptionDescription(OptionDescriptionWalk):
|
||||
"""
|
||||
__slots__ = ('_group_type',)
|
||||
|
||||
def __init__(self, name, doc, children, requires=None, properties=None):
|
||||
def __init__(self,
|
||||
name,
|
||||
doc,
|
||||
children,
|
||||
requires=None,
|
||||
properties=None):
|
||||
"""
|
||||
:param children: a list of options (including optiondescriptions)
|
||||
|
||||
"""
|
||||
super(OptionDescription, self).__init__(name, doc=doc,
|
||||
super(OptionDescription, self).__init__(name,
|
||||
doc=doc,
|
||||
requires=requires,
|
||||
properties=properties)
|
||||
child_names = []
|
||||
@ -453,12 +501,15 @@ class OptionDescription(OptionDescriptionWalk):
|
||||
def impl_getdoc(self):
|
||||
return self.impl_get_information('doc')
|
||||
|
||||
def impl_validate(self, *args, **kwargs):
|
||||
def impl_validate(self,
|
||||
*args,
|
||||
**kwargs):
|
||||
"""usefull for OptionDescription"""
|
||||
pass
|
||||
|
||||
# ____________________________________________________________
|
||||
def impl_set_group_type(self, group_type):
|
||||
def impl_set_group_type(self,
|
||||
group_type):
|
||||
"""sets a given group object to an OptionDescription
|
||||
|
||||
:param group_type: an instance of `GroupType` or `MasterGroupType`
|
||||
@ -474,7 +525,7 @@ class OptionDescription(OptionDescriptionWalk):
|
||||
raise Exception('please use MasterSlaves object instead of OptionDescription')
|
||||
children = self.impl_getchildren()
|
||||
for child in children:
|
||||
if child._is_symlinkoption(): # pragma: optional cover
|
||||
if child.impl_is_symlinkoption(): # pragma: optional cover
|
||||
raise ValueError(_("master group {0} shall not have "
|
||||
"a symlinkoption").format(self.impl_getname()))
|
||||
if not isinstance(child, Option): # pragma: optional cover
|
||||
@ -540,7 +591,7 @@ class DynOptionDescription(OptionDescription):
|
||||
'dynoptiondescription'))
|
||||
for chld in child._impl_getchildren():
|
||||
chld._impl_setsubdyn(self)
|
||||
if child._is_symlinkoption():
|
||||
if child.impl_is_symlinkoption():
|
||||
raise ConfigError(_('cannot set symlinkoption in a '
|
||||
'dynoptiondescription'))
|
||||
child._impl_setsubdyn(self)
|
||||
@ -586,7 +637,7 @@ class SynDynOptionDescription(object):
|
||||
return self._impl_getchildren()
|
||||
|
||||
def impl_getpath(self, context):
|
||||
path = self._impl_getopt().impl_getpath(context).split('.')
|
||||
path = self.impl_getopt().impl_getpath(context).split('.')
|
||||
path[-1] += self._suffix
|
||||
path.append(self._name)
|
||||
return '.'.join(path)
|
||||
@ -594,7 +645,7 @@ class SynDynOptionDescription(object):
|
||||
def impl_getpaths(self, include_groups=False, _currpath=None):
|
||||
return _impl_getpaths(self, include_groups, _currpath)
|
||||
|
||||
def _impl_getopt(self):
|
||||
def impl_getopt(self):
|
||||
return self._opt
|
||||
|
||||
|
||||
@ -624,7 +675,7 @@ class MasterSlaves(OptionDescription):
|
||||
slaves.append(child)
|
||||
child._add_dependency(self)
|
||||
for idx, child in enumerate(children):
|
||||
if child._is_symlinkoption(): # pragma: optional cover
|
||||
if child.impl_is_symlinkoption(): # pragma: optional cover
|
||||
raise ValueError(_("master group {0} shall not have "
|
||||
"a symlinkoption").format(self.impl_getname()))
|
||||
if not isinstance(child, Option): # pragma: optional cover
|
||||
|
Reference in New Issue
Block a user