From 6b9d5aed59589f174ed494e95ed6e700ec0bfcf7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 25 Aug 2013 21:57:11 +0200 Subject: [PATCH] add enumerate and delete method for manage storage --- tiramisu/config.py | 8 +++---- tiramisu/setting.py | 10 ++++----- tiramisu/storage/dictionary/storage.py | 11 ++++++++- tiramisu/storage/sqlite3/storage.py | 31 +++++++++++++++++++++----- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index 536aacf..dca0fef 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -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 diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 58059d7..edcccac 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -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) #____________________________________________________________ diff --git a/tiramisu/storage/dictionary/storage.py b/tiramisu/storage/dictionary/storage.py index 876f5da..759e5e0 100644 --- a/tiramisu/storage/dictionary/storage.py +++ b/tiramisu/storage/dictionary/storage.py @@ -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')) diff --git a/tiramisu/storage/sqlite3/storage.py b/tiramisu/storage/sqlite3/storage.py index e9d5e08..8c5bc86 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -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):