add enumerate and delete method for manage storage

This commit is contained in:
Emmanuel Garette 2013-08-25 21:57:11 +02:00
parent dc2c173b44
commit 6b9d5aed59
4 changed files with 45 additions and 15 deletions

View file

@ -506,7 +506,7 @@ class Config(CommonConfig):
"main configuration management entry"
__slots__ = tuple()
def __init__(self, descr, config_id=None, is_persistent=False):
def __init__(self, descr, session_id=None, is_persistent=False):
""" Configuration option management master class
:param descr: describes the configuration schema
@ -514,7 +514,7 @@ class Config(CommonConfig):
:param context: the current root config
:type context: `Config`
"""
storage = get_storage(self, config_id, is_persistent)
storage = get_storage(self, session_id, is_persistent)
self._impl_settings = Settings(self, storage)
self._impl_values = Values(self, storage)
super(Config, self).__init__(descr, self)
@ -534,7 +534,7 @@ class Config(CommonConfig):
class MetaConfig(CommonConfig):
__slots__ = ('_impl_children',)
def __init__(self, children, meta=True, config_id=None, is_persistent=False):
def __init__(self, children, meta=True, session_id=None, is_persistent=False):
if not isinstance(children, list):
raise ValueError(_("metaconfig's children must be a list"))
self._impl_descr = None
@ -555,7 +555,7 @@ class MetaConfig(CommonConfig):
child._impl_meta = self
self._impl_children = children
storage = get_storage(self, config_id, is_persistent)
storage = get_storage(self, session_id, is_persistent)
self._impl_settings = Settings(self, storage)
self._impl_values = Values(self, storage)
self._impl_meta = None

View file

@ -34,7 +34,7 @@ ro_append = ('frozen', 'disabled', 'validator', 'everything_frozen',
rw_remove = ('permissive', 'everything_frozen', 'mandatory')
rw_append = ('frozen', 'disabled', 'validator', 'hidden')
default_properties = ('expire', 'validator')
storage_type = 'sqlite3'
storage_type = 'dictionary'
class _const:
@ -189,15 +189,15 @@ def set_storage(name):
storage_type = name
def get_storage(context, config_id, is_persistent):
def get_storage(context, session_id, is_persistent):
def gen_id(config):
return str(id(config)) + str(time())
if config_id is None:
config_id = gen_id(context)
if session_id is None:
session_id = gen_id(context)
import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type)
return __import__(import_lib, globals(), locals(), ['Storage'],
-1).Storage(config_id, is_persistent)
-1).Storage(session_id, is_persistent)
#____________________________________________________________

View file

@ -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'))

View file

@ -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):