store session in dictionary storage
[tiramisu.git] / tiramisu / storage / sqlite3 / storage.py
index 6ce03c4..d855c88 100644 (file)
 
 from pickle import dumps, loads
 from os import unlink
+from os.path import basename, splitext, join
 import sqlite3
+from glob import glob
+
+
+class Setting(object):
+    extension = 'db'
+    dir_database = '/tmp'
+
+
+setting = Setting()
+
+
+def _gen_filename(name):
+    return join(setting.dir_database, '{0}.{1}'.format(name,
+                                                       setting.extension))
+
+
+def list_sessions():
+    names = []
+    for filename in glob(_gen_filename('*')):
+        names.append(basename(splitext(filename)[0]))
+    return names
+
+
+def delete_session(session_id):
+    unlink(_gen_filename(session_id))
 
 
 class Storage(object):
-    __slots__ = ('_conn', '_cursor', 'is_persistent', 'db_file')
+    __slots__ = ('_conn', '_cursor', 'persistent', '_session_id')
+    storage = 'sqlite3'
 
-    def __init__(self, config_id, is_persistent):
-        self.is_persistent = is_persistent
-        self.db_file = config_id + '.db'
-        self._conn = sqlite3.connect(self.db_file)
+    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
         self._cursor = self._conn.cursor()
 
@@ -50,8 +77,8 @@ class Storage(object):
     def __del__(self):
         self._cursor.close()
         self._conn.close()
-        if not self.is_persistent:
-            unlink(self.db_file)
+        if not self.persistent:
+            delete_session(self._session_id)
 
 
 class Cache(object):
@@ -66,6 +93,18 @@ class Cache(object):
         self.storage.execute(cache_table)
 
     # value
+    def _sqlite_decode_path(self, path):
+        if path == '_none':
+            return None
+        else:
+            return path
+
+    def _sqlite_encode_path(self, path):
+        if path is None:
+            return '_none'
+        else:
+            return path
+
     def _sqlite_decode(self, value):
         return loads(value)
 
@@ -76,6 +115,7 @@ class Cache(object):
 
     def setcache(self, cache_type, path, val, time):
         convert_value = self._sqlite_encode(val)
+        path = self._sqlite_encode_path(path)
         self.storage.execute("DELETE FROM cache_{0} WHERE path = ?".format(
             cache_type), (path,), False)
         self.storage.execute("INSERT INTO cache_{0}(path, value, time) "
@@ -83,6 +123,7 @@ class Cache(object):
                              (path, convert_value, time))
 
     def getcache(self, cache_type, path, exp):
+        path = self._sqlite_encode_path(path)
         cached = self.storage.select("SELECT value FROM cache_{0} WHERE "
                                      "path = ? AND time >= ?".format(
                                          cache_type), (path, exp))
@@ -92,6 +133,7 @@ class Cache(object):
             return True, self._sqlite_decode(cached[0])
 
     def hascache(self, cache_type, path):
+        path = self._sqlite_encode_path(path)
         return self.storage.select("SELECT value FROM cache_{0} WHERE "
                                    "path = ?".format(cache_type),
                                    (path,)) is not None
@@ -105,13 +147,13 @@ class Cache(object):
 
     def get_cached(self, cache_type, context):
         """return all values in a dictionary
-        example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')}
+        example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
         """
         ret = {}
         for path, value, time in self.storage.select("SELECT * FROM cache_{0}"
                                                      "".format(cache_type),
                                                      only_one=False):
-            opt = context.cfgimpl_get_description().impl_get_opt_by_path(path)
+            path = self._sqlite_decode_path(path)
             value = self._sqlite_decode(value)
-            ret[opt] = (value, time)
+            ret[path] = (value, time)
         return ret