can change storage with set_storage, storage's name is store in storage object and storage is create with get_storage

This commit is contained in:
Emmanuel Garette 2013-08-25 20:49:24 +02:00
parent 36ed6f874f
commit dcb12dac02
5 changed files with 26 additions and 21 deletions

View file

@ -20,19 +20,14 @@
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/ # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
from time import time
from tiramisu.error import PropertiesOptionError, ConfigError from tiramisu.error import PropertiesOptionError, ConfigError
from tiramisu.option import OptionDescription, Option, SymLinkOption, \ from tiramisu.option import OptionDescription, Option, SymLinkOption, \
BaseInformation 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.value import Values
from tiramisu.i18n import _ from tiramisu.i18n import _
def gen_id(config):
return str(id(config)) + str(time())
class SubConfig(BaseInformation): class SubConfig(BaseInformation):
"sub configuration management entry" "sub configuration management entry"
__slots__ = ('_impl_context', '_impl_descr', '_impl_path') __slots__ = ('_impl_context', '_impl_descr', '_impl_path')
@ -466,13 +461,6 @@ class CommonConfig(SubConfig):
"abstract base class for the Config and the MetaConfig" "abstract base class for the Config and the MetaConfig"
__slots__ = ('_impl_values', '_impl_settings', '_impl_meta') __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): def _impl_build_all_paths(self):
self.cfgimpl_get_description().impl_build_cache() self.cfgimpl_get_description().impl_build_cache()
@ -526,7 +514,7 @@ class Config(CommonConfig):
:param context: the current root config :param context: the current root config
:type context: `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_settings = Settings(self, storage)
self._impl_values = Values(self, storage) self._impl_values = Values(self, storage)
super(Config, self).__init__(descr, self) super(Config, self).__init__(descr, self)
@ -566,10 +554,8 @@ class MetaConfig(CommonConfig):
raise ValueError(_("child has already a metaconfig's")) raise ValueError(_("child has already a metaconfig's"))
child._impl_meta = self child._impl_meta = self
if config_id is None:
config_id = gen_id(self)
self._impl_children = children 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_settings = Settings(self, storage)
self._impl_values = Values(self, storage) self._impl_values = Values(self, storage)
self._impl_meta = None self._impl_meta = None

View file

@ -34,7 +34,7 @@ ro_append = ('frozen', 'disabled', 'validator', 'everything_frozen',
rw_remove = ('permissive', 'everything_frozen', 'mandatory') rw_remove = ('permissive', 'everything_frozen', 'mandatory')
rw_append = ('frozen', 'disabled', 'validator', 'hidden') rw_append = ('frozen', 'disabled', 'validator', 'hidden')
default_properties = ('expire', 'validator') default_properties = ('expire', 'validator')
storage_type = 'dictionary' storage_type = 'sqlite3'
class _const: class _const:
@ -184,6 +184,22 @@ class Property(object):
return str(list(self._properties)) 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): class Settings(object):
"``Config()``'s configuration options" "``Config()``'s configuration options"
@ -202,7 +218,7 @@ class Settings(object):
# generic owner # generic owner
self._owner = owners.user self._owner = owners.user
self.context = context 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'], self._p_ = __import__(import_lib, globals(), locals(), ['Settings'],
-1).Settings(storage) -1).Settings(storage)

View file

@ -24,6 +24,8 @@ from tiramisu.i18n import _
class Storage(object): class Storage(object):
__slots__ = tuple() __slots__ = tuple()
storage = 'dictionary'
def __init__(self, config_id, is_persistent): def __init__(self, config_id, is_persistent):
if is_persistent: if is_persistent:
raise ValueError(_('a dictionary cannot be persistent')) raise ValueError(_('a dictionary cannot be persistent'))

View file

@ -25,6 +25,7 @@ import sqlite3
class Storage(object): class Storage(object):
__slots__ = ('_conn', '_cursor', 'is_persistent', 'db_file') __slots__ = ('_conn', '_cursor', 'is_persistent', 'db_file')
storage = 'sqlite3'
def __init__(self, config_id, is_persistent): def __init__(self, config_id, is_persistent):
self.is_persistent = is_persistent self.is_persistent = is_persistent

View file

@ -70,12 +70,12 @@ class Values(Cache):
path = self._sqlite_encode_path(path) path = self._sqlite_encode_path(path)
self.storage.execute("DELETE FROM value WHERE 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 """return all values in a dictionary
example: {option1: (owner, 'value1'), option2: (owner, 'value2')} example: {option1: (owner, 'value1'), option2: (owner, 'value2')}
""" """
ret = {} 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): only_one=False):
path = self._sqlite_decode_path(path) path = self._sqlite_decode_path(path)
owner = getattr(owners, owner) owner = getattr(owners, owner)