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 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

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

View file

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

View file

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

View file

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