tiramisu/setting.py: _get_properties and validate_properties are now more easier to read and has best performance

This commit is contained in:
Emmanuel Garette 2013-05-02 11:23:04 +02:00
parent 2c5bbb7bc0
commit bcfc0cd41b

View File

@ -201,20 +201,20 @@ class Setting(object):
raise ValueError('you must only append/remove properties')
def _get_properties(self, opt=None, is_apply_req=True):
if opt is not None and opt in self._cache:
exp = time()
props, created = self._cache[opt]
if exp < created:
return props
if opt is None:
default = []
props = self._properties.get(opt, [])
else:
exp = None
if opt in self._cache:
exp = time()
props, created = self._cache[opt]
if exp < created:
return props
if is_apply_req:
apply_requires(opt, self.context)
default = list(opt._properties)
props = self._properties.get(opt, default)
if opt is not None:
self._set_cache(opt, props)
props = self._properties.get(opt, default)
self._set_cache(opt, props, exp)
return props
def append(self, propname):
@ -238,61 +238,47 @@ class Setting(object):
else:
self._properties[opt] = properties
def _validate_frozen(self, opt, value, is_write):
if not is_write:
return False
if 'permissive' in self and 'frozen' in self._get_permissive():
return False
if 'everything_frozen' in self or (
'frozen' in self and 'frozen' in self[opt]):
return True
return False
def _validate_mandatory(self, opt, value, force_properties):
if 'permissive' in self and 'mandatory' in self._get_permissive():
return False
check_mandatory = 'mandatory' in self
if force_properties is not None:
check_mandatory = ('mandatory' in force_properties or
check_mandatory)
if check_mandatory and 'mandatory' in self[opt] and \
self.context.cfgimpl_get_values()._is_empty(opt, value):
return True
return False
def _calc_properties(self, opt_or_descr, force_permissive, force_properties):
properties = set(self._get_properties(opt_or_descr))
#remove this properties, those properties are validate in after
properties = properties - set(['mandatory', 'frozen'])
set_properties = set(self._get_properties())
if force_properties is not None:
set_properties.update(set(force_properties))
properties = properties & set_properties
if force_permissive is True or 'permissive' in self:
properties = properties - set(self._get_permissive())
properties = properties - set(self._get_permissive(opt_or_descr))
return list(properties)
#____________________________________________________________
def validate_properties(self, opt_or_descr, is_descr, is_write,
value=None, force_permissive=False,
force_properties=None):
properties = self._calc_properties(opt_or_descr, force_permissive,
force_properties)
raise_text = _("trying to access"
" to an option named: {0} with properties"
" {1}")
if not is_descr:
if self._validate_mandatory(opt_or_descr, value, force_properties):
properties.append('mandatory')
if self._validate_frozen(opt_or_descr, value, is_write):
properties.append('frozen')
raise_text = _('cannot change the value for '
'option {0} this option is frozen')
if properties != []:
raise PropertiesOptionError(raise_text.format(opt_or_descr._name,
str(properties)),
properties)
#opt properties
properties = set(self._get_properties(opt_or_descr))
#remove opt permissive
properties -= frozenset(self._get_permissive(opt_or_descr))
#remove global permissive if need
self_properties = self._get_properties()
if force_permissive is True or 'permissive' in self_properties:
properties -= frozenset(self._get_permissive())
#global properties
set_properties = set(self_properties)
if force_properties is not None:
set_properties.update(frozenset(force_properties))
#calc properties
properties &= set_properties
#mandatory and frozen are special properties
if is_descr:
properties -= frozenset(['mandatory', 'frozen'])
else:
if 'mandatory' in properties and \
not self.context.cfgimpl_get_values()._is_empty(opt_or_descr,
value):
properties.remove('mandatory')
if is_write and 'everything_frozen' in self_properties:
properties.add('frozen')
elif 'frozen' in properties and not is_write:
properties.remove('frozen')
if properties != frozenset():
if 'frozen' in properties:
raise_text = 'cannot change the value for option {0} this option is frozen'
else:
raise_text = "trying to access to an option named: {0} with properties {1}"
raise PropertiesOptionError(_(raise_text).format(opt_or_descr._name,
str(properties)),
list(properties))
def _get_permissive(self, opt=None):
return self._permissives.get(opt, [])
@ -327,10 +313,11 @@ class Setting(object):
"convenience method to freeze, hidde and disable"
self._read(rw_remove, rw_append)
def _set_cache(self, opt, props):
def _set_cache(self, opt, props, exp):
if 'expire' in self:
if exp is None:
exp = time()
self._cache[opt] = (props, time() + expires_time)
pass
def reset_cache(self, only_expired):
if only_expired: