tiramisu/tiramisu/config.py

1143 lines
50 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2012-2018 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/>.
#
2012-10-05 16:00:07 +02:00
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
2013-09-23 22:55:54 +02:00
"options handler global entry point"
import weakref
from copy import copy
2015-04-18 22:53:45 +02:00
2017-12-19 23:11:45 +01:00
from .error import PropertiesOptionError, ConfigError, ConflictError, SlaveError
from .option import SynDynOptionDescription, DynOptionDescription, MasterSlaves
2017-12-19 23:11:45 +01:00
from .option.baseoption import BaseOption, valid_name
2018-08-01 08:37:58 +02:00
from .setting import OptionBag, ConfigBag, groups, Settings, undefined
from .storage import get_storages, gen_storage_id, get_default_values_storages
2017-11-12 14:33:05 +01:00
from .value import Values # , Multi
from .i18n import _
2013-02-22 11:09:17 +01:00
class SubConfig(object):
2013-09-23 22:55:54 +02:00
"""Sub configuration management entry.
Tree if OptionDescription's responsability. SubConfig are generated
on-demand. A Config is also a SubConfig.
Root Config is call context below
"""
2017-11-12 14:33:05 +01:00
__slots__ = ('_impl_context',
'_impl_descr',
'_impl_path',
'_impl_length')
def __init__(self,
descr,
context,
2017-12-19 23:11:45 +01:00
config_bag,
2018-08-01 08:37:58 +02:00
subpath=None,
fromconsistency=None):
2012-10-05 16:00:07 +02:00
""" Configuration option management master class
2013-02-19 11:24:17 +01:00
2012-10-05 16:00:07 +02:00
:param descr: describes the configuration schema
:type descr: an instance of ``option.OptionDescription``
2013-02-07 16:20:21 +01:00
:param context: the current root config
:type context: `Config`
:type subpath: `str` with the path name
2012-10-05 16:00:07 +02:00
"""
# main option description
2014-06-19 23:22:39 +02:00
error = False
2017-12-19 23:11:45 +01:00
if descr is not None and (not isinstance(descr, (BaseOption, SynDynOptionDescription)) or not descr.impl_is_optiondescription()):
2014-06-19 23:22:39 +02:00
error = True
if error:
try:
msg = descr.impl_get_displayname()
except AttributeError:
msg = descr
raise TypeError(_('"{0}" must be an optiondescription, not an {1}'
).format(msg, type(descr)))
self._impl_descr = descr
self._impl_context = context
self._impl_path = subpath
2018-01-03 21:07:51 +01:00
if descr is not None and \
2017-12-19 23:11:45 +01:00
descr.impl_get_group_type() == groups.master:
2017-12-02 22:53:57 +01:00
master = descr.getmaster()
2017-12-19 23:11:45 +01:00
masterpath = master.impl_getname()
2018-08-01 08:37:58 +02:00
full_masterpath = self._get_subpath(masterpath)
2018-08-02 22:35:40 +02:00
cconfig_bag = config_bag.copy()
2018-08-17 23:11:25 +02:00
cconfig_bag.remove_validation()
2018-08-01 08:37:58 +02:00
moption_bag = OptionBag()
moption_bag.set_option(master,
full_masterpath,
None,
cconfig_bag)
if fromconsistency:
moption_bag.fromconsistency = fromconsistency
2017-12-19 23:11:45 +01:00
value = self.getattr(masterpath,
2018-08-01 08:37:58 +02:00
moption_bag)
2017-12-02 22:53:57 +01:00
self._impl_length = len(value)
def cfgimpl_get_length(self):
2018-03-24 22:37:48 +01:00
return self._impl_length
2018-08-01 08:37:58 +02:00
def cfgimpl_get_length_slave(self,
option_bag):
if option_bag.option.impl_is_symlinkoption():
2018-04-19 08:19:03 +02:00
context = self.cfgimpl_get_context()
2018-09-06 23:16:17 +02:00
path = option_bag.option.impl_getopt().impl_getpath()
2018-08-01 08:37:58 +02:00
subconfig, _ = context.cfgimpl_get_home_by_path(path,
option_bag.config_bag)
2018-04-19 08:19:03 +02:00
return subconfig.cfgimpl_get_length()
else:
return self.cfgimpl_get_length()
2017-11-20 17:01:36 +01:00
def reset_one_option_cache(self,
2018-04-09 21:37:49 +02:00
desc,
2017-11-20 17:01:36 +01:00
values,
settings,
resetted_opts,
2018-08-01 08:37:58 +02:00
option_bag):
2017-12-13 22:15:34 +01:00
2018-08-01 08:37:58 +02:00
if option_bag.path in resetted_opts:
2018-04-09 21:37:49 +02:00
return
2018-08-01 08:37:58 +02:00
resetted_opts.append(option_bag.path)
for woption in option_bag.option._get_dependencies(self):
2017-11-20 17:01:36 +01:00
option = woption()
2018-04-09 21:37:49 +02:00
if option.impl_is_dynoptiondescription():
2018-09-30 20:32:00 +02:00
for doption in option.get_syndynoptiondescriptions(option_bag.config_bag,
remove_none=True):
2018-09-06 23:16:17 +02:00
doption_path = doption.impl_getpath()
2018-08-01 08:37:58 +02:00
doption_bag = OptionBag()
doption_bag.set_option(doption,
doption_path,
option_bag.index,
option_bag.config_bag)
2018-04-09 21:37:49 +02:00
self.reset_one_option_cache(desc,
values,
settings,
resetted_opts,
2018-08-01 08:37:58 +02:00
doption_bag)
2018-04-09 21:37:49 +02:00
elif option.issubdyn():
2018-08-01 08:37:58 +02:00
doption_bag = OptionBag()
2018-09-06 23:16:17 +02:00
doption_path = option.impl_getpath()
2018-08-01 08:37:58 +02:00
doption_bag.set_option(option,
doption_path,
option_bag.index,
option_bag.config_bag)
2018-09-30 20:32:00 +02:00
for doption in desc.get_dynoptions(doption_bag):
2018-09-06 23:16:17 +02:00
doption_path = doption.impl_getpath()
2018-08-01 08:37:58 +02:00
doption_bag = OptionBag()
doption_bag.set_option(doption,
doption_path,
option_bag.index,
option_bag.config_bag)
2018-04-09 21:37:49 +02:00
self.reset_one_option_cache(desc,
values,
settings,
resetted_opts,
2018-08-01 08:37:58 +02:00
doption_bag)
2018-04-09 21:37:49 +02:00
else:
2018-09-06 23:16:17 +02:00
option_path = option.impl_getpath()
2018-08-01 08:37:58 +02:00
doption_bag = OptionBag()
doption_bag.set_option(option,
option_path,
option_bag.index,
option_bag.config_bag)
2018-04-09 21:37:49 +02:00
self.reset_one_option_cache(desc,
values,
settings,
resetted_opts,
2018-08-01 08:37:58 +02:00
doption_bag)
2017-11-20 17:01:36 +01:00
del option
2018-08-01 08:37:58 +02:00
option_bag.option.reset_cache(option_bag.path,
values,
settings,
resetted_opts)
2017-11-13 22:45:53 +01:00
2017-07-08 15:59:56 +02:00
def cfgimpl_reset_cache(self,
2018-08-01 08:37:58 +02:00
option_bag,
resetted_opts=None):
2017-07-08 15:59:56 +02:00
"""reset all settings in cache
"""
if resetted_opts is None:
2017-12-19 23:11:45 +01:00
resetted_opts = []
2018-04-06 23:51:25 +02:00
context = self.cfgimpl_get_context()
2018-04-09 21:37:49 +02:00
desc = context.cfgimpl_get_description()
2017-11-20 17:01:36 +01:00
values = context.cfgimpl_get_values()
settings = context.cfgimpl_get_settings()
2018-08-01 08:37:58 +02:00
if option_bag is not None:
2018-04-09 21:37:49 +02:00
self.reset_one_option_cache(desc,
values,
settings,
resetted_opts,
2018-08-01 08:37:58 +02:00
option_bag)
2017-07-08 15:59:56 +02:00
else:
2017-12-13 22:15:34 +01:00
values._p_.reset_all_cache()
settings._p_.reset_all_cache()
2013-02-07 16:20:21 +01:00
2017-11-12 14:33:05 +01:00
def cfgimpl_get_home_by_path(self,
path,
2018-08-01 08:37:58 +02:00
config_bag,
fromconsistency=None):
2012-10-05 16:00:07 +02:00
""":returns: tuple (config, name)"""
path = path.split('.')
for step in path[:-1]:
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option = self.cfgimpl_get_description().impl_getchild(step,
config_bag,
self.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
subpath = self._get_subpath(step)
option_bag.set_option(option,
subpath,
None,
config_bag)
if fromconsistency is not None:
option_bag.fromconsistency = fromconsistency
self = self.get_subconfig(step,
option_bag)
2018-09-26 21:30:05 +02:00
assert isinstance(self, SubConfig), _('unknown option {}').format(path[-1])
return self, path[-1]
2012-10-05 16:00:07 +02:00
# ______________________________________________________________________
2018-04-06 23:51:25 +02:00
def cfgimpl_get_context(self):
2018-09-26 21:30:05 +02:00
return self._impl_context()
2013-05-02 11:34:57 +02:00
def cfgimpl_get_description(self):
2018-09-29 18:52:13 +02:00
assert self._impl_descr is not None, _('there is no option description for this config'
' (may be GroupConfig)')
return self._impl_descr
2013-05-02 11:34:57 +02:00
def cfgimpl_get_settings(self):
2018-04-06 23:51:25 +02:00
return self.cfgimpl_get_context()._impl_settings
2013-05-02 11:34:57 +02:00
def cfgimpl_get_values(self):
2018-04-06 23:51:25 +02:00
return self.cfgimpl_get_context()._impl_values
2013-05-02 11:34:57 +02:00
2017-11-12 14:33:05 +01:00
def setattr(self,
value,
2018-08-01 08:37:58 +02:00
option_bag,
2017-11-12 14:33:05 +01:00
_commit=True):
2016-01-25 15:57:34 +01:00
2018-08-01 08:37:58 +02:00
if option_bag.option.impl_is_symlinkoption():
2017-12-30 18:52:35 +01:00
raise ConfigError(_("can't assign to a SymLinkOption"))
2018-08-03 22:56:04 +02:00
context = option_bag.config_bag.context
2018-08-18 07:51:04 +02:00
context.cfgimpl_get_settings().validate_properties(option_bag)
2018-08-03 22:56:04 +02:00
self.cfgimpl_get_description().impl_validate_value(option_bag.option,
value,
self)
return context.cfgimpl_get_values().setvalue(value,
option_bag,
_commit)
2013-05-02 11:34:57 +02:00
2017-11-12 14:33:05 +01:00
def delattr(self,
2018-08-01 08:37:58 +02:00
option_bag):
option = option_bag.option
2018-04-06 23:51:25 +02:00
if option.impl_is_symlinkoption():
2017-12-04 20:05:36 +01:00
raise TypeError(_("can't delete a SymLinkOption"))
2017-11-28 22:42:30 +01:00
values = self.cfgimpl_get_values()
2018-08-01 08:37:58 +02:00
if option_bag.index is not None:
values.reset_slave(option_bag)
2017-11-28 22:42:30 +01:00
else:
2018-08-01 08:37:58 +02:00
values.reset(option_bag)
2014-06-19 23:22:39 +02:00
def _get_subpath(self, name):
if self._impl_path is None:
subpath = name
else:
subpath = self._impl_path + '.' + name
return subpath
def get_subconfig(self,
name,
option_bag):
2018-09-26 21:30:05 +02:00
if option_bag.fromconsistency:
fromconsistency = option_bag.fromconsistency.copy()
else:
fromconsistency = None
2018-08-18 07:51:04 +02:00
self.cfgimpl_get_settings().validate_properties(option_bag)
return SubConfig(option_bag.option,
self._impl_context,
option_bag.config_bag,
option_bag.path,
fromconsistency)
2017-11-12 14:33:05 +01:00
def getattr(self,
name,
2018-08-01 08:37:58 +02:00
option_bag):
2013-05-02 11:34:57 +02:00
"""
attribute notation mechanism for accessing the value of an option
:param name: attribute name
:return: option's value if name is an option name, OptionDescription
otherwise
"""
2018-08-01 08:37:58 +02:00
config_bag = option_bag.config_bag
2013-05-02 11:34:57 +02:00
if '.' in name:
2018-08-01 08:37:58 +02:00
if option_bag.fromconsistency:
fromconsistency = option_bag.fromconsistency.copy()
else:
fromconsistency = None
2017-11-28 22:42:30 +01:00
self, name = self.cfgimpl_get_home_by_path(name,
2018-08-01 08:37:58 +02:00
config_bag,
fromconsistency)
2017-11-28 22:42:30 +01:00
2018-08-01 08:37:58 +02:00
option = option_bag.option
2017-12-04 20:05:36 +01:00
if option.impl_is_symlinkoption():
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
soption_bag.set_option(option.impl_getopt(),
None,
option_bag.index,
config_bag)
soption_bag.ori_option = option
context = self.cfgimpl_get_context()
return context.getattr(soption_bag.path,
soption_bag)
2018-08-18 07:51:04 +02:00
self.cfgimpl_get_settings().validate_properties(option_bag)
2017-11-28 22:42:30 +01:00
if option.impl_is_master_slaves('slave'):
2018-08-01 08:37:58 +02:00
length = self.cfgimpl_get_length_slave(option_bag)
slave_len = self.cfgimpl_get_values()._p_.get_max_length(option_bag.path)
2017-12-19 23:11:45 +01:00
if slave_len > length:
2018-04-06 23:51:25 +02:00
raise SlaveError(_('slave option "{}" has higher length "{}" than the master '
'length "{}"').format(option.impl_get_display_name(),
slave_len,
length,
2018-08-01 08:37:58 +02:00
option_bag.index))
if option.impl_is_master_slaves('slave') and option_bag.index is None:
2017-12-23 10:40:41 +01:00
value = []
for idx in range(length):
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
soption_bag.set_option(option,
option_bag.path,
idx,
config_bag)
soption_bag.fromconsistency = option_bag.fromconsistency.copy()
2017-12-23 10:40:41 +01:00
value.append(self.getattr(name,
2018-08-01 08:37:58 +02:00
soption_bag))
2017-12-23 10:40:41 +01:00
else:
2018-08-01 08:37:58 +02:00
value = self.cfgimpl_get_values().get_cached_value(option_bag)
2018-08-17 23:11:25 +02:00
self.cfgimpl_get_settings().validate_mandatory(value,
option_bag)
2017-12-19 23:11:45 +01:00
return value
2013-04-03 12:20:26 +02:00
2017-11-23 16:56:14 +01:00
def find(self,
2018-04-10 12:33:51 +02:00
bytype,
byname,
byvalue,
2017-12-19 23:11:45 +01:00
config_bag,
2018-04-10 12:33:51 +02:00
_subpath=None,
raise_if_not_found=True,
only_path=undefined,
only_option=undefined):
2013-05-02 11:34:57 +02:00
"""
convenience method for finding an option that lives only in the subtree
:param first: return only one option if True, a list otherwise
:return: find list or an exception if nothing has been found
"""
2018-08-01 08:37:58 +02:00
def _filter_by_value(soption_bag):
2017-11-13 22:45:53 +01:00
try:
value = context.getattr(path,
2018-08-01 08:37:58 +02:00
soption_bag)
2017-11-13 22:45:53 +01:00
except PropertiesOptionError:
return False
if isinstance(value, list):
2016-01-03 21:18:52 +01:00
return byvalue in value
else:
return value == byvalue
2013-05-02 11:34:57 +02:00
2018-04-10 12:33:51 +02:00
found = False
if only_path is not undefined:
2018-09-30 20:32:00 +02:00
options = [only_option]
else:
2018-09-30 20:32:00 +02:00
options = self.cfgimpl_get_description().impl_get_options(bytype,
byname,
config_bag)
context = self.cfgimpl_get_context()
2018-09-30 20:32:00 +02:00
for option in options:
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
2018-09-30 20:32:00 +02:00
path = option.impl_getpath()
2018-08-01 08:37:58 +02:00
option_bag.set_option(option,
path,
None,
config_bag)
if byvalue is not undefined and not _filter_by_value(option_bag):
2013-05-02 11:34:57 +02:00
continue
2018-08-18 07:51:04 +02:00
elif config_bag.properties:
#remove option with propertyerror, ...
2017-11-13 22:45:53 +01:00
try:
if '.' in path:
subconfig, subpath = context.cfgimpl_get_home_by_path(path,
config_bag)
else:
subconfig = self
subpath = path
subconfig.cfgimpl_get_description().impl_getchild(subpath,
config_bag,
subconfig.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
self.cfgimpl_get_settings().validate_properties(option_bag)
2017-11-13 22:45:53 +01:00
except PropertiesOptionError:
continue
2018-04-10 12:33:51 +02:00
found = True
yield path
self._find_return_results(found,
raise_if_not_found)
2017-11-23 16:56:14 +01:00
def _find_return_results(self,
2018-04-10 12:33:51 +02:00
found,
2017-11-23 16:56:14 +01:00
raise_if_not_found):
2018-04-10 12:33:51 +02:00
if not found and raise_if_not_found:
raise AttributeError(_("no option found in config"
" with these criteria"))
2013-05-02 11:34:57 +02:00
2017-11-12 14:33:05 +01:00
def make_dict(self,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-12 14:33:05 +01:00
flatten=False,
_currpath=None,
withoption=None,
withvalue=undefined,
fullpath=False):
"""exports the whole config into a `dict`, for example:
2017-11-20 17:01:36 +01:00
>>> print(cfg.make_dict())
2013-05-23 17:51:50 +02:00
{'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
2013-05-23 17:51:50 +02:00
:param flatten: returns a dict(name=value) instead of a dict(path=value)
::
2017-11-20 17:01:36 +01:00
>>> print(cfg.make_dict(flatten=True))
2013-05-23 17:51:50 +02:00
{'var5': None, 'var4': None, 'var6': None}
2013-05-23 17:51:50 +02:00
:param withoption: returns the options that are present in the very same
`OptionDescription` than the `withoption` itself::
2017-11-20 17:01:36 +01:00
>>> print(cfg.make_dict(withoption='var1'))
2013-08-20 12:08:02 +02:00
{'od2.var4': None, 'od2.var5': None,
'od2.var6': None,
'od2.var1': u'value',
'od1.var1': None,
'od1.var3': None,
'od1.var2': None}
2013-05-23 17:51:50 +02:00
:param withvalue: returns the options that have the value `withvalue`
::
2017-11-20 17:01:36 +01:00
>>> print(c.make_dict(withoption='var1',
withvalue=u'value'))
2013-08-20 12:08:02 +02:00
{'od2.var4': None,
'od2.var5': None,
'od2.var6': None,
2013-05-23 17:51:50 +02:00
'od2.var1': u'value'}
2013-05-23 14:55:52 +02:00
:returns: dict of Option's name (or path) and values
"""
2018-08-19 15:19:42 +02:00
pathsvalues = {}
2013-04-04 11:24:00 +02:00
if _currpath is None:
_currpath = []
if withoption is None and withvalue is not undefined:
2013-05-02 11:34:57 +02:00
raise ValueError(_("make_dict can't filtering with value without "
"option"))
2018-04-06 23:51:25 +02:00
context = self.cfgimpl_get_context()
2018-08-19 15:19:42 +02:00
self._make_dict(context,
config_bag,
flatten,
_currpath,
withoption,
withvalue,
fullpath,
pathsvalues)
return pathsvalues
def _make_dict(self,
context,
config_bag,
flatten,
_currpath,
withoption,
withvalue,
fullpath,
pathsvalues):
2013-04-04 11:24:00 +02:00
if withoption is not None:
2018-04-11 16:36:15 +02:00
mypath = self.cfgimpl_get_path()
2018-04-10 12:33:51 +02:00
for path in context.find(bytype=None,
byname=withoption,
byvalue=withvalue,
_subpath=self.cfgimpl_get_path(False),
config_bag=config_bag):
2013-04-04 11:24:00 +02:00
path = '.'.join(path.split('.')[:-1])
if '.' in path:
subconfig, subpath = context.cfgimpl_get_home_by_path(path,
config_bag)
else:
subconfig = context
subpath = path
opt = subconfig.cfgimpl_get_description().impl_getchild(subpath,
config_bag,
subconfig.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
soption_bag = OptionBag()
soption_bag.set_option(opt,
path,
None,
config_bag)
2013-04-04 11:24:00 +02:00
if mypath is not None:
if mypath == path:
withoption = None
withvalue = undefined
2013-04-04 11:24:00 +02:00
break
else:
tmypath = mypath + '.'
2018-09-26 21:30:05 +02:00
assert path.startswith(tmypath), _('unexpected path "{0}", '
'should start with "{1}"'
'').format(path, mypath)
2013-04-04 11:24:00 +02:00
path = path[len(tmypath):]
2017-12-19 23:11:45 +01:00
self._make_sub_dict(path,
2017-11-23 16:56:14 +01:00
pathsvalues,
_currpath,
flatten,
2018-08-01 08:37:58 +02:00
soption_bag,
2018-08-19 15:19:42 +02:00
fullpath,
context,
withvalue)
2013-04-04 11:24:00 +02:00
#withoption can be set to None below !
if withoption is None:
2018-03-24 22:37:48 +01:00
for opt in self.cfgimpl_get_description().impl_getchildren(config_bag, context):
2018-08-01 08:37:58 +02:00
name = opt.impl_getname()
path = self._get_subpath(name)
soption_bag = OptionBag()
soption_bag.set_option(opt,
path,
None,
config_bag)
self._make_sub_dict(name,
2017-11-23 16:56:14 +01:00
pathsvalues,
_currpath,
flatten,
2018-08-01 08:37:58 +02:00
soption_bag,
2018-08-19 15:19:42 +02:00
fullpath,
context,
withvalue)
2013-04-04 11:24:00 +02:00
return pathsvalues
2017-11-23 16:56:14 +01:00
def _make_sub_dict(self,
2017-12-13 22:15:34 +01:00
name,
2017-11-23 16:56:14 +01:00
pathsvalues,
_currpath,
flatten,
2018-08-01 08:37:58 +02:00
option_bag,
2018-08-19 15:19:42 +02:00
fullpath,
context,
withvalue):
option = option_bag.option
if option.impl_is_optiondescription():
try:
self.cfgimpl_get_settings().validate_properties(option_bag)
subconfig = SubConfig(option_bag.option,
self._impl_context,
option_bag.config_bag,
option_bag.path)
subconfig._make_dict(context,
option_bag.config_bag,
flatten,
_currpath + [name],
None,
withvalue,
fullpath,
pathsvalues)
except PropertiesOptionError:
pass
else:
if option.impl_is_master_slaves('slave'):
ret = []
try:
self.cfgimpl_get_settings().validate_properties(option_bag)
length = self.cfgimpl_get_length_slave(option_bag)
2018-08-19 15:19:42 +02:00
except PropertiesOptionError:
return
if length:
for idx in range(length):
soption_bag = OptionBag()
soption_bag.set_option(option,
option_bag.path,
idx,
option_bag.config_bag)
try:
ret.append(self.getattr(name,
soption_bag))
2018-08-19 15:19:42 +02:00
except PropertiesOptionError as err:
ret.append(err)
else:
try:
ret = self.getattr(name,
option_bag)
2018-08-19 15:19:42 +02:00
except PropertiesOptionError:
return
if flatten:
name_ = option.impl_getname()
elif fullpath:
name_ = self._get_subpath(name)
2017-11-28 22:42:30 +01:00
else:
2018-08-19 15:19:42 +02:00
name_ = '.'.join(_currpath + [name])
pathsvalues[name_] = ret
2013-04-03 12:20:26 +02:00
2017-11-23 16:56:14 +01:00
def cfgimpl_get_path(self,
dyn=True):
2013-05-02 11:34:57 +02:00
descr = self.cfgimpl_get_description()
2014-06-19 23:22:39 +02:00
if not dyn and descr.impl_is_dynoptiondescription():
2018-09-06 23:16:17 +02:00
return descr.impl_getopt().impl_getpath()
2014-06-19 23:22:39 +02:00
return self._impl_path
2013-04-03 12:20:26 +02:00
2013-09-30 16:22:08 +02:00
class _CommonConfig(SubConfig):
2018-08-14 22:15:40 +02:00
"abstract base class for the Config, KernelGroupConfig and the KernelMetaConfig"
2017-12-19 23:11:45 +01:00
__slots__ = ('_impl_values',
'_impl_settings',
2018-09-29 18:52:13 +02:00
'_impl_meta',
'impl_type')
2013-04-03 12:20:26 +02:00
2018-04-06 23:51:25 +02:00
def _impl_build_all_caches(self):
2015-05-03 09:56:03 +02:00
descr = self.cfgimpl_get_description()
if not descr.impl_already_build_caches():
2017-09-17 15:55:32 +02:00
descr._build_cache_option()
descr._build_cache(self)
2018-04-06 23:51:25 +02:00
descr.impl_build_force_store_values(self)
2014-06-19 23:22:39 +02:00
def cfgimpl_get_path(self, dyn=True):
2013-04-04 11:24:00 +02:00
return None
2013-04-03 12:20:26 +02:00
2013-05-02 11:34:57 +02:00
def cfgimpl_get_meta(self):
2013-09-30 16:22:08 +02:00
if self._impl_meta is not None:
return self._impl_meta()
# information
def impl_set_information(self, key, value):
"""updates the information's attribute
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self._impl_values.set_information(key, value)
def impl_get_information(self, key, default=undefined):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
return self._impl_values.get_information(key, default)
def impl_del_information(self, key, raises=True):
self._impl_values.del_information(key, raises)
2018-10-07 10:55:52 +02:00
def impl_list_information(self):
return self._impl_values.list_information()
2013-09-22 20:57:52 +02:00
def __getstate__(self):
2017-07-21 18:46:11 +02:00
raise NotImplementedError()
2012-10-12 11:35:07 +02:00
2017-11-12 14:33:05 +01:00
def _gen_fake_values(self):
2018-08-14 22:15:40 +02:00
fake_config = KernelConfig(self._impl_descr,
persistent=False,
force_values=get_default_values_storages(),
force_settings=self.cfgimpl_get_settings())
2018-06-09 18:59:40 +02:00
fake_config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation())
2015-04-18 22:53:45 +02:00
return fake_config
2017-11-23 16:56:14 +01:00
def duplicate(self,
session_id=None,
force_values=None,
2018-06-09 18:59:40 +02:00
force_settings=None,
2018-09-05 20:22:16 +02:00
storage=None,
2018-09-12 17:26:12 +02:00
persistent=False,
2018-09-13 17:00:52 +02:00
metaconfig_prefix=None,
child=None,
2018-09-05 20:22:16 +02:00
deep=False):
2018-09-26 21:30:05 +02:00
assert isinstance(self, (KernelConfig, KernelMetaConfig)), _('cannot duplicate {}').format(self.__class__.__name__)
2018-09-05 20:22:16 +02:00
if isinstance(self, KernelConfig):
2018-09-13 17:00:52 +02:00
duplicated_config = KernelConfig(self._impl_descr,
_duplicate=True,
session_id=session_id,
force_values=force_values,
force_settings=force_settings,
persistent=persistent,
storage=storage)
2018-09-05 20:22:16 +02:00
elif isinstance(self, KernelMetaConfig):
2018-09-13 17:00:52 +02:00
if session_id is None and metaconfig_prefix is not None:
session_id = metaconfig_prefix + self.impl_getname()
duplicated_config = KernelMetaConfig([],
_duplicate=True,
optiondescription=self._impl_descr,
session_id=session_id,
persistent=persistent,
storage=storage)
duplicated_config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation())
duplicated_config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings(
)._p_.exportation())
2018-09-13 17:00:52 +02:00
duplicated_config.cfgimpl_get_settings()._pp_.importation(self.cfgimpl_get_settings(
)._pp_.exportation())
2018-09-13 17:00:52 +02:00
if child is not None:
duplicated_config._impl_children.append(child)
child._impl_meta = weakref.ref(duplicated_config)
2018-09-05 20:22:16 +02:00
if self._impl_meta:
if deep:
2018-09-13 17:00:52 +02:00
duplicated_config = self._impl_meta().duplicate(deep=deep,
storage=storage,
metaconfig_prefix=metaconfig_prefix,
child=duplicated_config,
persistent=persistent)
2018-09-05 20:22:16 +02:00
else:
2018-09-13 17:00:52 +02:00
duplicated_config._impl_meta = self._impl_meta
self._impl_meta()._impl_children.append(duplicated_config)
return duplicated_config
2015-07-24 17:54:10 +02:00
2013-09-30 16:22:08 +02:00
# ____________________________________________________________
2018-08-14 22:15:40 +02:00
class KernelConfig(_CommonConfig):
2013-09-30 16:22:08 +02:00
"main configuration management entry"
2018-09-29 18:52:13 +02:00
__slots__ = ('__weakref__',
'_impl_name')
impl_type = 'config'
2013-09-30 16:22:08 +02:00
def __init__(self,
descr,
session_id=None,
persistent=False,
force_values=None,
force_settings=None,
2018-06-09 18:59:40 +02:00
_duplicate=False,
storage=None):
2013-09-30 16:22:08 +02:00
""" Configuration option management master class
:param descr: describes the configuration schema
:type descr: an instance of ``option.OptionDescription``
:param context: the current root config
:type context: `Config`
:param session_id: session ID is import with persistent Config to
retrieve good session
:type session_id: `str`
:param persistent: if persistent, don't delete storage when leaving
:type persistent: `boolean`
"""
2017-12-19 23:11:45 +01:00
self._impl_meta = None
2018-01-01 21:32:39 +01:00
if isinstance(descr, MasterSlaves):
2018-04-11 18:32:13 +02:00
raise ConfigError(_('cannot set masterslaves object has root optiondescription'))
2018-04-12 23:04:33 +02:00
if isinstance(descr, DynOptionDescription):
raise ConfigError(_('cannot set dynoptiondescription object has root optiondescription'))
if force_settings is not None and force_values is not None:
2017-07-16 10:57:43 +02:00
if isinstance(force_settings, tuple):
2018-09-07 06:14:52 +02:00
self._impl_settings = Settings(force_settings[0],
force_settings[1])
2017-07-16 10:57:43 +02:00
else:
self._impl_settings = force_settings
2018-09-07 06:14:52 +02:00
self._impl_values = Values(force_values)
else:
properties, permissives, values, session_id = get_storages(self,
session_id,
2018-06-09 18:59:40 +02:00
persistent,
storage=storage)
if not valid_name(session_id):
raise ValueError(_("invalid session ID: {0} for config").format(session_id))
2018-09-07 06:14:52 +02:00
self._impl_settings = Settings(properties,
permissives)
2018-09-07 06:14:52 +02:00
self._impl_values = Values(values)
2018-09-05 20:22:16 +02:00
super().__init__(descr,
weakref.ref(self),
ConfigBag(self),
None)
if None in [force_settings, force_values]:
2018-04-06 23:51:25 +02:00
self._impl_build_all_caches()
self._impl_name = session_id
2013-09-30 16:22:08 +02:00
def impl_getname(self):
return self._impl_name
2018-08-14 22:15:40 +02:00
class KernelGroupConfig(_CommonConfig):
__slots__ = ('__weakref__',
'_impl_children',
'_impl_name')
2018-09-29 18:52:13 +02:00
impl_type = 'group'
2012-10-12 11:35:07 +02:00
def __init__(self,
children,
session_id=None,
_descr=None):
2018-09-26 21:30:05 +02:00
assert isinstance(children, list), _("groupconfig's children must be a list")
names = []
for child in children:
2018-09-26 21:30:05 +02:00
assert isinstance(child,
_CommonConfig), _("groupconfig's children must be Config, MetaConfig or GroupConfig")
name_ = child._impl_name
names.append(name_)
if len(names) != len(set(names)):
2018-04-06 23:51:25 +02:00
for idx in range(1, len(names) + 1):
name = names.pop(0)
if name in names:
raise ConflictError(_('config name must be uniq in '
2018-04-11 16:36:15 +02:00
'groupconfig for "{0}"').format(name))
2013-09-17 09:02:10 +02:00
self._impl_children = children
2018-01-03 21:07:51 +01:00
self._impl_meta = None
session_id = gen_storage_id(session_id, self)
assert valid_name(session_id), _("invalid session ID: {0} for config").format(session_id)
2018-09-05 20:22:16 +02:00
super().__init__(_descr,
weakref.ref(self),
ConfigBag(self),
None)
self._impl_name = session_id
2013-09-17 09:02:10 +02:00
def cfgimpl_get_children(self):
return self._impl_children
2013-08-20 12:08:02 +02:00
def cfgimpl_reset_cache(self,
2018-08-01 08:37:58 +02:00
option_bag,
2017-12-19 23:11:45 +01:00
resetted_opts=None):
if resetted_opts is None:
resetted_opts = []
2018-08-14 22:15:40 +02:00
if isinstance(self, KernelMetaConfig):
2018-09-05 20:22:16 +02:00
super().cfgimpl_reset_cache(option_bag,
resetted_opts=copy(resetted_opts))
2013-09-17 09:02:10 +02:00
for child in self._impl_children:
2018-08-01 08:37:58 +02:00
child.cfgimpl_reset_cache(option_bag,
resetted_opts=copy(resetted_opts))
2013-02-22 11:09:17 +01:00
2018-01-03 21:07:51 +01:00
def set_value(self,
path,
index,
value,
config_bag,
only_config=False,
_commit=True):
2018-08-14 22:15:40 +02:00
"""Setattr not in current KernelGroupConfig, but in each children
2013-09-30 16:22:08 +02:00
"""
ret = []
2018-09-29 21:39:58 +02:00
if self.impl_type == 'group':
2018-09-29 18:52:13 +02:00
commit = True
else:
#Commit only one time
commit = False
2013-09-17 09:02:10 +02:00
for child in self._impl_children:
2018-08-03 22:56:04 +02:00
cconfig_bag = config_bag.copy()
cconfig_bag.context = child
2018-01-03 21:07:51 +01:00
try:
2018-08-14 22:15:40 +02:00
if isinstance(child, KernelGroupConfig):
2018-01-03 21:07:51 +01:00
ret.extend(child.set_value(path,
index,
value,
2018-08-03 22:56:04 +02:00
cconfig_bag,
2018-01-03 21:07:51 +01:00
only_config=only_config,
2018-09-29 18:52:13 +02:00
_commit=commit))
2018-01-03 21:07:51 +01:00
else:
2018-08-01 08:37:58 +02:00
subconfig, name = child.cfgimpl_get_home_by_path(path,
2018-08-03 22:56:04 +02:00
cconfig_bag)
2018-08-01 08:37:58 +02:00
option = subconfig.cfgimpl_get_description().impl_getchild(name,
2018-08-03 22:56:04 +02:00
cconfig_bag,
child.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(option,
path,
index,
2018-08-03 22:56:04 +02:00
cconfig_bag)
2018-08-01 08:37:58 +02:00
child.setattr(value,
option_bag,
2018-09-29 18:52:13 +02:00
_commit=commit)
2018-01-03 23:33:35 +01:00
except PropertiesOptionError as err:
2018-08-01 08:37:58 +02:00
ret.append(PropertiesOptionError(err._option_bag,
err.proptype,
err._settings,
err._opt_type,
err._requires,
err._name,
err._orig_opt))
2018-01-03 23:33:35 +01:00
except (ValueError, SlaveError) as err:
2018-01-03 21:07:51 +01:00
ret.append(err)
2018-09-29 21:39:58 +02:00
if _commit and self.impl_type != 'group':
2018-09-29 18:52:13 +02:00
self.cfgimpl_get_values()._p_.commit()
return ret
2017-07-16 23:11:12 +02:00
def find_group(self,
config_bag,
byname=None,
bypath=undefined,
byoption=undefined,
byvalue=undefined,
raise_if_not_found=True,
_sub=False):
2018-08-14 22:15:40 +02:00
"""Find first not in current KernelGroupConfig, but in each children
2013-09-30 16:22:08 +02:00
"""
2018-08-14 22:15:40 +02:00
#if KernelMetaConfig, all children have same OptionDescription in context
#so search only one time the option for all children
if bypath is undefined and byname is not None and \
isinstance(self,
2018-08-14 22:15:40 +02:00
KernelMetaConfig):
2018-04-10 12:33:51 +02:00
bypath = next(self.find(bytype=None,
byvalue=undefined,
byname=byname,
config_bag=config_bag,
raise_if_not_found=raise_if_not_found))
byname = None
byoption = self.cfgimpl_get_description().impl_get_opt_by_path(bypath,
config_bag)
2018-04-10 12:33:51 +02:00
ret = []
2013-09-17 09:02:10 +02:00
for child in self._impl_children:
2018-08-14 22:15:40 +02:00
if isinstance(child, KernelGroupConfig):
ret.extend(child.find_group(byname=byname,
bypath=bypath,
byoption=byoption,
byvalue=byvalue,
config_bag=config_bag,
raise_if_not_found=False,
_sub=True))
2018-04-10 12:33:51 +02:00
else:
try:
2018-09-06 23:06:56 +02:00
cconfig_bag = config_bag.copy()
cconfig_bag.context = child
2018-04-10 12:33:51 +02:00
next(child.find(None,
byname,
byvalue,
2018-09-06 23:06:56 +02:00
config_bag=cconfig_bag,
2018-04-10 12:33:51 +02:00
raise_if_not_found=False,
only_path=bypath,
only_option=byoption))
ret.append(child)
except StopIteration:
pass
if not _sub:
2018-04-10 12:33:51 +02:00
self._find_return_results(ret != [],
raise_if_not_found)
return ret
2013-05-02 11:34:57 +02:00
2018-01-03 21:07:51 +01:00
def impl_getname(self):
return self._impl_name
2017-07-21 18:03:34 +02:00
2018-09-29 18:52:13 +02:00
def reset(self,
path):
for child in self._impl_children:
config_bag = ConfigBag(child)
config_bag.remove_validation()
subconfig, name = child.cfgimpl_get_home_by_path(path,
config_bag)
option = subconfig.cfgimpl_get_description().impl_getchild(name,
config_bag,
subconfig.cfgimpl_get_path())
option_bag = OptionBag()
option_bag.set_option(option,
path,
option,
config_bag)
option_bag.config_bag.context = child
child.cfgimpl_get_values().reset(option_bag,
_commit=True)
2017-11-23 16:56:14 +01:00
def getconfig(self,
name):
for child in self._impl_children:
if name == child.impl_getname():
return child
raise ConfigError(_('unknown config "{}"').format(name))
2014-11-10 23:15:08 +01:00
2018-08-14 22:15:40 +02:00
class KernelMetaConfig(KernelGroupConfig):
2013-09-30 16:22:08 +02:00
__slots__ = tuple()
2018-09-29 18:52:13 +02:00
impl_type = 'meta'
2013-09-30 16:22:08 +02:00
def __init__(self,
children,
session_id=None,
persistent=False,
2018-09-05 20:22:16 +02:00
optiondescription=None,
storage=None,
_duplicate=False):
2013-09-30 16:22:08 +02:00
descr = None
2017-07-11 22:31:58 +02:00
if optiondescription is not None:
2018-09-05 20:22:16 +02:00
if not _duplicate:
new_children = []
for child_session_id in children:
2018-09-26 21:30:05 +02:00
assert isinstance(child_session_id, str), _('MetaConfig with optiondescription'
' must have string has child, '
'not {}').format(child_session_id)
2018-09-05 20:22:16 +02:00
new_children.append(KernelConfig(optiondescription,
persistent=persistent,
session_id=child_session_id))
children = new_children
descr = optiondescription
2013-09-30 16:22:08 +02:00
for child in children:
if not isinstance(child, _CommonConfig):
2018-08-14 22:15:40 +02:00
try:
child = child._config
except:
raise TypeError(_("metaconfig's children "
"should be config, not {0}"
).format(type(child)))
2013-09-30 16:22:08 +02:00
if child.cfgimpl_get_meta() is not None:
raise ValueError(_("child has already a metaconfig's"))
if descr is None:
descr = child.cfgimpl_get_description()
elif not descr is child.cfgimpl_get_description():
raise ValueError(_('all config in metaconfig must '
'have the same optiondescription'))
child._impl_meta = weakref.ref(self)
properties, permissives, values, session_id = get_storages(self,
session_id,
persistent,
storage=storage)
self._impl_settings = Settings(properties,
permissives)
self._impl_values = Values(values)
2018-09-05 20:22:16 +02:00
super().__init__(children,
session_id=session_id,
2018-09-05 20:22:16 +02:00
_descr=descr)
self._impl_build_all_caches()
def set_value(self,
path,
2018-01-03 21:07:51 +01:00
index,
value,
2018-01-03 21:07:51 +01:00
config_bag,
force_default=False,
force_dont_change_value=False,
force_default_if_same=False,
only_config=False,
_commit=True):
"""only_config: could be set if you want modify value in all Config included in
2018-08-14 22:15:40 +02:00
this KernelMetaConfig
"""
if only_config:
if force_default or force_default_if_same or force_dont_change_value:
raise ValueError(_('force_default, force_default_if_same or '
'force_dont_change_value cannot be set with'
' only_config'))
2018-09-05 20:22:16 +02:00
return super().set_value(path,
index,
value,
config_bag,
only_config=only_config,
_commit=_commit)
ret = []
if force_default or force_default_if_same or force_dont_change_value:
if force_default and force_dont_change_value:
raise ValueError(_('force_default and force_dont_change_value'
' cannot be set together'))
opt = self.cfgimpl_get_description().impl_get_opt_by_path(path,
config_bag)
for child in self._impl_children:
2018-08-03 22:56:04 +02:00
cconfig_bag = config_bag.copy()
cconfig_bag.context = child
2018-08-01 08:37:58 +02:00
subconfig, name = child.cfgimpl_get_home_by_path(path,
2018-08-03 22:56:04 +02:00
cconfig_bag)
2018-08-01 08:37:58 +02:00
option = subconfig.cfgimpl_get_description().impl_getchild(name,
2018-08-03 22:56:04 +02:00
cconfig_bag,
child.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(option,
path,
index,
2018-08-03 22:56:04 +02:00
cconfig_bag)
2018-01-03 21:07:51 +01:00
if force_default_if_same:
if not child.cfgimpl_get_values()._p_.hasvalue(path):
child_value = undefined
else:
2018-08-01 08:37:58 +02:00
child_value = child.getattr(name,
option_bag)
2018-01-03 21:07:51 +01:00
if force_default or (force_default_if_same and value == child_value):
2018-08-01 08:37:58 +02:00
child.cfgimpl_get_values().reset(option_bag,
2018-01-03 21:07:51 +01:00
_commit=False)
continue
if force_dont_change_value:
2018-01-03 21:07:51 +01:00
try:
2018-08-01 08:37:58 +02:00
child_value = child.getattr(name,
option_bag)
2018-01-03 21:07:51 +01:00
if value != child_value:
2018-08-01 08:37:58 +02:00
child.setattr(child_value,
option_bag,
2018-01-03 21:07:51 +01:00
_commit=False)
except (PropertiesOptionError, ValueError, SlaveError) as err:
ret.append(err)
try:
2018-08-01 08:37:58 +02:00
subconfig, name = self.cfgimpl_get_home_by_path(path,
config_bag)
option = subconfig.cfgimpl_get_description().impl_getchild(name,
config_bag,
self.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(option,
path,
index,
config_bag)
self.setattr(value,
option_bag,
_commit=False)
2018-01-03 21:07:51 +01:00
except (PropertiesOptionError, ValueError, SlaveError) as err:
ret.append(err)
return ret
2017-07-08 15:59:56 +02:00
2018-08-01 08:37:58 +02:00
def reset(self,
path,
config_bag):
2018-08-02 22:35:40 +02:00
rconfig_bag = config_bag.copy()
2018-08-17 23:11:25 +02:00
rconfig_bag.remove_validation()
2018-08-01 08:37:58 +02:00
subconfig, name = self.cfgimpl_get_home_by_path(path,
config_bag)
option = subconfig.cfgimpl_get_description().impl_getchild(name,
config_bag,
2018-09-29 18:52:13 +02:00
subconfig.cfgimpl_get_path())
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(option,
path,
option,
rconfig_bag)
for child in self._impl_children:
2018-09-06 23:06:56 +02:00
option_bag.config_bag.context = child
2018-09-29 18:52:13 +02:00
child.cfgimpl_get_values().reset(option_bag)
def new_config(self,
session_id,
2018-09-13 09:17:38 +02:00
type_='config',
persistent=False):
if session_id in [child._impl_name for child in self._impl_children]:
2017-07-21 18:03:34 +02:00
raise ConflictError(_('config name must be uniq in '
2018-09-13 09:17:38 +02:00
'groupconfig for {0}').format(session_id))
2018-09-26 21:30:05 +02:00
assert type_ in ('config', 'metaconfig'), _('unknown type {}').format(type_)
2018-09-13 09:17:38 +02:00
if type_ == 'config':
config = KernelConfig(self._impl_descr,
session_id=session_id,
persistent=persistent)
elif type_ == 'metaconfig':
config = KernelMetaConfig([],
optiondescription=self._impl_descr,
session_id=session_id,
persistent=persistent)
# Copy context properties/permissives
config.cfgimpl_get_settings().set_context_properties(self.cfgimpl_get_settings().get_context_properties(), config)
config.cfgimpl_get_settings().set_context_permissives(self.cfgimpl_get_settings().get_context_permissives())
2017-07-21 18:03:34 +02:00
config._impl_meta = weakref.ref(self)
self._impl_children.append(config)
return config
2018-09-13 07:00:12 +02:00
def pop_config(self,
session_id):
2018-09-26 21:30:05 +02:00
for idx, child in enumerate(self._impl_children):
2018-09-13 07:00:12 +02:00
if session_id == child._impl_name:
return self._impl_children.pop(idx)
raise ConfigError(_('cannot find the config {}').format(session_id))