we can personalise storage easily

This commit is contained in:
Emmanuel Garette 2013-08-20 09:47:12 +02:00
parent df7d6759cd
commit e826f3d1c6
12 changed files with 237 additions and 242 deletions

View file

@ -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)

View file

@ -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)')

View file

@ -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 = {}

View file

@ -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")

View file

@ -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')}
"""

View file

@ -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())

View file

@ -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
"""

View file

@ -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

View file

@ -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]))

View file

@ -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()

View file

@ -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"

View file

@ -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