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

View File

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

View File

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

View File

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

View File

@ -197,7 +197,7 @@ class Option(OnlyOption):
force_index = None
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
self.valid_consistency(option_bag,
value,
@ -620,7 +620,7 @@ class Option(OnlyOption):
else:
all_cons_vals.append((is_multi, opt_value))
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
all_values = []
if length is None:

View File

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

View File

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