tiramisu/tiramisu/storage/__init__.py

152 lines
5.0 KiB
Python
Raw Normal View History

2019-02-12 06:55:47 +01:00
# Copyright (C) 2013-2019 Team tiramisu (see AUTHORS for all contributors)
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
2013-09-14 14:44:33 +02:00
"""Config's informations are, by default, volatiles. This means, all values and
settings changes will be lost.
The storage is the system Tiramisu uses to communicate with various DB.
You can specified a persistent storage.
Storage is basic components used to set Config informations in DB.
"""
from time import time
2015-04-18 22:53:45 +02:00
from random import randint
2019-02-23 12:25:20 +01:00
from os import environ
from os.path import split
from typing import Dict
from ..error import ConfigError
from ..i18n import _
2019-09-01 09:41:53 +02:00
from .cacheobj import Cache
2019-02-23 12:25:20 +01:00
DEFAULT_STORAGE = MEMORY_STORAGE = 'dictionary'
MODULE_PATH = split(split(split(__file__)[0])[0])[1]
2019-02-23 12:25:20 +01:00
class Storage:
2013-09-06 23:53:19 +02:00
"""Object to store storage's type. If a Config is already set,
default storage is store as selected storage. You cannot change it
after.
"""
2019-02-23 12:25:20 +01:00
def __init__(self,
**kwargs: Dict[str, str]) -> None:
self.storage_type = None
self.mod = None
2019-12-08 08:15:14 +01:00
if kwargs:
self.setting(**kwargs)
def get(self):
if self.storage_type is None:
2019-02-23 12:25:20 +01:00
self.storage_type = environ.get('TIRAMISU_STORAGE', DEFAULT_STORAGE)
2019-12-08 08:15:14 +01:00
self.setting()
if self.mod is None:
modulepath = '{0}.storage.{1}'.format(MODULE_PATH,
self.storage_type)
try:
mod = __import__(modulepath)
2017-07-21 18:03:34 +02:00
except ImportError: # pragma: no cover
raise SystemError(_('cannot import the storage {0}').format(
2019-02-23 12:25:20 +01:00
self.storage_type))
for token in modulepath.split(".")[1:]:
mod = getattr(mod, token)
self.mod = mod
return self.mod
2019-02-23 12:25:20 +01:00
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']
2019-02-23 22:52:06 +01:00
if kwargs: # pragma: no cover
2019-02-23 12:25:20 +01:00
mod = self.get()
for key, value in kwargs.items():
setattr(mod.SETTING, key, value)
def is_persistent(self):
mod = self.get()
2019-02-23 12:25:20 +01:00
return mod.PERSISTENT
2019-02-23 12:25:20 +01:00
default_storage = Storage()
memory_storage = Storage(engine=MEMORY_STORAGE)
def gen_storage_id(session_id,
config):
if session_id is not None:
return session_id
return 'c' + str(id(config)) + str(int(time())) + str(randint(0, 500))
def get_storages(context,
session_id,
persistent,
storage):
session_id = gen_storage_id(session_id,
context)
2018-06-09 18:59:40 +02:00
if storage is None:
2019-02-23 12:25:20 +01:00
storage = default_storage
2018-06-09 18:59:40 +02:00
imp = storage.get()
imp_storage = imp.Storage(session_id,
persistent)
2018-06-09 18:59:40 +02:00
properties = imp.Properties(imp_storage)
permissives = imp.Permissives(imp_storage)
values = imp.Values(imp_storage)
return properties, permissives, values, session_id
2017-07-16 10:57:43 +02:00
def get_default_values_storages():
2018-06-09 18:59:40 +02:00
imp = memory_storage.get()
storage = imp.Storage('__validator_storage', persistent=False, test=True)
return imp.Values(storage)
2015-04-18 22:53:45 +02:00
2017-07-16 10:57:43 +02:00
def get_default_settings_storages():
2018-06-09 18:59:40 +02:00
imp = memory_storage.get()
2017-07-16 10:57:43 +02:00
storage = imp.Storage('__validator_storage', persistent=False, test=True)
properties = imp.Properties(storage)
permissives = imp.Permissives(storage)
return properties, permissives
2019-02-23 12:25:20 +01:00
def list_sessions(storage=default_storage):
2013-09-06 23:53:19 +02:00
"""List all available session (persistent or not persistent)
"""
2019-02-23 12:25:20 +01:00
return storage.get().list_sessions()
2019-02-23 12:25:20 +01:00
def delete_session(session_id, storage=default_storage):
2013-09-06 23:53:19 +02:00
"""Delete a selected session, be careful, you can deleted a session
use by an other instance
2013-09-07 10:31:39 +02:00
:params session_id: id of session to delete
2013-09-06 23:53:19 +02:00
"""
2019-02-23 12:25:20 +01:00
storage_module = storage.get()
2017-07-21 22:34:41 +02:00
session = storage_module.storage.getsession()
2018-09-11 20:11:13 +02:00
storage_module.value.delete_session(session_id)
storage_module.storage.delete_session(session_id)
2018-09-29 21:39:58 +02:00
if session: # pragma: no cover
2017-07-22 16:26:06 +02:00
session.commit()
del(session)
2018-09-29 18:52:13 +02:00
__all__ = ('list_sessions', 'delete_session')