simplify _setvalue

This commit is contained in:
Emmanuel Garette 2015-12-31 18:20:36 +01:00
parent d0e2b5d8c4
commit ce162280ad
4 changed files with 60 additions and 73 deletions

View File

@ -51,6 +51,8 @@ def test_freeze_whole_config():
except PropertiesOptionError as err: except PropertiesOptionError as err:
prop = err.proptype prop = err.proptype
assert 'frozen' in prop assert 'frozen' in prop
assert conf.gc.dummy is False
#
setting.remove('everything_frozen') setting.remove('everything_frozen')
conf.gc.dummy = True conf.gc.dummy = True
assert conf.gc.dummy is True assert conf.gc.dummy is True

View File

@ -24,7 +24,7 @@ import weakref
from .error import PropertiesOptionError, ConfigError, ConflictError from .error import PropertiesOptionError, ConfigError, ConflictError
from .option import OptionDescription, Option, SymLinkOption, \ from .option import OptionDescription, Option, SymLinkOption, \
DynSymLinkOption DynSymLinkOption, SynDynOptionDescription
from .option.baseoption import valid_name from .option.baseoption import valid_name
from .setting import groups, Settings, default_encoding, undefined from .setting import groups, Settings, default_encoding, undefined
from .storage import get_storages, get_storage, set_storage, \ from .storage import get_storages, get_storage, set_storage, \
@ -52,10 +52,8 @@ class SubConfig(object):
""" """
# main option description # main option description
error = False error = False
try: if descr is not None and not isinstance(descr, OptionDescription) and \
if descr is not None and not descr.impl_is_optiondescription(): # pragma: optional cover not isinstance(descr, SynDynOptionDescription): # pragma: optional cover
error = True
except AttributeError:
error = True error = True
if error: if error:
raise TypeError(_('descr must be an optiondescription, not {0}' raise TypeError(_('descr must be an optiondescription, not {0}'
@ -309,7 +307,7 @@ class SubConfig(object):
force_permissive=force_permissive) force_permissive=force_permissive)
def find_first(self, bytype=None, byname=None, byvalue=undefined, def find_first(self, bytype=None, byname=None, byvalue=undefined,
type_='option', display_error=True, check_properties=True, type_='option', raise_if_not_found=True, check_properties=True,
force_permissive=False): force_permissive=False):
""" """
finds an option recursively in the config finds an option recursively in the config
@ -321,12 +319,12 @@ class SubConfig(object):
""" """
return self._cfgimpl_get_context()._find( return self._cfgimpl_get_context()._find(
bytype, byname, byvalue, first=True, type_=type_, bytype, byname, byvalue, first=True, type_=type_,
_subpath=self.cfgimpl_get_path(False), display_error=display_error, _subpath=self.cfgimpl_get_path(False), raise_if_not_found=raise_if_not_found,
check_properties=check_properties, check_properties=check_properties,
force_permissive=force_permissive) force_permissive=force_permissive)
def _find(self, bytype, byname, byvalue, first, type_='option', def _find(self, bytype, byname, byvalue, first, type_='option',
_subpath=None, check_properties=True, display_error=True, _subpath=None, check_properties=True, raise_if_not_found=True,
force_permissive=False, only_path=undefined, force_permissive=False, only_path=undefined,
only_option=undefined, setting_properties=undefined): only_option=undefined, setting_properties=undefined):
""" """
@ -385,16 +383,13 @@ class SubConfig(object):
return retval return retval
else: else:
find_results.append(retval) find_results.append(retval)
return self._find_return_results(find_results, display_error) return self._find_return_results(find_results, raise_if_not_found)
def _find_return_results(self, find_results, display_error): def _find_return_results(self, find_results, raise_if_not_found):
if find_results == []: # pragma: optional cover if find_results == []: # pragma: optional cover
if display_error: if raise_if_not_found:
raise AttributeError(_("no option found in config" raise AttributeError(_("no option found in config"
" with these criteria")) " with these criteria"))
else:
# translation is slow
raise AttributeError()
else: else:
return find_results return find_results
@ -749,7 +744,7 @@ class GroupConfig(_CommonConfig):
pass pass
def find_firsts(self, byname=None, bypath=undefined, byoption=undefined, def find_firsts(self, byname=None, bypath=undefined, byoption=undefined,
byvalue=undefined, display_error=True, _sub=False, byvalue=undefined, raise_if_not_found=True, _sub=False,
check_properties=True): check_properties=True):
"""Find first not in current GroupConfig, but in each children """Find first not in current GroupConfig, but in each children
""" """
@ -762,32 +757,28 @@ class GroupConfig(_CommonConfig):
bypath = self._find(bytype=None, byvalue=undefined, byname=byname, bypath = self._find(bytype=None, byvalue=undefined, byname=byname,
first=True, type_='path', first=True, type_='path',
check_properties=None, check_properties=None,
display_error=display_error) raise_if_not_found=raise_if_not_found)
byname = None byname = None
byoption = self.cfgimpl_get_description( byoption = self.cfgimpl_get_description(
).impl_get_opt_by_path(bypath) ).impl_get_opt_by_path(bypath)
for child in self._impl_children: for child in self._impl_children:
try:
if isinstance(child, GroupConfig): if isinstance(child, GroupConfig):
ret.extend(child.find_firsts(byname=byname, bypath=bypath, ret.extend(child.find_firsts(byname=byname, bypath=bypath,
byoption=byoption, byoption=byoption,
byvalue=byvalue, byvalue=byvalue,
check_properties=check_properties, check_properties=check_properties,
display_error=False, raise_if_not_found=False,
_sub=True)) _sub=True))
else: elif child._find(None, byname, byvalue, first=True,
child._find(None, byname, byvalue, first=True, type_='path', raise_if_not_found=False,
type_='path', display_error=False,
check_properties=check_properties, check_properties=check_properties,
only_path=bypath, only_option=byoption) only_path=bypath, only_option=byoption):
ret.append(child) ret.append(child)
except AttributeError:
pass
if _sub: if _sub:
return ret return ret
else: else:
return GroupConfig(self._find_return_results(ret, display_error)) return GroupConfig(self._find_return_results(ret, raise_if_not_found))
def __repr__(self): def __repr__(self):
return object.__repr__(self) return object.__repr__(self)
@ -796,10 +787,7 @@ class GroupConfig(_CommonConfig):
ret = '' ret = ''
for child in self._impl_children: for child in self._impl_children:
ret += '({0})\n'.format(child._impl_name) ret += '({0})\n'.format(child._impl_name)
try:
ret += super(GroupConfig, self).__str__() ret += super(GroupConfig, self).__str__()
except ConfigError:
pass
return ret return ret
def getattr(self, name, force_permissive=False, validate=True, def getattr(self, name, force_permissive=False, validate=True,

View File

@ -218,7 +218,7 @@ class MasterSlaves(object):
raise err raise err
return multi return multi
def setitem(self, values, opt, value, path): def validate(self, values, opt, value, path):
if self.is_master(opt): if self.is_master(opt):
masterlen = len(value) masterlen = len(value)
#for regen slave path #for regen slave path

View File

@ -372,46 +372,41 @@ class Values(object):
setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False) setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False)
if 'validator' in setting_properties: if 'validator' in setting_properties:
fake_context = context._gen_fake_values() fake_context = context._gen_fake_values()
fake_context.cfgimpl_get_values()._setitem(opt, value, path, fake_values = fake_context.cfgimpl_get_values()
force_permissive, fake_values.validate(opt, value, path, check_frozen,
check_frozen, force_permissive, setting_properties)
setting_properties) fake_values._setvalue(opt, path, value)
opt.impl_validate(value, fake_context) opt.impl_validate(value, fake_context)
self._setitem(opt, value, path, force_permissive, check_frozen, self._setvalue(opt, path, value)
setting_properties, validate_properties=False)
def _setitem(self, opt, value, path, force_permissive, check_frozen, def _setvalue(self, opt, path, value):
setting_properties, validate_properties=True):
if opt.impl_is_master_slaves():
opt.impl_get_master_slaves().setitem(self, opt, value, path)
self._setvalue(opt, path, value, force_permissive=force_permissive,
check_frozen=check_frozen,
setting_properties=setting_properties,
validate_properties=validate_properties)
def _setvalue(self, opt, path, value, force_permissive=False,
check_frozen=True, validate_properties=True,
setting_properties=undefined, index=None):
context = self._getcontext() context = self._getcontext()
context.cfgimpl_reset_cache() context.cfgimpl_reset_cache()
owner = context.cfgimpl_get_settings().getowner()
# in storage, value must not be a multi
if isinstance(value, Multi): if isinstance(value, Multi):
value = list(value) value = list(value)
if opt.impl_is_submulti(): if opt.impl_is_submulti():
for idx, val in enumerate(value): for idx, val in enumerate(value):
if isinstance(val, SubMulti): if isinstance(val, SubMulti):
value[idx] = list(val) value[idx] = list(val)
setting = context.cfgimpl_get_settings()
owner = setting.getowner()
#FIXME pourquoi là et pas dans masterslaves ?? #FIXME pourquoi là et pas dans masterslaves ??
if opt.impl_is_master_slaves('slave') and index is None: if opt.impl_is_master_slaves('slave'):
self._p_.resetvalue(path) self._p_.resetvalue(path)
for idx, val in enumerate(value): for idx, val in enumerate(value):
self._p_.setvalue(path, val, owner, idx) self._p_.setvalue(path, val, owner, idx)
else: else:
self._p_.setvalue(path, value, owner, index) self._p_.setvalue(path, value, owner, None)
if validate_properties:
props = setting.validate_properties(opt, False, check_frozen, def validate(self, opt, value, path, check_frozen=True, force_permissive=False,
value=value, path=path, setting_properties=undefined, valid_masterslave=True):
if valid_masterslave and opt.impl_is_master_slaves():
opt.impl_get_master_slaves().validate(self, opt, value, path)
props = self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False,
check_frozen,
value=value,
path=path,
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=setting_properties) setting_properties=setting_properties)
if props: if props:
@ -837,9 +832,11 @@ class Multi(list):
return ret return ret
def _store(self, force=False): def _store(self, force=False):
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, values = self._getcontext().cfgimpl_get_values()
self, if not force:
validate_properties=not force) #FIXME could get properties an pass it
values.validate(self.opt, self, self.path, valid_masterslave=False)
values._setvalue(self.opt, self.path, self)
class SubMulti(Multi): class SubMulti(Multi):
@ -864,9 +861,9 @@ class SubMulti(Multi):
def _store(self, force=False): def _store(self, force=False):
#force is unused here #force is unused here
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, values = self._getcontext().cfgimpl_get_values()
self.path, values.validate(self.opt, self, self.path, valid_masterslave=False)
self.submulti()) values._setvalue(self.opt, self.path, self.submulti())
def _validate(self, value, fake_context, force_index, submulti=False): def _validate(self, value, fake_context, force_index, submulti=False):
if value is not None: if value is not None: