store session in dictionary storage
authorEmmanuel Garette <egarette@cadoles.com>
Tue, 27 Aug 2013 07:46:52 +0000 (09:46 +0200)
committerEmmanuel Garette <egarette@cadoles.com>
Tue, 27 Aug 2013 07:46:52 +0000 (09:46 +0200)
test/test_storage.py [new file with mode: 0644]
tiramisu/config.py
tiramisu/setting.py
tiramisu/storage/dictionary/setting.py
tiramisu/storage/dictionary/storage.py
tiramisu/storage/dictionary/value.py
tiramisu/storage/sqlite3/storage.py

diff --git a/test/test_storage.py b/test/test_storage.py
new file mode 100644 (file)
index 0000000..44ba9d3
--- /dev/null
@@ -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
index 41ac364..1d7beca 100644 (file)
@@ -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
index 952d52a..836278f 100644 (file)
@@ -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():
index 68a144f..580cba3 100644 (file)
@@ -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):
index be95260..8da0587 100644 (file)
@@ -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)
index 713473e..e3d41ac 100644 (file)
@@ -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):
index f7a168e..d855c88 100644 (file)
@@ -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)