From dcb12dac02ca4f0b0bbd6ba90755c2344bf83de1 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 25 Aug 2013 20:49:24 +0200 Subject: [PATCH] can change storage with set_storage, storage's name is store in storage object and storage is create with get_storage --- tiramisu/config.py | 20 +++----------------- tiramisu/setting.py | 20 ++++++++++++++++++-- tiramisu/storage/dictionary/storage.py | 2 ++ tiramisu/storage/sqlite3/storage.py | 1 + tiramisu/storage/sqlite3/value.py | 4 ++-- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/tiramisu/config.py b/tiramisu/config.py index 4c830d2..536aacf 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -20,19 +20,14 @@ # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the whole pypy projet is under MIT licence # ____________________________________________________________ -from time import time from tiramisu.error import PropertiesOptionError, ConfigError from tiramisu.option import OptionDescription, Option, SymLinkOption, \ BaseInformation -from tiramisu.setting import groups, Settings, default_encoding, storage_type +from tiramisu.setting import groups, Settings, default_encoding, get_storage from tiramisu.value import Values from tiramisu.i18n import _ -def gen_id(config): - return str(id(config)) + str(time()) - - class SubConfig(BaseInformation): "sub configuration management entry" __slots__ = ('_impl_context', '_impl_descr', '_impl_path') @@ -466,13 +461,6 @@ class CommonConfig(SubConfig): "abstract base class for the Config and the MetaConfig" __slots__ = ('_impl_values', '_impl_settings', '_impl_meta') - def _init_storage(self, config_id, is_persistent): - if config_id is None: - config_id = gen_id(self) - import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type) - return __import__(import_lib, globals(), locals(), ['Storage'], - -1).Storage(config_id, is_persistent) - def _impl_build_all_paths(self): self.cfgimpl_get_description().impl_build_cache() @@ -526,7 +514,7 @@ class Config(CommonConfig): :param context: the current root config :type context: `Config` """ - storage = self._init_storage(config_id, is_persistent) + storage = get_storage(self, config_id, is_persistent) self._impl_settings = Settings(self, storage) self._impl_values = Values(self, storage) super(Config, self).__init__(descr, self) @@ -566,10 +554,8 @@ class MetaConfig(CommonConfig): raise ValueError(_("child has already a metaconfig's")) child._impl_meta = self - if config_id is None: - config_id = gen_id(self) self._impl_children = children - storage = self._init_storage(config_id, is_persistent) + storage = get_storage(self, config_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 3c649fe..58059d7 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 = 'dictionary' +storage_type = 'sqlite3' class _const: @@ -184,6 +184,22 @@ class Property(object): return str(list(self._properties)) +def set_storage(name): + global storage_type + storage_type = name + + +def get_storage(context, config_id, is_persistent): + def gen_id(config): + return str(id(config)) + str(time()) + + if config_id is None: + config_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) + + #____________________________________________________________ class Settings(object): "``Config()``'s configuration options" @@ -202,7 +218,7 @@ class Settings(object): # generic owner self._owner = owners.user self.context = context - import_lib = 'tiramisu.storage.{0}.setting'.format(storage_type) + import_lib = 'tiramisu.storage.{0}.setting'.format(storage.storage) self._p_ = __import__(import_lib, globals(), locals(), ['Settings'], -1).Settings(storage) diff --git a/tiramisu/storage/dictionary/storage.py b/tiramisu/storage/dictionary/storage.py index a7a135a..876f5da 100644 --- a/tiramisu/storage/dictionary/storage.py +++ b/tiramisu/storage/dictionary/storage.py @@ -24,6 +24,8 @@ from tiramisu.i18n import _ class Storage(object): __slots__ = tuple() + storage = 'dictionary' + def __init__(self, config_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 efc730b..e9d5e08 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -25,6 +25,7 @@ import sqlite3 class Storage(object): __slots__ = ('_conn', '_cursor', 'is_persistent', 'db_file') + storage = 'sqlite3' def __init__(self, config_id, is_persistent): self.is_persistent = is_persistent diff --git a/tiramisu/storage/sqlite3/value.py b/tiramisu/storage/sqlite3/value.py index 00be15c..af4164d 100644 --- a/tiramisu/storage/sqlite3/value.py +++ b/tiramisu/storage/sqlite3/value.py @@ -70,12 +70,12 @@ class Values(Cache): path = self._sqlite_encode_path(path) self.storage.execute("DELETE FROM value WHERE path = ?", (path,)) - def get_modified_values(self, context): + def get_modified_values(self): """return all values in a dictionary example: {option1: (owner, 'value1'), option2: (owner, 'value2')} """ ret = {} - for path, value, owner in self.storage.select("SELECT value", + for path, value, owner in self.storage.select("SELECT * FROM value", only_one=False): path = self._sqlite_decode_path(path) owner = getattr(owners, owner)