remove option's storage

This commit is contained in:
2017-07-22 16:26:06 +02:00
parent 5ca2e32ac5
commit 57a47763d6
10 changed files with 620 additions and 1720 deletions

View File

@ -23,30 +23,29 @@ from types import FunctionType
import warnings
import sys
from ..i18n import _
from ..setting import log, undefined, debug
from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
display_list)
if sys.version_info[0] >= 3: # pragma: no cover
from inspect import signature
else:
from inspect import getargspec
from ..i18n import _
from ..setting import log, undefined, debug, groups
from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
display_list)
from ..storage import get_storages_option
from . import MasterSlaves
static_tuple = tuple()
if sys.version_info[0] >= 3: # pragma: no cover
xrange = range
StorageBase = get_storages_option('base')
submulti = 2
name_regexp = re.compile(r'^[a-z][a-zA-Z\d_]*$')
forbidden_names = frozenset(['iter_all', 'iter_group', 'find', 'find_first',
FORBIDDEN_NAMES = frozenset(['iter_all', 'iter_group', 'find', 'find_first',
'make_dict', 'unwrap_from_path', 'read_only',
'read_write', 'getowner', 'set_contexts'])
allowed_const_list = ['_cons_not_equal']
ALLOWED_CONST_LIST = ['_cons_not_equal']
def valid_name(name):
@ -54,7 +53,7 @@ def valid_name(name):
if not isinstance(name, str):
return False
if re.match(name_regexp, name) is not None and \
name not in forbidden_names and \
name not in FORBIDDEN_NAMES and \
not name.startswith('impl_') and \
not name.startswith('cfgimpl_'):
return True
@ -113,14 +112,40 @@ def validate_callback(callback, callback_params, type_, callbackoption):
#
class Base(StorageBase):
__slots__ = tuple()
class Base(object):
__slots__ = ('_name',
'_informations',
'_extra',
'_warnings_only',
'_allow_empty_list',
#multi
'_multi',
'_unique',
#value
'_default',
'_default_multi',
#calcul
'_subdyn',
'_requires',
'_properties',
'_calc_properties',
'_val_call',
#
'_consistencies',
'_master_slaves',
'_choice_values',
'_choice_values_params',
#other
'_has_dependency',
'_dependencies',
'__weakref__'
)
def __init__(self, name, doc, default=None, default_multi=None,
requires=None, multi=False, unique=undefined, callback=None,
callback_params=None, validator=None, validator_params=None,
properties=None, warnings_only=False, extra=None,
allow_empty_list=undefined, session=None):
allow_empty_list=undefined):
if not valid_name(name):
raise ValueError(_("invalid name: {0} for option").format(name))
if not multi and default_multi is not None:
@ -159,7 +184,11 @@ class Base(StorageBase):
validator_params = self._build_validator_params(validator, validator_params)
validate_callback(validator, validator_params, 'validator', self)
self._set_validator(validator, validator_params)
if validator_params is None:
val_call = (validator,)
else:
val_call = (validator, validator_params)
self._val_call = (val_call, None)
self._set_has_dependency()
if calc_properties != frozenset([]) and properties is not tuple():
set_forbidden_properties = calc_properties & set(properties)
@ -167,21 +196,50 @@ class Base(StorageBase):
raise ValueError('conflict: properties already set in '
'requirement {0}'.format(
list(set_forbidden_properties)))
if session is None:
session = self.getsession()
StorageBase.__init__(self, name, _multi, warnings_only, doc, extra,
calc_properties, requires, properties,
allow_empty_list, unique, session=session)
_setattr = object.__setattr__
_setattr(self, '_name', name)
if sys.version_info[0] < 3 and isinstance(doc, str):
doc = doc.decode('utf8')
if extra is not None:
_setattr(self, '_extra', extra)
_setattr(self, '_informations', {'doc': doc})
if _multi != 1:
_setattr(self, '_multi', _multi)
if warnings_only is True:
_setattr(self, '_warnings_only', warnings_only)
if calc_properties is not undefined:
_setattr(self, '_calc_properties', calc_properties)
if requires is not undefined:
_setattr(self, '_requires', requires)
if properties is not undefined:
_setattr(self, '_properties', properties)
if multi is not False and default is None:
default = []
if allow_empty_list is not undefined:
_setattr(self, '_allow_empty_list', allow_empty_list)
if unique is not undefined:
_setattr(self, '_unique', unique)
err = self.impl_validate(default, is_multi=is_multi)
if err:
raise err
self._set_default_values(default, default_multi, is_multi)
if (is_multi and default != []) or \
(not is_multi and default is not None):
if is_multi:
default = tuple(default)
_setattr(self, '_default', default)
if is_multi and default_multi is not None:
err = self._validate(default_multi)
if err:
raise ValueError(_("invalid default_multi value {0} "
"for option {1}: {2}").format(
str(default_multi),
self.impl_getname(), str(err)))
_setattr(self, '_default_multi', default_multi)
##callback is False in optiondescription
if callback is not False:
self.impl_set_callback(callback, callback_params, _init=True)
self.commit(session)
def _build_validator_params(self, validator, validator_params):
if sys.version_info[0] < 3:
@ -237,7 +295,12 @@ class Base(StorageBase):
self._validate_callback(callback, callback_params)
if callback is not None:
validate_callback(callback, callback_params, 'callback', self)
self._set_callback(callback, callback_params)
val = getattr(self, '_val_call', (None,))[0]
if callback_params is None or callback_params == {}:
val_call = (callback,)
else:
val_call = tuple([callback, callback_params])
self._val_call = (val, val_call)
def impl_is_optiondescription(self):
return self.__class__.__name__ in ['OptionDescription',
@ -248,6 +311,93 @@ class Base(StorageBase):
return self.__class__.__name__ in ['DynOptionDescription',
'SynDynOptionDescription']
def impl_getname(self):
return self._name
def impl_is_multi(self):
return getattr(self, '_multi', 1) != 1
def impl_is_readonly(self):
return not isinstance(getattr(self, '_informations', dict()), dict)
def impl_getproperties(self):
return self._properties
def _set_readonly(self, has_extra):
if not self.impl_is_readonly():
_setattr = object.__setattr__
dico = self._informations
keys = tuple(dico.keys())
if len(keys) == 1:
dico = dico['doc']
else:
dico = tuple([keys, tuple(dico.values())])
_setattr(self, '_informations', dico)
if has_extra:
extra = getattr(self, '_extra', None)
if extra is not None:
_setattr(self, '_extra', tuple([tuple(extra.keys()), tuple(extra.values())]))
def _impl_setsubdyn(self, subdyn):
self._subdyn = subdyn
def impl_getrequires(self):
return getattr(self, '_requires', static_tuple)
def impl_get_callback(self):
call = getattr(self, '_val_call', (None, None))[1]
if call is None:
ret_call = (None, {})
elif len(call) == 1:
ret_call = (call[0], {})
else:
ret_call = call
return ret_call
# ____________________________________________________________
# information
def impl_get_information(self, key, default=undefined):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
def _is_string(infos):
if sys.version_info[0] >= 3: # pragma: no cover
return isinstance(infos, str)
else:
return isinstance(infos, str) or isinstance(infos, unicode)
dico = self._informations
if isinstance(dico, tuple):
if key in dico[0]:
return dico[1][dico[0].index(key)]
elif _is_string(dico):
if key == 'doc':
return dico
elif isinstance(dico, dict):
if key in dico:
return dico[key]
if default is not undefined:
return default
raise ValueError(_("information's item not found: {0}").format(
key))
def impl_set_information(self, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
if self.impl_is_readonly():
raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
" read-only").format(
self.__class__.__name__,
self,
#self.impl_getname(),
key))
self._informations[key] = value
class BaseOption(Base):
"""This abstract base class stands for attribute access
@ -429,6 +579,19 @@ class Option(OnlyOption):
else:
return err
def impl_is_unique(self):
return getattr(self, '_unique', False)
def impl_get_validator(self):
val = getattr(self, '_val_call', (None,))[0]
if val is None:
ret_val = (None, {})
elif len(val) == 1:
ret_val = (val[0], {})
else:
ret_val = val
return ret_val
def impl_validate(self, value, context=undefined, validate=True,
force_index=None, force_submulti_index=None,
current_opt=undefined, is_multi=None,
@ -455,6 +618,7 @@ class Option(OnlyOption):
if display_warnings and setting_properties is undefined and context is not undefined:
setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False)
display_warnings = display_warnings and (setting_properties is undefined or 'warnings' in setting_properties)
def _is_not_unique(value):
if display_error and self.impl_is_unique() and len(set(value)) != len(value):
for idx, val in enumerate(value):
@ -511,16 +675,17 @@ class Option(OnlyOption):
self.impl_get_display_name())
return ValueError(msg)
error = None
if ((display_error and not self._is_warnings_only()) or
(display_warnings and self._is_warnings_only())):
is_warnings_only = getattr(self, '_warnings_only', False)
if ((display_error and not is_warnings_only) or
(display_warnings and is_warnings_only)):
error = calculation_validator(_value, _index)
if not error:
error = self._second_level_validation(_value, self._is_warnings_only())
error = self._second_level_validation(_value, is_warnings_only)
if error:
if debug: # pragma: no cover
log.debug(_('do_validation for {0}: error in value').format(
self.impl_getname()), exc_info=True)
if self._is_warnings_only():
if is_warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}').format(
_value, self._display_name, self.impl_get_display_name(), error)
warnings.warn_explicit(ValueWarning(msg, self),
@ -629,8 +794,7 @@ class Option(OnlyOption):
return False
def impl_get_master_slaves(self):
masterslaves = self._get_master_slave()
return masterslaves
return getattr(self, '_master_slaves', None)
def impl_getdoc(self):
"accesses the Option's doc"
@ -638,7 +802,7 @@ class Option(OnlyOption):
def _valid_consistencies(self, other_opts, init=True, func=None):
if self._is_subdyn():
dynod = self._impl_getsubdyn()
dynod = self._subdyn
else:
dynod = None
if self.impl_is_submulti():
@ -653,10 +817,10 @@ class Option(OnlyOption):
if dynod is None:
raise ConfigError(_('almost one option in consistency is '
'in a dynoptiondescription but not all'))
if dynod != opt._impl_getsubdyn():
if dynod != opt._subdyn:
raise ConfigError(_('option in consistency must be in same'
' dynoptiondescription'))
dynod = opt._impl_getsubdyn()
dynod = opt._subdyn
elif dynod is not None:
raise ConfigError(_('almost one option in consistency is in a '
'dynoptiondescription but not all'))
@ -699,7 +863,7 @@ class Option(OnlyOption):
if err:
self._del_consistency()
raise err
if func in allowed_const_list:
if func in ALLOWED_CONST_LIST:
for opt in all_cons_opts:
if getattr(opt, '_unique', undefined) == undefined:
opt._unique = True
@ -779,6 +943,10 @@ class Option(OnlyOption):
def _impl_to_dyn(self, name, path):
return DynSymLinkOption(name, self, dyn=path)
def impl_getdefault_multi(self):
"accessing the default value for a multi"
return getattr(self, '_default_multi', None)
def _validate_callback(self, callback, callback_params):
"""callback_params:
* None
@ -794,6 +962,52 @@ class Option(OnlyOption):
raise ValueError(_("default value not allowed if option: {0} "
"is calculated").format(self.impl_getname()))
def impl_getdefault(self):
"accessing the default value"
is_multi = self.impl_is_multi()
default = getattr(self, '_default', undefined)
if default is undefined:
if is_multi:
default = []
else:
default = None
else:
if is_multi:
default = list(default)
return default
def _get_extra(self, key):
extra = self._extra
if isinstance(extra, tuple):
return extra[1][extra[0].index(key)]
else:
return extra[key]
def impl_is_submulti(self):
return getattr(self, '_multi', 1) == 2
def impl_allow_empty_list(self):
return getattr(self, '_allow_empty_list', undefined)
#____________________________________________________________
# consistency
def _add_consistency(self, func, all_cons_opts, params):
cons = (func, all_cons_opts, params)
consistencies = getattr(self, '_consistencies', None)
if consistencies is None:
self._consistencies = [cons]
else:
consistencies.append(cons)
def _del_consistency(self):
self._consistencies.pop(-1)
def _get_consistencies(self):
return getattr(self, '_consistencies', static_tuple)
def _has_consistencies(self):
return hasattr(self, '_consistencies')
def validate_requires_arg(new_option, multi, requires, name):
"""check malformed requirements
@ -951,16 +1165,17 @@ class SymLinkOption(OnlyOption):
raise ValueError(_('malformed symlinkoption '
'must be an option '
'for symlink {0}').format(name))
session = self.getsession()
super(Base, self).__init__(name, undefined, undefined, undefined,
undefined, undefined, undefined, undefined,
undefined, undefined, opt=opt, session=session)
_setattr = object.__setattr__
_setattr(self, '_name', name)
_setattr(self, '_opt', opt)
opt._set_has_dependency()
self.commit(session)
def __getattr__(self, name, context=undefined):
return getattr(self._impl_getopt(), name)
def _impl_getopt(self):
return self._opt
def impl_get_information(self, key, default=undefined):
return self._impl_getopt().impl_get_information(key, default)

View File

@ -22,57 +22,50 @@
from ..i18n import _
from ..setting import log, undefined, debug
from ..error import SlaveError, PropertiesOptionError
from ..storage import get_storages_option
StorageMasterSlaves = get_storages_option('masterslaves')
class MasterSlaves(object):
__slots__ = ('_p_')
__slots__ = ('master', 'slaves')
def __init__(self, name, childs=None, validate=True, add=True):
if isinstance(name, StorageMasterSlaves): # pragma: no cover
# only for sqlalchemy
self._p_ = name
#if master (same name has group) is set
#for collect all slaves
slaves = []
if childs[0].impl_getname() == name:
master = childs[0]
else:
#if master (same name has group) is set
#for collect all slaves
slaves = []
if childs[0].impl_getname() == name:
master = childs[0]
else:
raise ValueError(_('master group with wrong'
' master name for {0}'
).format(name))
for child in childs[1:]:
if child.impl_getdefault() != []:
raise ValueError(_("not allowed default value for option {0} "
"in master/slave object {1}").format(child.impl_getname(),
name))
slaves.append(child)
if validate:
callback, callback_params = master.impl_get_callback()
if callback is not None and callback_params != {}:
for callbacks in callback_params.values():
for callbk in callbacks:
if isinstance(callbk, tuple):
if callbk[0] in slaves:
raise ValueError(_("callback of master's option shall "
"not refered a slave's ones"))
#everything is ok, store references
self._p_ = StorageMasterSlaves(master, slaves)
if add:
for child in childs:
child._set_master_slaves(self)
raise ValueError(_('master group with wrong'
' master name for {0}'
).format(name))
for child in childs[1:]:
if child.impl_getdefault() != []:
raise ValueError(_("not allowed default value for option {0} "
"in master/slave object {1}").format(child.impl_getname(),
name))
slaves.append(child)
if validate:
callback, callback_params = master.impl_get_callback()
if callback is not None and callback_params != {}:
for callbacks in callback_params.values():
for callbk in callbacks:
if isinstance(callbk, tuple):
if callbk[0] in slaves:
raise ValueError(_("callback of master's option shall "
"not refered a slave's ones"))
#everything is ok, store references
self.master = master
self.slaves = tuple(slaves)
if add:
for child in childs:
child._master_slaves = self
def is_master(self, opt):
master = self._p_._sm_getmaster().impl_getname()
master = self.master.impl_getname()
return opt.impl_getname() == master or (opt.impl_is_dynsymlinkoption() and
opt._opt.impl_getname() == master)
def getmaster(self, opt):
master = self._p_._sm_getmaster()
master = self.master
if opt.impl_is_dynsymlinkoption():
suffix = opt.impl_getsuffix()
name = master.impl_getname() + suffix
@ -83,21 +76,21 @@ class MasterSlaves(object):
def getslaves(self, opt):
if opt.impl_is_dynsymlinkoption():
for slave in self._p_._sm_getslaves():
for slave in self.slaves:
suffix = opt.impl_getsuffix()
name = slave.impl_getname() + suffix
base_path = opt._dyn.split('.')[0] + '.'
path = base_path + name
yield slave._impl_to_dyn(name, path)
else:
for slave in self._p_._sm_getslaves():
for slave in self.slaves:
yield slave
def in_same_group(self, opt):
if opt.impl_is_dynsymlinkoption():
return opt._opt == self._p_._sm_getmaster() or opt._opt in self._p_._sm_getslaves()
return opt._opt == self.master or opt._opt in self.slaves
else:
return opt == self._p_._sm_getmaster() or opt in self._p_._sm_getslaves()
return opt == self.master or opt in self.slaves
def reset(self, opt, values, setting_properties, _commit=True):
for slave in self.getslaves(opt):

View File

@ -56,8 +56,9 @@ class ChoiceOption(Option):
if not isinstance(values, tuple):
raise TypeError(_('values must be a tuple or a function for {0}'
).format(name))
session = self.getsession()
self.impl_set_choice_values_params(values, values_params, session)
self._choice_values = values
if values_params is not None:
self._choice_values_params = values_params
super(ChoiceOption, self).__init__(name, doc, default=default,
default_multi=default_multi,
callback=callback,
@ -67,9 +68,7 @@ class ChoiceOption(Option):
validator=validator,
validator_params=validator_params,
properties=properties,
warnings_only=warnings_only,
session=session)
self.commit(session)
warnings_only=warnings_only)
def impl_get_values(self, context, current_opt=undefined):
if current_opt is undefined:
@ -82,7 +81,7 @@ class ChoiceOption(Option):
else:
values = carry_out_calculation(current_opt, context=context,
callback=values,
callback_params=self.impl_get_choice_values_params())
callback_params=getattr(self, '_choice_values_params', {}))
if isinstance(values, Exception):
return values
if values is not undefined and not isinstance(values, list):

View File

@ -24,15 +24,12 @@ import re
from ..i18n import _
from ..setting import groups, undefined, owners # , log
from .baseoption import BaseOption, SymLinkOption, Option, allowed_const_list
from .baseoption import BaseOption, SymLinkOption, Option, ALLOWED_CONST_LIST
from . import MasterSlaves
from ..error import ConfigError, ConflictError
from ..storage import get_storages_option
from ..autolib import carry_out_calculation
StorageOptionDescription = get_storages_option('optiondescription')
name_regexp = re.compile(r'^[a-zA-Z\d\-_]*$')
import sys
@ -41,61 +38,8 @@ if sys.version_info[0] >= 3: # pragma: no cover
del(sys)
class OptionDescription(BaseOption, StorageOptionDescription):
"""Config's schema (organisation, group) and container of Options
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
"""
__slots__ = tuple()
def __init__(self, name, doc, children, requires=None, properties=None):
"""
:param children: a list of options (including optiondescriptions)
"""
super(OptionDescription, self).__init__(name, doc=doc,
requires=requires,
properties=properties,
callback=False)
child_names = []
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if isinstance(child, DynOptionDescription):
dynopt_names.append(name)
#better performance like this
valid_child = copy(child_names)
valid_child.sort()
old = None
for child in valid_child:
if child == old: # pragma: optional cover
raise ConflictError(_('duplicate option name: '
'{0}').format(child))
if dynopt_names:
for dynopt in dynopt_names:
if child != dynopt and child.startswith(dynopt):
raise ConflictError(_('option must not start as '
'dynoptiondescription'))
old = child
self._add_children(child_names, children)
_setattr = object.__setattr__
_setattr(self, '_cache_consistencies', None)
# the group_type is useful for filtering OptionDescriptions in a config
_setattr(self, '_group_type', groups.default)
def impl_getdoc(self):
return self.impl_get_information('doc')
def impl_validate(self, *args, **kwargs):
"""usefull for OptionDescription"""
pass
def impl_getpaths(self, include_groups=False, _currpath=None):
"""returns a list of all paths in self, recursively
_currpath should not be provided (helps with recursion)
"""
return _impl_getpaths(self, include_groups, _currpath)
class CacheOptionDescription(BaseOption):
__slots__ = ('_cache_paths', '_cache_consistencies', '_cache_force_store_values')
def impl_build_cache(self, config, path='', _consistencies=None,
cache_option=None, force_store_values=None):
@ -112,9 +56,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
else:
init = False
for option in self._impl_getchildren(dyn=False):
#FIXME specifique id for sqlalchemy?
#FIXME avec sqlalchemy ca marche le multi parent ? (dans des configs différentes)
cache_option.append(option._get_id())
cache_option.append(option)
if path == '':
subpath = option.impl_getname()
else:
@ -138,7 +80,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
force_store_values.append((subpath, option))
for func, all_cons_opts, params in option._get_consistencies():
option._valid_consistencies(all_cons_opts[1:], init=False)
if func not in allowed_const_list and is_multi:
if func not in ALLOWED_CONST_LIST and is_multi:
is_masterslaves = option.impl_is_master_slaves()
if not is_masterslaves:
raise ConfigError(_('malformed consistency option "{0}" '
@ -146,7 +88,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
option.impl_getname()))
masterslaves = option.impl_get_master_slaves()
for opt in all_cons_opts:
if func not in allowed_const_list and is_multi:
if func not in ALLOWED_CONST_LIST and is_multi:
if not opt.impl_is_master_slaves():
raise ConfigError(_('malformed consistency option "{0}" '
'must not be a multi for "{1}"').format(
@ -193,7 +135,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
if _consistencies != {}:
self._cache_consistencies = {}
for opt, cons in _consistencies.items():
if opt._get_id() not in cache_option: # pragma: optional cover
if opt not in cache_option: # pragma: optional cover
raise ConfigError(_('consistency with option {0} '
'which is not in Config').format(
opt.impl_getname()))
@ -201,6 +143,8 @@ class OptionDescription(BaseOption, StorageOptionDescription):
self._cache_force_store_values = force_store_values
self._set_readonly(False)
def impl_already_build_caches(self):
return getattr(self, '_cache_paths', None) is not None
def impl_build_force_store_values(self, config, force_store_values):
session = config._impl_values._p_.getsession()
@ -226,6 +170,279 @@ class OptionDescription(BaseOption, StorageOptionDescription):
if value_set:
config._impl_values._p_.commit()
def impl_build_cache_option(self, _currpath=None, cache_path=None,
cache_option=None):
if self.impl_is_readonly() or (_currpath is None and getattr(self, '_cache_paths', None) is not None):
# cache already set
return
if _currpath is None:
save = True
_currpath = []
else:
save = False
if cache_path is None:
cache_path = []
cache_option = []
for option in self._impl_getchildren(dyn=False):
attr = option.impl_getname()
path = str('.'.join(_currpath + [attr]))
cache_option.append(option)
cache_path.append(path)
if option.impl_is_optiondescription():
_currpath.append(attr)
option.impl_build_cache_option(_currpath, cache_path,
cache_option)
_currpath.pop()
if save:
_setattr = object.__setattr__
_setattr(self, '_cache_paths', (tuple(cache_option), tuple(cache_path)))
class OptionDescriptionWalk(CacheOptionDescription):
__slots__ = ('_children',)
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: # pragma: no cover
raise ConfigError(_('cannot find dynpath'))
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 xrange(0, len(paths)):
option = opts[index]
if option.impl_is_optiondescription():
continue
path = paths[index]
if _subpath is not None and not path.startswith(_subpath + '.'):
continue
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 find_results
return find_results
def _impl_st_getchildren(self, context, only_dyn=False):
for child in self._children[1]:
if only_dyn is False or child.impl_is_dynoptiondescription():
yield(child)
def _getattr(self, name, suffix=undefined, context=undefined, dyn=True):
error = False
if suffix is not undefined:
if undefined in [suffix, context]: # pragma: no cover
raise ConfigError(_("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
else:
if name in self._children[0]:
child = self._children[1][self._children[0].index(name)]
if dyn and child.impl_is_dynoptiondescription():
error = True
else:
return child
else:
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.impl_getname()))
def impl_getpaths(self, include_groups=False, _currpath=None):
"""returns a list of all paths in self, recursively
_currpath should not be provided (helps with recursion)
"""
return _impl_getpaths(self, include_groups, _currpath)
def impl_get_opt_by_path(self, path):
if getattr(self, '_cache_paths', None) is None:
raise ConfigError(_('use impl_get_opt_by_path only with root OptionDescription'))
if path not in self._cache_paths[1]:
raise AttributeError(_('no option for path {0}').format(path))
return self._cache_paths[0][self._cache_paths[1].index(path)]
def impl_get_path_by_opt(self, opt):
if getattr(self, '_cache_paths', None) is None:
raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription'))
if opt not in self._cache_paths[0]:
raise AttributeError(_('no option {0} found').format(opt))
return self._cache_paths[1][self._cache_paths[0].index(opt)]
def _impl_getchildren(self, dyn=True, context=undefined):
for child in self._impl_st_getchildren(context):
cname = child.impl_getname()
if dyn and child.impl_is_dynoptiondescription():
path = cname
for value in child._impl_get_suffixes(context):
yield SynDynOptionDescription(child,
cname + value,
path + value, value)
else:
yield child
def impl_getchildren(self):
return list(self._impl_getchildren())
def __getattr__(self, name, context=undefined):
if name.startswith('_'): # or name.startswith('impl_'):
return object.__getattribute__(self, name)
if '.' in name:
path = name.split('.')[0]
subpath = '.'.join(name.split('.')[1:])
return self.__getattr__(path, context=context).__getattr__(subpath, context=context)
return self._getattr(name, context=context)
def _impl_search_dynchild(self, name, context):
ret = []
for child in self._impl_st_getchildren(context, only_dyn=True):
cname = child.impl_getname()
if name.startswith(cname):
path = cname
for value in child._impl_get_suffixes(context):
if name == cname + value:
return SynDynOptionDescription(child, name, path + value, value)
return ret
def _impl_get_dynchild(self, child, suffix):
name = child.impl_getname() + suffix
path = self.impl_getname() + suffix + '.' + name
if isinstance(child, OptionDescription):
return SynDynOptionDescription(child, name, path, suffix)
else:
return child._impl_to_dyn(name, path)
class OptionDescription(OptionDescriptionWalk):
"""Config's schema (organisation, group) and container of Options
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
"""
__slots__ = ('_group_type',)
def __init__(self, name, doc, children, requires=None, properties=None):
"""
:param children: a list of options (including optiondescriptions)
"""
super(OptionDescription, self).__init__(name, doc=doc,
requires=requires,
properties=properties,
callback=False)
child_names = []
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if isinstance(child, DynOptionDescription):
dynopt_names.append(name)
#better performance like this
valid_child = copy(child_names)
valid_child.sort()
old = None
for child in valid_child:
if child == old: # pragma: optional cover
raise ConflictError(_('duplicate option name: '
'{0}').format(child))
if dynopt_names:
for dynopt in dynopt_names:
if child != dynopt and child.startswith(dynopt):
raise ConflictError(_('option must not start as '
'dynoptiondescription'))
old = child
_setattr = object.__setattr__
_setattr(self, '_children', (tuple(child_names), tuple(children)))
_setattr(self, '_cache_consistencies', None)
# the group_type is useful for filtering OptionDescriptions in a config
_setattr(self, '_group_type', groups.default)
def impl_getdoc(self):
return self.impl_get_information('doc')
def impl_validate(self, *args, **kwargs):
"""usefull for OptionDescription"""
pass
# ____________________________________________________________
def impl_set_group_type(self, group_type):
"""sets a given group object to an OptionDescription
@ -260,6 +477,9 @@ class OptionDescription(BaseOption, StorageOptionDescription):
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))
def impl_get_group_type(self):
return self._group_type
def __getstate__(self):
raise NotImplementedError()
@ -275,49 +495,6 @@ class OptionDescription(BaseOption, StorageOptionDescription):
raise ValueError(_("invalid suffix: {0} for option").format(val))
return values
def _impl_search_dynchild(self, name, context):
ret = []
for child in self._impl_st_getchildren(context, only_dyn=True):
cname = child.impl_getname()
if name.startswith(cname):
path = cname
for value in child._impl_get_suffixes(context):
if name == cname + value:
return SynDynOptionDescription(child, name, path + value, value)
return ret
def _impl_get_dynchild(self, child, suffix):
name = child.impl_getname() + suffix
path = self.impl_getname() + suffix + '.' + name
if isinstance(child, OptionDescription):
return SynDynOptionDescription(child, name, path, suffix)
else:
return child._impl_to_dyn(name, path)
def _impl_getchildren(self, dyn=True, context=undefined):
for child in self._impl_st_getchildren(context):
cname = child.impl_getname()
if dyn and child.impl_is_dynoptiondescription():
path = cname
for value in child._impl_get_suffixes(context):
yield SynDynOptionDescription(child,
cname + value,
path + value, value)
else:
yield child
def impl_getchildren(self):
return list(self._impl_getchildren())
def __getattr__(self, name, context=undefined):
if name.startswith('_'): # or name.startswith('impl_'):
return object.__getattribute__(self, name)
if '.' in name:
path = name.split('.')[0]
subpath = '.'.join(name.split('.')[1:])
return self.__getattr__(path, context=context).__getattr__(subpath, context=context)
return self._getattr(name, context=context)
class DynOptionDescription(OptionDescription):
def __init__(self, name, doc, children, requires=None, properties=None,
@ -378,19 +555,19 @@ class SynDynOptionDescription(object):
def _impl_getpaths(klass, include_groups, _currpath):
"""returns a list of all paths in klass, recursively
_currpath should not be provided (helps with recursion)
"""
if _currpath is None:
_currpath = []
paths = []
for option in klass._impl_getchildren():
attr = option.impl_getname()
if option.impl_is_optiondescription():
if include_groups:
paths.append('.'.join(_currpath + [attr]))
paths += option.impl_getpaths(include_groups=include_groups,
_currpath=_currpath + [attr])
else:
"""returns a list of all paths in klass, recursively
_currpath should not be provided (helps with recursion)
"""
if _currpath is None:
_currpath = []
paths = []
for option in klass._impl_getchildren():
attr = option.impl_getname()
if option.impl_is_optiondescription():
if include_groups:
paths.append('.'.join(_currpath + [attr]))
return paths
paths += option.impl_getpaths(include_groups=include_groups,
_currpath=_currpath + [attr])
else:
paths.append('.'.join(_currpath + [attr]))
return paths