corrections in sqlite3 support

This commit is contained in:
Emmanuel Garette 2019-02-23 12:25:20 +01:00
parent db074a78a5
commit b9ae1ec656
10 changed files with 60 additions and 44 deletions

View File

@ -12,12 +12,19 @@ from tiramisu.option import BoolOption, IPOption, IntOption, StrOption, OptionDe
from tiramisu import Config
from tiramisu.error import ConfigError, PropertiesOptionError
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.storage import list_sessions
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__)

View File

@ -5,8 +5,7 @@ from .autopath import do_autopath
do_autopath()
from tiramisu.setting import owners
from tiramisu.option import ChoiceOption, StrOption, OptionDescription
from tiramisu import Config
from tiramisu import ChoiceOption, StrOption, OptionDescription, Config
from tiramisu.error import ConfigError
from tiramisu import undefined, Params, ParamValue, ParamOption
from tiramisu.api import TIRAMISU_VERSION

View File

@ -19,7 +19,7 @@ from .error import APIError
from .api import Config, MetaConfig, GroupConfig, MixConfig
from .option import __all__ as all_options
from .setting import owners, undefined
from .storage import default_storage_type, StorageType, list_sessions, \
from .storage import default_storage, Storage, list_sessions, \
delete_session
@ -33,8 +33,8 @@ allfuncs = ['Params',
'Config',
'APIError',
'undefined',
'default_storage_type',
'StorageType',
'default_storage',
'Storage',
'list_sessions',
'delete_session',
'tiramisu_copy']

View File

@ -26,38 +26,31 @@ Storage is basic components used to set Config informations in DB.
from time import time
from random import randint
import os
from os import environ
from os.path import split
from typing import Dict
from ..error import ConfigError
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'
DEFAULT_STORAGE = MEMORY_STORAGE
class StorageType(object):
class Storage:
"""Object to store storage's type. If a Config is already set,
default storage is store as selected storage. You cannot change it
after.
"""
default_storage = os.environ.get('TIRAMISU_STORAGE', DEFAULT_STORAGE)
storage_type = None
mod = None
def set(self, name):
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 __init__(self,
**kwargs: Dict[str, str]) -> None:
self.storage_type = None
self.mod = None
self.setting(**kwargs)
def get(self):
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:
modulepath = '{0}.storage.{1}'.format(MODULE_PATH,
self.storage_type)
@ -65,21 +58,33 @@ class StorageType(object):
mod = __import__(modulepath)
except ImportError: # pragma: no cover
raise SystemError(_('cannot import the storage {0}').format(
self.default_storage))
self.storage_type))
for token in modulepath.split(".")[1:]:
mod = getattr(mod, token)
self.mod = 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()
for key, value in kwargs.items():
setattr(mod.SETTING, key, value)
def is_persistent(self):
mod = self.get()
return mod.PERSISTENT
default_storage_type = StorageType()
memory_storage = StorageType()
memory_storage.set(MEMORY_STORAGE)
default_storage = Storage()
memory_storage = Storage(engine=MEMORY_STORAGE)
def gen_storage_id(session_id,
@ -96,7 +101,7 @@ def get_storages(context,
session_id = gen_storage_id(session_id,
context)
if storage is None:
storage = default_storage_type
storage = default_storage
imp = storage.get()
imp_storage = imp.Storage(session_id,
persistent)
@ -120,18 +125,18 @@ def get_default_settings_storages():
return properties, permissives
def list_sessions():
def list_sessions(storage=default_storage):
"""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
use by an other instance
:params session_id: id of session to delete
"""
storage_module = default_storage_type.get()
storage_module = storage.get()
session = storage_module.storage.getsession()
storage_module.value.delete_session(session_id)
storage_module.storage.delete_session(session_id)

View File

@ -24,10 +24,11 @@ use it. But if something goes wrong, you will lost your modifications.
"""
from .value import Values
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',
'Properties',
'Permissives',

View File

@ -57,8 +57,8 @@ class Properties(Cache):
return copy(self._properties)
def importation(self, properties):
self.reset_all_cache()
self._properties = properties
self.reset_all_cache()
class Permissives(Cache):

View File

@ -26,6 +26,7 @@ class Setting:
SETTING = Setting()
_list_sessions = []
PERSISTENT = False
def list_sessions():

View File

@ -22,10 +22,11 @@ You should not configure differents Configs with same session_id.
"""
from .value import Values
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',
'Properties',
'Permissives',

View File

@ -76,6 +76,7 @@ class Properties(Sqlite3DB):
self._session_id,
), False)
self._storage._conn.commit()
self.reset_all_cache()
class Permissives(Sqlite3DB):

View File

@ -46,6 +46,7 @@ class Setting:
super().__setattr__(key, value)
PERSISTENT = True
SETTING = Setting()