add enumerate and delete method for manage storage
[tiramisu.git] / tiramisu / storage / sqlite3 / storage.py
index e9d5e08..8c5bc86 100644 (file)
 
 from pickle import dumps, loads
 from os import unlink
+from os.path import basename, splitext, join
 import sqlite3
+from glob import glob
+
+
+extension = 'db'
+dir_database = '/tmp'
+
+
+def _gen_filename(name):
+    return join(dir_database, '{0}.{1}'.format(name, extension))
+
+
+def enumerate():
+    names = []
+    for filename in glob(_gen_filename('*')):
+        names.append(basename(splitext(filename)[0]))
+    return names
+
+
+def delete(session_id):
+    unlink(_gen_filename(session_id))
 
 
 class Storage(object):
-    __slots__ = ('_conn', '_cursor', 'is_persistent', 'db_file')
+    __slots__ = ('_conn', '_cursor', 'is_persistent', '_session_id')
     storage = 'sqlite3'
 
-    def __init__(self, config_id, is_persistent):
+    def __init__(self, session_id, is_persistent):
         self.is_persistent = is_persistent
-        self.db_file = config_id + '.db'
-        self._conn = sqlite3.connect(self.db_file)
+        self._session_id = session_id
+        self._conn = sqlite3.connect(_gen_filename(self._session_id))
         self._conn.text_factory = str
         self._cursor = self._conn.cursor()
 
@@ -52,7 +73,7 @@ class Storage(object):
         self._cursor.close()
         self._conn.close()
         if not self.is_persistent:
-            unlink(self.db_file)
+            delete(self._session_id)
 
 
 class Cache(object):