tiramisu/tiramisu/value.py

796 lines
32 KiB
Python
Raw Normal View History

2013-02-07 16:20:21 +01:00
# -*- coding: utf-8 -*-
"takes care of the option's values and multi values"
2018-01-26 07:33:47 +01:00
# Copyright (C) 2013-2018 Team tiramisu (see AUTHORS for all contributors)
2013-02-07 16:20:21 +01:00
#
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-02-07 16:20:21 +01:00
#
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-02-07 16:20:21 +01:00
#
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/>.
2013-02-07 16:20:21 +01:00
# ____________________________________________________________
from time import time
import weakref
2017-12-07 21:42:04 +01:00
from .error import ConfigError, PropertiesOptionError
2017-11-23 16:56:14 +01:00
from .setting import owners, expires_time, undefined, forbidden_owners
from .autolib import carry_out_calculation
from .i18n import _
2013-02-07 16:20:21 +01:00
2013-04-03 12:20:26 +02:00
2013-08-20 09:47:12 +02:00
class Values(object):
2013-05-23 17:51:50 +02:00
"""The `Config`'s root is indeed in charge of the `Option()`'s values,
but the values are physicaly located here, in `Values`, wich is also
responsible of a caching utility.
2013-05-23 14:55:52 +02:00
"""
2017-12-07 21:42:04 +01:00
__slots__ = ('context',
'_p_',
'__weakref__')
2013-04-03 12:20:26 +02:00
2017-12-07 21:42:04 +01:00
def __init__(self,
context,
storage):
2013-02-21 17:07:00 +01:00
"""
Initializes the values's dict.
2013-04-03 12:20:26 +02:00
:param context: the context is the home config's values
2017-12-07 21:42:04 +01:00
:param storage: where values or owners are stored
2013-05-23 14:55:52 +02:00
2013-02-21 17:07:00 +01:00
"""
self.context = weakref.ref(context)
2017-12-07 21:42:04 +01:00
# store the storage
self._p_ = storage
2013-08-20 09:47:12 +02:00
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# get context
def _getcontext(self):
2017-12-07 21:42:04 +01:00
"""context is a weakref so context could be None, we need to test it
context is None only if all reference to `Config` object is deleted
(for example we delete a `Config` and we manipulate a reference to
old `SubConfig`, `Values`, `Multi` or `Settings`)
"""
context = self.context()
2017-02-11 17:22:50 +01:00
if context is None:
raise ConfigError(_('the context does not exist anymore'))
return context
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# get value
def get_cached_value(self,
2017-12-07 21:42:04 +01:00
path,
2017-12-19 23:11:45 +01:00
index,
config_bag):
2017-12-07 21:42:04 +01:00
"""get value directly in cache if set
otherwise calculated value and set it in cache
:param opt: the `Option` that we want to get value
:param path: the path of the `Option`
:param validate: the value must be valid
:param force_permissive: force permissive when check properties
:param setting_properties: global properties
:param self_properties: properties for this `Option`
:param index: index for a slave `Option`
:param display_warnings: display warnings or not
:returns: value
"""
2017-11-20 17:01:36 +01:00
ntime = None
2017-12-07 21:42:04 +01:00
# try to retrive value in cache
2017-12-19 23:11:45 +01:00
setting_properties = config_bag.setting_properties
is_cached = False
2017-12-13 22:15:34 +01:00
if setting_properties and 'cache' in setting_properties and \
self._p_.hascache(path,
index):
2017-11-20 17:01:36 +01:00
if 'expire' in setting_properties:
ntime = int(time())
is_cached, value = self._p_.getcache(path,
ntime,
2017-12-13 22:15:34 +01:00
index)
2017-12-07 21:42:04 +01:00
2017-12-19 23:11:45 +01:00
if not is_cached:
# no cached value so get value
value = self.getvalue(path,
2017-12-07 21:42:04 +01:00
index,
2017-12-19 23:11:45 +01:00
config_bag)
#FIXME suboptimal ...
# validate value
if config_bag.validate:
context = self._getcontext()
opt = config_bag.option
opt.impl_validate(value,
context=context,
force_index=index,
check_error=True,
config_bag=config_bag)
if config_bag.display_warnings:
opt.impl_validate(value,
context=context,
force_index=index,
check_error=False,
config_bag=config_bag)
2017-12-07 21:42:04 +01:00
# store value in cache
2017-12-19 23:11:45 +01:00
if not is_cached and \
2017-12-23 20:33:36 +01:00
setting_properties and 'cache' in setting_properties:
2017-11-20 17:01:36 +01:00
if 'expire' in setting_properties:
if ntime is None:
ntime = int(time())
ntime = ntime + expires_time
2017-12-13 22:15:34 +01:00
self._p_.setcache(path, value, ntime, index)
2017-12-07 21:42:04 +01:00
# and return it
return value
2017-11-20 17:01:36 +01:00
def getvalue(self,
path,
index,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-20 17:01:36 +01:00
"""actually retrieves the value
2017-12-07 21:42:04 +01:00
:param path: the path of the `Option`
:param index: index for a slave `Option`
:returns: value
2017-11-20 17:01:36 +01:00
"""
2017-12-07 21:42:04 +01:00
# get owner and value from store
# index allowed only for slave
2017-12-19 23:11:45 +01:00
opt = config_bag.option
2017-12-07 21:42:04 +01:00
is_slave = opt.impl_is_master_slaves('slave')
if index is None or not is_slave:
2017-11-20 17:01:36 +01:00
_index = None
else:
_index = index
owner, value = self._p_.getowner(path,
owners.default,
index=_index,
with_value=True)
2017-12-07 21:42:04 +01:00
if owner != owners.default:
# if a value is store in storage, check if not frozen + force_default_on_freeze
# if frozen + force_default_on_freeze => force default value
2017-12-19 23:11:45 +01:00
self_properties = config_bag.properties
if self_properties is None:
2017-12-07 21:42:04 +01:00
settings = self._getcontext().cfgimpl_get_settings()
2017-12-19 23:11:45 +01:00
self_properties = settings.getproperties(path,
index,
config_bag)
config_bag.properties = self_properties
2017-12-07 21:42:04 +01:00
if not ('frozen' in self_properties and \
'force_default_on_freeze' in self_properties):
if index is not None and not is_slave:
if len(value) > index:
return value[index]
#value is smaller than expected
#so return default value
else:
return value
2017-12-19 23:11:45 +01:00
return self._getdefaultvalue(path,
2017-11-20 17:01:36 +01:00
index,
2017-12-19 23:11:45 +01:00
config_bag)
2017-11-20 17:01:36 +01:00
2017-11-12 14:33:05 +01:00
def getdefaultvalue(self,
path,
2017-12-19 23:11:45 +01:00
index,
config_bag):
2017-11-12 14:33:05 +01:00
"""get default value:
- get meta config value or
- get calculated value or
- get default value
:param opt: the `option.Option()` object
:param path: path for `option.Option()` object
:type path: str
:param index: index of a multi/submulti
:type index: int
:returns: default value
"""
2017-12-19 23:11:45 +01:00
return self._getdefaultvalue(path,
2017-11-12 14:33:05 +01:00
index,
2017-12-19 23:11:45 +01:00
config_bag)
2017-11-12 14:33:05 +01:00
def _getdefaultvalue(self,
path,
index,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-23 16:56:14 +01:00
context = self._getcontext()
2017-12-19 23:11:45 +01:00
opt = config_bag.option
2017-11-12 14:33:05 +01:00
def _reset_cache():
# calculated value could be a new value, so reset cache
2017-11-23 16:56:14 +01:00
context.cfgimpl_reset_cache(opt=opt,
path=path)
2017-12-05 21:49:19 +01:00
if opt.impl_is_master_slaves('slave'):
index_ = index
else:
index_ = None
2017-12-19 23:11:45 +01:00
if self._is_meta(path,
2017-12-05 21:49:19 +01:00
index_,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-23 16:56:14 +01:00
meta = context.cfgimpl_get_meta()
# retrieved value from meta config
try:
value = meta.getattr(path,
2017-12-19 23:11:45 +01:00
index,
config_bag)
2017-11-23 16:56:14 +01:00
except PropertiesOptionError:
# if properties error, return an other default value
# unexpected error, should not happened
pass
else:
return value
2017-11-12 14:33:05 +01:00
2017-07-16 23:11:12 +02:00
if opt.impl_has_callback():
2017-11-12 14:33:05 +01:00
# if value has callback, calculate value
2017-07-16 23:11:12 +02:00
callback, callback_params = opt.impl_get_callback()
2017-11-12 14:33:05 +01:00
value = carry_out_calculation(opt,
2017-11-23 16:56:14 +01:00
context=context,
2017-07-16 23:11:12 +02:00
callback=callback,
callback_params=callback_params,
2017-11-12 14:33:05 +01:00
index=index,
2017-12-19 23:11:45 +01:00
config_bag=config_bag)
2017-07-16 23:11:12 +02:00
if isinstance(value, list) and index is not None:
2017-11-12 14:33:05 +01:00
# if value is a list and index is set
if opt.impl_is_submulti() and (value == [] or not isinstance(value[0], list)):
# return value only if it's a submulti and not a list of list
_reset_cache()
2017-07-16 23:11:12 +02:00
return value
2017-11-12 14:33:05 +01:00
if len(value) > index:
# return the value for specified index if found
_reset_cache()
2017-07-16 23:11:12 +02:00
return value[index]
2017-11-12 14:33:05 +01:00
# there is no calculate value for this index,
# so return an other default value
elif isinstance(value, list):
# value is a list, but no index specified
_reset_cache()
2017-12-30 18:31:56 +01:00
if opt.impl_is_submulti() and (value != [] and not isinstance(value[0], list)):
2017-11-12 14:33:05 +01:00
# if submulti, return a list of value
return [value]
# otherwise just return the value
return value
elif index is not None:
# if not list but with index
_reset_cache()
if opt.impl_is_submulti():
# if submulti, return a list of value
return [value]
# otherwise just return the value
return value
2017-07-16 23:11:12 +02:00
else:
2017-11-12 14:33:05 +01:00
_reset_cache()
# not a list or index is None
if opt.impl_is_submulti():
# return a list of list for a submulti
return [[value]]
elif opt.impl_is_multi():
# return a list for a multi
return [value]
# not a list, return value
2017-07-16 23:11:12 +02:00
return value
2017-11-12 14:33:05 +01:00
# now try to get default value:
# - if opt is a submulti, return a list a list
# - if opt is a multi, return a list
# - default value
value = opt.impl_getdefault()
if opt.impl_is_multi() and index is not None:
2017-11-12 14:33:05 +01:00
# if index, must return good value for this index
if len(value) > index:
value = value[index]
else:
2017-11-12 14:33:05 +01:00
# no value for this index, retrieve default multi value
# default_multi is already a list for submulti
value = opt.impl_getdefault_multi()
return value
2013-02-07 16:20:21 +01:00
2017-11-20 17:01:36 +01:00
def isempty(self,
opt,
value,
force_allow_empty_list=False,
index=None):
"convenience method to know if an option is empty"
if value is undefined:
return False
else:
empty = opt._empty
2015-11-19 22:25:00 +01:00
if index in [None, undefined] and opt.impl_is_multi():
if force_allow_empty_list:
allow_empty_list = True
else:
allow_empty_list = opt.impl_allow_empty_list()
if allow_empty_list is undefined:
2017-12-05 21:49:19 +01:00
allow_empty_list = opt.impl_is_master_slaves('slave')
isempty = value is None or (not allow_empty_list and value == []) or \
None in value or empty in value
else:
isempty = value is None or value == empty
return isempty
2017-11-20 17:01:36 +01:00
def get_modified_values(self):
return self._p_.get_modified_values()
2015-04-18 23:46:37 +02:00
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# set value
2017-11-20 17:01:36 +01:00
def setvalue(self,
path,
index,
2017-12-19 23:11:45 +01:00
value,
config_bag,
2017-11-20 17:01:36 +01:00
_commit):
2013-04-18 23:06:14 +02:00
context = self._getcontext()
2017-11-12 14:33:05 +01:00
owner = context.cfgimpl_get_settings().getowner()
2018-03-31 21:06:19 +02:00
if config_bag.setting_properties is not None and \
'validator' in config_bag.setting_properties and \
config_bag.validate:
2017-12-27 15:48:49 +01:00
if index is not None or config_bag.option._has_consistencies(context):
2017-11-12 14:33:05 +01:00
# set value to a fake config when option has dependency
# validation will be complet in this case (consistency, ...)
tested_context = context._gen_fake_values()
2017-12-19 23:11:45 +01:00
sconfig_bag = config_bag.copy()
sconfig_bag.validate = False
tested_context.cfgimpl_get_values().setvalue(path,
index,
value,
sconfig_bag,
True)
2017-12-23 10:40:41 +01:00
sconfig_bag.validate = True
2017-12-19 23:11:45 +01:00
tested_context.getattr(path,
index,
2017-12-23 10:40:41 +01:00
sconfig_bag)
2017-07-16 10:57:43 +02:00
else:
2017-12-19 23:11:45 +01:00
self.setvalue_validation(path,
index,
value,
config_bag)
self._setvalue(path,
index,
2017-11-12 14:33:05 +01:00
value,
owner,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-12 14:33:05 +01:00
commit=_commit)
2017-12-13 22:15:34 +01:00
def setvalue_validation(self,
path,
2017-12-19 23:11:45 +01:00
index,
value,
config_bag):
2017-11-12 14:33:05 +01:00
context = self._getcontext()
2017-11-23 16:56:14 +01:00
settings = context.cfgimpl_get_settings()
2017-11-12 14:33:05 +01:00
# First validate properties with this value
2017-12-19 23:11:45 +01:00
self_properties = config_bag.self_properties
if self_properties is None:
self_properties = settings.getproperties(path,
index,
config_bag)
config_bag.properties = self_properties
opt = config_bag.option
if settings.validate_frozen(config_bag):
datas = {'path': path,
'config_bag': config_bag,
2017-11-23 16:56:14 +01:00
'index': index,
'debug': True}
raise PropertiesOptionError(None,
['frozen'],
settings,
datas,
'option')
2017-12-19 23:11:45 +01:00
settings.validate_mandatory(path,
index,
value,
config_bag)
2017-11-12 14:33:05 +01:00
# Value must be valid for option
2017-11-20 17:01:36 +01:00
opt.impl_validate(value,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-20 17:01:36 +01:00
context,
2017-12-13 22:15:34 +01:00
check_error=True,
2017-12-19 23:11:45 +01:00
force_index=index)
2017-11-12 14:33:05 +01:00
# No error found so emit warnings
opt.impl_validate(value,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-12 14:33:05 +01:00
context,
2017-12-13 22:15:34 +01:00
check_error=False,
2017-12-19 23:11:45 +01:00
force_index=index)
2017-11-12 14:33:05 +01:00
def _setvalue(self,
path,
2017-12-19 23:11:45 +01:00
index,
2017-11-12 14:33:05 +01:00
value,
owner,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-12 14:33:05 +01:00
commit=True):
2017-12-19 23:11:45 +01:00
self._getcontext().cfgimpl_reset_cache(opt=config_bag.option,
2017-11-20 17:01:36 +01:00
path=path)
2017-11-12 14:33:05 +01:00
if isinstance(value, list):
# copy
value = list(value)
self._p_.setvalue(path,
value,
owner,
index,
commit)
2017-11-20 17:01:36 +01:00
def _is_meta(self,
path,
2017-11-23 16:56:14 +01:00
index,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-23 16:56:14 +01:00
force_owner_is_default=False):
if not force_owner_is_default and self._p_.hasvalue(path,
index=index):
# has already a value, so not meta
return False
context = self._getcontext()
2017-11-23 16:56:14 +01:00
meta = context.cfgimpl_get_meta()
if meta is None:
2017-10-14 13:33:25 +02:00
return False
2017-12-19 23:11:45 +01:00
opt = config_bag.option
2017-11-23 16:56:14 +01:00
if opt.impl_is_master_slaves('slave'):
2017-12-02 22:53:57 +01:00
master = opt.impl_get_master_slaves().getmaster()
2017-11-23 16:56:14 +01:00
masterp = master.impl_getpath(context)
# slave could be a "meta" only if master hasn't value
if self._p_.hasvalue(masterp,
2018-01-03 21:07:51 +01:00
index=None):
2017-11-23 16:56:14 +01:00
return False
2017-12-19 23:11:45 +01:00
return not meta.cfgimpl_get_values().is_default_owner(path,
index,
config_bag)
2017-11-23 16:56:14 +01:00
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# owner
2017-12-23 12:29:45 +01:00
def is_default_owner(self,
path,
index,
config_bag,
validate_meta=undefined):
return self._getowner(path,
index,
config_bag,
validate_meta=validate_meta,
only_default=True) == owners.default
2017-11-20 17:01:36 +01:00
def getowner(self,
path,
2017-12-19 23:11:45 +01:00
index,
config_bag):
2013-08-21 11:09:11 +02:00
"""
retrieves the option's owner
2013-08-21 14:52:48 +02:00
:param opt: the `option.Option` object
:param force_permissive: behaves as if the permissive property
was present
2013-08-21 11:09:11 +02:00
:returns: a `setting.owners.Owner` object
"""
2017-12-19 23:11:45 +01:00
return self._getowner(path,
index,
config_bag)
2017-11-12 14:33:05 +01:00
def _getowner(self,
path,
2017-12-19 23:11:45 +01:00
index,
config_bag,
2018-01-03 21:07:51 +01:00
validate_meta=True,
2017-12-19 23:11:45 +01:00
only_default=False):
2015-05-03 09:56:03 +02:00
"""get owner of an option
"""
context = self._getcontext()
2017-12-19 23:11:45 +01:00
opt = config_bag.option
2017-12-04 20:05:36 +01:00
if opt.impl_is_symlinkoption():
2017-12-19 23:11:45 +01:00
config_bag.ori_option = opt
2017-12-04 20:05:36 +01:00
opt = opt.impl_getopt()
2017-12-19 23:11:45 +01:00
config_bag.option = opt
2017-12-04 20:05:36 +01:00
path = opt.impl_getpath(context)
2017-12-19 23:11:45 +01:00
self_properties = config_bag.properties
2018-03-31 21:06:19 +02:00
settings = context.cfgimpl_get_settings()
2017-12-19 23:11:45 +01:00
if self_properties is None:
2018-03-31 21:06:19 +02:00
self_properties = settings.getproperties(path,
index,
config_bag)
2017-12-19 23:11:45 +01:00
config_bag.properties = self_properties
2018-03-31 21:06:19 +02:00
if config_bag.setting_properties is not None:
settings.validate_properties(path,
index,
config_bag)
2015-10-29 09:03:13 +01:00
if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
return owners.default
2017-11-23 16:56:14 +01:00
if only_default:
if self._p_.hasvalue(path,
index):
owner = undefined
else:
2017-11-23 16:56:14 +01:00
owner = owners.default
else:
owner = self._p_.getowner(path,
owners.default,
index=index)
if owner is owners.default and validate_meta is not False:
2018-01-03 21:07:51 +01:00
meta = context.cfgimpl_get_meta()
if meta is not None and self._is_meta(path,
index,
config_bag):
owner = meta.cfgimpl_get_values()._getowner(path,
index,
config_bag,
only_default=only_default)
2013-05-02 11:34:57 +02:00
return owner
2013-04-03 12:20:26 +02:00
2017-11-20 17:01:36 +01:00
def setowner(self,
path,
2017-12-19 23:11:45 +01:00
index,
2017-11-20 17:01:36 +01:00
owner,
2017-12-19 23:11:45 +01:00
config_bag):
2013-08-21 11:09:11 +02:00
"""
sets a owner to an option
2013-08-21 14:52:48 +02:00
:param opt: the `option.Option` object
2013-08-21 11:09:11 +02:00
:param owner: a valid owner, that is a `setting.owners.Owner` object
"""
2017-12-19 23:11:45 +01:00
opt = config_bag.option
2017-12-04 20:05:36 +01:00
if opt.impl_is_symlinkoption():
2018-03-12 11:58:49 +01:00
raise ConfigError(_("can't set owner for the SymLinkOption \"{}\""
"").format(opt.impl_get_display_name()))
2017-02-11 17:22:50 +01:00
if not isinstance(owner, owners.Owner):
2018-03-12 11:58:49 +01:00
raise ConfigError(_("invalid owner {0}").format(str(owner)))
2017-11-23 16:56:14 +01:00
if owner in forbidden_owners:
raise ConfigError(_('set owner "{0}" is forbidden').format(str(owner)))
2017-11-12 14:33:05 +01:00
if not self._p_.hasvalue(path):
2017-10-22 09:48:08 +02:00
raise ConfigError(_('no value for {0} cannot change owner to {1}'
'').format(path, owner))
2017-12-19 23:11:45 +01:00
self.setowner_validation(path,
index,
config_bag)
2017-11-12 14:33:05 +01:00
self._p_.setowner(path, owner, index=index)
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# reset
def reset(self,
path,
2017-12-19 23:11:45 +01:00
config_bag,
_commit=True):
2017-11-20 17:01:36 +01:00
context = self._getcontext()
setting = context.cfgimpl_get_settings()
hasvalue = self._p_.hasvalue(path)
2017-12-19 23:11:45 +01:00
if config_bag.validate and hasvalue and 'validator' in config_bag.setting_properties:
2017-11-20 17:01:36 +01:00
fake_context = context._gen_fake_values()
fake_value = fake_context.cfgimpl_get_values()
2017-12-19 23:11:45 +01:00
sconfig_bag = config_bag.copy()
sconfig_bag.validate = False
fake_value.reset(path,
sconfig_bag)
value = fake_value._getdefaultvalue(path,
2017-12-13 22:15:34 +01:00
None,
2017-12-19 23:11:45 +01:00
config_bag)
fake_value.setvalue_validation(path,
None,
2017-12-13 22:15:34 +01:00
value,
2017-12-19 23:11:45 +01:00
config_bag)
opt = config_bag.option
2017-11-20 17:01:36 +01:00
if opt.impl_is_master_slaves('master'):
2017-12-19 23:11:45 +01:00
opt.impl_get_master_slaves().reset(self,
config_bag,
_commit=_commit)
2017-11-20 17:01:36 +01:00
if hasvalue:
2017-12-19 23:11:45 +01:00
if 'force_store_value' in setting.getproperties(path,
None,
config_bag):
value = self._getdefaultvalue(path,
2017-11-20 17:01:36 +01:00
None,
2017-12-19 23:11:45 +01:00
config_bag)
self._setvalue(path,
None,
2017-11-20 17:01:36 +01:00
value,
owners.forced,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-20 17:01:36 +01:00
commit=_commit)
else:
self._p_.resetvalue(path,
_commit)
context.cfgimpl_reset_cache(opt=opt,
path=path)
def reset_slave(self,
path,
index,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-20 17:01:36 +01:00
2018-03-24 22:37:48 +01:00
if self._p_.hasvalue(path, index=index):
context = self._getcontext()
if config_bag.validate and 'validator' in config_bag.setting_properties:
fake_context = context._gen_fake_values()
fake_value = fake_context.cfgimpl_get_values()
sconfig_bag = config_bag.copy()
sconfig_bag.validate = False
fake_value.reset_slave(path,
index,
sconfig_bag)
value = fake_value._getdefaultvalue(path,
index,
config_bag)
fake_value.setvalue_validation(path,
index,
value,
config_bag)
self._p_.resetvalue_index(path, index)
context.cfgimpl_reset_cache(opt=config_bag.option,
path=path)
2017-11-20 17:01:36 +01:00
def reset_master(self,
subconfig,
path,
index,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-20 17:01:36 +01:00
2017-12-19 23:11:45 +01:00
current_value = self.get_cached_value(path,
None,
config_bag)
2018-03-24 22:37:48 +01:00
length = len(current_value)
if index >= length:
raise IndexError(_('index "{}" is higher than the length "{}" '
'for option "{}"').format(index,
length,
config_bag.option.impl_get_display_name()))
2017-11-20 17:01:36 +01:00
current_value.pop(index)
2017-12-19 23:11:45 +01:00
self.setvalue(path,
None,
2017-11-20 17:01:36 +01:00
current_value,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-20 17:01:36 +01:00
_commit=True)
2017-12-02 22:53:57 +01:00
subconfig.cfgimpl_get_description().pop(self,
2017-11-28 22:42:30 +01:00
index,
2017-12-19 23:11:45 +01:00
config_bag)
2017-11-20 17:01:36 +01:00
2017-12-13 22:15:34 +01:00
def setowner_validation(self,
path,
2017-12-19 23:11:45 +01:00
index,
config_bag):
2017-12-13 22:15:34 +01:00
context = self._getcontext()
settings = context.cfgimpl_get_settings()
# First validate properties with this value
2017-12-19 23:11:45 +01:00
self_properties = config_bag.properties
if self_properties is None:
self_properties = settings.getproperties(path,
None,
config_bag)
config_bag.properties = self_properties
if settings.validate_frozen(config_bag):
datas = {'path': path,
'config_bag': config_bag,
2017-12-13 22:15:34 +01:00
'index': index,
'debug': True}
raise PropertiesOptionError(None,
['frozen'],
settings,
datas,
'option')
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# information
2017-11-20 17:01:36 +01:00
def 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._p_.set_information(key, value)
def get_information(self, key, default=undefined):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
2015-12-28 22:00:46 +01:00
return self._p_.get_information(key, default)
def del_information(self, key, raises=True):
self._p_.del_information(key, raises)
2017-11-20 17:01:36 +01:00
#______________________________________________________________________
# mandatory warnings
2017-12-05 21:49:19 +01:00
def mandatory_warnings(self,
2017-12-19 23:11:45 +01:00
config_bag):
"""convenience function to trace Options that are mandatory and
where no value has been set
2015-10-29 09:03:13 +01:00
:returns: generator of mandatory Option's path
"""
2015-10-29 09:03:13 +01:00
context = self._getcontext()
settings = context.cfgimpl_get_settings()
2017-12-13 22:15:34 +01:00
# copy
2017-12-19 23:11:45 +01:00
od_setting_properties = config_bag.setting_properties - {'mandatory', 'empty'}
setting_properties = set(config_bag.setting_properties)
setting_properties.update(['mandatory', 'empty'])
2017-12-19 23:11:45 +01:00
config_bag.setting_properties = frozenset(setting_properties)
config_bag.force_permissive = True
config_bag.display_warnings = False
def _mandatory_warnings(description, currpath, config):
2018-01-05 23:32:00 +01:00
is_masterslaves = description.impl_is_master_slaves()
2017-12-13 22:15:34 +01:00
lenmaster = None
2017-12-19 23:11:45 +01:00
optmaster = None
pathmaster = None
2018-03-24 22:37:48 +01:00
for option in description.impl_getchildren(config_bag, context):
2017-12-19 23:11:45 +01:00
sconfig_bag = config_bag.copy('nooption')
sconfig_bag.option = option
name = option.impl_getname()
2015-10-29 09:03:13 +01:00
path = '.'.join(currpath + [name])
2017-12-19 23:11:45 +01:00
if option.impl_is_optiondescription():
sconfig_bag.setting_properties = od_setting_properties
2017-12-13 22:15:34 +01:00
try:
2017-12-19 23:11:45 +01:00
subconfig = config.getattr(name,
None,
sconfig_bag)
except PropertiesOptionError as err:
2017-12-13 22:15:34 +01:00
pass
2017-12-19 23:11:45 +01:00
else:
for path in _mandatory_warnings(option,
currpath + [name],
subconfig):
yield path
elif not option.impl_is_symlinkoption():
# don't check symlink
self_properties = settings.getproperties(path,
None,
sconfig_bag)
sconfig_bag.properties = self_properties
if 'mandatory' in self_properties or 'empty' in self_properties:
2017-12-13 22:15:34 +01:00
try:
2017-12-19 23:11:45 +01:00
if option.impl_is_master_slaves('slave'):
2017-12-13 22:15:34 +01:00
if lenmaster is None:
# master is a length (so int) if value is already calculated
# otherwise get value and calculate length
2017-12-19 23:11:45 +01:00
nconfig_bag = config_bag.copy('nooption')
nconfig_bag.option = optmaster
values = config.getattr(pathmaster,
None,
nconfig_bag)
2017-12-13 22:15:34 +01:00
lenmaster = len(values)
2017-12-19 23:11:45 +01:00
#if not lenmaster:
# settings.validate_properties(path,
# None,
# sconfig_bag)
#else:
for index in range(lenmaster):
values = config.getattr(name,
index,
sconfig_bag)
2017-12-13 22:15:34 +01:00
else:
2017-12-19 23:11:45 +01:00
value = config.getattr(name,
None,
sconfig_bag)
if is_masterslaves:
lenmaster = len(value)
pathmaster = name
optmaster = option
2017-12-13 22:15:34 +01:00
except PropertiesOptionError as err:
2017-12-19 23:11:45 +01:00
if err.proptype == ['mandatory']:
2017-12-13 22:15:34 +01:00
yield path
if is_masterslaves and lenmaster is None:
break
except ConfigError as err:
#assume that uncalculated value is an empty value
yield path
if is_masterslaves and lenmaster is None:
break
2015-10-29 09:03:13 +01:00
2017-06-16 18:25:01 +02:00
descr = context.cfgimpl_get_description()
2017-12-19 23:11:45 +01:00
for path in _mandatory_warnings(descr, [], context):
2015-10-29 09:03:13 +01:00
yield path