diff --git a/test/test_cache.py b/test/test_cache.py index 13d44e3..32704e8 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -12,13 +12,20 @@ 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): - assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__) + 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__) global incr diff --git a/test/test_choice_option.py b/test/test_choice_option.py index 3bf51ed..b521eb6 100644 --- a/test/test_choice_option.py +++ b/test/test_choice_option.py @@ -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 diff --git a/tiramisu/__init__.py b/tiramisu/__init__.py index e2ff65b..4826832 100644 --- a/tiramisu/__init__.py +++ b/tiramisu/__init__.py @@ -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'] diff --git a/tiramisu/storage/__init__.py b/tiramisu/storage/__init__.py index 234f379..5d52679 100644 --- a/tiramisu/storage/__init__.py +++ b/tiramisu/storage/__init__.py @@ -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() - for key, value in kwargs.items(): - setattr(mod.SETTING, key, value) + 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) diff --git a/tiramisu/storage/dictionary/__init__.py b/tiramisu/storage/dictionary/__init__.py index 43d53e5..3373235 100644 --- a/tiramisu/storage/dictionary/__init__.py +++ b/tiramisu/storage/dictionary/__init__.py @@ -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', diff --git a/tiramisu/storage/dictionary/setting.py b/tiramisu/storage/dictionary/setting.py index 6bd9642..8dc5ef3 100644 --- a/tiramisu/storage/dictionary/setting.py +++ b/tiramisu/storage/dictionary/setting.py @@ -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): diff --git a/tiramisu/storage/dictionary/storage.py b/tiramisu/storage/dictionary/storage.py index b97c4e6..f4fffac 100644 --- a/tiramisu/storage/dictionary/storage.py +++ b/tiramisu/storage/dictionary/storage.py @@ -26,6 +26,7 @@ class Setting: SETTING = Setting() _list_sessions = [] +PERSISTENT = False def list_sessions(): diff --git a/tiramisu/storage/sqlite3/__init__.py b/tiramisu/storage/sqlite3/__init__.py index 2b02520..10cad94 100644 --- a/tiramisu/storage/sqlite3/__init__.py +++ b/tiramisu/storage/sqlite3/__init__.py @@ -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', diff --git a/tiramisu/storage/sqlite3/setting.py b/tiramisu/storage/sqlite3/setting.py index 24bc5f9..f7adaba 100644 --- a/tiramisu/storage/sqlite3/setting.py +++ b/tiramisu/storage/sqlite3/setting.py @@ -76,6 +76,7 @@ class Properties(Sqlite3DB): self._session_id, ), False) self._storage._conn.commit() + self.reset_all_cache() class Permissives(Sqlite3DB): diff --git a/tiramisu/storage/sqlite3/storage.py b/tiramisu/storage/sqlite3/storage.py index 897de6d..81b17fc 100644 --- a/tiramisu/storage/sqlite3/storage.py +++ b/tiramisu/storage/sqlite3/storage.py @@ -46,6 +46,7 @@ class Setting: super().__setattr__(key, value) +PERSISTENT = True SETTING = Setting()