From 36def6533f08a5564b41fdcb8478b0f96da414e5 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 27 Aug 2013 09:46:52 +0200 Subject: [PATCH] store session in dictionary storage --- test/test_storage.py | 81 ++++++++++++++++++++++++++ tiramisu/config.py | 13 +++-- tiramisu/setting.py | 7 ++- tiramisu/storage/dictionary/setting.py | 2 +- tiramisu/storage/dictionary/storage.py | 21 +++++-- tiramisu/storage/dictionary/value.py | 2 +- tiramisu/storage/sqlite3/storage.py | 8 +-- 7 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 test/test_storage.py diff --git a/test/test_storage.py b/test/test_storage.py new file mode 100644 index 0000000..44ba9d3 --- /dev/null +++ b/test/test_storage.py @@ -0,0 +1,81 @@ +import autopath +#from py.test import raises + +from tiramisu.config import Config +from tiramisu.option import BoolOption, OptionDescription + + +def test_non_persistent(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + Config(o, session_id='test_non_persistent') + + +def test_list(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + c = Config(o, session_id='test_non_persistent') + from tiramisu.setting import list_sessions + assert 'test_non_persistent' in list_sessions() + del(c) + assert 'test_non_persistent' not in list_sessions() + + +def test_create_persistent(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + try: + Config(o, session_id='test_persistent', persistent=True) + except ValueError: + # storage is not persistent + pass + + +def test_list_sessions_persistent(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + try: + Config(o, session_id='test_persistent', persistent=True) + except ValueError: + # storage is not persistent + pass + else: + from tiramisu.setting import list_sessions + assert 'test_persistent' in list_sessions() + + +def test_delete_session_persistent(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + try: + Config(o, session_id='test_persistent', persistent=True) + except ValueError: + # storage is not persistent + pass + else: + from tiramisu.setting import list_sessions, delete_session + assert 'test_persistent' in list_sessions + delete_session('test_persistent') + assert 'test_persistent' not in list_sessions + + +def test_create_persistent_retrieve(): + b = BoolOption('b', '') + o = OptionDescription('od', '', [b]) + try: + c = Config(o, session_id='test_persistent', persistent=True) + except ValueError: + # storage is not persistent + pass + else: + assert c.b is False + c.b = True + assert c.b is True + del(c) + c = Config(o, session_id='test_persistent', persistent=True) + assert c.b is True + from tiramisu.setting import list_sessions, delete_session + assert 'test_persistent' in list_sessions + delete_session('test_persistent') + c = Config(o, session_id='test_persistent', persistent=True) + assert c.b is False diff --git a/tiramisu/config.py b/tiramisu/config.py index 41ac364..1d7beca 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -506,15 +506,20 @@ class Config(CommonConfig): "main configuration management entry" __slots__ = tuple() - def __init__(self, descr, session_id=None, is_persistent=False): + def __init__(self, descr, session_id=None, persistent=False): """ Configuration option management master class :param descr: describes the configuration schema :type descr: an instance of ``option.OptionDescription`` :param context: the current root config :type context: `Config` + :param session_id: session ID is import with persistent Config to + retrieve good session + :type session_id: `str` + :param persistent: if persistent, don't delete storage when leaving + :type persistent: `boolean` """ - storage = get_storage(self, session_id, is_persistent) + storage = get_storage(self, session_id, persistent) self._impl_settings = Settings(self, storage) self._impl_values = Values(self, storage) super(Config, self).__init__(descr, self) @@ -534,7 +539,7 @@ class Config(CommonConfig): #class MetaConfig(CommonConfig): # __slots__ = ('_impl_children',) -# def __init__(self, children, meta=True, session_id=None, is_persistent=False): +# def __init__(self, children, meta=True, session_id=None, persistent=False): # if not isinstance(children, list): # raise ValueError(_("metaconfig's children must be a list")) # self._impl_descr = None @@ -555,7 +560,7 @@ class Config(CommonConfig): # child._impl_meta = self # self._impl_children = children -# storage = get_storage(self, session_id, is_persistent) +# storage = get_storage(self, session_id, persistent) # self._impl_settings = Settings(self, storage) # self._impl_values = Values(self, storage) # self._impl_meta = None diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 952d52a..836278f 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -215,14 +215,15 @@ def set_storage(name, **args): '').format(option, name)) -def get_storage(context, session_id, is_persistent): +def get_storage(context, session_id, persistent): def gen_id(config): return str(id(config)) + str(time()) if session_id is None: session_id = gen_id(context) - return __import__(storage_type.get_storage(), globals(), locals(), - ['Storage'], -1).Storage(session_id, is_persistent) + a=__import__(storage_type.get_storage(), globals(), locals(), + ['Storage'], -1).Storage(session_id, persistent) + return a def list_sessions(): diff --git a/tiramisu/storage/dictionary/setting.py b/tiramisu/storage/dictionary/setting.py index 68a144f..580cba3 100644 --- a/tiramisu/storage/dictionary/setting.py +++ b/tiramisu/storage/dictionary/setting.py @@ -29,7 +29,7 @@ class Settings(Cache): self._properties = {} # permissive properties self._permissives = {} - super(Settings, self).__init__() + super(Settings, self).__init__(storage) # propertives def setproperties(self, path, properties): diff --git a/tiramisu/storage/dictionary/storage.py b/tiramisu/storage/dictionary/storage.py index be95260..8da0587 100644 --- a/tiramisu/storage/dictionary/storage.py +++ b/tiramisu/storage/dictionary/storage.py @@ -28,10 +28,11 @@ class Setting(object): setting = Setting() +_list_sessions = [] def list_sessions(): - return [] + return _list_sessions def delete_session(session_id): @@ -39,20 +40,28 @@ def delete_session(session_id): class Storage(object): - __slots__ = tuple() + __slots__ = ('session_id',) storage = 'dictionary' - def __init__(self, session_id, is_persistent): - if is_persistent: + def __init__(self, session_id, persistent): + if session_id in _list_sessions: + raise ValueError(_('session already used')) + if persistent: raise ValueError(_('a dictionary cannot be persistent')) + self.session_id = session_id + _list_sessions.append(self.session_id) + + def __del__(self): + _list_sessions.remove(self.session_id) class Cache(object): - __slots__ = ('_cache',) + __slots__ = ('_cache', 'storage') key_is_path = False - def __init__(self): + def __init__(self, storage): self._cache = {} + self.storage = storage def setcache(self, cache_type, path, val, time): self._cache[path] = (val, time) diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index 713473e..e3d41ac 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -29,7 +29,7 @@ class Values(Cache): """ self._values = {} # should init cache too - super(Values, self).__init__() + super(Values, self).__init__(storage) # value def setvalue(self, path, value, owner): diff --git a/tiramisu/storage/sqlite3/storage.py b/tiramisu/storage/sqlite3/storage.py index f7a168e..d855c88 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -50,11 +50,11 @@ def delete_session(session_id): class Storage(object): - __slots__ = ('_conn', '_cursor', 'is_persistent', '_session_id') + __slots__ = ('_conn', '_cursor', 'persistent', '_session_id') storage = 'sqlite3' - def __init__(self, session_id, is_persistent): - self.is_persistent = is_persistent + def __init__(self, session_id, persistent): + self.persistent = persistent self._session_id = session_id self._conn = sqlite3.connect(_gen_filename(self._session_id)) self._conn.text_factory = str @@ -77,7 +77,7 @@ class Storage(object): def __del__(self): self._cursor.close() self._conn.close() - if not self.is_persistent: + if not self.persistent: delete_session(self._session_id)