add enumerate and delete method for manage storage
This commit is contained in:
@ -20,13 +20,22 @@
|
||||
|
||||
|
||||
from tiramisu.i18n import _
|
||||
from tiramisu.error import ConfigError
|
||||
|
||||
|
||||
def enumerate():
|
||||
return []
|
||||
|
||||
|
||||
def delete(session_id):
|
||||
raise ConfigError(_('dictionary storage cannot delete session'))
|
||||
|
||||
|
||||
class Storage(object):
|
||||
__slots__ = tuple()
|
||||
storage = 'dictionary'
|
||||
|
||||
def __init__(self, config_id, is_persistent):
|
||||
def __init__(self, session_id, is_persistent):
|
||||
if is_persistent:
|
||||
raise ValueError(_('a dictionary cannot be persistent'))
|
||||
|
||||
|
@ -20,17 +20,38 @@
|
||||
|
||||
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):
|
||||
|
Reference in New Issue
Block a user