From f6f709f83db0190954de494093da1a05f1968f05 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 14 Aug 2013 23:06:31 +0200 Subject: [PATCH 1/3] split cache/value/setting in plugin --- tiramisu/config.py | 6 +- tiramisu/plugins/__init__.py | 0 tiramisu/plugins/dictionary/__init__.py | 0 tiramisu/plugins/dictionary/cache.py | 48 ++++++++++ tiramisu/plugins/dictionary/setting.py | 58 ++++++++++++ tiramisu/plugins/dictionary/value.py | 76 +++++++++++++++ tiramisu/setting.py | 99 ++++++++----------- tiramisu/value.py | 121 +++++++++++++----------- 8 files changed, 290 insertions(+), 118 deletions(-) create mode 100644 tiramisu/plugins/__init__.py create mode 100644 tiramisu/plugins/dictionary/__init__.py create mode 100644 tiramisu/plugins/dictionary/cache.py create mode 100644 tiramisu/plugins/dictionary/setting.py create mode 100644 tiramisu/plugins/dictionary/value.py diff --git a/tiramisu/config.py b/tiramisu/config.py index 510d489..bd71d9b 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -23,7 +23,7 @@ from tiramisu.error import PropertiesOptionError, ConfigError from tiramisu.option import OptionDescription, Option, SymLinkOption, \ BaseInformation -from tiramisu.setting import groups, Setting, default_encoding +from tiramisu.setting import groups, Settings, default_encoding from tiramisu.value import Values from tiramisu.i18n import _ @@ -505,7 +505,7 @@ class Config(CommonConfig): :param context: the current root config :type context: `Config` """ - self._impl_settings = Setting(self) + self._impl_settings = Settings(self) self._impl_values = Values(self) super(Config, self).__init__(descr, self) self._impl_build_all_paths() @@ -541,7 +541,7 @@ class MetaConfig(CommonConfig): child._impl_meta = self self._impl_children = children - self._impl_settings = Setting(self) + self._impl_settings = Settings(self) self._impl_values = Values(self) self._impl_meta = None self._impl_informations = {} diff --git a/tiramisu/plugins/__init__.py b/tiramisu/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tiramisu/plugins/dictionary/__init__.py b/tiramisu/plugins/dictionary/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tiramisu/plugins/dictionary/cache.py b/tiramisu/plugins/dictionary/cache.py new file mode 100644 index 0000000..fd67847 --- /dev/null +++ b/tiramisu/plugins/dictionary/cache.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +"default plugin for cache: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ + + +class PluginCache(object): + __slots__ = ('_cache',) + + def __init__(self): + self._cache = {} + + def _p_setcache(self, cache_type, opt, val, time): + self._cache[opt] = (val, time) + + def _p_getcache(self, cache_type, opt, exp): + value, created = self._cache[opt] + if exp < created: + return True, value + return False, None + + def _p_hascache(self, cache_type, opt): + return opt in self._cache + + def _p_reset_expired_cache(self, cache_type, exp): + keys = self._cache.keys() + for key in keys: + val, created = self._cache[key] + if exp > created: + del(self._cache[key]) + + def _p_reset_all_cache(self, cache_type): + self._cache.clear() diff --git a/tiramisu/plugins/dictionary/setting.py b/tiramisu/plugins/dictionary/setting.py new file mode 100644 index 0000000..3ccbeb1 --- /dev/null +++ b/tiramisu/plugins/dictionary/setting.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +"default plugin for setting: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ +from tiramisu.plugins.dictionary.cache import PluginCache + + +class PluginSettings(PluginCache): + __slots__ = ('_properties', '_permissives') + + def __init__(self): + # properties attribute: the name of a property enables this property + # key is None for global properties + self._properties = {} + # permissive properties + self._permissives = {} + super(PluginSettings, self).__init__() + + # propertives + def _p_setproperties(self, opt, properties): + self._properties[opt] = properties + + def _p_getproperties(self, opt, default_properties): + return self._properties.get(opt, set(default_properties)) + + def _p_hasproperties(self, opt): + return opt in self._properties + + def _p_reset_all_propertives(self): + self._properties = {} + + def _p_reset_properties(self, opt): + try: + del(self._properties[opt]) + except KeyError: + pass + + # permissive + def _p_setpermissive(self, opt, permissive): + self._permissives[opt] = frozenset(permissive) + + def _p_getpermissive(self, opt=None): + return self._permissives.get(opt, frozenset()) diff --git a/tiramisu/plugins/dictionary/value.py b/tiramisu/plugins/dictionary/value.py new file mode 100644 index 0000000..84f60c2 --- /dev/null +++ b/tiramisu/plugins/dictionary/value.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +"default plugin for value: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ + +#FIXME +from tiramisu.plugins.dictionary.cache import PluginCache + + +class PluginValues(PluginCache): + __slots__ = ('_values',) + + def __init__(self): + """init plugin means create values storage + """ + self._values = {} + # should init cache too + super(PluginValues, self).__init__() + + # value + def _p_setvalue(self, opt, value): + """set value for an option + a specified value must be associated to an owner + """ + owner = self.context.cfgimpl_get_settings().getowner() + self._values[opt] = (owner, value) + + def _p_getvalue(self, opt): + """get value for an option + return: only value, not the owner + """ + return self._values[opt][1] + + def _p_hasvalue(self, opt): + """if opt has a value + return: boolean + """ + return opt in self._values + + def _p_resetvalue(self, opt): + """remove value means delete value in storage + """ + del(self._values[opt]) + + def _p_get_modified_values(self): + """return all values in a dictionary + example: {option1: (owner, 'value1'), option2: (owner, 'value2')} + """ + return self._values + + # owner + def _p_setowner(self, opt, owner): + """change owner for an option + """ + self._values[opt] = (owner, self._values[opt][1]) + + def _p_getowner(self, opt, default): + """get owner for an option + return: owner object + """ + return self._values.get(opt, (default, None))[0] diff --git a/tiramisu/setting.py b/tiramisu/setting.py index ecfc5bf..43e2d70 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -24,6 +24,8 @@ from time import time from copy import copy from tiramisu.error import RequirementError, PropertiesOptionError from tiramisu.i18n import _ +from tiramisu.plugins.dictionary.setting import PluginSettings + default_encoding = 'utf-8' expires_time = 5 @@ -158,12 +160,12 @@ class Property(object): def append(self, propname): self._properties.add(propname) - self._setting._set_properties(self._properties, self._opt) + self._setting._setproperties(self._properties, self._opt) def remove(self, propname): if propname in self._properties: self._properties.remove(propname) - self._setting._set_properties(self._properties, self._opt) + self._setting._setproperties(self._properties, self._opt) def reset(self): self._setting.reset(opt=self._opt) @@ -176,31 +178,26 @@ class Property(object): #____________________________________________________________ -class Setting(object): +class Settings(PluginSettings): "``Config()``'s configuration options" - __slots__ = ('context', '_properties', '_permissives', '_owner', '_cache') + __slots__ = ('context', '_owner') def __init__(self, context): - # properties attribute: the name of a property enables this property - # key is None for global properties - self._properties = {} - # permissive properties - self._permissives = {} # generic owner self._owner = owners.user self.context = context - self._cache = {} + super(Settings, self).__init__() #____________________________________________________________ # properties methods def __contains__(self, propname): - return propname in self._get_properties() + return propname in self._getproperties() def __repr__(self): - return str(list(self._get_properties())) + return str(list(self._getproperties())) def __getitem__(self, opt): - return Property(self, self._get_properties(opt), opt) + return Property(self, self._getproperties(opt), opt) def __setitem__(self, opt, value): raise ValueError('you must only append/remove properties') @@ -210,50 +207,49 @@ class Setting(object): raise ValueError(_('opt and all_properties must not be set ' 'together in reset')) if all_properties: - self._properties = {} + self._p_reset_all_propertives() else: - try: - del(self._properties[opt]) - except KeyError: - pass + self._p_reset_properties(opt) self.context.cfgimpl_reset_cache() - def _get_properties(self, opt=None, is_apply_req=True): + def _getproperties(self, opt=None, is_apply_req=True): if opt is None: - props = self._properties.get(opt, set(default_properties)) + props = self._p_getproperties(opt, default_properties) else: - exp = None - if opt in self._cache: - exp = time() - props, created = self._cache[opt] - if exp < created: + ntime = None + if self._p_hascache('properties', opt): + ntime = time() + is_cached, props = self._p_getcache('properties', opt, ntime) + if is_cached: return props if is_apply_req: self.apply_requires(opt) - props = self._properties.get(opt, set(opt._properties)) - self._set_cache(opt, props, exp) + props = self._p_getproperties(opt, opt._properties) + if 'expire' in self: + if ntime is None: + ntime = time() + self._p_setcache('properties', opt, props, ntime + expires_time) return props def append(self, propname): "puts property propname in the Config's properties attribute" - Property(self, self._get_properties()).append(propname) + Property(self, self._getproperties()).append(propname) def remove(self, propname): "deletes property propname in the Config's properties attribute" - Property(self, self._get_properties()).remove(propname) + Property(self, self._getproperties()).remove(propname) - def _set_properties(self, properties, opt=None): + def _setproperties(self, properties, opt=None): """save properties for specified opt (never save properties if same has option properties) """ if opt is None: - self._properties[opt] = properties + self._p_setproperties(opt, properties) else: if set(opt._properties) == properties: - if opt in self._properties: - del(self._properties[opt]) + self._p_reset_properties(opt) else: - self._properties[opt] = properties + self._p_setproperties(opt, properties) self.context.cfgimpl_reset_cache() #____________________________________________________________ @@ -261,13 +257,13 @@ class Setting(object): value=None, force_permissive=False, force_properties=None): #opt properties - properties = copy(self._get_properties(opt_or_descr)) + properties = copy(self._getproperties(opt_or_descr)) #remove opt permissive - properties -= self._get_permissive(opt_or_descr) + properties -= self._p_getpermissive(opt_or_descr) #remove global permissive if need - self_properties = copy(self._get_properties()) + self_properties = copy(self._getproperties()) if force_permissive is True or 'permissive' in self_properties: - properties -= self._get_permissive() + properties -= self._p_getpermissive() #global properties if force_properties is not None: @@ -280,8 +276,8 @@ class Setting(object): properties -= frozenset(('mandatory', 'frozen')) else: if 'mandatory' in properties and \ - not self.context.cfgimpl_get_values()._is_empty(opt_or_descr, - value): + not self.context.cfgimpl_get_values()._isempty(opt_or_descr, + value): properties.remove('mandatory') if is_write and 'everything_frozen' in self_properties: properties.add('frozen') @@ -299,13 +295,11 @@ class Setting(object): "named: {0} with properties {1}" "").format(opt_or_descr._name, str(props)), props) - def _get_permissive(self, opt=None): - return self._permissives.get(opt, frozenset()) - + #FIXME should be setpermissive def set_permissive(self, permissive, opt=None): if not isinstance(permissive, tuple): raise TypeError(_('permissive must be a tuple')) - self._permissives[opt] = frozenset(permissive) + self._p_setpermissive(opt, permissive) #____________________________________________________________ def setowner(self, owner): @@ -332,22 +326,11 @@ class Setting(object): "convenience method to freeze, hidde and disable" self._read(rw_remove, rw_append) - def _set_cache(self, opt, props, exp): - if 'expire' in self: - if exp is None: - exp = time() - self._cache[opt] = (props, time() + expires_time) - def reset_cache(self, only_expired): if only_expired: - exp = time() - keys = self._cache.keys() - for key in keys: - props, created = self._cache[key] - if exp > created: - del(self._cache[key]) + self._p_reset_expired_cache('properties', time()) else: - self._cache.clear() + self._p_reset_all_cache('properties') def apply_requires(self, opt): "carries out the jit (just in time requirements between options" @@ -355,7 +338,7 @@ class Setting(object): return # filters the callbacks - setting = Property(self, self._get_properties(opt, False), opt) + setting = Property(self, self._getproperties(opt, False), opt) descr = self.context.cfgimpl_get_description() optpath = descr.impl_get_path_by_opt(opt) for requires in opt._requires: diff --git a/tiramisu/value.py b/tiramisu/value.py index 65a23b1..d22bec3 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -25,13 +25,16 @@ from tiramisu.autolib import carry_out_calculation from tiramisu.i18n import _ from tiramisu.option import SymLinkOption +#FIXME +from tiramisu.plugins.dictionary.value import PluginValues -class Values(object): + +class Values(PluginValues): """The `Config`'s root is indeed in charge of the `Option()`'s values, but the values are physicaly located here, in `Values`, wich is also responsible of a caching utility. """ - __slots__ = ('context', '_values', '_cache') + __slots__ = ('context',) def __init__(self, context): """ @@ -42,11 +45,9 @@ class Values(object): """ self.context = context - self._values = {} - self._cache = {} super(Values, self).__init__() - def _get_default(self, opt): + def _getdefault(self, opt): meta = self.context.cfgimpl_get_meta() if meta is not None: value = meta.cfgimpl_get_values()[opt] @@ -57,33 +58,42 @@ class Values(object): else: return value - def _get_value(self, opt, validate=True): + def _getvalue(self, opt, validate=True): "return value or default value if not set" - #if no value - if opt not in self._values: - value = self._get_default(opt) + if not self._p_hasvalue(opt): + #if no value + value = self._getdefault(opt) if opt.impl_is_multi(): value = Multi(value, self.context, opt, validate) else: #if value - value = self._values[opt][1] + value = self._p_getvalue(opt) + if opt.impl_is_multi() and not isinstance(value, Multi): + #load value so don't need to validate if is not a Multi + value = Multi(value, self.context, opt, validate=False) return value - def __delitem__(self, opt): - self._reset(opt) + def get_modified_values(self): + return self._p_get_modified_values() - def _reset(self, opt): - if opt in self._values: + def __contains__(self, opt): + return self._p_hascache('value', opt) + + def __delitem__(self, opt): + self.reset(opt) + + def reset(self, opt): + if self._p_hasvalue(opt): setting = self.context.cfgimpl_get_settings() opt.impl_validate(opt.impl_getdefault(), self.context, 'validator' in setting) self.context.cfgimpl_reset_cache() if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master: for slave in opt.impl_get_master_slaves(): - self._reset(slave) - del(self._values[opt]) + self.reset(slave) + self._p_resetvalue(opt) - def _is_empty(self, opt, value): + def _isempty(self, opt, value): "convenience method to know if an option is empty" empty = opt._empty if (not opt.impl_is_multi() and (value is None or value == empty)) or \ @@ -104,21 +114,23 @@ class Values(object): def __getitem__(self, opt): return self.getitem(opt) - def get_modified_values(self): - return self._values - def getitem(self, opt, validate=True, force_permissive=False, force_properties=None, validate_properties=True): - if opt in self._cache: - exp = time() - value, created = self._cache[opt] - if exp < created: + ntime = None + if self._p_hascache('value', opt): + ntime = time() + is_cached, value = self._p_getcache('value', opt, ntime) + if is_cached: return value val = self._getitem(opt, validate, force_permissive, force_properties, validate_properties) - if validate and validate_properties and force_permissive is False and \ + if 'expire' in self.context.cfgimpl_get_settings() and validate and \ + validate_properties and force_permissive is False and \ force_properties is None: - self._set_cache(opt, val) + if ntime is None: + ntime = time() + self._p_setcache('value', opt, val, ntime + expires_time) + return val def _getitem(self, opt, validate, force_permissive, force_properties, @@ -148,14 +160,14 @@ class Values(object): if opt.impl_is_multi(): value = Multi(value, self.context, opt, validate) #suppress value if already set - self._reset(opt) + self.reset(opt) # frozen and force default elif is_frozen and 'force_default_on_freeze' in setting[opt]: - value = self._get_default(opt) + value = self._getdefault(opt) if opt.impl_is_multi(): value = Multi(value, self.context, opt, validate) else: - value = self._get_value(opt, validate) + value = self._getvalue(opt, validate) if validate: opt.impl_validate(value, self.context, 'validator' in setting) if self.is_default_owner(opt) and \ @@ -184,29 +196,30 @@ class Values(object): def _setvalue(self, opt, value, force_permissive=False, force_properties=None, is_write=True, validate_properties=True): self.context.cfgimpl_reset_cache() - setting = self.context.cfgimpl_get_settings() if validate_properties: + setting = self.context.cfgimpl_get_settings() setting.validate_properties(opt, False, is_write, value=value, force_permissive=force_permissive, force_properties=force_properties) - self._values[opt] = (setting.getowner(), value) + self._p_setvalue(opt, value) def getowner(self, opt): if isinstance(opt, SymLinkOption): opt = opt._opt - owner = self._values.get(opt, (owners.default, None))[0] + owner = self._p_getowner(opt, owners.default) meta = self.context.cfgimpl_get_meta() if owner is owners.default and meta is not None: owner = meta.cfgimpl_get_values().getowner(opt) return owner def setowner(self, opt, owner): - if opt not in self._values: - raise ConfigError(_('no value for {0} cannot change owner to {1}').format(opt._name, owner)) if not isinstance(owner, owners.Owner): raise TypeError(_("invalid generic owner {0}").format(str(owner))) - self._values[opt] = (owner, self._values[opt][1]) + if self.getowner(opt) == owners.default: + raise ConfigError(_('no value for {0} cannot change owner to {1}' + '').format(opt._name, owner)) + self._p_setowner(opt, owner) def is_default_owner(self, opt): """ @@ -216,23 +229,17 @@ class Values(object): """ return self.getowner(opt) == owners.default - def _set_cache(self, opt, val): - if 'expire' in self.context.cfgimpl_get_settings(): - self._cache[opt] = (val, time() + expires_time) - def reset_cache(self, only_expired): if only_expired: - exp = time() - keys = self._cache.keys() - for key in keys: - val, created = self._cache[key] - if exp > created: - del(self._cache[key]) + self._p_reset_expired_cache('value', time()) else: - self._cache.clear() + self._p_reset_all_cache('value') - def __contains__(self, opt): - return opt in self._values + def _get_opt_path(self, opt): + return self.context.cfgimpl_get_description().impl_get_path_by_opt(opt) + + def _get_owner_from_str(self, owner): + return getattr(owners, owner) # ____________________________________________________________ # multi types @@ -281,7 +288,7 @@ class Multi(list): values = self.context.cfgimpl_get_values() for slave in self.opt._master_slaves: if not values.is_default_owner(slave): - value_slave = values._get_value(slave) + value_slave = values._getvalue(slave) if len(value_slave) > masterlen: raise SlaveError(_("invalid len for the master: {0}" " which has {1} as slave with" @@ -294,8 +301,8 @@ class Multi(list): def __setitem__(self, key, value): self._validate(value) #assume not checking mandatory property - self.context.cfgimpl_get_values()._setvalue(self.opt, self) super(Multi, self).__setitem__(key, value) + self.context.cfgimpl_get_values()._setvalue(self.opt, self) def append(self, value, force=False): """the list value can be updated (appened) @@ -313,8 +320,6 @@ class Multi(list): if isinstance(value, list): value = None self._validate(value) - #set value without valid properties - self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) super(Multi, self).append(value) if not force and self.opt.impl_get_multitype() == multitypes.master: for slave in self.opt.impl_get_master_slaves(): @@ -327,38 +332,39 @@ class Multi(list): #get multi without valid properties values.getitem(slave, validate_properties=False).append( dvalue, force=True) + self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) def sort(self, cmp=None, key=None, reverse=False): if self.opt.impl_get_multitype() in [multitypes.slave, multitypes.master]: raise SlaveError(_("cannot sort multi option {0} if master or slave" "").format(self.opt._name)) - self.context.cfgimpl_get_values()._setvalue(self.opt, self) super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse) + self.context.cfgimpl_get_values()._setvalue(self.opt, self) def reverse(self): if self.opt.impl_get_multitype() in [multitypes.slave, multitypes.master]: raise SlaveError(_("cannot reverse multi option {0} if master or " "slave").format(self.opt._name)) - self.context.cfgimpl_get_values()._setvalue(self.opt, self) super(Multi, self).reverse() + self.context.cfgimpl_get_values()._setvalue(self.opt, self) def insert(self, index, obj): if self.opt.impl_get_multitype() in [multitypes.slave, multitypes.master]: raise SlaveError(_("cannot insert multi option {0} if master or " "slave").format(self.opt._name)) - self.context.cfgimpl_get_values()._setvalue(self.opt, self) super(Multi, self).insert(index, obj) + self.context.cfgimpl_get_values()._setvalue(self.opt, self) def extend(self, iterable): if self.opt.impl_get_multitype() in [multitypes.slave, multitypes.master]: raise SlaveError(_("cannot extend multi option {0} if master or " "slave").format(self.opt._name)) - self.context.cfgimpl_get_values()._setvalue(self.opt, self) super(Multi, self).extend(iterable) + self.context.cfgimpl_get_values()._setvalue(self.opt, self) def _validate(self, value): if value is not None: @@ -387,5 +393,6 @@ class Multi(list): #get multi without valid properties values.getitem(slave, validate_properties=False).pop(key, force=True) #set value without valid properties + ret = super(Multi, self).pop(key) self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) - return super(Multi, self).pop(key) + return ret From df7d6759cd9ba170afc43c360a5f18547f0b8514 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 19 Aug 2013 11:01:21 +0200 Subject: [PATCH 2/3] add sqlite plugin --- test/test_cache.py | 156 ++++++++++++------------- test/test_option_setting.py | 18 +-- tiramisu/config.py | 10 +- tiramisu/option.py | 3 +- tiramisu/plugins/dictionary/cache.py | 6 + tiramisu/plugins/dictionary/setting.py | 7 +- tiramisu/plugins/dictionary/value.py | 2 +- tiramisu/plugins/sqlite3/__init__.py | 0 tiramisu/plugins/sqlite3/cache.py | 85 ++++++++++++++ tiramisu/plugins/sqlite3/setting.py | 101 ++++++++++++++++ tiramisu/plugins/sqlite3/value.py | 109 +++++++++++++++++ tiramisu/setting.py | 20 ++-- tiramisu/value.py | 23 ++-- 13 files changed, 429 insertions(+), 111 deletions(-) create mode 100644 tiramisu/plugins/sqlite3/__init__.py create mode 100644 tiramisu/plugins/sqlite3/cache.py create mode 100644 tiramisu/plugins/sqlite3/setting.py create mode 100644 tiramisu/plugins/sqlite3/value.py diff --git a/test/test_cache.py b/test/test_cache.py index 2bc35fe..c7bf3b4 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -20,13 +20,13 @@ def test_cache(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u2 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache - assert od1.u2 in values._cache - assert od1.u2 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') + assert od1.u2 in values._p_get_cached('value') + assert od1.u2 in settings._p_get_cached('property') def test_cache_reset(): @@ -36,44 +36,44 @@ def test_cache_reset(): settings = c.cfgimpl_get_settings() #when change a value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u2 = 1 - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when remove a value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') del(c.u2) - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when add/del property c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_get_settings()[od1.u2].append('test') - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_get_settings()[od1.u2].remove('test') - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when enable/disabled property c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_get_settings().append('test') - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_get_settings().remove('test') - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') def test_cache_reset_multi(): @@ -83,32 +83,32 @@ def test_cache_reset_multi(): settings = c.cfgimpl_get_settings() #when change a value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u3 = [1] - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when append value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u3.append(1) - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when pop value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u3.pop(1) - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') #when remove a value c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') del(c.u3) - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') def test_reset_cache(): @@ -117,23 +117,23 @@ def test_reset_cache(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_reset_cache() - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') c.u1 sleep(1) c.u2 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache - assert od1.u2 in values._cache - assert od1.u2 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') + assert od1.u2 in values._p_get_cached('value') + assert od1.u2 in settings._p_get_cached('property') c.cfgimpl_reset_cache() - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache - assert od1.u2 not in values._cache - assert od1.u2 not in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') + assert od1.u2 not in values._p_get_cached('value') + assert od1.u2 not in settings._p_get_cached('property') def test_reset_cache_only_expired(): @@ -142,22 +142,22 @@ def test_reset_cache_only_expired(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_reset_cache(True) - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') sleep(1) c.u2 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache - assert od1.u2 in values._cache - assert od1.u2 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') + assert od1.u2 in values._p_get_cached('value') + assert od1.u2 in settings._p_get_cached('property') c.cfgimpl_reset_cache(True) - assert od1.u1 not in values._cache - assert od1.u1 not in settings._cache - assert od1.u2 in values._cache - assert od1.u2 in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') + assert od1.u2 in values._p_get_cached('value') + assert od1.u2 in settings._p_get_cached('property') def test_reset_cache_only(): @@ -166,14 +166,14 @@ def test_reset_cache_only(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_reset_cache(only=('values',)) - assert od1.u1 not in values._cache - assert od1.u1 in settings._cache + assert od1.u1 not in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.u1 - assert od1.u1 in values._cache - assert od1.u1 in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 in settings._p_get_cached('property') c.cfgimpl_reset_cache(only=('settings',)) - assert od1.u1 in values._cache - assert od1.u1 not in settings._cache + assert od1.u1 in values._p_get_cached('value') + assert od1.u1 not in settings._p_get_cached('property') diff --git a/test/test_option_setting.py b/test/test_option_setting.py index 74993c2..66121ec 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -327,23 +327,23 @@ def test_reset_properties(): cfg = Config(descr) setting = cfg.cfgimpl_get_settings() option = cfg.cfgimpl_get_description().gc.dummy - assert setting._properties == {} + assert setting._p_get_properties() == {} setting.append('frozen') - assert setting._properties == {None: set(('frozen', 'expire', 'validator'))} + assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator'))} setting.reset() - assert setting._properties == {} + assert setting._p_get_properties() == {} setting[option].append('test') - assert setting._properties == {option: set(('test',))} + assert setting._p_get_properties() == {option: set(('test',))} setting.reset() - assert setting._properties == {option: set(('test',))} + assert setting._p_get_properties() == {option: set(('test',))} setting.append('frozen') - assert setting._properties == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} + assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} setting.reset(option) - assert setting._properties == {None: set(('frozen', 'expire', 'validator'))} + assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator'))} setting[option].append('test') - assert setting._properties == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} + assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} setting.reset(all_properties=True) - assert setting._properties == {} + assert setting._p_get_properties() == {} raises(ValueError, 'setting.reset(all_properties=True, opt=option)') diff --git a/tiramisu/config.py b/tiramisu/config.py index bd71d9b..3703937 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -505,8 +505,9 @@ class Config(CommonConfig): :param context: the current root config :type context: `Config` """ - self._impl_settings = Settings(self) - self._impl_values = Values(self) + config_id = str(id(self)) + self._impl_settings = Settings(self, config_id) + self._impl_values = Values(self, config_id) super(Config, self).__init__(descr, self) self._impl_build_all_paths() self._impl_meta = None @@ -540,9 +541,10 @@ class MetaConfig(CommonConfig): raise ValueError(_("child has already a metaconfig's")) child._impl_meta = self + config_id = str(id(self)) self._impl_children = children - self._impl_settings = Settings(self) - self._impl_values = Values(self) + self._impl_settings = Settings(self, config_id) + self._impl_values = Values(self, config_id) self._impl_meta = None self._impl_informations = {} diff --git a/tiramisu/option.py b/tiramisu/option.py index 23cbd27..3175995 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -438,7 +438,8 @@ class StrOption(Option): def _validate(self, value): if not isinstance(value, str): - raise ValueError(_('value must be a string')) + raise ValueError(_('value must be a string, not ' + '{0}').format(type(value))) class UnicodeOption(Option): diff --git a/tiramisu/plugins/dictionary/cache.py b/tiramisu/plugins/dictionary/cache.py index fd67847..2f189e6 100644 --- a/tiramisu/plugins/dictionary/cache.py +++ b/tiramisu/plugins/dictionary/cache.py @@ -46,3 +46,9 @@ class PluginCache(object): def _p_reset_all_cache(self, cache_type): self._cache.clear() + + def _p_get_cached(self, cache_type): + """return all values in a dictionary + example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')} + """ + return self._cache diff --git a/tiramisu/plugins/dictionary/setting.py b/tiramisu/plugins/dictionary/setting.py index 3ccbeb1..24ff86c 100644 --- a/tiramisu/plugins/dictionary/setting.py +++ b/tiramisu/plugins/dictionary/setting.py @@ -23,7 +23,7 @@ from tiramisu.plugins.dictionary.cache import PluginCache class PluginSettings(PluginCache): __slots__ = ('_properties', '_permissives') - def __init__(self): + def __init__(self, config_id): # properties attribute: the name of a property enables this property # key is None for global properties self._properties = {} @@ -42,7 +42,7 @@ class PluginSettings(PluginCache): return opt in self._properties def _p_reset_all_propertives(self): - self._properties = {} + self._properties.clear() def _p_reset_properties(self, opt): try: @@ -50,6 +50,9 @@ class PluginSettings(PluginCache): except KeyError: pass + def _p_get_properties(self, cache_type): + return self._properties + # permissive def _p_setpermissive(self, opt, permissive): self._permissives[opt] = frozenset(permissive) diff --git a/tiramisu/plugins/dictionary/value.py b/tiramisu/plugins/dictionary/value.py index 84f60c2..0095f28 100644 --- a/tiramisu/plugins/dictionary/value.py +++ b/tiramisu/plugins/dictionary/value.py @@ -25,7 +25,7 @@ from tiramisu.plugins.dictionary.cache import PluginCache class PluginValues(PluginCache): __slots__ = ('_values',) - def __init__(self): + def __init__(self, config_id): """init plugin means create values storage """ self._values = {} diff --git a/tiramisu/plugins/sqlite3/__init__.py b/tiramisu/plugins/sqlite3/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tiramisu/plugins/sqlite3/cache.py b/tiramisu/plugins/sqlite3/cache.py new file mode 100644 index 0000000..8bd8ce6 --- /dev/null +++ b/tiramisu/plugins/sqlite3/cache.py @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- +"default plugin for cache: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ + +from pickle import dumps, loads +from os import unlink +import sqlite3 + + +class PluginCache(object): + __slots__ = ('_conn', '_cursor') + + def __init__(self, config_id, cache_type): + cache_table = 'CREATE TABLE IF NOT EXISTS cache_{0}(path text primary key, value text, time real)'.format(cache_type) + self._conn = sqlite3.connect(config_id + '.db') + self._conn.text_factory = str + self._cursor = self._conn.cursor() + self._cursor.execute(cache_table) + + # value + def _p_sqlite_decode(self, value): + return loads(value) + + def _p_sqlite_encode(self, value): + if isinstance(value, list): + value = list(value) + return dumps(value) + + def _p_setcache(self, cache_type, opt, val, time): + path = self._get_opt_path(opt) + convert_value = self._p_sqlite_encode(val) + self._cursor.execute("DELETE FROM cache_{0} WHERE path = ?".format(cache_type), (path,)) + self._cursor.execute("INSERT INTO cache_{0}(path, value, time) VALUES (?, ?, ?)".format(cache_type), + (path, convert_value, time)) + self._conn.commit() + + def _p_getcache(self, cache_type, opt, exp): + path = self._get_opt_path(opt) + self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ? AND time >= ?".format(cache_type), (path, exp)) + cached = self._cursor.fetchone() + if cached is None: + return False, None + else: + return True, self._p_sqlite_decode(cached[0]) + + def _p_hascache(self, cache_type, opt): + path = self._get_opt_path(opt) + self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ?".format(cache_type), (path,)) + return self._cursor.fetchone() is not None + + def _p_reset_expired_cache(self, cache_type, exp): + self._cursor.execute("DELETE FROM cache_{0} WHERE time < ?".format(cache_type), (exp,)) + self._conn.commit() + + def _p_reset_all_cache(self, cache_type): + self._cursor.execute("DELETE FROM cache_{0}".format(cache_type)) + self._conn.commit() + + def _p_get_cached(self, cache_type): + """return all values in a dictionary + example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')} + """ + self._cursor.execute("SELECT * FROM cache_{0}".format(cache_type)) + ret = {} + for path, value, time in self._cursor.fetchall(): + opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) + value = self._p_sqlite_decode(value) + ret[opt] = (value, time) + return ret diff --git a/tiramisu/plugins/sqlite3/setting.py b/tiramisu/plugins/sqlite3/setting.py new file mode 100644 index 0000000..816c510 --- /dev/null +++ b/tiramisu/plugins/sqlite3/setting.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +"default plugin for setting: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ +from tiramisu.plugins.sqlite3.cache import PluginCache + + +class PluginSettings(PluginCache): + __slots__ = tuple() + + def __init__(self, config_id): + settings_table = 'CREATE TABLE IF NOT EXISTS property(path text primary key, properties text)' + permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text primary key, permissives text)' + # should init cache too + super(PluginSettings, self).__init__(config_id, 'property') + self._cursor.execute(settings_table) + self._cursor.execute(permissives_table) + self._conn.commit() + # sqlite + + def _p_sqlite_getpath(self, opt): + if opt is None: + return '_none' + else: + return self._get_opt_path(opt) + + # propertives + def _p_setproperties(self, opt, properties): + path = self._p_sqlite_getpath(opt) + self._cursor.execute("DELETE FROM property WHERE path = ?", (path,)) + self._cursor.execute("INSERT INTO property(path, properties) VALUES (?, ?)", + (path, self._p_sqlite_encode(properties))) + self._conn.commit() + + def _p_getproperties(self, opt, default_properties): + path = self._p_sqlite_getpath(opt) + self._cursor.execute("SELECT properties FROM property WHERE path = ?", (path,)) + value = self._cursor.fetchone() + if value is None: + return set(default_properties) + else: + return set(self._p_sqlite_decode(value[0])) + + def _p_hasproperties(self, opt): + path = self._p_sqlite_getpath(opt) + return self._cursor.execute("SELECT properties FROM property WHERE path = ?", (path,)) is not None + + def _p_reset_all_propertives(self): + self._cursor.execute("DELETE FROM property") + self._conn.commit() + + def _p_reset_properties(self, opt): + path = self._p_sqlite_getpath(opt) + self._cursor.execute("DELETE FROM property WHERE path = ?", (path,)) + self._conn.commit() + + def _p_get_properties(self): + """return all properties in a dictionary + """ + self._cursor.execute("SELECT * FROM property") + ret = {} + for path, properties in self._cursor.fetchall(): + if path == '_none': + opt = None + else: + opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) + properties = self._p_sqlite_decode(properties) + ret[opt] = properties + return ret + + # permissive + def _p_setpermissive(self, opt, permissive): + path = self._p_sqlite_getpath(opt) + self._cursor.execute("DELETE FROM permissive WHERE path = ?", (path,)) + self._cursor.execute("INSERT INTO permissive(path, permissives) VALUES (?, ?)", + (path, self._p_sqlite_encode(permissive))) + self._conn.commit() + + def _p_getpermissive(self, opt=None): + path = self._p_sqlite_getpath(opt) + self._cursor.execute("SELECT permissives FROM permissive WHERE path = ?", (path,)) + permissives = self._cursor.fetchone() + if permissives is None: + return frozenset() + else: + return frozenset(self._p_sqlite_decode(permissives[0])) diff --git a/tiramisu/plugins/sqlite3/value.py b/tiramisu/plugins/sqlite3/value.py new file mode 100644 index 0000000..b499f8b --- /dev/null +++ b/tiramisu/plugins/sqlite3/value.py @@ -0,0 +1,109 @@ +# -*- coding: utf-8 -*- +"default plugin for value: set it in a simple dictionary" +# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# ____________________________________________________________ + +from tiramisu.plugins.sqlite3.cache import PluginCache + + +class PluginValues(PluginCache): + __slots__ = tuple() + + def __init__(self, config_id): + """init plugin means create values storage + """ + values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary key, value text, owner text)' + # should init cache too + super(PluginValues, self).__init__(config_id, 'value') + self._cursor.execute(values_table) + self._conn.commit() + + # sqlite + def _p_sqlite_select(self, opt): + path = self._get_opt_path(opt) + self._cursor.execute("SELECT value FROM value WHERE path = ?", (path,)) + return self._cursor.fetchone() + + # value + def _p_setvalue(self, opt, value): + """set value for an option + a specified value must be associated to an owner + """ + path = self._get_opt_path(opt) + owner = self.context.cfgimpl_get_settings().getowner() + self._p_resetvalue(opt, path) + self._cursor.execute("INSERT INTO value(path, value, owner) VALUES (?, ?, ?)", + (path, self._p_sqlite_encode(value), str(owner))) + self._conn.commit() + + def _p_getvalue(self, opt): + """get value for an option + return: only value, not the owner + """ + return self._p_sqlite_decode(self._p_sqlite_select(opt)[0]) + + def _p_hasvalue(self, opt): + """if opt has a value + return: boolean + """ + return self._p_sqlite_select(opt) is not None + + def _p_resetvalue(self, opt, path=None): + """remove value means delete value in storage + """ + if not path: + path = self._get_opt_path(opt) + self._cursor.execute("DELETE FROM value WHERE path = ?", (path,)) + self._conn.commit() + + def _p_get_modified_values(self): + """return all values in a dictionary + example: {option1: (owner, 'value1'), option2: (owner, 'value2')} + """ + self._cursor.execute("SELECT value") + ret = {} + for path, value, owner in self._cursor.fetchall(): + opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) + owner = self._get_owner_from_str(owner) + value = self._p_sqlite_decode(value) + ret[opt] = (owner, value) + return ret + + # owner + def _p_setowner(self, opt, owner): + """change owner for an option + """ + path = self._get_opt_path(opt) + self._cursor.execute("UPDATE value SET owner = ? WHERE path = ?", (str(owner), path)) + self._conn.commit() + + def _p_getowner(self, opt, default): + """get owner for an option + return: owner object + """ + path = self._get_opt_path(opt) + self._cursor.execute("SELECT owner FROM value WHERE path = ?", (path,)) + owner = self._cursor.fetchone() + if owner is None: + return default + else: + return self._get_owner_from_str(owner[0]) + + def __del__(self): + self._cursor.close() + self._conn.close() diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 43e2d70..508b40f 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -24,7 +24,8 @@ from time import time from copy import copy from tiramisu.error import RequirementError, PropertiesOptionError from tiramisu.i18n import _ -from tiramisu.plugins.dictionary.setting import PluginSettings +#from tiramisu.plugins.dictionary.setting import PluginSettings +from tiramisu.plugins.sqlite3.setting import PluginSettings default_encoding = 'utf-8' @@ -182,11 +183,11 @@ class Settings(PluginSettings): "``Config()``'s configuration options" __slots__ = ('context', '_owner') - def __init__(self, context): + def __init__(self, context, config_id): # generic owner self._owner = owners.user self.context = context - super(Settings, self).__init__() + super(Settings, self).__init__(config_id) #____________________________________________________________ # properties methods @@ -217,9 +218,9 @@ class Settings(PluginSettings): props = self._p_getproperties(opt, default_properties) else: ntime = None - if self._p_hascache('properties', opt): + if self._p_hascache('property', opt): ntime = time() - is_cached, props = self._p_getcache('properties', opt, ntime) + is_cached, props = self._p_getcache('property', opt, ntime) if is_cached: return props if is_apply_req: @@ -228,7 +229,7 @@ class Settings(PluginSettings): if 'expire' in self: if ntime is None: ntime = time() - self._p_setcache('properties', opt, props, ntime + expires_time) + self._p_setcache('property', opt, props, ntime + expires_time) return props def append(self, propname): @@ -328,9 +329,9 @@ class Settings(PluginSettings): def reset_cache(self, only_expired): if only_expired: - self._p_reset_expired_cache('properties', time()) + self._p_reset_expired_cache('property', time()) else: - self._p_reset_all_cache('properties') + self._p_reset_all_cache('property') def apply_requires(self, opt): "carries out the jit (just in time requirements between options" @@ -373,3 +374,6 @@ class Settings(PluginSettings): # no requirement has been triggered, then just reverse the action if not matches: setting.remove(action) + + def _get_opt_path(self, opt): + return self.context.cfgimpl_get_description().impl_get_path_by_opt(opt) diff --git a/tiramisu/value.py b/tiramisu/value.py index d22bec3..4348ff7 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -26,7 +26,8 @@ from tiramisu.i18n import _ from tiramisu.option import SymLinkOption #FIXME -from tiramisu.plugins.dictionary.value import PluginValues +#from tiramisu.plugins.dictionary.value import PluginValues +from tiramisu.plugins.sqlite3.value import PluginValues class Values(PluginValues): @@ -36,7 +37,7 @@ class Values(PluginValues): """ __slots__ = ('context',) - def __init__(self, context): + def __init__(self, context, config_id): """ Initializes the values's dict. @@ -45,7 +46,7 @@ class Values(PluginValues): """ self.context = context - super(Values, self).__init__() + super(Values, self).__init__(config_id) def _getdefault(self, opt): meta = self.context.cfgimpl_get_meta() @@ -77,7 +78,7 @@ class Values(PluginValues): return self._p_get_modified_values() def __contains__(self, opt): - return self._p_hascache('value', opt) + return self._p_hasvalue('value', opt) def __delitem__(self, opt): self.reset(opt) @@ -121,6 +122,9 @@ class Values(PluginValues): ntime = time() is_cached, value = self._p_getcache('value', opt, ntime) if is_cached: + if opt.impl_is_multi() and not isinstance(value, Multi): + #load value so don't need to validate if is not a Multi + value = Multi(value, self.context, opt, validate=False) return value val = self._getitem(opt, validate, force_permissive, force_properties, validate_properties) @@ -321,6 +325,7 @@ class Multi(list): value = None self._validate(value) super(Multi, self).append(value) + self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) if not force and self.opt.impl_get_multitype() == multitypes.master: for slave in self.opt.impl_get_master_slaves(): if not values.is_default_owner(slave): @@ -329,10 +334,12 @@ class Multi(list): dvalue = values._getcallback_value(slave, index=index) else: dvalue = slave.impl_getdefault_multi() - #get multi without valid properties - values.getitem(slave, validate_properties=False).append( - dvalue, force=True) - self.context.cfgimpl_get_values()._setvalue(self.opt, self, validate_properties=not force) + old_value = values.getitem(slave, validate_properties=False) + if len(old_value) < self.__len__(): + values.getitem(slave, validate_properties=False).append( + dvalue, force=True) + else: + values.getitem(slave, validate_properties=False)[index] = dvalue def sort(self, cmp=None, key=None, reverse=False): if self.opt.impl_get_multitype() in [multitypes.slave, From e826f3d1c6c9ea6c3d3e9e5c73b8c1b6ceb04f59 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 20 Aug 2013 09:47:12 +0200 Subject: [PATCH 3/3] we can personalise storage easily --- test/test_cache.py | 156 ++++++++++++------------- test/test_option_setting.py | 18 +-- tiramisu/config.py | 13 ++- tiramisu/option.py | 2 +- tiramisu/plugins/dictionary/cache.py | 15 +-- tiramisu/plugins/dictionary/setting.py | 22 ++-- tiramisu/plugins/dictionary/value.py | 21 ++-- tiramisu/plugins/sqlite3/cache.py | 31 +++-- tiramisu/plugins/sqlite3/setting.py | 47 +++----- tiramisu/plugins/sqlite3/value.py | 47 ++++---- tiramisu/setting.py | 52 +++++---- tiramisu/value.py | 55 +++++---- 12 files changed, 237 insertions(+), 242 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index c7bf3b4..61eb8f4 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -20,13 +20,13 @@ def test_cache(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u2 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') - assert od1.u2 in values._p_get_cached('value') - assert od1.u2 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) + assert od1.u2 in values._p_.get_cached('value', c) + assert od1.u2 in settings._p_.get_cached('property', c) def test_cache_reset(): @@ -36,44 +36,44 @@ def test_cache_reset(): settings = c.cfgimpl_get_settings() #when change a value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u2 = 1 - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when remove a value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) del(c.u2) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when add/del property c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_get_settings()[od1.u2].append('test') - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_get_settings()[od1.u2].remove('test') - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when enable/disabled property c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_get_settings().append('test') - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_get_settings().remove('test') - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) def test_cache_reset_multi(): @@ -83,32 +83,32 @@ def test_cache_reset_multi(): settings = c.cfgimpl_get_settings() #when change a value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u3 = [1] - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when append value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u3.append(1) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when pop value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u3.pop(1) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) #when remove a value c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) del(c.u3) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) def test_reset_cache(): @@ -117,23 +117,23 @@ def test_reset_cache(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache() - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) c.u1 sleep(1) c.u2 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') - assert od1.u2 in values._p_get_cached('value') - assert od1.u2 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) + assert od1.u2 in values._p_.get_cached('value', c) + assert od1.u2 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache() - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') - assert od1.u2 not in values._p_get_cached('value') - assert od1.u2 not in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) + assert od1.u2 not in values._p_.get_cached('value', c) + assert od1.u2 not in settings._p_.get_cached('property', c) def test_reset_cache_only_expired(): @@ -142,22 +142,22 @@ def test_reset_cache_only_expired(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache(True) - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) sleep(1) c.u2 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') - assert od1.u2 in values._p_get_cached('value') - assert od1.u2 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) + assert od1.u2 in values._p_.get_cached('value', c) + assert od1.u2 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache(True) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') - assert od1.u2 in values._p_get_cached('value') - assert od1.u2 in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) + assert od1.u2 in values._p_.get_cached('value', c) + assert od1.u2 in settings._p_.get_cached('property', c) def test_reset_cache_only(): @@ -166,14 +166,14 @@ def test_reset_cache_only(): values = c.cfgimpl_get_values() settings = c.cfgimpl_get_settings() c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache(only=('values',)) - assert od1.u1 not in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 not in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.u1 - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 in settings._p_.get_cached('property', c) c.cfgimpl_reset_cache(only=('settings',)) - assert od1.u1 in values._p_get_cached('value') - assert od1.u1 not in settings._p_get_cached('property') + assert od1.u1 in values._p_.get_cached('value', c) + assert od1.u1 not in settings._p_.get_cached('property', c) diff --git a/test/test_option_setting.py b/test/test_option_setting.py index 66121ec..979a79e 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -327,23 +327,23 @@ def test_reset_properties(): cfg = Config(descr) setting = cfg.cfgimpl_get_settings() option = cfg.cfgimpl_get_description().gc.dummy - assert setting._p_get_properties() == {} + assert setting._p_.get_properties(cfg) == {} setting.append('frozen') - assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator'))} + assert setting._p_.get_properties(cfg) == {None: set(('frozen', 'expire', 'validator'))} setting.reset() - assert setting._p_get_properties() == {} + assert setting._p_.get_properties(cfg) == {} setting[option].append('test') - assert setting._p_get_properties() == {option: set(('test',))} + assert setting._p_.get_properties(cfg) == {option: set(('test',))} setting.reset() - assert setting._p_get_properties() == {option: set(('test',))} + assert setting._p_.get_properties(cfg) == {option: set(('test',))} setting.append('frozen') - assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} + assert setting._p_.get_properties(cfg) == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} setting.reset(option) - assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator'))} + assert setting._p_.get_properties(cfg) == {None: set(('frozen', 'expire', 'validator'))} setting[option].append('test') - assert setting._p_get_properties() == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} + assert setting._p_.get_properties(cfg) == {None: set(('frozen', 'expire', 'validator')), option: set(('test',))} setting.reset(all_properties=True) - assert setting._p_get_properties() == {} + assert setting._p_.get_properties(cfg) == {} raises(ValueError, 'setting.reset(all_properties=True, opt=option)') diff --git a/tiramisu/config.py b/tiramisu/config.py index 3703937..d27bf22 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -20,10 +20,11 @@ # 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 PropertiesOptionError, ConfigError from tiramisu.option import OptionDescription, Option, SymLinkOption, \ BaseInformation -from tiramisu.setting import groups, Settings, default_encoding +from tiramisu.setting import groups, Settings, default_encoding, default_storage from tiramisu.value import Values from tiramisu.i18n import _ @@ -505,9 +506,9 @@ class Config(CommonConfig): :param context: the current root config :type context: `Config` """ - config_id = str(id(self)) - self._impl_settings = Settings(self, config_id) - self._impl_values = Values(self, config_id) + config_id = str(id(self)) + str(time()) + self._impl_settings = Settings(self, config_id, default_storage) + self._impl_values = Values(self, config_id, default_storage) super(Config, self).__init__(descr, self) self._impl_build_all_paths() self._impl_meta = None @@ -543,8 +544,8 @@ class MetaConfig(CommonConfig): config_id = str(id(self)) self._impl_children = children - self._impl_settings = Settings(self, config_id) - self._impl_values = Values(self, config_id) + self._impl_settings = Settings(self, config_id, default_storage) + self._impl_values = Values(self, config_id, default_storage) self._impl_meta = None self._impl_informations = {} diff --git a/tiramisu/option.py b/tiramisu/option.py index 3175995..3b25fc2 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -59,7 +59,7 @@ class BaseInformation(object): def impl_set_information(self, key, value): """updates the information's attribute - (wich is a dictionnary) + (which is a dictionary) :param key: information's key (ex: "help", "doc" :param value: information's value (ex: "the help string") diff --git a/tiramisu/plugins/dictionary/cache.py b/tiramisu/plugins/dictionary/cache.py index 2f189e6..d84964d 100644 --- a/tiramisu/plugins/dictionary/cache.py +++ b/tiramisu/plugins/dictionary/cache.py @@ -19,35 +19,36 @@ # ____________________________________________________________ -class PluginCache(object): +class Cache(object): __slots__ = ('_cache',) + key_is_path = False def __init__(self): self._cache = {} - def _p_setcache(self, cache_type, opt, val, time): + def setcache(self, cache_type, opt, val, time): self._cache[opt] = (val, time) - def _p_getcache(self, cache_type, opt, exp): + def getcache(self, cache_type, opt, exp): value, created = self._cache[opt] if exp < created: return True, value return False, None - def _p_hascache(self, cache_type, opt): + def hascache(self, cache_type, opt): return opt in self._cache - def _p_reset_expired_cache(self, cache_type, exp): + def reset_expired_cache(self, cache_type, exp): keys = self._cache.keys() for key in keys: val, created = self._cache[key] if exp > created: del(self._cache[key]) - def _p_reset_all_cache(self, cache_type): + def reset_all_cache(self, cache_type): self._cache.clear() - def _p_get_cached(self, cache_type): + def get_cached(self, cache_type, context): """return all values in a dictionary example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')} """ diff --git a/tiramisu/plugins/dictionary/setting.py b/tiramisu/plugins/dictionary/setting.py index 24ff86c..5b7b67d 100644 --- a/tiramisu/plugins/dictionary/setting.py +++ b/tiramisu/plugins/dictionary/setting.py @@ -17,10 +17,10 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ____________________________________________________________ -from tiramisu.plugins.dictionary.cache import PluginCache +from tiramisu.plugins.dictionary.cache import Cache -class PluginSettings(PluginCache): +class Settings(Cache): __slots__ = ('_properties', '_permissives') def __init__(self, config_id): @@ -29,33 +29,33 @@ class PluginSettings(PluginCache): self._properties = {} # permissive properties self._permissives = {} - super(PluginSettings, self).__init__() + super(Settings, self).__init__() # propertives - def _p_setproperties(self, opt, properties): + def setproperties(self, opt, properties): self._properties[opt] = properties - def _p_getproperties(self, opt, default_properties): + def getproperties(self, opt, default_properties): return self._properties.get(opt, set(default_properties)) - def _p_hasproperties(self, opt): + def hasproperties(self, opt): return opt in self._properties - def _p_reset_all_propertives(self): + def reset_all_propertives(self): self._properties.clear() - def _p_reset_properties(self, opt): + def reset_properties(self, opt): try: del(self._properties[opt]) except KeyError: pass - def _p_get_properties(self, cache_type): + def get_properties(self, context): return self._properties # permissive - def _p_setpermissive(self, opt, permissive): + def setpermissive(self, opt, permissive): self._permissives[opt] = frozenset(permissive) - def _p_getpermissive(self, opt=None): + def getpermissive(self, opt=None): return self._permissives.get(opt, frozenset()) diff --git a/tiramisu/plugins/dictionary/value.py b/tiramisu/plugins/dictionary/value.py index 0095f28..501c318 100644 --- a/tiramisu/plugins/dictionary/value.py +++ b/tiramisu/plugins/dictionary/value.py @@ -19,10 +19,10 @@ # ____________________________________________________________ #FIXME -from tiramisu.plugins.dictionary.cache import PluginCache +from tiramisu.plugins.dictionary.cache import Cache -class PluginValues(PluginCache): +class Values(Cache): __slots__ = ('_values',) def __init__(self, config_id): @@ -30,46 +30,45 @@ class PluginValues(PluginCache): """ self._values = {} # should init cache too - super(PluginValues, self).__init__() + super(Values, self).__init__() # value - def _p_setvalue(self, opt, value): + def setvalue(self, opt, value, owner): """set value for an option a specified value must be associated to an owner """ - owner = self.context.cfgimpl_get_settings().getowner() self._values[opt] = (owner, value) - def _p_getvalue(self, opt): + def getvalue(self, opt): """get value for an option return: only value, not the owner """ return self._values[opt][1] - def _p_hasvalue(self, opt): + def hasvalue(self, opt): """if opt has a value return: boolean """ return opt in self._values - def _p_resetvalue(self, opt): + def resetvalue(self, opt): """remove value means delete value in storage """ del(self._values[opt]) - def _p_get_modified_values(self): + def get_modified_values(self): """return all values in a dictionary example: {option1: (owner, 'value1'), option2: (owner, 'value2')} """ return self._values # owner - def _p_setowner(self, opt, owner): + def setowner(self, opt, owner): """change owner for an option """ self._values[opt] = (owner, self._values[opt][1]) - def _p_getowner(self, opt, default): + def getowner(self, opt, default): """get owner for an option return: owner object """ diff --git a/tiramisu/plugins/sqlite3/cache.py b/tiramisu/plugins/sqlite3/cache.py index 8bd8ce6..8d47e35 100644 --- a/tiramisu/plugins/sqlite3/cache.py +++ b/tiramisu/plugins/sqlite3/cache.py @@ -19,12 +19,12 @@ # ____________________________________________________________ from pickle import dumps, loads -from os import unlink import sqlite3 -class PluginCache(object): +class Cache(object): __slots__ = ('_conn', '_cursor') + key_is_path = True def __init__(self, config_id, cache_type): cache_table = 'CREATE TABLE IF NOT EXISTS cache_{0}(path text primary key, value text, time real)'.format(cache_type) @@ -34,52 +34,49 @@ class PluginCache(object): self._cursor.execute(cache_table) # value - def _p_sqlite_decode(self, value): + def _sqlite_decode(self, value): return loads(value) - def _p_sqlite_encode(self, value): + def _sqlite_encode(self, value): if isinstance(value, list): value = list(value) return dumps(value) - def _p_setcache(self, cache_type, opt, val, time): - path = self._get_opt_path(opt) - convert_value = self._p_sqlite_encode(val) + def setcache(self, cache_type, path, val, time): + convert_value = self._sqlite_encode(val) self._cursor.execute("DELETE FROM cache_{0} WHERE path = ?".format(cache_type), (path,)) self._cursor.execute("INSERT INTO cache_{0}(path, value, time) VALUES (?, ?, ?)".format(cache_type), (path, convert_value, time)) self._conn.commit() - def _p_getcache(self, cache_type, opt, exp): - path = self._get_opt_path(opt) + def getcache(self, cache_type, path, exp): self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ? AND time >= ?".format(cache_type), (path, exp)) cached = self._cursor.fetchone() if cached is None: return False, None else: - return True, self._p_sqlite_decode(cached[0]) + return True, self._sqlite_decode(cached[0]) - def _p_hascache(self, cache_type, opt): - path = self._get_opt_path(opt) + def hascache(self, cache_type, path): self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ?".format(cache_type), (path,)) return self._cursor.fetchone() is not None - def _p_reset_expired_cache(self, cache_type, exp): + def reset_expired_cache(self, cache_type, exp): self._cursor.execute("DELETE FROM cache_{0} WHERE time < ?".format(cache_type), (exp,)) self._conn.commit() - def _p_reset_all_cache(self, cache_type): + def reset_all_cache(self, cache_type): self._cursor.execute("DELETE FROM cache_{0}".format(cache_type)) self._conn.commit() - def _p_get_cached(self, cache_type): + def get_cached(self, cache_type, context): """return all values in a dictionary example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')} """ self._cursor.execute("SELECT * FROM cache_{0}".format(cache_type)) ret = {} for path, value, time in self._cursor.fetchall(): - opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) - value = self._p_sqlite_decode(value) + opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) + value = self._sqlite_decode(value) ret[opt] = (value, time) return ret diff --git a/tiramisu/plugins/sqlite3/setting.py b/tiramisu/plugins/sqlite3/setting.py index 816c510..bee0550 100644 --- a/tiramisu/plugins/sqlite3/setting.py +++ b/tiramisu/plugins/sqlite3/setting.py @@ -17,59 +17,48 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ____________________________________________________________ -from tiramisu.plugins.sqlite3.cache import PluginCache +from tiramisu.plugins.sqlite3.cache import Cache -class PluginSettings(PluginCache): +class Settings(Cache): __slots__ = tuple() def __init__(self, config_id): settings_table = 'CREATE TABLE IF NOT EXISTS property(path text primary key, properties text)' permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text primary key, permissives text)' # should init cache too - super(PluginSettings, self).__init__(config_id, 'property') + super(Settings, self).__init__(config_id, 'property') self._cursor.execute(settings_table) self._cursor.execute(permissives_table) self._conn.commit() - # sqlite - - def _p_sqlite_getpath(self, opt): - if opt is None: - return '_none' - else: - return self._get_opt_path(opt) # propertives - def _p_setproperties(self, opt, properties): - path = self._p_sqlite_getpath(opt) + def setproperties(self, path, properties): self._cursor.execute("DELETE FROM property WHERE path = ?", (path,)) self._cursor.execute("INSERT INTO property(path, properties) VALUES (?, ?)", - (path, self._p_sqlite_encode(properties))) + (path, self._sqlite_encode(properties))) self._conn.commit() - def _p_getproperties(self, opt, default_properties): - path = self._p_sqlite_getpath(opt) + def getproperties(self, path, default_properties): self._cursor.execute("SELECT properties FROM property WHERE path = ?", (path,)) value = self._cursor.fetchone() if value is None: return set(default_properties) else: - return set(self._p_sqlite_decode(value[0])) + return set(self._sqlite_decode(value[0])) - def _p_hasproperties(self, opt): - path = self._p_sqlite_getpath(opt) + def hasproperties(self, path): return self._cursor.execute("SELECT properties FROM property WHERE path = ?", (path,)) is not None - def _p_reset_all_propertives(self): + def reset_all_propertives(self): self._cursor.execute("DELETE FROM property") self._conn.commit() - def _p_reset_properties(self, opt): - path = self._p_sqlite_getpath(opt) + def reset_properties(self, path): self._cursor.execute("DELETE FROM property WHERE path = ?", (path,)) self._conn.commit() - def _p_get_properties(self): + def get_properties(self, context): """return all properties in a dictionary """ self._cursor.execute("SELECT * FROM property") @@ -78,24 +67,22 @@ class PluginSettings(PluginCache): if path == '_none': opt = None else: - opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) - properties = self._p_sqlite_decode(properties) + opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) + properties = self._sqlite_decode(properties) ret[opt] = properties return ret # permissive - def _p_setpermissive(self, opt, permissive): - path = self._p_sqlite_getpath(opt) + def setpermissive(self, path, permissive): self._cursor.execute("DELETE FROM permissive WHERE path = ?", (path,)) self._cursor.execute("INSERT INTO permissive(path, permissives) VALUES (?, ?)", - (path, self._p_sqlite_encode(permissive))) + (path, self._sqlite_encode(permissive))) self._conn.commit() - def _p_getpermissive(self, opt=None): - path = self._p_sqlite_getpath(opt) + def getpermissive(self, path='_none'): self._cursor.execute("SELECT permissives FROM permissive WHERE path = ?", (path,)) permissives = self._cursor.fetchone() if permissives is None: return frozenset() else: - return frozenset(self._p_sqlite_decode(permissives[0])) + return frozenset(self._sqlite_decode(permissives[0])) diff --git a/tiramisu/plugins/sqlite3/value.py b/tiramisu/plugins/sqlite3/value.py index b499f8b..85fb640 100644 --- a/tiramisu/plugins/sqlite3/value.py +++ b/tiramisu/plugins/sqlite3/value.py @@ -18,10 +18,11 @@ # # ____________________________________________________________ -from tiramisu.plugins.sqlite3.cache import PluginCache +from tiramisu.plugins.sqlite3.cache import Cache +from tiramisu.setting import owners -class PluginValues(PluginCache): +class Values(Cache): __slots__ = tuple() def __init__(self, config_id): @@ -29,80 +30,74 @@ class PluginValues(PluginCache): """ values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary key, value text, owner text)' # should init cache too - super(PluginValues, self).__init__(config_id, 'value') + super(Values, self).__init__(config_id, 'value') self._cursor.execute(values_table) self._conn.commit() # sqlite - def _p_sqlite_select(self, opt): - path = self._get_opt_path(opt) + def _sqlite_select(self, path): self._cursor.execute("SELECT value FROM value WHERE path = ?", (path,)) return self._cursor.fetchone() # value - def _p_setvalue(self, opt, value): + def setvalue(self, path, value, owner): """set value for an option a specified value must be associated to an owner """ - path = self._get_opt_path(opt) - owner = self.context.cfgimpl_get_settings().getowner() - self._p_resetvalue(opt, path) + self.resetvalue(path) self._cursor.execute("INSERT INTO value(path, value, owner) VALUES (?, ?, ?)", - (path, self._p_sqlite_encode(value), str(owner))) + (path, self._sqlite_encode(value), str(owner))) self._conn.commit() - def _p_getvalue(self, opt): + def getvalue(self, path): """get value for an option return: only value, not the owner """ - return self._p_sqlite_decode(self._p_sqlite_select(opt)[0]) + return self._sqlite_decode(self._sqlite_select(path)[0]) - def _p_hasvalue(self, opt): + def hasvalue(self, path): """if opt has a value return: boolean """ - return self._p_sqlite_select(opt) is not None + return self._sqlite_select(path) is not None - def _p_resetvalue(self, opt, path=None): + def resetvalue(self, path): """remove value means delete value in storage """ - if not path: - path = self._get_opt_path(opt) self._cursor.execute("DELETE FROM value WHERE path = ?", (path,)) self._conn.commit() - def _p_get_modified_values(self): + def get_modified_values(self, context): """return all values in a dictionary example: {option1: (owner, 'value1'), option2: (owner, 'value2')} """ self._cursor.execute("SELECT value") ret = {} for path, value, owner in self._cursor.fetchall(): - opt = self.context.cfgimpl_get_description().impl_get_opt_by_path(path) - owner = self._get_owner_from_str(owner) - value = self._p_sqlite_decode(value) + opt = context.cfgimpl_get_description().impl_get_opt_by_path(path) + owner = getattr(owners, owner) + + value = self._sqlite_decode(value) ret[opt] = (owner, value) return ret # owner - def _p_setowner(self, opt, owner): + def setowner(self, path, owner): """change owner for an option """ - path = self._get_opt_path(opt) self._cursor.execute("UPDATE value SET owner = ? WHERE path = ?", (str(owner), path)) self._conn.commit() - def _p_getowner(self, opt, default): + def getowner(self, path, default): """get owner for an option return: owner object """ - path = self._get_opt_path(opt) self._cursor.execute("SELECT owner FROM value WHERE path = ?", (path,)) owner = self._cursor.fetchone() if owner is None: return default else: - return self._get_owner_from_str(owner[0]) + return getattr(owners, owner[0]) def __del__(self): self._cursor.close() diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 508b40f..4989c7f 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -24,8 +24,6 @@ from time import time from copy import copy from tiramisu.error import RequirementError, PropertiesOptionError from tiramisu.i18n import _ -#from tiramisu.plugins.dictionary.setting import PluginSettings -from tiramisu.plugins.sqlite3.setting import PluginSettings default_encoding = 'utf-8' @@ -35,6 +33,7 @@ ro_append = ('frozen', 'disabled', 'validator', 'everything_frozen', 'mandatory' rw_remove = ('permissive', 'everything_frozen', 'mandatory') rw_append = ('frozen', 'disabled', 'validator', 'hidden') default_properties = ('expire', 'validator') +default_storage = 'dictionary' class _const: @@ -179,15 +178,26 @@ class Property(object): #____________________________________________________________ -class Settings(PluginSettings): +class Settings(object): "``Config()``'s configuration options" - __slots__ = ('context', '_owner') + __slots__ = ('context', '_owner', '_p_') - def __init__(self, context, config_id): + def __init__(self, context, config_id, plugin_name): # generic owner self._owner = owners.user self.context = context - super(Settings, self).__init__(config_id) + import_lib = 'tiramisu.plugins.{0}.setting'.format(plugin_name) + self._p_ = __import__(import_lib, globals(), locals(), ['Settings'], + -1).Settings(config_id) + + def _getkey(self, opt): + if self._p_.key_is_path: + if opt is None: + return '_none' + else: + return self._get_opt_path(opt) + else: + return opt #____________________________________________________________ # properties methods @@ -208,28 +218,28 @@ class Settings(PluginSettings): raise ValueError(_('opt and all_properties must not be set ' 'together in reset')) if all_properties: - self._p_reset_all_propertives() + self._p_.reset_all_propertives() else: - self._p_reset_properties(opt) + self._p_.reset_properties(self._getkey(opt)) self.context.cfgimpl_reset_cache() def _getproperties(self, opt=None, is_apply_req=True): if opt is None: - props = self._p_getproperties(opt, default_properties) + props = self._p_.getproperties(self._getkey(opt), default_properties) else: ntime = None - if self._p_hascache('property', opt): + if self._p_.hascache('property', self._getkey(opt)): ntime = time() - is_cached, props = self._p_getcache('property', opt, ntime) + is_cached, props = self._p_.getcache('property', self._getkey(opt), ntime) if is_cached: return props if is_apply_req: self.apply_requires(opt) - props = self._p_getproperties(opt, opt._properties) + props = self._p_.getproperties(self._getkey(opt), opt._properties) if 'expire' in self: if ntime is None: ntime = time() - self._p_setcache('property', opt, props, ntime + expires_time) + self._p_.setcache('property', self._getkey(opt), props, ntime + expires_time) return props def append(self, propname): @@ -245,12 +255,12 @@ class Settings(PluginSettings): (never save properties if same has option properties) """ if opt is None: - self._p_setproperties(opt, properties) + self._p_.setproperties(self._getkey(opt), properties) else: if set(opt._properties) == properties: - self._p_reset_properties(opt) + self._p_.reset_properties(self._getkey(opt)) else: - self._p_setproperties(opt, properties) + self._p_.setproperties(self._getkey(opt), properties) self.context.cfgimpl_reset_cache() #____________________________________________________________ @@ -260,11 +270,11 @@ class Settings(PluginSettings): #opt properties properties = copy(self._getproperties(opt_or_descr)) #remove opt permissive - properties -= self._p_getpermissive(opt_or_descr) + properties -= self._p_.getpermissive(self._getkey(opt_or_descr)) #remove global permissive if need self_properties = copy(self._getproperties()) if force_permissive is True or 'permissive' in self_properties: - properties -= self._p_getpermissive() + properties -= self._p_.getpermissive() #global properties if force_properties is not None: @@ -300,7 +310,7 @@ class Settings(PluginSettings): def set_permissive(self, permissive, opt=None): if not isinstance(permissive, tuple): raise TypeError(_('permissive must be a tuple')) - self._p_setpermissive(opt, permissive) + self._p_.setpermissive(self._getkey(opt), permissive) #____________________________________________________________ def setowner(self, owner): @@ -329,9 +339,9 @@ class Settings(PluginSettings): def reset_cache(self, only_expired): if only_expired: - self._p_reset_expired_cache('property', time()) + self._p_.reset_expired_cache('property', time()) else: - self._p_reset_all_cache('property') + self._p_.reset_all_cache('property') def apply_requires(self, opt): "carries out the jit (just in time requirements between options" diff --git a/tiramisu/value.py b/tiramisu/value.py index 4348ff7..cb8fe6b 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -25,19 +25,15 @@ from tiramisu.autolib import carry_out_calculation from tiramisu.i18n import _ from tiramisu.option import SymLinkOption -#FIXME -#from tiramisu.plugins.dictionary.value import PluginValues -from tiramisu.plugins.sqlite3.value import PluginValues - -class Values(PluginValues): +class Values(object): """The `Config`'s root is indeed in charge of the `Option()`'s values, but the values are physicaly located here, in `Values`, wich is also responsible of a caching utility. """ - __slots__ = ('context',) + __slots__ = ('context', '_p_') - def __init__(self, context, config_id): + def __init__(self, context, config_id, plugin_name): """ Initializes the values's dict. @@ -46,7 +42,15 @@ class Values(PluginValues): """ self.context = context - super(Values, self).__init__(config_id) + import_lib = 'tiramisu.plugins.{0}.value'.format(plugin_name) + self._p_ = __import__(import_lib, globals(), locals(), ['Values'], + -1).Values(config_id) + + def _getkey(self, opt): + if self._p_.key_is_path: + return self._get_opt_path(opt) + else: + return opt def _getdefault(self, opt): meta = self.context.cfgimpl_get_meta() @@ -61,30 +65,32 @@ class Values(PluginValues): def _getvalue(self, opt, validate=True): "return value or default value if not set" - if not self._p_hasvalue(opt): + key = self._getkey(opt) + if not self._p_.hasvalue(key): #if no value value = self._getdefault(opt) if opt.impl_is_multi(): value = Multi(value, self.context, opt, validate) else: #if value - value = self._p_getvalue(opt) + value = self._p_.getvalue(key) if opt.impl_is_multi() and not isinstance(value, Multi): #load value so don't need to validate if is not a Multi value = Multi(value, self.context, opt, validate=False) return value def get_modified_values(self): - return self._p_get_modified_values() + return self._p_.get_modified_values() def __contains__(self, opt): - return self._p_hasvalue('value', opt) + return self._p_.hasvalue('value', self._getkey(opt)) def __delitem__(self, opt): self.reset(opt) def reset(self, opt): - if self._p_hasvalue(opt): + key = self._getkey(opt) + if self._p_.hasvalue(key): setting = self.context.cfgimpl_get_settings() opt.impl_validate(opt.impl_getdefault(), self.context, 'validator' in setting) @@ -92,7 +98,7 @@ class Values(PluginValues): if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master: for slave in opt.impl_get_master_slaves(): self.reset(slave) - self._p_resetvalue(opt) + self._p_.resetvalue(key) def _isempty(self, opt, value): "convenience method to know if an option is empty" @@ -118,9 +124,10 @@ class Values(PluginValues): def getitem(self, opt, validate=True, force_permissive=False, force_properties=None, validate_properties=True): ntime = None - if self._p_hascache('value', opt): + key = self._getkey(opt) + if self._p_.hascache('value', self._getkey(opt)): ntime = time() - is_cached, value = self._p_getcache('value', opt, ntime) + is_cached, value = self._p_.getcache('value', key, ntime) if is_cached: if opt.impl_is_multi() and not isinstance(value, Multi): #load value so don't need to validate if is not a Multi @@ -133,7 +140,7 @@ class Values(PluginValues): force_properties is None: if ntime is None: ntime = time() - self._p_setcache('value', opt, val, ntime + expires_time) + self._p_.setcache('value', key, val, ntime + expires_time) return val @@ -206,12 +213,13 @@ class Values(PluginValues): value=value, force_permissive=force_permissive, force_properties=force_properties) - self._p_setvalue(opt, value) + owner = self.context.cfgimpl_get_settings().getowner() + self._p_.setvalue(self._getkey(opt), value, owner) def getowner(self, opt): if isinstance(opt, SymLinkOption): opt = opt._opt - owner = self._p_getowner(opt, owners.default) + owner = self._p_.getowner(self._getkey(opt), owners.default) meta = self.context.cfgimpl_get_meta() if owner is owners.default and meta is not None: owner = meta.cfgimpl_get_values().getowner(opt) @@ -223,7 +231,7 @@ class Values(PluginValues): if self.getowner(opt) == owners.default: raise ConfigError(_('no value for {0} cannot change owner to {1}' '').format(opt._name, owner)) - self._p_setowner(opt, owner) + self._p_.setowner(self._getkey(opt), owner) def is_default_owner(self, opt): """ @@ -235,16 +243,13 @@ class Values(PluginValues): def reset_cache(self, only_expired): if only_expired: - self._p_reset_expired_cache('value', time()) + self._p_.reset_expired_cache('value', time()) else: - self._p_reset_all_cache('value') + self._p_.reset_all_cache('value') def _get_opt_path(self, opt): return self.context.cfgimpl_get_description().impl_get_path_by_opt(opt) - def _get_owner_from_str(self, owner): - return getattr(owners, owner) - # ____________________________________________________________ # multi types