simplify config_bag

This commit is contained in:
Emmanuel Garette 2018-08-17 23:11:25 +02:00
parent bf519499d4
commit ee1f07ce7e
7 changed files with 67 additions and 104 deletions

View File

@ -594,7 +594,7 @@ class TiramisuOption(CommonTiramisu):
groups.GroupType): groups.GroupType):
raise TypeError(_("unknown group_type: {0}").format(group_type)) raise TypeError(_("unknown group_type: {0}").format(group_type))
def _filter(opt): def _filter(opt):
if not self.config_bag.force_unrestraint: if self.config_bag.setting_properties:
name = opt.impl_getname() name = opt.impl_getname()
path = subconfig._get_subpath(name) path = subconfig._get_subpath(name)
option_bag = OptionBag() option_bag = OptionBag()
@ -850,12 +850,11 @@ class TiramisuContextOption(TiramisuContext):
withoption=None, withoption=None,
fullpath=False): fullpath=False):
"""return dict with path as key and value""" """return dict with path as key and value"""
#config_bag = self.config_bag if not self.config_bag.setting_properties:
if self.config_bag.setting_properties is None:
config_bag = self.config_bag config_bag = self.config_bag
else: else:
config_bag = self.config_bag.copy() config_bag = self.config_bag.copy()
config_bag._setting_properties = self.config_bag.setting_properties - {'warnings'} config_bag.setting_properties = self.config_bag.setting_properties - {'warnings'}
return config_bag.context.make_dict(config_bag, return config_bag.context.make_dict(config_bag,
flatten=flatten, flatten=flatten,
fullpath=fullpath, fullpath=fullpath,
@ -868,7 +867,7 @@ class TiramisuContextOption(TiramisuContext):
recursive=False): recursive=False):
"""list content of an optiondescription""" """list content of an optiondescription"""
def _filter(opt): def _filter(opt):
if not self.config_bag.force_unrestraint: if self.config_bag.setting_properties:
name = opt.impl_getname() name = opt.impl_getname()
option_bag = OptionBag() option_bag = OptionBag()
option_bag.set_option(opt, option_bag.set_option(opt,
@ -886,7 +885,7 @@ class TiramisuContextOption(TiramisuContext):
if recursive: if recursive:
if group_type: if group_type:
raise APIError(_('recursive with group_type is not implemented yet')) raise APIError(_('recursive with group_type is not implemented yet'))
if not self.config_bag.force_unrestraint: if self.config_bag.setting_properties:
raise APIError(_('not implemented yet')) raise APIError(_('not implemented yet'))
for option in self.config_bag.context.cfgimpl_get_description()._cache_paths[1]: for option in self.config_bag.context.cfgimpl_get_description()._cache_paths[1]:
if type == 'optiondescription' and not isinstance(option, OptionDescription): if type == 'optiondescription' and not isinstance(option, OptionDescription):
@ -977,14 +976,14 @@ class TiramisuAPI(TiramisuHelp):
if subfunc == 'forcepermissive': if subfunc == 'forcepermissive':
if isinstance(self._config, ConfigBag): if isinstance(self._config, ConfigBag):
config = self._config.config config = self._config.config
force = self._config.force_unrestraint force = not self._config.setting_properties
else: else:
config = self._config config = self._config
force = None force = None
config_bag = ConfigBag(context=config, config_bag = ConfigBag(context=config,
force_permissive=True) force_permissive=True)
if force is not None: if force is True:
config_bag.force_unrestraint = force config_bag.setting_properties = frozenset()
return TiramisuAPI(config_bag) return TiramisuAPI(config_bag)
elif subfunc == 'unrestraint': elif subfunc == 'unrestraint':
if isinstance(self._config, ConfigBag): if isinstance(self._config, ConfigBag):
@ -993,8 +992,8 @@ class TiramisuAPI(TiramisuHelp):
else: else:
config = self._config config = self._config
force = None force = None
config_bag = ConfigBag(context=config, config_bag = ConfigBag(context=config)
force_unrestraint=True) config_bag.setting_properties = frozenset()
if force is not None: if force is not None:
config_bag.force_permissive = force config_bag.force_permissive = force
return TiramisuAPI(config_bag) return TiramisuAPI(config_bag)
@ -1018,9 +1017,8 @@ class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
config = self.config_bag.context config = self.config_bag.context
for spath in spaths: for spath in spaths:
config = config.getconfig(spath) config = config.getconfig(spath)
config_bag = ConfigBag(context=config, config_bag = self.config_bag.copy()
force_unrestraint=self.config_bag.force_unrestraint, config_bag.context = config
force_permissive=self.config_bag.force_permissive)
return TiramisuAPI(config_bag) return TiramisuAPI(config_bag)

View File

@ -83,8 +83,8 @@ def manager_callback(callbk: Union[ParamOption, ParamValue],
if option_bag.fromconsistency: if option_bag.fromconsistency:
soption_bag.fromconsistency = option_bag.fromconsistency.copy() soption_bag.fromconsistency = option_bag.fromconsistency.copy()
if opt == option: if opt == option:
soption_bag.config_bag.force_unrestraint = True soption_bag.config_bag.setting_properties = frozenset()
soption_bag.config_bag.validate = False soption_bag.config_bag.remove_validation()
try: try:
# get value # get value
value = context.getattr(path, value = context.getattr(path,

View File

@ -79,7 +79,7 @@ class SubConfig(object):
masterpath = master.impl_getname() masterpath = master.impl_getname()
full_masterpath = self._get_subpath(masterpath) full_masterpath = self._get_subpath(masterpath)
cconfig_bag = config_bag.copy() cconfig_bag = config_bag.copy()
cconfig_bag.validate = False cconfig_bag.remove_validation()
moption_bag = OptionBag() moption_bag = OptionBag()
moption_bag.set_option(master, moption_bag.set_option(master,
full_masterpath, full_masterpath,
@ -359,7 +359,6 @@ class SubConfig(object):
soption_bag)) soption_bag))
else: else:
value = self.cfgimpl_get_values().get_cached_value(option_bag) value = self.cfgimpl_get_values().get_cached_value(option_bag)
if config_bag.validate_properties:
self.cfgimpl_get_settings().validate_mandatory(value, self.cfgimpl_get_settings().validate_mandatory(value,
option_bag) option_bag)
return value return value
@ -406,7 +405,7 @@ class SubConfig(object):
config_bag) config_bag)
if byvalue is not undefined and not _filter_by_value(option_bag): if byvalue is not undefined and not _filter_by_value(option_bag):
continue continue
elif config_bag.validate_properties: elif config_bag.setting_properties:
#remove option with propertyerror, ... #remove option with propertyerror, ...
try: try:
if '.' in path: if '.' in path:
@ -1034,7 +1033,7 @@ class KernelMetaConfig(KernelGroupConfig):
path, path,
config_bag): config_bag):
rconfig_bag = config_bag.copy() rconfig_bag = config_bag.copy()
rconfig_bag.validate = False rconfig_bag.remove_validation()
subconfig, name = self.cfgimpl_get_home_by_path(path, subconfig, name = self.cfgimpl_get_home_by_path(path,
config_bag) config_bag)
option = subconfig.cfgimpl_get_description().impl_getchild(name, option = subconfig.cfgimpl_get_description().impl_getchild(name,

View File

@ -108,7 +108,7 @@ class MasterSlaves(OptionDescription):
_commit=True): _commit=True):
config_bag = option_bag.config_bag.copy() config_bag = option_bag.config_bag.copy()
config_bag.validate = False config_bag.remove_validation()
for slave in self.getslaves(): for slave in self.getslaves():
slave_path = slave.impl_getpath(values._getcontext()) slave_path = slave.impl_getpath(values._getcontext())
soption_bag = OptionBag() soption_bag = OptionBag()
@ -129,7 +129,7 @@ class MasterSlaves(OptionDescription):
if slaves is undefined: if slaves is undefined:
slaves = self.getslaves() slaves = self.getslaves()
config_bag = option_bag.config_bag.copy() config_bag = option_bag.config_bag.copy()
config_bag.validate = False config_bag.remove_validation()
for slave in slaves: for slave in slaves:
slave_path = slave.impl_getpath(context) slave_path = slave.impl_getpath(context)
slavelen = values._p_.get_max_length(slave_path) slavelen = values._p_.get_max_length(slave_path)

View File

@ -197,7 +197,7 @@ class Option(OnlyOption):
force_index = None force_index = None
if check_error and config_bag is not undefined and \ if check_error and config_bag is not undefined and \
not config_bag.validate: not 'validator' in config_bag.setting_properties:
# just to check propertieserror # just to check propertieserror
self.valid_consistency(option_bag, self.valid_consistency(option_bag,
value, value,
@ -620,7 +620,7 @@ class Option(OnlyOption):
else: else:
all_cons_vals.append((is_multi, opt_value)) all_cons_vals.append((is_multi, opt_value))
all_cons_opts.append(opt) all_cons_opts.append(opt)
if config_bag is not undefined and not config_bag.validate: if config_bag is not undefined and not 'validator' in config_bag.setting_properties:
return return
all_values = [] all_values = []
if length is None: if length is None:

View File

@ -160,61 +160,36 @@ class OptionBag:
class ConfigBag: class ConfigBag:
__slots__ = ('context', # link to the current config (context) __slots__ = ('context', # link to the current config (context)
'_setting_properties', # properties for current config 'setting_properties', # properties for current config
'force_permissive', # force permissive 'force_permissive', # force permissive
'force_unrestraint', # do not validate properties
'_validate', # validate '_validate', # validate
) )
def __init__(self, context, **kwargs): def __init__(self, context, **kwargs):
self.force_permissive = False self.force_permissive = False
self.force_unrestraint = False
self.context = context self.context = context
self._validate = True self._validate = True
for key, value in kwargs.items(): for key, value in kwargs.items():
setattr(self, key, value) setattr(self, key, value)
def __getattr__(self, key): def __getattr__(self, key):
if key == 'validate_properties':
return not self.force_unrestraint
if key == 'validate':
if self._validate and self._setting_properties is not None:
return 'validator' in self._setting_properties
return self._validate
if key == 'setting_properties': if key == 'setting_properties':
#if self.force_unrestraint or not self._validate: self.setting_properties = self.context.cfgimpl_get_settings().get_context_properties()
if self.force_unrestraint: return self.setting_properties
return None
return self._setting_properties
if key == '_setting_properties':
self._setting_properties = self.context.cfgimpl_get_settings().get_context_properties()
if self._validate is False:
self._setting_properties = self._setting_properties - {'validator'}
return self._setting_properties
if key not in self.__slots__:
raise KeyError('unknown key {} for ConfigBag'.format(key)) raise KeyError('unknown key {} for ConfigBag'.format(key))
return None
def __setattr__(self, key, value): def remove_validation(self):
if key == 'validate': self._validate = 'validator' in self.setting_properties
self._validate = value if self._validate:
#FIXME self.setting_properties = frozenset(self.setting_properties - {'validator'})
try:
del self._setting_properties def restore_validation(self):
except AttributeError: if self._validate:
pass self.setting_properties = frozenset(self.setting_properties | {'validator'})
else:
super().__setattr__(key, value)
def copy(self): def copy(self):
kwargs = {} kwargs = {}
for key in self.__slots__: for key in self.__slots__:
if key == 'fromconsistency' and self.fromconsistency != []: kwargs[key] = getattr(self, key)
kwargs['fromconsistency'] = copy(self.fromconsistency)
value = getattr(self, key)
if key == '_validate' and value == True:
pass
else:
kwargs[key] = value
return ConfigBag(**kwargs) return ConfigBag(**kwargs)
@ -527,13 +502,14 @@ class Settings(object):
idx, idx,
config_bag) config_bag)
if option_bag.option == option: if option_bag.option == option:
soption_bag.config_bag.force_unrestraint = True soption_bag.config_bag.setting_properties = frozenset()
soption_bag.config_bag.validate = False soption_bag.config_bag.remove_validation()
soption_bag.apply_requires = False soption_bag.apply_requires = False
try: try:
value = context.getattr(reqpath, value = context.getattr(reqpath,
soption_bag) soption_bag)
if is_indexed: #if is_indexed:
if is_indexed and option_bag.index is not None:
value = value[option_bag.index] value = value[option_bag.index]
except PropertiesOptionError as err: except PropertiesOptionError as err:
properties = err.proptype properties = err.proptype
@ -736,19 +712,18 @@ class Settings(object):
def validate_mandatory(self, def validate_mandatory(self,
value, value,
option_bag): option_bag):
if 'mandatory' in option_bag.config_bag.setting_properties:
values = self._getcontext().cfgimpl_get_values() values = self._getcontext().cfgimpl_get_values()
opt = option_bag.option
config_bag = option_bag.config_bag
is_mandatory = False is_mandatory = False
if config_bag.setting_properties and 'mandatory' in config_bag.setting_properties: if (option_bag.config_bag.force_permissive is True or \
if (config_bag.force_permissive is True or 'permissive' in config_bag.setting_properties) and \ 'permissive' in option_bag.config_bag.setting_properties) and \
'mandatory' in self.get_context_permissive(): 'mandatory' in self.get_context_permissive():
pass pass
elif 'mandatory' in option_bag.properties and values.isempty(opt, elif 'mandatory' in option_bag.properties and values.isempty(option_bag.option,
value, value,
index=option_bag.index): index=option_bag.index):
is_mandatory = True is_mandatory = True
if 'empty' in option_bag.properties and values.isempty(opt, if 'empty' in option_bag.properties and values.isempty(option_bag.option,
value, value,
force_allow_empty_list=True, force_allow_empty_list=True,
index=option_bag.index): index=option_bag.index):

View File

@ -295,25 +295,21 @@ class Values(object):
context = self._getcontext() context = self._getcontext()
owner = context.cfgimpl_get_settings().getowner() owner = context.cfgimpl_get_settings().getowner()
if option_bag.config_bag.validate: if 'validator' in option_bag.config_bag.setting_properties:
if option_bag.index is not None or option_bag.option._has_consistencies(context): if option_bag.index is not None or option_bag.option._has_consistencies(context):
# set value to a fake config when option has dependency # set value to a fake config when option has dependency
# validation will be complet in this case (consistency, ...) # validation will be complet in this case (consistency, ...)
tested_context = context._gen_fake_values() tested_context = context._gen_fake_values()
#sconfig_bag = config_bag.copy() option_bag.config_bag.remove_validation()
ori_validate = option_bag.config_bag.validate
if ori_validate is True:
option_bag.config_bag.validate = False
try: try:
tested_context.cfgimpl_get_values().setvalue(value, tested_context.cfgimpl_get_values().setvalue(value,
option_bag, option_bag,
True) True)
option_bag.config_bag.validate = True option_bag.config_bag.restore_validation()
tested_context.getattr(option_bag.path, tested_context.getattr(option_bag.path,
option_bag) option_bag)
except Exception as exc: except Exception as exc:
if ori_validate is True: option_bag.config_bag.restore_validation()
option_bag.config_bag.validate = True
raise exc raise exc
else: else:
self.setvalue_validation(value, self.setvalue_validation(value,
@ -418,7 +414,6 @@ class Values(object):
option_bag.path = opt.impl_getpath(context) option_bag.path = opt.impl_getpath(context)
self_properties = option_bag.properties self_properties = option_bag.properties
settings = context.cfgimpl_get_settings() settings = context.cfgimpl_get_settings()
if option_bag.config_bag.setting_properties is not None:
settings.validate_properties(option_bag) settings.validate_properties(option_bag)
if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties: if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
return owners.default return owners.default
@ -473,15 +468,12 @@ class Values(object):
setting = context.cfgimpl_get_settings() setting = context.cfgimpl_get_settings()
hasvalue = self._p_.hasvalue(option_bag.path) hasvalue = self._p_.hasvalue(option_bag.path)
if hasvalue and option_bag.config_bag.validate: if hasvalue and 'validator' in option_bag.config_bag.setting_properties:
ori_validate = option_bag.config_bag.validate option_bag.config_bag.remove_validation()
if ori_validate is True:
option_bag.config_bag.validate = False
fake_context = context._gen_fake_values() fake_context = context._gen_fake_values()
fake_value = fake_context.cfgimpl_get_values() fake_value = fake_context.cfgimpl_get_values()
fake_value.reset(option_bag) fake_value.reset(option_bag)
if ori_validate is True: option_bag.config_bag.restore_validation()
option_bag.config_bag.validate = True
value = fake_value.getdefaultvalue(option_bag) value = fake_value.getdefaultvalue(option_bag)
fake_value.setvalue_validation(value, fake_value.setvalue_validation(value,
option_bag) option_bag)
@ -511,19 +503,18 @@ class Values(object):
if self._p_.hasvalue(option_bag.path, index=option_bag.index): if self._p_.hasvalue(option_bag.path, index=option_bag.index):
context = self._getcontext() context = self._getcontext()
if option_bag.config_bag.validate: if 'validator' in option_bag.config_bag.setting_properties:
fake_context = context._gen_fake_values() fake_context = context._gen_fake_values()
fake_value = fake_context.cfgimpl_get_values() fake_value = fake_context.cfgimpl_get_values()
ori_validate = option_bag.config_bag.validate option_bag.config_bag.remove_validation()
option_bag.config_bag.validate = False
try: try:
fake_value.reset_slave(option_bag) fake_value.reset_slave(option_bag)
value = fake_value.getdefaultvalue(option_bag) value = fake_value.getdefaultvalue(option_bag)
fake_value.setvalue_validation(value, fake_value.setvalue_validation(value,
option_bag) option_bag)
option_bag.config_bag.validate = ori_validate option_bag.config_bag.restore_validation()
except Exception as err: except Exception as err:
option_bag.config_bag.validate = ori_validate option_bag.config_bag.restore_validation()
raise err raise err
self._p_.resetvalue_index(option_bag.path, option_bag.index) self._p_.resetvalue_index(option_bag.path, option_bag.index)
context.cfgimpl_reset_cache(option_bag) context.cfgimpl_reset_cache(option_bag)
@ -585,8 +576,8 @@ class Values(object):
path = '.'.join(currpath + [name]) path = '.'.join(currpath + [name])
if option.impl_is_optiondescription(): if option.impl_is_optiondescription():
ori_setting_properties = config_bag._setting_properties ori_setting_properties = config_bag.setting_properties
config_bag._setting_properties = od_setting_properties config_bag.setting_properties = od_setting_properties
try: try:
option_bag = OptionBag() option_bag = OptionBag()
option_bag.set_option(option, option_bag.set_option(option,
@ -598,7 +589,7 @@ class Values(object):
except PropertiesOptionError as err: except PropertiesOptionError as err:
pass pass
else: else:
config_bag._setting_properties = ori_setting_properties config_bag.setting_properties = ori_setting_properties
for path in self._mandatory_warnings(context, for path in self._mandatory_warnings(context,
config_bag, config_bag,
option, option,
@ -648,7 +639,7 @@ class Values(object):
setting_properties = set(config_bag.setting_properties) - {'warnings'} setting_properties = set(config_bag.setting_properties) - {'warnings'}
setting_properties.update(['mandatory', 'empty']) setting_properties.update(['mandatory', 'empty'])
config_bag = ConfigBag(context=config_bag.context) config_bag = ConfigBag(context=config_bag.context)
config_bag._setting_properties = frozenset(setting_properties) config_bag.setting_properties = frozenset(setting_properties)
config_bag.force_permissive = True config_bag.force_permissive = True
descr = context.cfgimpl_get_description() descr = context.cfgimpl_get_description()