* to "reset" a value, now you just have to delete it
config.unwrap_from_path("string").reset(config) => del(config.string) * add cache for value/setting to 5 secds to "reset" cache just do: config.cfgimpl_clean_cache() * can desactivate cache by removing "expire" property
This commit is contained in:
@ -20,11 +20,14 @@
|
||||
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
|
||||
# the whole pypy projet is under MIT licence
|
||||
# ____________________________________________________________
|
||||
|
||||
from time import time
|
||||
from tiramisu.error import RequirementRecursionError, PropertiesOptionError
|
||||
from tiramisu.i18n import _
|
||||
|
||||
|
||||
expires_time = 5
|
||||
|
||||
|
||||
class _const:
|
||||
"""convenient class that emulates a module
|
||||
and builds constants (that is, unique names)"""
|
||||
@ -139,17 +142,18 @@ populate_multitypes()
|
||||
#____________________________________________________________
|
||||
class Setting(object):
|
||||
"``Config()``'s configuration options"
|
||||
__slots__ = ('properties', 'permissives', 'owner', 'context')
|
||||
__slots__ = ('properties', 'permissives', 'owner', 'context', '_cache')
|
||||
|
||||
def __init__(self, context):
|
||||
# properties attribute: the name of a property enables this property
|
||||
# key is None for global properties
|
||||
self.properties = {None: []} # ['hidden', 'disabled', 'mandatory', 'frozen', 'validator']}
|
||||
self.properties = {None: ['expire']}
|
||||
# permissive properties
|
||||
self.permissives = {}
|
||||
# generic owner
|
||||
self.owner = owners.user
|
||||
self.context = context
|
||||
self._cache = {}
|
||||
|
||||
#____________________________________________________________
|
||||
# properties methods
|
||||
@ -177,6 +181,7 @@ class Setting(object):
|
||||
if propname not in props:
|
||||
props.append(propname)
|
||||
self.set_properties(props)
|
||||
self.context.cfgimpl_clean_cache()
|
||||
|
||||
def disable_property(self, propname):
|
||||
"deletes property propname in the Config's properties attribute"
|
||||
@ -184,6 +189,7 @@ class Setting(object):
|
||||
if propname in props:
|
||||
props.remove(propname)
|
||||
self.set_properties(props)
|
||||
self.context.cfgimpl_clean_cache()
|
||||
|
||||
def set_properties(self, properties, opt=None):
|
||||
"""save properties for specified opt
|
||||
@ -203,44 +209,55 @@ class Setting(object):
|
||||
if not propname in properties:
|
||||
properties.append(propname)
|
||||
self.set_properties(properties, opt)
|
||||
self.context.cfgimpl_clean_cache()
|
||||
|
||||
def del_property(self, propname, opt, is_apply_req=True):
|
||||
properties = self.get_properties(opt, is_apply_req)
|
||||
if propname in properties:
|
||||
properties.remove(propname)
|
||||
self.set_properties(properties, opt)
|
||||
self.context.cfgimpl_clean_cache()
|
||||
|
||||
#____________________________________________________________
|
||||
def validate_properties(self, opt_or_descr, is_descr, is_write,
|
||||
value=None, force_permissive=False,
|
||||
force_properties=None):
|
||||
properties = set(self.get_properties(opt_or_descr))
|
||||
#remove this properties, those properties are validate in after
|
||||
properties = properties - set(['mandatory', 'frozen'])
|
||||
set_properties = self.get_properties()
|
||||
if force_properties is not None:
|
||||
set_properties.extend(force_properties)
|
||||
set_properties = set(set_properties)
|
||||
properties = properties & set_properties
|
||||
if force_permissive is True or self.has_property('permissive', is_apply_req=False):
|
||||
properties = properties - set(self.get_permissive())
|
||||
properties = properties - set(self.get_permissive(opt_or_descr))
|
||||
properties = list(properties)
|
||||
raise_text = _("trying to access"
|
||||
" to an option named: {0} with properties"
|
||||
" {1}")
|
||||
if not is_descr:
|
||||
if self.context.cfgimpl_get_values().is_mandatory_err(opt_or_descr,
|
||||
value,
|
||||
force_properties=force_properties):
|
||||
properties.append('mandatory')
|
||||
if is_write and (self.has_property('everything_frozen') or (
|
||||
self.has_property('frozen') and
|
||||
self.has_property('frozen', opt_or_descr,
|
||||
is_apply_req=False))):
|
||||
properties.append('frozen')
|
||||
raise_text = _('cannot change the value to {0} for '
|
||||
'option {1} this option is frozen')
|
||||
is_cached = False
|
||||
if opt_or_descr in self._cache:
|
||||
t = time()
|
||||
props, raise_text, created = self._cache[opt_or_descr]
|
||||
if t - created < expires_time:
|
||||
properties = props
|
||||
is_cached = True
|
||||
if not is_cached:
|
||||
properties = set(self.get_properties(opt_or_descr))
|
||||
#remove this properties, those properties are validate in after
|
||||
properties = properties - set(['mandatory', 'frozen'])
|
||||
set_properties = self.get_properties()
|
||||
if force_properties is not None:
|
||||
set_properties.extend(force_properties)
|
||||
set_properties = set(set_properties)
|
||||
properties = properties & set_properties
|
||||
if force_permissive is True or self.has_property('permissive', is_apply_req=False):
|
||||
properties = properties - set(self.get_permissive())
|
||||
properties = properties - set(self.get_permissive(opt_or_descr))
|
||||
properties = list(properties)
|
||||
raise_text = _("trying to access"
|
||||
" to an option named: {0} with properties"
|
||||
" {1}")
|
||||
if not is_descr:
|
||||
if self.context.cfgimpl_get_values().is_mandatory_err(opt_or_descr,
|
||||
value,
|
||||
force_properties=force_properties):
|
||||
properties.append('mandatory')
|
||||
if is_write and (self.has_property('everything_frozen') or (
|
||||
self.has_property('frozen') and
|
||||
self.has_property('frozen', opt_or_descr,
|
||||
is_apply_req=False))):
|
||||
properties.append('frozen')
|
||||
raise_text = _('cannot change the value to {0} for '
|
||||
'option {1} this option is frozen')
|
||||
self._set_cache(opt_or_descr, properties, raise_text)
|
||||
if properties != []:
|
||||
raise PropertiesOptionError(raise_text.format(opt_or_descr._name,
|
||||
str(properties)),
|
||||
@ -285,6 +302,13 @@ class Setting(object):
|
||||
self.enable_property('validator')
|
||||
self.disable_property('permissive')
|
||||
|
||||
def _set_cache(self, opt, props, raise_text):
|
||||
if self.has_property('expire'):
|
||||
self._cache[opt] = (props, raise_text, time())
|
||||
|
||||
def reset_cache(self):
|
||||
self._cache = {}
|
||||
|
||||
|
||||
def apply_requires(opt, config):
|
||||
"carries out the jit (just in time requirements between options"
|
||||
|
Reference in New Issue
Block a user