corrections in sqlite3 support
This commit is contained in:
parent
db074a78a5
commit
b9ae1ec656
10 changed files with 60 additions and 44 deletions
|
@ -12,12 +12,19 @@ from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDe
|
||||||
from tiramisu import Config
|
from tiramisu import Config
|
||||||
from tiramisu.error import ConfigError, PropertiesOptionError
|
from tiramisu.error import ConfigError, PropertiesOptionError
|
||||||
from tiramisu.setting import groups
|
from tiramisu.setting import groups
|
||||||
from tiramisu import undefined, Params, ParamValue, ParamOption
|
from tiramisu import undefined, Params, ParamValue, ParamOption, \
|
||||||
|
list_sessions, default_storage, delete_session
|
||||||
from tiramisu.api import TIRAMISU_VERSION
|
from tiramisu.api import TIRAMISU_VERSION
|
||||||
from tiramisu.storage import list_sessions
|
|
||||||
|
|
||||||
|
|
||||||
def teardown_function(function):
|
def teardown_function(function):
|
||||||
|
if default_storage.is_persistent:
|
||||||
|
sessions = list_sessions()
|
||||||
|
if not sessions:
|
||||||
|
return
|
||||||
|
assert len(sessions) == 1
|
||||||
|
delete_session(sessions[0])
|
||||||
|
else:
|
||||||
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
|
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,7 @@ from .autopath import do_autopath
|
||||||
do_autopath()
|
do_autopath()
|
||||||
|
|
||||||
from tiramisu.setting import owners
|
from tiramisu.setting import owners
|
||||||
from tiramisu.option import ChoiceOption, StrOption, OptionDescription
|
from tiramisu import ChoiceOption, StrOption, OptionDescription, Config
|
||||||
from tiramisu import Config
|
|
||||||
from tiramisu.error import ConfigError
|
from tiramisu.error import ConfigError
|
||||||
from tiramisu import undefined, Params, ParamValue, ParamOption
|
from tiramisu import undefined, Params, ParamValue, ParamOption
|
||||||
from tiramisu.api import TIRAMISU_VERSION
|
from tiramisu.api import TIRAMISU_VERSION
|
||||||
|
|
|
@ -19,7 +19,7 @@ from .error import APIError
|
||||||
from .api import Config, MetaConfig, GroupConfig, MixConfig
|
from .api import Config, MetaConfig, GroupConfig, MixConfig
|
||||||
from .option import __all__ as all_options
|
from .option import __all__ as all_options
|
||||||
from .setting import owners, undefined
|
from .setting import owners, undefined
|
||||||
from .storage import default_storage_type, StorageType, list_sessions, \
|
from .storage import default_storage, Storage, list_sessions, \
|
||||||
delete_session
|
delete_session
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,8 +33,8 @@ allfuncs = ['Params',
|
||||||
'Config',
|
'Config',
|
||||||
'APIError',
|
'APIError',
|
||||||
'undefined',
|
'undefined',
|
||||||
'default_storage_type',
|
'default_storage',
|
||||||
'StorageType',
|
'Storage',
|
||||||
'list_sessions',
|
'list_sessions',
|
||||||
'delete_session',
|
'delete_session',
|
||||||
'tiramisu_copy']
|
'tiramisu_copy']
|
||||||
|
|
|
@ -26,38 +26,31 @@ Storage is basic components used to set Config informations in DB.
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
from random import randint
|
from random import randint
|
||||||
import os
|
from os import environ
|
||||||
|
from os.path import split
|
||||||
|
from typing import Dict
|
||||||
from ..error import ConfigError
|
from ..error import ConfigError
|
||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
|
|
||||||
|
|
||||||
MODULE_PATH = os.path.split(os.path.split(os.path.split(__file__)[0])[0])[1]
|
DEFAULT_STORAGE = MEMORY_STORAGE = 'dictionary'
|
||||||
|
MODULE_PATH = split(split(split(__file__)[0])[0])[1]
|
||||||
|
|
||||||
|
|
||||||
MEMORY_STORAGE = 'dictionary'
|
class Storage:
|
||||||
DEFAULT_STORAGE = MEMORY_STORAGE
|
|
||||||
|
|
||||||
|
|
||||||
class StorageType(object):
|
|
||||||
"""Object to store storage's type. If a Config is already set,
|
"""Object to store storage's type. If a Config is already set,
|
||||||
default storage is store as selected storage. You cannot change it
|
default storage is store as selected storage. You cannot change it
|
||||||
after.
|
after.
|
||||||
"""
|
"""
|
||||||
default_storage = os.environ.get('TIRAMISU_STORAGE', DEFAULT_STORAGE)
|
def __init__(self,
|
||||||
storage_type = None
|
**kwargs: Dict[str, str]) -> None:
|
||||||
mod = None
|
self.storage_type = None
|
||||||
|
self.mod = None
|
||||||
def set(self, name):
|
self.setting(**kwargs)
|
||||||
if self.storage_type is not None: # pragma: no cover
|
|
||||||
if self.storage_type == name:
|
|
||||||
return
|
|
||||||
raise ConfigError(_('storage_type is already set, '
|
|
||||||
'cannot rebind it'))
|
|
||||||
self.storage_type = name
|
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
if self.storage_type is None:
|
if self.storage_type is None:
|
||||||
self.storage_type = self.default_storage
|
self.storage_type = environ.get('TIRAMISU_STORAGE', DEFAULT_STORAGE)
|
||||||
if self.mod is None:
|
if self.mod is None:
|
||||||
modulepath = '{0}.storage.{1}'.format(MODULE_PATH,
|
modulepath = '{0}.storage.{1}'.format(MODULE_PATH,
|
||||||
self.storage_type)
|
self.storage_type)
|
||||||
|
@ -65,21 +58,33 @@ class StorageType(object):
|
||||||
mod = __import__(modulepath)
|
mod = __import__(modulepath)
|
||||||
except ImportError: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
raise SystemError(_('cannot import the storage {0}').format(
|
raise SystemError(_('cannot import the storage {0}').format(
|
||||||
self.default_storage))
|
self.storage_type))
|
||||||
for token in modulepath.split(".")[1:]:
|
for token in modulepath.split(".")[1:]:
|
||||||
mod = getattr(mod, token)
|
mod = getattr(mod, token)
|
||||||
self.mod = mod
|
self.mod = mod
|
||||||
return self.mod
|
return self.mod
|
||||||
|
|
||||||
def setting(self, **kwargs):
|
def setting(self,
|
||||||
|
**kwargs: Dict[str, str]) -> None:
|
||||||
|
if 'engine' in kwargs:
|
||||||
|
name = kwargs['engine']
|
||||||
|
if self.storage_type is not None and self.storage_type != name: # pragma: no cover
|
||||||
|
raise ConfigError(_('storage_type is already set, '
|
||||||
|
'cannot rebind it'))
|
||||||
|
self.storage_type = name
|
||||||
|
del kwargs['engine']
|
||||||
|
if kwargs:
|
||||||
mod = self.get()
|
mod = self.get()
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
setattr(mod.SETTING, key, value)
|
setattr(mod.SETTING, key, value)
|
||||||
|
|
||||||
|
def is_persistent(self):
|
||||||
|
mod = self.get()
|
||||||
|
return mod.PERSISTENT
|
||||||
|
|
||||||
default_storage_type = StorageType()
|
|
||||||
memory_storage = StorageType()
|
default_storage = Storage()
|
||||||
memory_storage.set(MEMORY_STORAGE)
|
memory_storage = Storage(engine=MEMORY_STORAGE)
|
||||||
|
|
||||||
|
|
||||||
def gen_storage_id(session_id,
|
def gen_storage_id(session_id,
|
||||||
|
@ -96,7 +101,7 @@ def get_storages(context,
|
||||||
session_id = gen_storage_id(session_id,
|
session_id = gen_storage_id(session_id,
|
||||||
context)
|
context)
|
||||||
if storage is None:
|
if storage is None:
|
||||||
storage = default_storage_type
|
storage = default_storage
|
||||||
imp = storage.get()
|
imp = storage.get()
|
||||||
imp_storage = imp.Storage(session_id,
|
imp_storage = imp.Storage(session_id,
|
||||||
persistent)
|
persistent)
|
||||||
|
@ -120,18 +125,18 @@ def get_default_settings_storages():
|
||||||
return properties, permissives
|
return properties, permissives
|
||||||
|
|
||||||
|
|
||||||
def list_sessions():
|
def list_sessions(storage=default_storage):
|
||||||
"""List all available session (persistent or not persistent)
|
"""List all available session (persistent or not persistent)
|
||||||
"""
|
"""
|
||||||
return default_storage_type.get().list_sessions()
|
return storage.get().list_sessions()
|
||||||
|
|
||||||
|
|
||||||
def delete_session(session_id):
|
def delete_session(session_id, storage=default_storage):
|
||||||
"""Delete a selected session, be careful, you can deleted a session
|
"""Delete a selected session, be careful, you can deleted a session
|
||||||
use by an other instance
|
use by an other instance
|
||||||
:params session_id: id of session to delete
|
:params session_id: id of session to delete
|
||||||
"""
|
"""
|
||||||
storage_module = default_storage_type.get()
|
storage_module = storage.get()
|
||||||
session = storage_module.storage.getsession()
|
session = storage_module.storage.getsession()
|
||||||
storage_module.value.delete_session(session_id)
|
storage_module.value.delete_session(session_id)
|
||||||
storage_module.storage.delete_session(session_id)
|
storage_module.storage.delete_session(session_id)
|
||||||
|
|
|
@ -24,10 +24,11 @@ use it. But if something goes wrong, you will lost your modifications.
|
||||||
"""
|
"""
|
||||||
from .value import Values
|
from .value import Values
|
||||||
from .setting import Properties, Permissives
|
from .setting import Properties, Permissives
|
||||||
from .storage import SETTING, Storage, list_sessions
|
from .storage import PERSISTENT, SETTING, Storage, list_sessions
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('SETTING',
|
__all__ = ('PERSISTENT',
|
||||||
|
'SETTING',
|
||||||
'Values',
|
'Values',
|
||||||
'Properties',
|
'Properties',
|
||||||
'Permissives',
|
'Permissives',
|
||||||
|
|
|
@ -57,8 +57,8 @@ class Properties(Cache):
|
||||||
return copy(self._properties)
|
return copy(self._properties)
|
||||||
|
|
||||||
def importation(self, properties):
|
def importation(self, properties):
|
||||||
self.reset_all_cache()
|
|
||||||
self._properties = properties
|
self._properties = properties
|
||||||
|
self.reset_all_cache()
|
||||||
|
|
||||||
|
|
||||||
class Permissives(Cache):
|
class Permissives(Cache):
|
||||||
|
|
|
@ -26,6 +26,7 @@ class Setting:
|
||||||
|
|
||||||
SETTING = Setting()
|
SETTING = Setting()
|
||||||
_list_sessions = []
|
_list_sessions = []
|
||||||
|
PERSISTENT = False
|
||||||
|
|
||||||
|
|
||||||
def list_sessions():
|
def list_sessions():
|
||||||
|
|
|
@ -22,10 +22,11 @@ You should not configure differents Configs with same session_id.
|
||||||
"""
|
"""
|
||||||
from .value import Values
|
from .value import Values
|
||||||
from .setting import Properties, Permissives
|
from .setting import Properties, Permissives
|
||||||
from .storage import SETTING, Storage, list_sessions
|
from .storage import PERSISTENT, SETTING, Storage, list_sessions
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('SETTING',
|
__all__ = ('PERSISTENT',
|
||||||
|
'SETTING',
|
||||||
'Values',
|
'Values',
|
||||||
'Properties',
|
'Properties',
|
||||||
'Permissives',
|
'Permissives',
|
||||||
|
|
|
@ -76,6 +76,7 @@ class Properties(Sqlite3DB):
|
||||||
self._session_id,
|
self._session_id,
|
||||||
), False)
|
), False)
|
||||||
self._storage._conn.commit()
|
self._storage._conn.commit()
|
||||||
|
self.reset_all_cache()
|
||||||
|
|
||||||
|
|
||||||
class Permissives(Sqlite3DB):
|
class Permissives(Sqlite3DB):
|
||||||
|
|
|
@ -46,6 +46,7 @@ class Setting:
|
||||||
super().__setattr__(key, value)
|
super().__setattr__(key, value)
|
||||||
|
|
||||||
|
|
||||||
|
PERSISTENT = True
|
||||||
SETTING = Setting()
|
SETTING = Setting()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue