cache for properties is now in get_properties and not for validate_properties

This commit is contained in:
Emmanuel Garette 2013-04-20 11:25:14 +02:00
parent a11768ef43
commit 54fe8d0f4b
5 changed files with 57 additions and 52 deletions

View File

@ -162,7 +162,7 @@ def test_hidden_if_in():
assert not setting.has_property('hidden', stroption) assert not setting.has_property('hidden', stroption)
cfg.int = 1 cfg.int = 1
raises(PropertiesOptionError, "cfg.str") raises(PropertiesOptionError, "cfg.str")
raises(PropertiesOptionError, 'cfg.str= "uvw"') raises(PropertiesOptionError, 'cfg.str="uvw"')
assert setting.has_property('hidden', stroption) assert setting.has_property('hidden', stroption)
def test_hidden_if_in_with_group(): def test_hidden_if_in_with_group():

View File

@ -72,8 +72,8 @@ class SubConfig(object):
return return
self._setattr(name, value) self._setattr(name, value)
def cfgimpl_reset_cache(self): def cfgimpl_reset_cache(self, only_expired=False, only=('values', 'settings')):
self.cfgimpl_get_context().cfgimpl_reset_cache() self.cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
def _setattr(self, name, value, force_permissive=False): def _setattr(self, name, value, force_permissive=False):
if '.' in name: if '.' in name:
@ -361,8 +361,10 @@ class Config(SubConfig):
def _cfgimpl_build_all_paths(self): def _cfgimpl_build_all_paths(self):
self._cfgimpl_descr.build_cache() self._cfgimpl_descr.build_cache()
def cfgimpl_reset_cache(self, only_expired=False): def cfgimpl_reset_cache(self, only_expired=False, only=('values', 'settings')):
if 'values' in only:
self.cfgimpl_get_values().reset_cache(only_expired=only_expired) self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
if 'settings' in only:
self.cfgimpl_get_settings().reset_cache(only_expired=only_expired) self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
def unwrap_from_path(self, path): def unwrap_from_path(self, path):
@ -502,11 +504,12 @@ def mandatory_warnings(config):
:returns: generator of mandatory Option's path :returns: generator of mandatory Option's path
""" """
config.cfgimpl_reset_cache() #if value in cache, properties are not calculated
config.cfgimpl_reset_cache(only=('values',))
for path in config.cfgimpl_get_description().getpaths(include_groups=True): for path in config.cfgimpl_get_description().getpaths(include_groups=True):
try: try:
config._getattr(path, force_properties=('mandatory',)) config._getattr(path, force_properties=('mandatory',))
except PropertiesOptionError, err: except PropertiesOptionError, err:
if err.proptype == ['mandatory']: if err.proptype == ['mandatory']:
yield path yield path
config.cfgimpl_reset_cache() config.cfgimpl_reset_cache(only=('values',))

View File

@ -157,23 +157,31 @@ class Setting(object):
#____________________________________________________________ #____________________________________________________________
# properties methods # properties methods
def has_properties(self, opt=None, is_apply_req=True): def has_properties(self, opt=None):
"has properties means the Config's properties attribute is not empty" "has properties means the Config's properties attribute is not empty"
return bool(len(self.get_properties(opt, is_apply_req))) return bool(len(self.get_properties(opt)))
def get_properties(self, opt=None, is_apply_req=True): 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: if opt is None:
default = [] default = []
else: else:
if is_apply_req: if is_apply_req:
apply_requires(opt, self.context) apply_requires(opt, self.context)
default = list(opt._properties) default = list(opt._properties)
return self.properties.get(opt, default) props = self.properties.get(opt, default)
if opt is not None:
self._set_cache(opt, props)
return props
def has_property(self, propname, opt=None, is_apply_req=True): def has_property(self, propname, opt=None):
"""has property propname in the Config's properties attribute """has property propname in the Config's properties attribute
:param property: string wich is the name of the property""" :param property: string wich is the name of the property"""
return propname in self.get_properties(opt, is_apply_req) return propname in self.get_properties(opt)
def enable_property(self, propname): def enable_property(self, propname):
"puts property propname in the Config's properties attribute" "puts property propname in the Config's properties attribute"
@ -205,6 +213,8 @@ class Setting(object):
self.properties[opt] = properties self.properties[opt] = properties
def add_property(self, propname, opt, is_apply_req=True): def add_property(self, propname, opt, is_apply_req=True):
if opt is None:
raise ValueError("option must not be None in add_property")
properties = self.get_properties(opt, is_apply_req) properties = self.get_properties(opt, is_apply_req)
if not propname in properties: if not propname in properties:
properties.append(propname) properties.append(propname)
@ -212,6 +222,8 @@ class Setting(object):
self.context.cfgimpl_reset_cache() self.context.cfgimpl_reset_cache()
def del_property(self, propname, opt, is_apply_req=True): def del_property(self, propname, opt, is_apply_req=True):
if opt is None:
raise ValueError("option must not be None in del_property")
properties = self.get_properties(opt, is_apply_req) properties = self.get_properties(opt, is_apply_req)
if propname in properties: if propname in properties:
properties.remove(propname) properties.remove(propname)
@ -223,7 +235,7 @@ class Setting(object):
if force_properties is not None: if force_properties is not None:
set_mandatory = ('mandatory' in force_properties or set_mandatory = ('mandatory' in force_properties or
set_mandatory) set_mandatory)
if set_mandatory and self.has_property('mandatory', opt, False) and \ if set_mandatory and self.has_property('mandatory', opt) and \
self.context.cfgimpl_get_values()._is_empty(opt, value): self.context.cfgimpl_get_values()._is_empty(opt, value):
return True return True
return False return False
@ -232,12 +244,11 @@ class Setting(object):
properties = set(self.get_properties(opt_or_descr)) properties = set(self.get_properties(opt_or_descr))
#remove this properties, those properties are validate in after #remove this properties, those properties are validate in after
properties = properties - set(['mandatory', 'frozen']) properties = properties - set(['mandatory', 'frozen'])
set_properties = self.get_properties() set_properties = set(self.get_properties())
if force_properties is not None: if force_properties is not None:
set_properties.extend(force_properties) set_properties.update(set(force_properties))
set_properties = set(set_properties)
properties = properties & set_properties properties = properties & set_properties
if force_permissive is True or self.has_property('permissive', is_apply_req=False): if force_permissive is True or self.has_property('permissive'):
properties = properties - set(self.get_permissive()) properties = properties - set(self.get_permissive())
properties = properties - set(self.get_permissive(opt_or_descr)) properties = properties - set(self.get_permissive(opt_or_descr))
return list(properties) return list(properties)
@ -246,14 +257,6 @@ class Setting(object):
def validate_properties(self, opt_or_descr, is_descr, is_write, def validate_properties(self, opt_or_descr, is_descr, is_write,
value=None, force_permissive=False, value=None, force_permissive=False,
force_properties=None): force_properties=None):
is_cached = False
if opt_or_descr in self._cache:
exp = time()
props, raise_text, created = self._cache[opt_or_descr]
if exp < created:
properties = props
is_cached = True
if not is_cached:
properties = self._calc_properties(opt_or_descr, force_permissive, properties = self._calc_properties(opt_or_descr, force_permissive,
force_properties) force_properties)
raise_text = _("trying to access" raise_text = _("trying to access"
@ -266,12 +269,10 @@ class Setting(object):
#frozen #frozen
if is_write and (self.has_property('everything_frozen') or ( if is_write and (self.has_property('everything_frozen') or (
self.has_property('frozen') and self.has_property('frozen') and
self.has_property('frozen', opt_or_descr, self.has_property('frozen', opt_or_descr))):
is_apply_req=False))):
properties.append('frozen') properties.append('frozen')
raise_text = _('cannot change the value to {0} for ' raise_text = _('cannot change the value to {0} for '
'option {1} this option is frozen') 'option {1} this option is frozen')
self._set_cache(opt_or_descr, properties, raise_text)
if properties != []: if properties != []:
raise PropertiesOptionError(raise_text.format(opt_or_descr._name, raise PropertiesOptionError(raise_text.format(opt_or_descr._name,
str(properties)), str(properties)),
@ -316,16 +317,17 @@ class Setting(object):
self.enable_property('validator') self.enable_property('validator')
self.disable_property('permissive') self.disable_property('permissive')
def _set_cache(self, opt, props, raise_text): def _set_cache(self, opt, props):
if self.has_property('expire'): if self.has_property('expire'):
self._cache[opt] = (props, raise_text, time() + expires_time) self._cache[opt] = (props, time() + expires_time)
pass
def reset_cache(self, only_expired): def reset_cache(self, only_expired):
if only_expired: if only_expired:
exp = time() exp = time()
keys = self._cache.keys() keys = self._cache.keys()
for key in keys: for key in keys:
props, raise_text, created = self._cache[key] props, created = self._cache[key]
if exp > created: if exp > created:
del(self._cache[key]) del(self._cache[key])
else: else:

View File

@ -96,14 +96,14 @@ class Values(object):
# options with callbacks # options with callbacks
setting = self.context.cfgimpl_get_settings() setting = self.context.cfgimpl_get_settings()
value = self._get_value(opt) value = self._get_value(opt)
is_frozen = setting.has_property('frozen', opt, False) is_frozen = setting.has_property('frozen', opt)
if opt.has_callback(): if opt.has_callback():
#if value is set and : #if value is set and :
# - not frozen # - not frozen
# - frozen and not force_default_on_freeze # - frozen and not force_default_on_freeze
if not self.is_default_owner(opt) and ( if not self.is_default_owner(opt) and (
not is_frozen or (is_frozen and not is_frozen or (is_frozen and
not setting.has_property('force_default_on_freeze', opt, False))): not setting.has_property('force_default_on_freeze', opt))):
pass pass
else: else:
value = self._getcallback_value(opt) value = self._getcallback_value(opt)
@ -112,7 +112,7 @@ class Values(object):
#suppress value if already set #suppress value if already set
self._reset(opt) self._reset(opt)
# frozen and force default # frozen and force default
elif is_frozen and setting.has_property('force_default_on_freeze', opt, False): elif is_frozen and setting.has_property('force_default_on_freeze', opt):
value = opt.getdefault() value = opt.getdefault()
if opt.is_multi(): if opt.is_multi():
value = Multi(value, self.context, opt) value = Multi(value, self.context, opt)
@ -120,7 +120,7 @@ class Values(object):
raise ValueError(_('invalid calculated value returned' raise ValueError(_('invalid calculated value returned'
' for option {0}: {1}').format(opt._name, value)) ' for option {0}: {1}').format(opt._name, value))
if self.is_default_owner(opt) and \ if self.is_default_owner(opt) and \
setting.has_property('force_store_value', opt, False): setting.has_property('force_store_value', opt):
self.setitem(opt, value) self.setitem(opt, value)
setting.validate_properties(opt, False, False, value=value, setting.validate_properties(opt, False, False, value=value,
force_permissive=force_permissive, force_permissive=force_permissive,