add DynOptionDescription

This commit is contained in:
2014-06-19 23:22:39 +02:00
parent 888446e4c5
commit b64189f763
21 changed files with 2382 additions and 543 deletions

View File

@ -40,7 +40,7 @@ class StorageType(object):
storage_type = None
mod = None
def set(self, name):
def set(self, name): # pragma: optional cover
if self.storage_type is not None:
if self.storage_type == name:
return
@ -63,7 +63,7 @@ storage_type = StorageType()
storage_option_type = StorageType()
def set_storage(type_, name, **kwargs):
def set_storage(type_, name, **kwargs): # pragma: optional cover
"""Change storage's configuration
:params name: is the storage name. If storage is already set, cannot
@ -95,7 +95,7 @@ def _impl_getstate_setting():
return state
def get_storage(type_, session_id, persistent, test):
def get_storage(type_, session_id, persistent, test): # pragma: optional cover
"""all used when __setstate__ a Config
"""
if type_ == 'option':
@ -123,7 +123,7 @@ def get_storages_option(type_):
return imp.OptionDescription
def list_sessions(type_):
def list_sessions(type_): # pragma: optional cover
"""List all available session (persistent or not persistent)
"""
if type_ == 'option':
@ -132,7 +132,7 @@ def list_sessions(type_):
return storage_type.get().list_sessions()
def delete_session(type_, session_id):
def delete_session(type_, session_id): # pragma: optional cover
"""Delete a selected session, be careful, you can deleted a session
use by an other instance
:params session_id: id of session to delete

View File

@ -18,7 +18,8 @@
#
# ____________________________________________________________
from tiramisu.i18n import _
from tiramisu.setting import groups
from tiramisu.setting import groups, undefined
from tiramisu.error import ConfigError
#____________________________________________________________
@ -32,9 +33,14 @@ class Base(object):
'_default', '_default_multi', '_state_callback', '_callback',
'_state_callback_params', '_callback_params', '_multitype',
'_consistencies', '_warnings_only', '_master_slaves',
'_state_consistencies', '_extra', '__weakref__')
'_state_consistencies', '_extra', '_subdyn', '__weakref__',
'_state_master_slaves')
def __init__(self):
try:
self._subdyn
except AttributeError:
self._subdyn = False
try:
self._consistencies
except AttributeError:
@ -65,6 +71,12 @@ class Base(object):
def _get_id(self):
return id(self)
def _is_subdyn(self):
try:
return self._subdyn is not False
except AttributeError:
return False
class OptionDescription(Base):
__slots__ = ('_children', '_cache_paths', '_cache_consistencies',
@ -82,20 +94,21 @@ class OptionDescription(Base):
def impl_get_opt_by_path(self, path):
try:
return self._cache_paths[0][self._cache_paths[1].index(path)]
except ValueError:
except ValueError: # pragma: optional cover
raise AttributeError(_('no option for path {0}').format(path))
def impl_get_path_by_opt(self, opt):
try:
return self._cache_paths[1][self._cache_paths[0].index(opt)]
except ValueError:
except ValueError: # pragma: optional cover
raise AttributeError(_('no option {0} found').format(opt))
def impl_get_group_type(self):
def impl_get_group_type(self): # pragma: optional cover
return getattr(groups, self._group_type)
def impl_build_cache_option(self, _currpath=None, cache_path=None, cache_option=None):
if _currpath is None and self._cache_paths is not None:
def impl_build_cache_option(self, _currpath=None, cache_path=None,
cache_option=None):
if _currpath is None and self._cache_paths is not None: # pragma: optional cover
# cache already set
return
if _currpath is None:
@ -106,11 +119,12 @@ class OptionDescription(Base):
if cache_path is None:
cache_path = []
cache_option = []
for option in self.impl_getchildren():
for option in self._impl_getchildren(dyn=False):
attr = option._name
path = str('.'.join(_currpath + [attr]))
cache_option.append(option)
cache_path.append(str('.'.join(_currpath + [attr])))
if option.__class__.__name__ == 'OptionDescription':
cache_path.append(path)
if option.impl_is_optiondescription():
_currpath.append(attr)
option.impl_build_cache_option(_currpath, cache_path,
cache_option)
@ -118,52 +132,141 @@ class OptionDescription(Base):
if save:
self._cache_paths = (tuple(cache_option), tuple(cache_path))
def impl_get_options_paths(self, bytype, byname, _subpath, only_first):
def _filter_by_name():
if byname is None or path == byname or \
path.endswith('.' + byname):
return True
return False
def _filter_by_type():
if bytype is None:
return True
if isinstance(option, bytype):
return True
return False
def impl_get_options_paths(self, bytype, byname, _subpath, only_first, context):
find_results = []
def _rebuild_dynpath(path, suffix, dynopt):
found = False
spath = path.split('.')
for length in xrange(1, len(spath)):
subpath = '.'.join(spath[0:length])
subopt = self.impl_get_opt_by_path(subpath)
if dynopt == subopt:
found = True
break
if not found:
#FIXME
raise ConfigError(_('hu?'))
subpath = subpath + suffix
for slength in xrange(length, len(spath)):
subpath = subpath + '.' + spath[slength] + suffix
return subpath
def _filter_by_name(path, option):
name = option.impl_getname()
if option._is_subdyn():
if byname.startswith(name):
found = False
for suffix in option._subdyn._impl_get_suffixes(
context):
if byname == name + suffix:
found = True
path = _rebuild_dynpath(path, suffix,
option._subdyn)
option = option._impl_to_dyn(
name + suffix, path)
break
if not found:
return False
else:
if not byname == name:
return False
find_results.append((path, option))
return True
def _filter_by_type(path, option):
if isinstance(option, bytype):
#if byname is not None, check option byname in _filter_by_name
#not here
if byname is None:
if option._is_subdyn():
name = option.impl_getname()
for suffix in option._subdyn._impl_get_suffixes(
context):
spath = _rebuild_dynpath(path, suffix,
option._subdyn)
find_results.append((spath, option._impl_to_dyn(
name + suffix, spath)))
else:
find_results.append((path, option))
return True
return False
def _filter(path, option):
if bytype is not None:
retval = _filter_by_type(path, option)
if byname is None:
return retval
if byname is not None:
return _filter_by_name(path, option)
opts, paths = self._cache_paths
for index in range(0, len(paths)):
option = opts[index]
if option.__class__.__name__ == 'OptionDescription':
if option.impl_is_optiondescription():
continue
path = paths[index]
if _subpath is not None and not path.startswith(_subpath + '.'):
continue
if not _filter_by_name():
continue
if not _filter_by_type():
continue
retval = (path, option)
if bytype == byname is None:
if option._is_subdyn():
name = option.impl_getname()
for suffix in option._subdyn._impl_get_suffixes(
context):
spath = _rebuild_dynpath(path, suffix,
option._subdyn)
find_results.append((spath, option._impl_to_dyn(
name + suffix, spath)))
else:
find_results.append((path, option))
else:
if _filter(path, option) is False:
continue
if only_first:
return retval
find_results.append(retval)
return find_results[0]
return find_results
def impl_getchildren(self):
def _impl_st_getchildren(self):
return self._children[1]
def __getattr__(self, name):
def __getattr__(self, name, context=undefined):
if name == '_name':
return object.__getattribute__(self, name)
try:
if name == '_readonly':
raise AttributeError("{0} instance has no attribute "
"'_readonly'".format(
self.__class__.__name__))
return self._children[1][self._children[0].index(name)]
except ValueError:
return self._getattr(name, context=context)
def _getattr(self, name, dyn_od=undefined, suffix=undefined,
context=undefined, dyn=True):
error = False
if suffix is not undefined:
try:
if undefined in [dyn_od, suffix, context]: # pragma: optional cover
raise ConfigError(_("dyn_od, suffix and context needed if "
"it's a dyn option"))
if name.endswith(suffix):
oname = name[:-len(suffix)]
child = self._children[1][self._children[0].index(oname)]
return self._impl_get_dynchild(child, suffix)
else:
error = True
except ValueError: # pragma: optional cover
error = True
else:
try: # pragma: optional cover
if name == '_readonly':
raise AttributeError(_("{0} instance has no attribute "
"'_readonly'").format(
self.__class__.__name__))
child = self._children[1][self._children[0].index(name)]
if dyn and child.impl_is_dynoptiondescription():
error = True
else:
return child
except ValueError:
child = self._impl_search_dynchild(name, context=context)
if child != []:
return child
error = True
if error:
raise AttributeError(_('unknown Option {0} '
'in OptionDescription {1}'
'').format(name, self._name))

View File

@ -29,11 +29,11 @@ setting = Setting()
_list_sessions = []
def list_sessions():
def list_sessions(): # pragma: optional cover
return _list_sessions
def delete_session(session_id):
def delete_session(session_id): # pragma: optional cover
raise ConfigError(_('dictionary storage cannot delete session'))
@ -44,9 +44,9 @@ class Storage(object):
serializable = True
def __init__(self, session_id, persistent, test=False):
if not test and session_id in _list_sessions:
if not test and session_id in _list_sessions: # pragma: optional cover
raise ValueError(_('session already used'))
if persistent:
if persistent: # pragma: optional cover
raise ValueError(_('a dictionary cannot be persistent'))
self.session_id = session_id
self.persistent = persistent
@ -55,5 +55,5 @@ class Storage(object):
def __del__(self):
try:
_list_sessions.remove(self.session_id)
except AttributeError:
except AttributeError: # pragma: optional cover
pass

View File

@ -88,5 +88,5 @@ class Values(Cache):
"""
if key in self._informations:
return self._informations[key]
else:
else: # pragma: optional cover
raise ValueError("not found")