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:
2013-08-26 21:48:42 +02:00
parent 551b9fb1e3
commit 0bc47b1cf9
7 changed files with 83 additions and 29 deletions

View File

@ -22,7 +22,8 @@
# ____________________________________________________________
from time import time
from copy import copy
from tiramisu.error import RequirementError, PropertiesOptionError
from tiramisu.error import (RequirementError, PropertiesOptionError,
ConstError, ConfigError)
from tiramisu.i18n import _
@ -34,28 +35,46 @@ ro_append = ('frozen', 'disabled', 'validator', 'everything_frozen',
rw_remove = ('permissive', 'everything_frozen', 'mandatory')
rw_append = ('frozen', 'disabled', 'validator', 'hidden')
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
and builds constants (that is, unique names)"""
class ConstError(TypeError):
pass
def __setattr__(self, name, value):
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
def __delattr__(self, name):
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)
# ____________________________________________________________
class GroupModule(_const):
class GroupModule(_NameSpace):
"emulates a module to manage unique group (OptionDescription) names"
class GroupType(str):
"""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.
owners are living in `Config._cfgimpl_value_owners`
@ -124,7 +143,7 @@ def populate_owners():
populate_owners()
class MultiTypeModule(_const):
class MultiTypeModule(_NameSpace):
"namespace for the master/slaves"
class MultiType(str):
pass
@ -184,9 +203,17 @@ class Property(object):
return str(list(self._properties))
def set_storage(name):
global storage_type
storage_type = name
def set_storage(name, **args):
storage_type.set_storage(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):
@ -195,9 +222,8 @@ def get_storage(context, session_id, is_persistent):
if session_id is None:
session_id = gen_id(context)
import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type)
return __import__(import_lib, globals(), locals(), ['Storage'],
-1).Storage(session_id, is_persistent)
return __import__(storage_type.get_storage(), globals(), locals(),
['Storage'], -1).Storage(session_id, is_persistent)
#____________________________________________________________