simplify config_bag
This commit is contained in:
@ -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):
|
||||
|
Reference in New Issue
Block a user