setting_properties > properties

This commit is contained in:
Emmanuel Garette 2018-08-18 07:51:04 +02:00
parent ee1f07ce7e
commit 493cec1b80
8 changed files with 66 additions and 76 deletions

View File

@ -457,11 +457,11 @@ def test_cache_master_and_slaves_master():
#has value #has value
idx_val2 = 0 idx_val2 = 0
val_val2 = None val_val2 = None
val_val2_props = {idx_val2: (val1_val2_props, None)} val_val2_props = {idx_val2: (val1_val2_props, None), None: (set(), None)}
compare(cfg._config.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)}, compare(cfg._config.cfgimpl_get_settings()._p_.get_cached(), {None: {None: (global_props, None)},
'val1': {None: (val1_props, None)}, 'val1': {None: (val1_props, None)},
'val1.val1': {None: (val1_val1_props, None)}, 'val1.val1': {None: (val1_val1_props, None)},
'val1.val2': val_val2_props}) 'val1.val2': val_val2_props})
compare(cfg._config.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([None], None)}, compare(cfg._config.cfgimpl_get_values()._p_.get_cached(), {'val1.val1': {None: ([None], None)},
'val1.val2': {idx_val2: (val_val2, None)}}) 'val1.val2': {idx_val2: (val_val2, None)}})
cfg.option('val1.val1').value.set([undefined, undefined]) cfg.option('val1.val1').value.set([undefined, undefined])

View File

@ -118,8 +118,7 @@ class CommonTiramisu(TiramisuHelp):
self.option_bag.path, self.option_bag.path,
self.option_bag.index, self.option_bag.index,
self.option_bag.config_bag) self.option_bag.config_bag)
if self.option_bag.config_bag.setting_properties: self.option_bag.config_bag.context.cfgimpl_get_settings().validate_properties(self.option_bag)
self.option_bag.config_bag.context.cfgimpl_get_settings().validate_properties(self.option_bag)
index = self.option_bag.index index = self.option_bag.index
if index is not None: if index is not None:
if option.impl_is_optiondescription() or not option.impl_is_master_slaves('slave'): if option.impl_is_optiondescription() or not option.impl_is_master_slaves('slave'):
@ -594,7 +593,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 self.config_bag.setting_properties: if self.config_bag.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()
@ -731,7 +730,7 @@ class TiramisuContextProperty(TiramisuContext):
settings = self.config_bag.context.cfgimpl_get_settings() settings = self.config_bag.context.cfgimpl_get_settings()
settings.read_only() settings.read_only()
try: try:
del self.config_bag.setting_properties del self.config_bag.properties
except AttributeError: except AttributeError:
pass pass
@ -742,7 +741,7 @@ class TiramisuContextProperty(TiramisuContext):
# #FIXME ? # #FIXME ?
settings.set_context_permissive(frozenset(['hidden'])) settings.set_context_permissive(frozenset(['hidden']))
try: try:
del self.config_bag.setting_properties del self.config_bag.properties
except AttributeError: except AttributeError:
pass pass
#/FIXME ? #/FIXME ?
@ -762,7 +761,7 @@ class TiramisuContextProperty(TiramisuContext):
def get(self): def get(self):
"""get all configuration properties""" """get all configuration properties"""
return set(self.config_bag.setting_properties) return set(self.config_bag.properties)
def set(self, props): def set(self, props):
"""personalise configuration properties""" """personalise configuration properties"""
@ -850,11 +849,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"""
if not self.config_bag.setting_properties: if not self.config_bag.properties:
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.properties = self.config_bag.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,
@ -867,7 +866,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 self.config_bag.setting_properties: if self.config_bag.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,
@ -885,7 +884,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 self.config_bag.setting_properties: if self.config_bag.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):
@ -976,14 +975,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 = not self._config.setting_properties force = not self._config.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 True: if force is True:
config_bag.setting_properties = frozenset() config_bag.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,7 +992,7 @@ class TiramisuAPI(TiramisuHelp):
config = self._config config = self._config
force = None force = None
config_bag = ConfigBag(context=config) config_bag = ConfigBag(context=config)
config_bag.setting_properties = frozenset() config_bag.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)

View File

@ -83,7 +83,7 @@ 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.setting_properties = frozenset() soption_bag.config_bag.properties = frozenset()
soption_bag.config_bag.remove_validation() soption_bag.config_bag.remove_validation()
try: try:
# get value # get value

View File

@ -252,8 +252,7 @@ class SubConfig(object):
if option_bag.option.impl_is_symlinkoption(): if option_bag.option.impl_is_symlinkoption():
raise ConfigError(_("can't assign to a SymLinkOption")) raise ConfigError(_("can't assign to a SymLinkOption"))
context = option_bag.config_bag.context context = option_bag.config_bag.context
if option_bag.config_bag.setting_properties: context.cfgimpl_get_settings().validate_properties(option_bag)
context.cfgimpl_get_settings().validate_properties(option_bag)
self.cfgimpl_get_description().impl_validate_value(option_bag.option, self.cfgimpl_get_description().impl_validate_value(option_bag.option,
value, value,
self) self)
@ -295,8 +294,7 @@ class SubConfig(object):
else: else:
fromconsistency = None fromconsistency = None
if option_bag.config_bag.setting_properties: self.cfgimpl_get_settings().validate_properties(option_bag)
self.cfgimpl_get_settings().validate_properties(option_bag)
return SubConfig(option_bag.option, return SubConfig(option_bag.option,
self._impl_context, self._impl_context,
option_bag.config_bag, option_bag.config_bag,
@ -334,8 +332,7 @@ class SubConfig(object):
return context.getattr(soption_bag.path, return context.getattr(soption_bag.path,
soption_bag) soption_bag)
if config_bag.setting_properties: self.cfgimpl_get_settings().validate_properties(option_bag)
self.cfgimpl_get_settings().validate_properties(option_bag)
if option.impl_is_master_slaves('slave'): if option.impl_is_master_slaves('slave'):
length = self.cfgimpl_get_length_slave(option_bag) length = self.cfgimpl_get_length_slave(option_bag)
@ -405,7 +402,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.setting_properties: elif config_bag.properties:
#remove option with propertyerror, ... #remove option with propertyerror, ...
try: try:
if '.' in path: if '.' in path:
@ -566,8 +563,7 @@ class SubConfig(object):
option_bag.config_bag) option_bag.config_bag)
ret.append(self.getattr(name, ret.append(self.getattr(name,
soption_bag)) soption_bag))
elif option_bag.config_bag.setting_properties: self.cfgimpl_get_settings().validate_properties(option_bag)
self.cfgimpl_get_settings().validate_properties(option_bag)
else: else:
ret = self.getattr(name, ret = self.getattr(name,
option_bag) option_bag)

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 'validator' in config_bag.setting_properties: not 'validator' in config_bag.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 'validator' in config_bag.setting_properties: if config_bag is not undefined and not 'validator' in config_bag.properties:
return return
all_values = [] all_values = []
if length is None: if length is None:

View File

@ -159,8 +159,9 @@ class OptionBag:
class ConfigBag: class ConfigBag:
__slots__ = ('context', # link to the current config (context) __slots__ = ('context', # link to the current context
'setting_properties', # properties for current config 'properties', # properties for current context
#'permissives', # permissives for current context
'force_permissive', # force permissive 'force_permissive', # force permissive
'_validate', # validate '_validate', # validate
) )
@ -172,19 +173,19 @@ class ConfigBag:
setattr(self, key, value) setattr(self, key, value)
def __getattr__(self, key): def __getattr__(self, key):
if key == 'setting_properties': if key == 'properties':
self.setting_properties = self.context.cfgimpl_get_settings().get_context_properties() self.properties = self.context.cfgimpl_get_settings().get_context_properties()
return self.setting_properties return self.properties
raise KeyError('unknown key {} for ConfigBag'.format(key)) raise KeyError('unknown key {} for ConfigBag'.format(key))
def remove_validation(self): def remove_validation(self):
self._validate = 'validator' in self.setting_properties self._validate = 'validator' in self.properties
if self._validate: if self._validate:
self.setting_properties = frozenset(self.setting_properties - {'validator'}) self.properties = frozenset(self.properties - {'validator'})
def restore_validation(self): def restore_validation(self):
if self._validate: if self._validate:
self.setting_properties = frozenset(self.setting_properties | {'validator'}) self.properties = frozenset(self.properties | {'validator'})
def copy(self): def copy(self):
kwargs = {} kwargs = {}
@ -341,7 +342,7 @@ class Settings(object):
is_cached, props = self._p_.getcache(None, is_cached, props = self._p_.getcache(None,
None, None,
None, None,
None, {},
None, None,
'context_props') 'context_props')
if not is_cached: if not is_cached:
@ -372,7 +373,7 @@ class Settings(object):
path = opt.impl_getpath(self._getcontext()) path = opt.impl_getpath(self._getcontext())
if apply_requires: if apply_requires:
props = config_bag.setting_properties props = config_bag.properties
is_cached, props = self._p_.getcache(path, is_cached, props = self._p_.getcache(path,
expires_time, expires_time,
index, index,
@ -398,8 +399,9 @@ class Settings(object):
self._p_.setcache(path, self._p_.setcache(path,
index, index,
props, props,
config_bag.setting_properties, config_bag.properties,
config_bag.setting_properties) #FIXME
config_bag.properties)
return props return props
def get_context_permissive(self): def get_context_permissive(self):
@ -502,7 +504,7 @@ class Settings(object):
idx, idx,
config_bag) config_bag)
if option_bag.option == option: if option_bag.option == option:
soption_bag.config_bag.setting_properties = frozenset() soption_bag.config_bag.properties = frozenset()
soption_bag.config_bag.remove_validation() soption_bag.config_bag.remove_validation()
soption_bag.apply_requires = False soption_bag.apply_requires = False
try: try:
@ -694,13 +696,15 @@ class Settings(object):
:param force_permissive: behaves as if the permissive property :param force_permissive: behaves as if the permissive property
was present was present
""" """
# calc properties
config_bag = option_bag.config_bag config_bag = option_bag.config_bag
properties = option_bag.properties & config_bag.setting_properties - {'frozen', 'mandatory', 'empty'} if not config_bag.properties:
return
# calc properties
properties = option_bag.properties & config_bag.properties - {'frozen', 'mandatory', 'empty'}
# remove permissive properties # remove permissive properties
if (config_bag.force_permissive is True or \ if (config_bag.force_permissive is True or \
'permissive' in config_bag.setting_properties) and properties: 'permissive' in config_bag.properties) and properties:
# remove global permissive if need # remove global permissive if need
properties -= self.get_context_permissive() properties -= self.get_context_permissive()
# at this point an option should not remain in properties # at this point an option should not remain in properties
@ -712,11 +716,11 @@ 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: if 'mandatory' in option_bag.config_bag.properties:
values = self._getcontext().cfgimpl_get_values() values = self._getcontext().cfgimpl_get_values()
is_mandatory = False is_mandatory = False
if (option_bag.config_bag.force_permissive is True or \ if (option_bag.config_bag.force_permissive is True or \
'permissive' in option_bag.config_bag.setting_properties) and \ 'permissive' in option_bag.config_bag.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(option_bag.option, elif 'mandatory' in option_bag.properties and values.isempty(option_bag.option,
@ -735,11 +739,11 @@ class Settings(object):
def validate_frozen(self, def validate_frozen(self,
option_bag): option_bag):
if option_bag.config_bag.setting_properties and \ if option_bag.config_bag.properties and \
('everything_frozen' in option_bag.config_bag.setting_properties or ('everything_frozen' in option_bag.config_bag.properties or
'frozen' in option_bag.properties) and \ 'frozen' in option_bag.properties) and \
not ((option_bag.config_bag.force_permissive is True or not ((option_bag.config_bag.force_permissive is True or
'permissive' in option_bag.config_bag.setting_properties) and 'permissive' in option_bag.config_bag.properties) and
'frozen' in self.get_context_permissive()): 'frozen' in self.get_context_permissive()):
raise PropertiesOptionError(option_bag, raise PropertiesOptionError(option_bag,
['frozen'], ['frozen'],

View File

@ -37,7 +37,8 @@ class Cache(DictCache):
"""add val in cache for a specified path """add val in cache for a specified path
if slave, add index if slave, add index
""" """
if props is None or 'cache' in props or \ #FIXME self_props n'est pas None !
if 'cache' in props or \
(self_props is not None and 'cache' in self_props): (self_props is not None and 'cache' in self_props):
if DEBUG: if DEBUG:
print('setcache {} with index {} and value {} in {} ({})'.format(path, index, val, print('setcache {} with index {} and value {} in {} ({})'.format(path, index, val,
@ -61,7 +62,7 @@ class Cache(DictCache):
self_props, self_props,
type_): type_):
no_cache = False, None no_cache = False, None
if props is None or 'cache' in props: if 'cache' in props:
indexed = self._getcache(path, index) indexed = self._getcache(path, index)
if indexed is None: if indexed is None:
return no_cache return no_cache
@ -74,7 +75,7 @@ class Cache(DictCache):
# so value is self_props # so value is self_props
self_props = value self_props = value
# recheck "cache" value # recheck "cache" value
if props is None or 'cache' in props or (self_props is not None and 'cache' in props): if 'cache' in props or (self_props is not None and 'cache' in props):
if expires_time and timestamp and \ if expires_time and timestamp and \
(props is not None and 'expire' in props or \ (props is not None and 'expire' in props or \
self_props is not None and 'expire' in self_props): self_props is not None and 'expire' in self_props):

View File

@ -67,19 +67,10 @@ class Values(object):
"""get value directly in cache if set """get value directly in cache if set
otherwise calculated value and set it in cache otherwise calculated value and set it in cache
:param opt: the `Option` that we want to get value
:param path: the path of the `Option`
:param validate: the value must be valid
:param force_permissive: force permissive when check properties
:param setting_properties: global properties
:param self_properties: properties for this `Option`
:param index: index for a slave `Option`
:param display_warnings: display warnings or not
:returns: value :returns: value
""" """
# try to retrive value in cache # try to retrive value in cache
setting_properties = option_bag.config_bag.setting_properties setting_properties = option_bag.config_bag.properties
is_cached, value = self._p_.getcache(option_bag.path, is_cached, value = self._p_.getcache(option_bag.path,
expires_time, expires_time,
option_bag.index, option_bag.index,
@ -97,7 +88,7 @@ class Values(object):
option_bag, option_bag,
context=context, context=context,
check_error=True) check_error=True)
if setting_properties and 'warnings' in setting_properties: if 'warnings' in setting_properties:
opt.impl_validate(value, opt.impl_validate(value,
option_bag, option_bag,
context=context, context=context,
@ -172,7 +163,7 @@ class Values(object):
is_cache, cache_value = self._p_.getcache(option_bag.path, is_cache, cache_value = self._p_.getcache(option_bag.path,
expires_time, expires_time,
index, index,
config_bag.setting_properties, config_bag.properties,
option_bag.properties, option_bag.properties,
'value') 'value')
if is_cache and cache_value == _value: if is_cache and cache_value == _value:
@ -295,7 +286,7 @@ class Values(object):
context = self._getcontext() context = self._getcontext()
owner = context.cfgimpl_get_settings().getowner() owner = context.cfgimpl_get_settings().getowner()
if 'validator' in option_bag.config_bag.setting_properties: if 'validator' in option_bag.config_bag.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, ...)
@ -336,8 +327,7 @@ class Values(object):
option_bag, option_bag,
context, context,
check_error=True) check_error=True)
if option_bag.config_bag.setting_properties and \ if 'warnings' in option_bag.config_bag.properties:
'warnings' in option_bag.config_bag.setting_properties:
# No error found so emit warnings # No error found so emit warnings
opt.impl_validate(value, opt.impl_validate(value,
option_bag, option_bag,
@ -468,7 +458,7 @@ 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 'validator' in option_bag.config_bag.setting_properties: if hasvalue and 'validator' in option_bag.config_bag.properties:
option_bag.config_bag.remove_validation() option_bag.config_bag.remove_validation()
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()
@ -503,7 +493,7 @@ 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 'validator' in option_bag.config_bag.setting_properties: if 'validator' in option_bag.config_bag.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()
option_bag.config_bag.remove_validation() option_bag.config_bag.remove_validation()
@ -576,8 +566,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.properties
config_bag.setting_properties = od_setting_properties config_bag.properties = od_setting_properties
try: try:
option_bag = OptionBag() option_bag = OptionBag()
option_bag.set_option(option, option_bag.set_option(option,
@ -589,7 +579,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.properties = ori_setting_properties
for path in self._mandatory_warnings(context, for path in self._mandatory_warnings(context,
config_bag, config_bag,
option, option,
@ -635,11 +625,11 @@ class Values(object):
""" """
context = self._getcontext() context = self._getcontext()
# copy # copy
od_setting_properties = config_bag.setting_properties - {'mandatory', 'empty'} od_setting_properties = config_bag.properties - {'mandatory', 'empty'}
setting_properties = set(config_bag.setting_properties) - {'warnings'} setting_properties = set(config_bag.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.properties = frozenset(setting_properties)
config_bag.force_permissive = True config_bag.force_permissive = True
descr = context.cfgimpl_get_description() descr = context.cfgimpl_get_description()