ConstError in tiramisu/error.py
storage_type is now unique rename _const => _NameSpace can change storage's options in set_storage storage : add Setting object in storage rename enumerate to list_sessions rename delete to delete_session auto-create owner when load sqlite3 storage and in getowner
This commit is contained in:
parent
551b9fb1e3
commit
0bc47b1cf9
|
@ -56,3 +56,8 @@ class RequirementError(StandardError):
|
||||||
class SlaveError(StandardError):
|
class SlaveError(StandardError):
|
||||||
"problem with a slave's value length"
|
"problem with a slave's value length"
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ConstError(TypeError):
|
||||||
|
"no uniq value in _NameSpace"
|
||||||
|
pass
|
||||||
|
|
|
@ -488,7 +488,7 @@ class SymLinkOption(object):
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name in ('_name', '_opt', '_consistencies'):
|
if name in ('_name', '_opt', '_consistencies'):
|
||||||
return object.__gettattr__(self, name)
|
return object.__getattr__(self, name)
|
||||||
else:
|
else:
|
||||||
return getattr(self._opt, name)
|
return getattr(self._opt, name)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
from time import time
|
from time import time
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from tiramisu.error import RequirementError, PropertiesOptionError
|
from tiramisu.error import (RequirementError, PropertiesOptionError,
|
||||||
|
ConstError, ConfigError)
|
||||||
from tiramisu.i18n import _
|
from tiramisu.i18n import _
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,28 +35,46 @@ ro_append = ('frozen', 'disabled', 'validator', 'everything_frozen',
|
||||||
rw_remove = ('permissive', 'everything_frozen', 'mandatory')
|
rw_remove = ('permissive', 'everything_frozen', 'mandatory')
|
||||||
rw_append = ('frozen', 'disabled', 'validator', 'hidden')
|
rw_append = ('frozen', 'disabled', 'validator', 'hidden')
|
||||||
default_properties = ('expire', 'validator')
|
default_properties = ('expire', 'validator')
|
||||||
storage_type = 'dictionary'
|
|
||||||
|
|
||||||
|
|
||||||
class _const:
|
class StorageType:
|
||||||
|
default_storage = 'dictionary'
|
||||||
|
storage_type = None
|
||||||
|
|
||||||
|
def set_storage(self, name):
|
||||||
|
if self.storage_type is not None:
|
||||||
|
raise ConfigError(_('storage_type is already set, cannot rebind it'))
|
||||||
|
self.storage_type = name
|
||||||
|
|
||||||
|
def get_storage(self):
|
||||||
|
if self.storage_type is None:
|
||||||
|
storage = self.default_storage
|
||||||
|
else:
|
||||||
|
storage = self.storage_type
|
||||||
|
return 'tiramisu.storage.{0}.storage'.format(
|
||||||
|
storage)
|
||||||
|
|
||||||
|
|
||||||
|
storage_type = StorageType()
|
||||||
|
|
||||||
|
|
||||||
|
class _NameSpace:
|
||||||
"""convenient class that emulates a module
|
"""convenient class that emulates a module
|
||||||
and builds constants (that is, unique names)"""
|
and builds constants (that is, unique names)"""
|
||||||
class ConstError(TypeError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
if name in self.__dict__:
|
if name in self.__dict__:
|
||||||
raise self.ConstError, _("can't rebind group ({})").format(name)
|
raise ConstError(_("can't rebind {0}").format(name))
|
||||||
self.__dict__[name] = value
|
self.__dict__[name] = value
|
||||||
|
|
||||||
def __delattr__(self, name):
|
def __delattr__(self, name):
|
||||||
if name in self.__dict__:
|
if name in self.__dict__:
|
||||||
raise self.ConstError, _("can't unbind group ({})").format(name)
|
raise ConstError(_("can't unbind {0}").format(name))
|
||||||
raise ValueError(name)
|
raise ValueError(name)
|
||||||
|
|
||||||
|
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
class GroupModule(_const):
|
class GroupModule(_NameSpace):
|
||||||
"emulates a module to manage unique group (OptionDescription) names"
|
"emulates a module to manage unique group (OptionDescription) names"
|
||||||
class GroupType(str):
|
class GroupType(str):
|
||||||
"""allowed normal group (OptionDescription) names
|
"""allowed normal group (OptionDescription) names
|
||||||
|
@ -87,7 +106,7 @@ populate_groups()
|
||||||
|
|
||||||
|
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
class OwnerModule(_const):
|
class OwnerModule(_NameSpace):
|
||||||
"""emulates a module to manage unique owner names.
|
"""emulates a module to manage unique owner names.
|
||||||
|
|
||||||
owners are living in `Config._cfgimpl_value_owners`
|
owners are living in `Config._cfgimpl_value_owners`
|
||||||
|
@ -124,7 +143,7 @@ def populate_owners():
|
||||||
populate_owners()
|
populate_owners()
|
||||||
|
|
||||||
|
|
||||||
class MultiTypeModule(_const):
|
class MultiTypeModule(_NameSpace):
|
||||||
"namespace for the master/slaves"
|
"namespace for the master/slaves"
|
||||||
class MultiType(str):
|
class MultiType(str):
|
||||||
pass
|
pass
|
||||||
|
@ -184,9 +203,17 @@ class Property(object):
|
||||||
return str(list(self._properties))
|
return str(list(self._properties))
|
||||||
|
|
||||||
|
|
||||||
def set_storage(name):
|
def set_storage(name, **args):
|
||||||
global storage_type
|
storage_type.set_storage(name)
|
||||||
storage_type = name
|
settings = __import__(storage_type.get_storage(), globals(), locals(),
|
||||||
|
['Setting'], -1).Setting()
|
||||||
|
for option, value in args.items():
|
||||||
|
try:
|
||||||
|
getattr(settings, option)
|
||||||
|
setattr(settings, option, value)
|
||||||
|
except AttributeError:
|
||||||
|
raise ValueError(_('option {0} not already exists in storage {1}'
|
||||||
|
'').format(option, name))
|
||||||
|
|
||||||
|
|
||||||
def get_storage(context, session_id, is_persistent):
|
def get_storage(context, session_id, is_persistent):
|
||||||
|
@ -195,9 +222,8 @@ def get_storage(context, session_id, is_persistent):
|
||||||
|
|
||||||
if session_id is None:
|
if session_id is None:
|
||||||
session_id = gen_id(context)
|
session_id = gen_id(context)
|
||||||
import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type)
|
return __import__(storage_type.get_storage(), globals(), locals(),
|
||||||
return __import__(import_lib, globals(), locals(), ['Storage'],
|
['Storage'], -1).Storage(session_id, is_persistent)
|
||||||
-1).Storage(session_id, is_persistent)
|
|
||||||
|
|
||||||
|
|
||||||
#____________________________________________________________
|
#____________________________________________________________
|
||||||
|
|
|
@ -23,11 +23,18 @@ from tiramisu.i18n import _
|
||||||
from tiramisu.error import ConfigError
|
from tiramisu.error import ConfigError
|
||||||
|
|
||||||
|
|
||||||
def enumerate():
|
class Setting(object):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
setting = Setting()
|
||||||
|
|
||||||
|
|
||||||
|
def list_sessions():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def delete(session_id):
|
def delete_session(session_id):
|
||||||
raise ConfigError(_('dictionary storage cannot delete session'))
|
raise ConfigError(_('dictionary storage cannot delete session'))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,22 +25,27 @@ import sqlite3
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
|
|
||||||
|
class Setting(object):
|
||||||
extension = 'db'
|
extension = 'db'
|
||||||
dir_database = '/tmp'
|
dir_database = '/tmp'
|
||||||
|
|
||||||
|
|
||||||
|
setting = Setting()
|
||||||
|
|
||||||
|
|
||||||
def _gen_filename(name):
|
def _gen_filename(name):
|
||||||
return join(dir_database, '{0}.{1}'.format(name, extension))
|
return join(setting.dir_database, '{0}.{1}'.format(name,
|
||||||
|
setting.extension))
|
||||||
|
|
||||||
|
|
||||||
def enumerate():
|
def list_sessions():
|
||||||
names = []
|
names = []
|
||||||
for filename in glob(_gen_filename('*')):
|
for filename in glob(_gen_filename('*')):
|
||||||
names.append(basename(splitext(filename)[0]))
|
names.append(basename(splitext(filename)[0]))
|
||||||
return names
|
return names
|
||||||
|
|
||||||
|
|
||||||
def delete(session_id):
|
def delete_session(session_id):
|
||||||
unlink(_gen_filename(session_id))
|
unlink(_gen_filename(session_id))
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,7 +78,7 @@ class Storage(object):
|
||||||
self._cursor.close()
|
self._cursor.close()
|
||||||
self._conn.close()
|
self._conn.close()
|
||||||
if not self.is_persistent:
|
if not self.is_persistent:
|
||||||
delete(self._session_id)
|
delete_session(self._session_id)
|
||||||
|
|
||||||
|
|
||||||
class Cache(object):
|
class Cache(object):
|
||||||
|
|
|
@ -28,11 +28,16 @@ class Values(Cache):
|
||||||
def __init__(self, storage):
|
def __init__(self, storage):
|
||||||
"""init plugin means create values storage
|
"""init plugin means create values storage
|
||||||
"""
|
"""
|
||||||
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
|
|
||||||
values_table += 'key, value text, owner text)'
|
|
||||||
# should init cache too
|
# should init cache too
|
||||||
super(Values, self).__init__('value', storage)
|
super(Values, self).__init__('value', storage)
|
||||||
|
values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary '
|
||||||
|
values_table += 'key, value text, owner text)'
|
||||||
self.storage.execute(values_table)
|
self.storage.execute(values_table)
|
||||||
|
for owner in self.storage.select("SELECT DISTINCT owner FROM value", tuple(), False):
|
||||||
|
try:
|
||||||
|
getattr(owners, owner[0])
|
||||||
|
except AttributeError:
|
||||||
|
owners.add_owner(owner[0])
|
||||||
|
|
||||||
# sqlite
|
# sqlite
|
||||||
def _sqlite_select(self, path):
|
def _sqlite_select(self, path):
|
||||||
|
@ -102,4 +107,10 @@ class Values(Cache):
|
||||||
if owner is None:
|
if owner is None:
|
||||||
return default
|
return default
|
||||||
else:
|
else:
|
||||||
return getattr(owners, owner[0])
|
owner = owner[0]
|
||||||
|
# autocreate owners
|
||||||
|
try:
|
||||||
|
return getattr(owners, owner)
|
||||||
|
except AttributeError:
|
||||||
|
owners.add_owner(owner)
|
||||||
|
return getattr(owners, owner)
|
||||||
|
|
|
@ -142,7 +142,7 @@ class Values(object):
|
||||||
index=index)
|
index=index)
|
||||||
|
|
||||||
def __getitem__(self, opt):
|
def __getitem__(self, opt):
|
||||||
"enables us to use the pythonic dictionnary-like access to values"
|
"enables us to use the pythonic dictionary-like access to values"
|
||||||
return self.getitem(opt)
|
return self.getitem(opt)
|
||||||
|
|
||||||
def getitem(self, opt, path=None, validate=True, force_permissive=False,
|
def getitem(self, opt, path=None, validate=True, force_permissive=False,
|
||||||
|
|
Loading…
Reference in New Issue