refactor and better test in api

This commit is contained in:
2017-11-13 22:45:53 +01:00
parent a8d022b67f
commit 007a22ca94
9 changed files with 406 additions and 348 deletions

View File

@ -20,6 +20,7 @@
# ____________________________________________________________
from copy import copy
import re
import weakref
from ..i18n import _
@ -72,8 +73,6 @@ class CacheOptionDescription(BaseOption):
cache_option, force_store_values,
_dependencies)
else:
if option.impl_is_master_slaves('master'):
option._add_dependency(option.impl_get_master_slaves())
option._set_readonly()
is_multi = option.impl_is_multi()
if not option._is_symlinkoption() and 'force_store_value' in option.impl_getproperties():
@ -207,10 +206,17 @@ class CacheOptionDescription(BaseOption):
class OptionDescriptionWalk(CacheOptionDescription):
__slots__ = ('_children',)
def impl_get_options_paths(self, bytype, byname, _subpath, only_first, context):
def impl_get_options_paths(self,
bytype,
byname,
_subpath,
only_first,
context):
find_results = []
def _rebuild_dynpath(path, suffix, dynopt):
def _rebuild_dynpath(path,
suffix,
dynopt):
found = False
spath = path.split('.')
for length in xrange(1, len(spath)):
@ -226,19 +232,22 @@ class OptionDescriptionWalk(CacheOptionDescription):
subpath = subpath + '.' + spath[slength] + suffix
return subpath
def _filter_by_name(path, option):
def _filter_by_name(path,
option):
name = option.impl_getname()
if option._is_subdyn():
if byname.startswith(name):
found = False
for suffix in option._subdyn._impl_get_suffixes(
subdyn = option._subdyn()
for suffix in subdyn._impl_get_suffixes(
context):
if byname == name + suffix:
found = True
path = _rebuild_dynpath(path, suffix,
option._subdyn)
option = option._impl_to_dyn(
name + suffix, path)
path = _rebuild_dynpath(path,
suffix,
subdyn)
option = option._impl_to_dyn(name + suffix,
path)
break
if not found:
return False
@ -248,8 +257,10 @@ class OptionDescriptionWalk(CacheOptionDescription):
find_results.append((path, option))
return True
def _filter_by_type(path, option):
if isinstance(option, bytype):
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:
@ -257,7 +268,8 @@ class OptionDescriptionWalk(CacheOptionDescription):
name = option.impl_getname()
for suffix in option._subdyn._impl_get_suffixes(
context):
spath = _rebuild_dynpath(path, suffix,
spath = _rebuild_dynpath(path,
suffix,
option._subdyn)
find_results.append((spath, option._impl_to_dyn(
name + suffix, spath)))
@ -485,9 +497,11 @@ class OptionDescription(OptionDescriptionWalk):
def __getstate__(self):
raise NotImplementedError()
def _impl_get_suffixes(self, context):
def _impl_get_suffixes(self,
context):
callback, callback_params = self.impl_get_callback()
values = carry_out_calculation(self, context=context,
values = carry_out_calculation(self,
context=context,
callback=callback,
callback_params=callback_params)
if len(values) > len(set(values)):
@ -497,15 +511,28 @@ class OptionDescription(OptionDescriptionWalk):
raise ValueError(_("invalid suffix: {0} for option").format(val))
return values
def impl_validate_value(self, option, value, context):
def impl_validate_value(self,
option,
value,
context):
pass
class DynOptionDescription(OptionDescription):
def __init__(self, name, doc, children, requires=None, properties=None,
callback=None, callback_params=None):
super(DynOptionDescription, self).__init__(name, doc, children,
requires, properties)
def __init__(self,
name,
doc,
children,
requires=None,
properties=None,
callback=None,
callback_params=None):
super(DynOptionDescription, self).__init__(name,
doc,
children,
requires,
properties)
for child in children:
if isinstance(child, OptionDescription):
if child.impl_get_group_type() != groups.master:
@ -517,9 +544,12 @@ class DynOptionDescription(OptionDescription):
raise ConfigError(_('cannot set symlinkoption in a '
'dynoptiondescription'))
child._impl_setsubdyn(self)
self.impl_set_callback(callback, callback_params)
self.impl_set_callback(callback,
callback_params)
def _validate_callback(self, callback, callback_params):
def _validate_callback(self,
callback,
callback_params):
if callback is None:
raise ConfigError(_('callback is mandatory for dynoptiondescription'))
@ -532,15 +562,22 @@ class SynDynOptionDescription(object):
self._name = name
self._suffix = suffix
def __getattr__(self, name, context=undefined):
def __getattr__(self,
name,
context=undefined):
if name in dir(self._opt):
return getattr(self._opt, name)
return self._opt._getattr(name, suffix=self._suffix, context=context)
return getattr(self._opt,
name)
return self._opt._getattr(name,
suffix=self._suffix,
context=context)
def impl_getname(self):
return self._name
def _impl_getchildren(self, dyn=True, context=undefined):
def _impl_getchildren(self,
dyn=True,
context=undefined):
children = []
for child in self._opt._impl_getchildren():
yield(self._opt._impl_get_dynchild(child, self._suffix))
@ -608,7 +645,7 @@ class MasterSlaves(OptionDescription):
"not refered a slave's ones"))
#everything is ok, store references
for child in children:
child._master_slaves = self
child._master_slaves = weakref.ref(self)
master._add_dependency(self)
def is_master(self, opt):
@ -643,7 +680,7 @@ class MasterSlaves(OptionDescription):
c_opt = opt._opt
else:
c_opt = opt
return c_opt in self._children[1]
return child in self._children[1]
def reset(self,
opt,
@ -714,10 +751,16 @@ class MasterSlaves(OptionDescription):
setting_properties,
check_frozen)
else:
return self._getslave(values, opt, path, validate,
force_permissive, trusted_cached_properties,
validate_properties, setting_properties,
self_properties, index,
return self._getslave(values,
opt,
path,
validate,
force_permissive,
trusted_cached_properties,
validate_properties,
setting_properties,
self_properties,
index,
check_frozen)
def _getmaster(self,
@ -768,17 +811,19 @@ class MasterSlaves(OptionDescription):
master = self.getmaster(opt)
context = values._getcontext()
masterp = master.impl_getpath(context)
mastervalue = values.get_cached_value(master, path=masterp, validate=validate,
force_permissive=force_permissive,
validate_properties=validate_properties,
self_properties=self_properties,
from_masterslave=True,
setting_properties=setting_properties,
check_frozen=check_frozen)
if isinstance(mastervalue, Exception):
if isinstance(mastervalue, PropertiesOptionError):
mastervalue.set_orig_opt(opt)
return mastervalue
try:
mastervalue = values.get_cached_value(master,
path=masterp,
validate=validate,
force_permissive=force_permissive,
validate_properties=validate_properties,
self_properties=self_properties,
from_masterslave=True,
setting_properties=setting_properties,
check_frozen=check_frozen)
except PropertiesOptionError as mastervalue:
mastervalue.set_orig_opt(opt)
raise mastervalue
masterlen = len(mastervalue)
#self._master_is_meta = values._is_meta(master, masterp, force_permissive=force_permissive)
multi = list() # values._get_multi(opt, path)
@ -797,27 +842,27 @@ class MasterSlaves(OptionDescription):
else:
indexes = [index]
for idx in indexes:
value = values.get_cached_value(opt, path, validate,
force_permissive,
trusted_cached_properties,
validate_properties,
index=idx,
# not self_properties,
# depends to index
#self_properties=self_properties,
setting_properties=setting_properties,
from_masterslave=True,
check_frozen=check_frozen)
if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError):
err = value
if index is None:
multi.append(value)
else:
multi = value
try:
value = values.get_cached_value(opt,
path,
validate,
force_permissive,
trusted_cached_properties,
validate_properties,
index=idx,
# not self_properties,
# depends to index
#self_properties=self_properties,
setting_properties=setting_properties,
from_masterslave=True,
check_frozen=check_frozen)
except PropertiesOptionError as perr:
err = perr
if index is None:
multi.append(err)
else:
return value
elif index is None:
multi = err
if index is None:
multi.append(value)
else:
multi = value
@ -861,14 +906,16 @@ class MasterSlaves(OptionDescription):
master = self.getmaster(None)
if masterp is None:
masterp = master.impl_getpath(values._getcontext())
value = self.getitem(values,
master,
masterp,
validate,
force_permissive,
None,
True,
setting_properties=setting_properties)
value = self._getmaster(values,
master,
masterp,
validate,
force_permissive,
validate,
undefined,
None,
setting_properties,
False)
if isinstance(value, Exception):
return value
return len(value)