tiramisu/tiramisu/storage/__init__.py

193 lines
6.3 KiB
Python
Raw Normal View History

2017-07-04 19:59:42 +02:00
# Copyright (C) 2013-2017 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.
2013-09-10 21:04:12 +02:00
The primary "entry point" class is the StorageType and it's public
configurator ``set_storage()``.
"""
from time import time
2015-04-18 22:53:45 +02:00
from random import randint
import os
from ..error import ConfigError
from ..i18n import _
MODULE_PATH = os.path.split(os.path.split(os.path.split(__file__)[0])[0])[1]
2017-07-04 19:59:42 +02:00
DEFAULT_STORAGE = 'dictionary'
class StorageType(object):
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.
"""
2017-07-04 19:59:42 +02:00
default_storage = os.environ.get('TIRAMISU_STORAGE', DEFAULT_STORAGE)
storage_type = None
mod = None
2014-06-19 23:22:39 +02:00
def set(self, name): # pragma: optional cover
if self.storage_type is not None:
2013-09-10 21:04:12 +02:00
if self.storage_type == name:
return
raise ConfigError(_('storage_type is already set, cannot rebind it'))
self.storage_type = name
def get(self):
if self.storage_type is None:
self.storage_type = self.default_storage
2017-07-04 19:59:42 +02:00
set_to_default = True
else:
set_to_default = False
if self.mod is None:
modulepath = '{0}.storage.{1}'.format(MODULE_PATH, self.storage_type)
try:
mod = __import__(modulepath)
except ImportError:
raise SystemError(_('cannot import the storage {0}').format(
self.default_storage))
for token in modulepath.split(".")[1:]:
mod = getattr(mod, token)
self.mod = mod
return self.mod
storage_type = StorageType()
2014-04-12 22:47:52 +02:00
storage_option_type = StorageType()
2017-07-04 19:59:42 +02:00
storage_option_type.set(DEFAULT_STORAGE)
2017-07-16 10:57:43 +02:00
default_validation = StorageType()
default_validation.set(DEFAULT_STORAGE)
2014-06-19 23:22:39 +02:00
def set_storage(type_, name, **kwargs): # pragma: optional cover
2013-09-06 23:53:19 +02:00
"""Change storage's configuration
:params name: is the storage name. If storage is already set, cannot
reset storage name
Other attributes are differents according to the selected storage's name
"""
2014-04-12 22:47:52 +02:00
if type_ == 'option':
storage_option_type.set(name)
setting = storage_option_type.get().setting
else:
storage_type.set(name)
setting = storage_type.get().setting
2013-09-22 20:57:52 +02:00
for option, value in kwargs.items():
try:
2013-09-22 20:57:52 +02:00
getattr(setting, option)
setattr(setting, option, value)
except AttributeError:
raise ValueError(_('option {0} not already exists in storage {1}'
'').format(option, name))
2013-09-22 20:57:52 +02:00
def _impl_getstate_setting():
setting = storage_type.get().setting
state = {'name': storage_type.storage_type}
for var in dir(setting):
if not var.startswith('_'):
state[var] = getattr(setting, var)
return state
2014-06-19 23:22:39 +02:00
def get_storage(type_, session_id, persistent, test): # pragma: optional cover
2013-09-22 20:57:52 +02:00
"""all used when __setstate__ a Config
"""
#FIXME ca sert ???
2014-04-12 22:47:52 +02:00
if type_ == 'option':
return storage_option_type.get().Storage(session_id, persistent, test)
2015-04-18 22:53:45 +02:00
elif type_ == 'config':
2014-04-12 22:47:52 +02:00
return storage_type.get().Storage(session_id, persistent, test)
2015-04-18 22:53:45 +02:00
else:
2017-07-16 10:57:43 +02:00
return default_validation.get().Storage(session_id, persistent, test)
2013-09-22 20:57:52 +02:00
def get_storages(context, session_id, persistent):
def gen_id(config):
return 'c' + str(id(config)) + str(int(time())) + str(randint(0, 500))
if session_id is None:
session_id = gen_id(context)
imp = storage_type.get()
storage = imp.Storage(session_id, persistent)
2017-07-13 22:04:06 +02:00
properties = imp.Properties(storage)
permissives = imp.Permissives(storage)
2015-05-03 09:56:03 +02:00
values = imp.Values(storage)
return properties, permissives, values, session_id
def get_storages_option(type_):
2014-04-12 22:47:52 +02:00
imp = storage_option_type.get()
if type_ == 'base':
2014-07-06 15:31:57 +02:00
return imp.StorageBase
2016-09-30 22:45:33 +02:00
elif type_ == 'masterslaves':
return imp.StorageMasterSlaves
else:
2014-07-06 15:31:57 +02:00
return imp.StorageOptionDescription
2014-04-12 22:47:52 +02:00
2017-07-16 10:57:43 +02:00
def get_default_values_storages():
imp = default_validation.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():
imp = default_validation.get()
storage = imp.Storage('__validator_storage', persistent=False, test=True)
properties = imp.Properties(storage)
permissives = imp.Permissives(storage)
return properties, permissives
2014-06-19 23:22:39 +02:00
def list_sessions(type_): # pragma: optional cover
2013-09-06 23:53:19 +02:00
"""List all available session (persistent or not persistent)
"""
2014-04-12 22:47:52 +02:00
if type_ == 'option':
return storage_option_type.get().list_sessions()
else:
return storage_type.get().list_sessions()
2014-06-19 23:22:39 +02:00
def delete_session(type_, session_id): # pragma: optional cover
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
"""
2014-04-12 22:47:52 +02:00
if type_ == 'option':
storage_option_type.get().delete_session(session_id)
2014-04-12 22:47:52 +02:00
else:
storage_module = storage_type.get()
session = storage_module.storage.getsession()
storage_module.value.delete_session(session_id, session)
storage_module.storage.delete_session(session_id, session)
2017-07-04 19:59:42 +02:00
if session:
session.commit()
del(session)
2017-07-04 19:59:42 +02:00
__all__ = ('set_storage', 'list_sessions', 'delete_session')