2013-02-07 16:20:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-08 11:50:22 +01:00
|
|
|
"takes care of the option's values and multi values"
|
2019-02-12 06:55:47 +01:00
|
|
|
# Copyright (C) 2013-2019 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
|
|
|
# ____________________________________________________________
|
2013-08-27 11:39:32 +02:00
|
|
|
import weakref
|
2019-03-13 08:49:18 +01:00
|
|
|
from typing import Optional, Any, Callable
|
2019-04-17 19:13:40 +02:00
|
|
|
from .error import ConfigError, PropertiesOptionError, RequirementError
|
2019-02-24 10:36:42 +01:00
|
|
|
from .setting import owners, undefined, forbidden_owners, OptionBag, ConfigBag
|
2015-11-29 23:03:08 +01:00
|
|
|
from .autolib import carry_out_calculation
|
2019-03-13 08:49:18 +01:00
|
|
|
from .function import Params
|
2015-11-29 23:03:08 +01:00
|
|
|
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
|
|
|
"""
|
2018-09-07 06:14:52 +02:00
|
|
|
__slots__ = ('_p_',
|
2017-12-07 21:42:04 +01:00
|
|
|
'__weakref__')
|
2013-04-03 12:20:26 +02:00
|
|
|
|
2017-12-07 21:42:04 +01:00
|
|
|
def __init__(self,
|
2018-09-15 22:44:49 +02:00
|
|
|
storage):
|
2013-02-21 17:07:00 +01:00
|
|
|
"""
|
|
|
|
Initializes the values's dict.
|
|
|
|
|
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
|
|
|
"""
|
2017-12-07 21:42:04 +01:00
|
|
|
# store the storage
|
2013-09-06 23:15:28 +02:00
|
|
|
self._p_ = storage
|
2018-09-15 10:34:15 +02:00
|
|
|
# set default owner
|
2018-09-15 22:44:49 +02:00
|
|
|
if self._p_.getowner(None, None) is None:
|
|
|
|
self._p_.setvalue(None,
|
|
|
|
None,
|
|
|
|
owners.user,
|
|
|
|
None,
|
|
|
|
True)
|
2013-08-20 09:47:12 +02:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
|
|
|
# get value
|
|
|
|
|
|
|
|
def get_cached_value(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag):
|
2017-12-07 21:42:04 +01:00
|
|
|
"""get value directly in cache if set
|
|
|
|
otherwise calculated value and set it in cache
|
|
|
|
|
|
|
|
:returns: value
|
|
|
|
"""
|
|
|
|
# try to retrive value in cache
|
2018-08-18 07:51:04 +02:00
|
|
|
setting_properties = option_bag.config_bag.properties
|
2019-06-12 08:45:56 +02:00
|
|
|
is_cached, value, validated = self._p_.getcache(option_bag.path,
|
|
|
|
option_bag.config_bag.expiration_time,
|
|
|
|
option_bag.index,
|
|
|
|
setting_properties,
|
|
|
|
option_bag.properties,
|
|
|
|
'value')
|
|
|
|
if not validated:
|
2017-12-19 23:11:45 +01:00
|
|
|
# no cached value so get value
|
2018-08-01 08:37:58 +02:00
|
|
|
value = self.getvalue(option_bag)
|
2019-06-12 08:45:56 +02:00
|
|
|
# validate value
|
|
|
|
option_bag.option.impl_validate(value,
|
|
|
|
option_bag,
|
|
|
|
check_error=True)
|
|
|
|
# store value in cache
|
|
|
|
validator = 'validator' in option_bag.config_bag.properties
|
2019-06-21 23:04:04 +02:00
|
|
|
if not option_bag.fromconsistency and (not is_cached or validator):
|
2019-06-12 08:45:56 +02:00
|
|
|
self._p_.setcache(option_bag.path,
|
|
|
|
option_bag.index,
|
|
|
|
value,
|
|
|
|
option_bag.properties,
|
|
|
|
setting_properties,
|
|
|
|
validator)
|
2018-08-18 07:51:04 +02:00
|
|
|
if 'warnings' in setting_properties:
|
2019-06-12 08:45:56 +02:00
|
|
|
option_bag.option.impl_validate(value,
|
|
|
|
option_bag,
|
|
|
|
check_error=False)
|
2018-07-22 11:51:48 +02:00
|
|
|
if isinstance(value, list):
|
|
|
|
# return a copy, so value cannot be modified
|
|
|
|
return value.copy()
|
2017-12-07 21:42:04 +01:00
|
|
|
# and return it
|
|
|
|
return value
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2019-02-24 08:34:50 +01:00
|
|
|
def force_to_metaconfig(self, option_bag):
|
|
|
|
# force_metaconfig_on_freeze in config => to metaconfig
|
|
|
|
# force_metaconfig_on_freeze in option + config is kernelconfig => to metaconfig
|
|
|
|
settings = option_bag.config_bag.context.cfgimpl_get_settings()
|
|
|
|
if 'force_metaconfig_on_freeze' in option_bag.properties:
|
|
|
|
settings = option_bag.config_bag.context.cfgimpl_get_settings()
|
|
|
|
if 'force_metaconfig_on_freeze' in option_bag.option.impl_getproperties() and \
|
|
|
|
not settings._p_.getproperties(option_bag.path, frozenset()):
|
|
|
|
# if force_metaconfig_on_freeze is only in option (not in config)
|
|
|
|
return option_bag.config_bag.context.impl_type == 'config'
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
def getvalue(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_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`
|
2019-02-23 19:06:23 +01:00
|
|
|
:param index: index for a follower `Option`
|
2017-12-07 21:42:04 +01:00
|
|
|
|
|
|
|
:returns: value
|
2017-11-20 17:01:36 +01:00
|
|
|
"""
|
2017-12-07 21:42:04 +01:00
|
|
|
# get owner and value from store
|
2019-02-23 19:06:23 +01:00
|
|
|
# index allowed only for follower
|
2018-08-01 08:37:58 +02:00
|
|
|
index = option_bag.index
|
2019-02-23 19:06:23 +01:00
|
|
|
is_follower = option_bag.option.impl_is_follower()
|
|
|
|
if index is None or not is_follower:
|
2017-11-20 17:01:36 +01:00
|
|
|
_index = None
|
|
|
|
else:
|
|
|
|
_index = index
|
2018-08-01 08:37:58 +02:00
|
|
|
owner, value = self._p_.getowner(option_bag.path,
|
2017-11-20 17:01:36 +01:00
|
|
|
owners.default,
|
|
|
|
index=_index,
|
|
|
|
with_value=True)
|
2019-02-23 22:10:43 +01:00
|
|
|
if owner != owners.default and \
|
|
|
|
not ('frozen' in option_bag.properties and 'force_default_on_freeze' in option_bag.properties) and \
|
2019-02-24 08:34:50 +01:00
|
|
|
not ('frozen' in option_bag.properties and self.force_to_metaconfig(option_bag)):
|
2018-09-12 21:05:14 +02:00
|
|
|
return value
|
2018-08-01 08:37:58 +02:00
|
|
|
return self.getdefaultvalue(option_bag)
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2017-11-12 14:33:05 +01:00
|
|
|
def getdefaultvalue(self,
|
2019-03-13 08:49:18 +01:00
|
|
|
option_bag):
|
2017-11-12 14:33:05 +01:00
|
|
|
"""get default value:
|
|
|
|
- get meta config value or
|
|
|
|
- get calculated value or
|
|
|
|
- get default value
|
|
|
|
"""
|
2019-03-13 08:49:18 +01:00
|
|
|
moption_bag = self._get_meta(option_bag)
|
|
|
|
if moption_bag:
|
|
|
|
# retrieved value from meta config
|
|
|
|
return moption_bag.config_bag.context.cfgimpl_get_values().get_cached_value(moption_bag)
|
|
|
|
|
|
|
|
if option_bag.option.impl_has_callback():
|
|
|
|
# default value is a calculated value
|
|
|
|
value = self.calculate_value(option_bag)
|
|
|
|
if value is not undefined:
|
|
|
|
return value
|
|
|
|
|
|
|
|
# now try to get default value:
|
|
|
|
value = option_bag.option.impl_getdefault()
|
|
|
|
|
|
|
|
# - if option is a submulti, return a list a list
|
|
|
|
# - if option is a multi, return a list
|
|
|
|
# - default value
|
|
|
|
if option_bag.option.impl_is_multi() and option_bag.index is not None:
|
|
|
|
# if index, must return good value for this index
|
|
|
|
if len(value) > option_bag.index:
|
|
|
|
value = value[option_bag.index]
|
|
|
|
else:
|
|
|
|
# no value for this index, retrieve default multi value
|
|
|
|
# default_multi is already a list for submulti
|
|
|
|
value = option_bag.option.impl_getdefault_multi()
|
|
|
|
return value
|
|
|
|
|
|
|
|
def calculate_value(self,
|
|
|
|
option_bag: OptionBag) -> Any:
|
2018-03-31 23:09:40 +02:00
|
|
|
def _reset_cache(_value):
|
2018-09-04 08:36:02 +02:00
|
|
|
if not 'expire' in option_bag.properties:
|
|
|
|
return
|
2019-06-12 08:45:56 +02:00
|
|
|
is_cache, cache_value, validated = self._p_.getcache(option_bag.path,
|
|
|
|
None,
|
|
|
|
option_bag.index,
|
|
|
|
option_bag.config_bag.properties,
|
|
|
|
option_bag.properties,
|
|
|
|
'value')
|
2018-09-04 08:36:02 +02:00
|
|
|
if not is_cache or cache_value == _value:
|
2018-06-25 21:40:16 +02:00
|
|
|
# calculation return same value as previous value,
|
|
|
|
# so do not invalidate cache
|
|
|
|
return
|
2018-03-31 23:09:40 +02:00
|
|
|
# calculated value is a new value, so reset cache
|
2019-03-13 08:49:18 +01:00
|
|
|
option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
|
|
|
|
|
|
|
|
# if value has callback, calculate value
|
|
|
|
callback, callback_params = option_bag.option.impl_get_callback()
|
|
|
|
value = self.carry_out_calculation(option_bag,
|
|
|
|
callback,
|
|
|
|
callback_params)
|
|
|
|
if isinstance(value, list) and option_bag.index is not None:
|
|
|
|
# if value is a list and index is set
|
|
|
|
if option_bag.option.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(value)
|
|
|
|
return value
|
|
|
|
if len(value) > option_bag.index:
|
|
|
|
# return the value for specified index if found
|
|
|
|
_reset_cache(value[option_bag.index])
|
|
|
|
return value[option_bag.index]
|
|
|
|
# there is no calculate value for this index,
|
|
|
|
# so return an other default value
|
2018-11-16 07:12:44 +01:00
|
|
|
else:
|
2019-03-13 08:49:18 +01:00
|
|
|
if option_bag.option.impl_is_submulti():
|
|
|
|
if isinstance(value, list):
|
|
|
|
# value is a list, but no index specified
|
|
|
|
if (value != [] and not isinstance(value[0], list)):
|
2018-04-10 22:42:20 +02:00
|
|
|
# if submulti, return a list of value
|
|
|
|
value = [value]
|
2019-03-13 08:49:18 +01:00
|
|
|
elif option_bag.index is not None:
|
|
|
|
# if submulti, return a list of value
|
2018-03-31 23:09:40 +02:00
|
|
|
value = [value]
|
2019-03-13 08:49:18 +01:00
|
|
|
else:
|
|
|
|
# return a list of list for a submulti
|
|
|
|
value = [[value]]
|
|
|
|
elif option_bag.option.impl_is_multi() and not isinstance(value, list) and option_bag.index is None:
|
|
|
|
# return a list for a multi
|
|
|
|
value = [value]
|
|
|
|
_reset_cache(value)
|
|
|
|
return value
|
|
|
|
return undefined
|
|
|
|
|
|
|
|
def carry_out_calculation(self,
|
|
|
|
option_bag: OptionBag,
|
|
|
|
callback: Callable,
|
|
|
|
callback_params: Optional[Params]) -> Any:
|
|
|
|
return carry_out_calculation(option_bag.option,
|
|
|
|
callback=callback,
|
|
|
|
callback_params=callback_params,
|
|
|
|
index=option_bag.index,
|
|
|
|
config_bag=option_bag.config_bag,
|
|
|
|
fromconsistency=option_bag.fromconsistency)
|
2017-11-20 17:01:36 +01:00
|
|
|
def isempty(self,
|
|
|
|
opt,
|
|
|
|
value,
|
|
|
|
force_allow_empty_list=False,
|
|
|
|
index=None):
|
2013-02-08 11:50:22 +01:00
|
|
|
"convenience method to know if an option is empty"
|
2018-04-11 16:36:15 +02:00
|
|
|
empty = opt._empty
|
|
|
|
if index in [None, undefined] and opt.impl_is_multi():
|
|
|
|
if force_allow_empty_list:
|
|
|
|
allow_empty_list = True
|
2015-07-26 18:55:21 +02:00
|
|
|
else:
|
2018-04-11 16:36:15 +02:00
|
|
|
allow_empty_list = opt.impl_allow_empty_list()
|
|
|
|
if allow_empty_list is undefined:
|
2019-02-23 19:06:23 +01:00
|
|
|
allow_empty_list = opt.impl_is_follower()
|
2018-04-11 16:36:15 +02:00
|
|
|
isempty = value is None or (not allow_empty_list and value == []) or \
|
|
|
|
None in value or empty in value
|
|
|
|
else:
|
2019-04-17 19:13:40 +02:00
|
|
|
isempty = value is None or value == empty or (opt.impl_is_submulti() and value == [])
|
2015-07-26 18:55:21 +02:00
|
|
|
return isempty
|
2013-04-08 16:05:56 +02:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
|
|
|
# set value
|
2013-02-08 11:50:22 +01:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
def setvalue(self,
|
2017-12-19 23:11:45 +01:00
|
|
|
value,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
2017-11-20 17:01:36 +01:00
|
|
|
_commit):
|
2013-04-18 23:06:14 +02:00
|
|
|
|
2018-09-06 23:06:56 +02:00
|
|
|
context = option_bag.config_bag.context
|
2018-09-15 10:34:15 +02:00
|
|
|
owner = self.get_context_owner()
|
2018-08-18 07:51:04 +02:00
|
|
|
if 'validator' in option_bag.config_bag.properties:
|
2018-09-30 11:36:09 +02:00
|
|
|
if option_bag.index is not None or option_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()
|
2018-08-18 10:03:08 +02:00
|
|
|
config_bag = option_bag.config_bag.copy()
|
|
|
|
config_bag.remove_validation()
|
2018-09-06 23:06:56 +02:00
|
|
|
config_bag.context = tested_context
|
2018-08-18 10:03:08 +02:00
|
|
|
soption_bag = option_bag.copy()
|
|
|
|
soption_bag.config_bag = config_bag
|
|
|
|
tested_context.cfgimpl_get_values().setvalue(value,
|
|
|
|
soption_bag,
|
|
|
|
True)
|
2018-09-06 23:06:56 +02:00
|
|
|
soption_bag.config_bag.properties = option_bag.config_bag.properties
|
|
|
|
tested_context.getattr(soption_bag.path,
|
|
|
|
soption_bag)
|
2017-07-16 10:57:43 +02:00
|
|
|
else:
|
2018-08-01 08:37:58 +02:00
|
|
|
self.setvalue_validation(value,
|
|
|
|
option_bag)
|
2017-12-19 23:11:45 +01:00
|
|
|
|
2018-08-01 08:37:58 +02:00
|
|
|
self._setvalue(option_bag,
|
2017-11-12 14:33:05 +01:00
|
|
|
value,
|
|
|
|
owner,
|
|
|
|
commit=_commit)
|
|
|
|
|
2017-12-13 22:15:34 +01:00
|
|
|
def setvalue_validation(self,
|
2017-12-19 23:11:45 +01:00
|
|
|
value,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag):
|
2017-11-12 14:33:05 +01:00
|
|
|
|
2018-09-30 20:32:00 +02:00
|
|
|
settings = option_bag.config_bag.context.cfgimpl_get_settings()
|
2017-11-12 14:33:05 +01:00
|
|
|
# First validate properties with this value
|
2018-08-01 08:37:58 +02:00
|
|
|
opt = option_bag.option
|
|
|
|
settings.validate_frozen(option_bag)
|
|
|
|
settings.validate_mandatory(value,
|
|
|
|
option_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,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
|
|
|
check_error=True)
|
2018-08-18 07:51:04 +02:00
|
|
|
if 'warnings' in option_bag.config_bag.properties:
|
2018-06-02 08:35:05 +02:00
|
|
|
# No error found so emit warnings
|
|
|
|
opt.impl_validate(value,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
|
|
|
check_error=False)
|
2017-11-12 14:33:05 +01:00
|
|
|
|
|
|
|
def _setvalue(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
2017-11-12 14:33:05 +01:00
|
|
|
value,
|
|
|
|
owner,
|
|
|
|
commit=True):
|
|
|
|
|
2018-09-06 23:06:56 +02:00
|
|
|
option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
|
2017-11-12 14:33:05 +01:00
|
|
|
if isinstance(value, list):
|
|
|
|
# copy
|
2018-08-01 08:37:58 +02:00
|
|
|
value = value.copy()
|
|
|
|
self._p_.setvalue(option_bag.path,
|
2017-11-12 14:33:05 +01:00
|
|
|
value,
|
|
|
|
owner,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag.index,
|
2017-11-12 14:33:05 +01:00
|
|
|
commit)
|
|
|
|
|
2018-10-31 09:55:05 +01:00
|
|
|
def _get_meta(self,
|
|
|
|
option_bag):
|
2018-09-06 23:06:56 +02:00
|
|
|
context = option_bag.config_bag.context
|
2017-11-23 16:56:14 +01:00
|
|
|
meta = context.cfgimpl_get_meta()
|
|
|
|
if meta is None:
|
2018-10-31 09:55:05 +01:00
|
|
|
return None
|
2019-02-23 19:06:23 +01:00
|
|
|
if option_bag.option.impl_is_follower():
|
|
|
|
leader = option_bag.option.impl_get_leadership().get_leader()
|
|
|
|
leaderpath = leader.impl_getpath()
|
|
|
|
# follower could be a "meta" only if leader hasn't value
|
|
|
|
if self._p_.hasvalue(leaderpath,
|
2018-01-03 21:07:51 +01:00
|
|
|
index=None):
|
2018-10-31 09:55:05 +01:00
|
|
|
return None
|
2018-09-06 23:06:56 +02:00
|
|
|
doption_bag = option_bag.copy()
|
|
|
|
config_bag = option_bag.config_bag.copy()
|
|
|
|
config_bag.context = meta
|
|
|
|
doption_bag.config_bag = config_bag
|
2019-02-23 22:10:43 +01:00
|
|
|
if 'force_metaconfig_on_freeze' in option_bag.properties:
|
|
|
|
# remove force_metaconfig_on_freeze only if option in metaconfig
|
|
|
|
# hasn't force_metaconfig_on_freeze properties
|
|
|
|
ori_properties = doption_bag.properties
|
|
|
|
del doption_bag.properties
|
2019-02-24 08:34:50 +01:00
|
|
|
if not self.force_to_metaconfig(doption_bag):
|
2019-02-23 22:10:43 +01:00
|
|
|
doption_bag.properties = ori_properties - {'force_metaconfig_on_freeze'}
|
|
|
|
else:
|
|
|
|
doption_bag.properties = ori_properties
|
|
|
|
config_bag.unrestraint()
|
2018-11-16 07:12:44 +01:00
|
|
|
meta_option_bag = meta.cfgimpl_get_values().getowner(doption_bag,
|
|
|
|
only_default=True)
|
|
|
|
if meta_option_bag == owners.default:
|
2018-10-31 09:55:05 +01:00
|
|
|
return None
|
2018-11-16 07:12:44 +01:00
|
|
|
return meta_option_bag
|
2017-11-23 16:56:14 +01:00
|
|
|
|
2014-12-01 21:49:50 +01:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
|
|
|
# owner
|
|
|
|
|
2017-12-23 12:29:45 +01:00
|
|
|
def is_default_owner(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
2018-11-16 07:12:44 +01:00
|
|
|
validate_meta=True):
|
2018-08-01 08:37:58 +02:00
|
|
|
return self.getowner(option_bag,
|
|
|
|
validate_meta=validate_meta,
|
|
|
|
only_default=True) == owners.default
|
2017-12-23 12:29:45 +01:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
def getowner(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
|
|
|
validate_meta=True,
|
|
|
|
only_default=False):
|
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
|
2014-03-29 20:31:56 +01:00
|
|
|
: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
|
|
|
|
"""
|
2018-09-06 23:06:56 +02:00
|
|
|
context = option_bag.config_bag.context
|
2018-08-01 08:37:58 +02:00
|
|
|
opt = option_bag.option
|
2017-12-04 20:05:36 +01:00
|
|
|
if opt.impl_is_symlinkoption():
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag.ori_option = opt
|
2017-12-04 20:05:36 +01:00
|
|
|
opt = opt.impl_getopt()
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag.option = opt
|
2018-09-06 23:16:17 +02:00
|
|
|
option_bag.path = opt.impl_getpath()
|
2018-03-31 21:06:19 +02:00
|
|
|
settings = context.cfgimpl_get_settings()
|
2018-08-17 23:11:25 +02:00
|
|
|
settings.validate_properties(option_bag)
|
2018-09-05 22:46:45 +02:00
|
|
|
if 'frozen' in option_bag.properties and \
|
|
|
|
'force_default_on_freeze' in option_bag.properties:
|
2014-10-26 08:51:45 +01:00
|
|
|
return owners.default
|
2017-11-23 16:56:14 +01:00
|
|
|
if only_default:
|
2018-08-01 08:37:58 +02:00
|
|
|
if self._p_.hasvalue(option_bag.path,
|
|
|
|
option_bag.index):
|
2018-11-16 07:12:44 +01:00
|
|
|
owner = option_bag
|
2014-12-01 21:49:50 +01:00
|
|
|
else:
|
2017-11-23 16:56:14 +01:00
|
|
|
owner = owners.default
|
|
|
|
else:
|
2018-08-01 08:37:58 +02:00
|
|
|
owner = self._p_.getowner(option_bag.path,
|
2017-11-23 16:56:14 +01:00
|
|
|
owners.default,
|
2018-08-01 08:37:58 +02:00
|
|
|
index=option_bag.index)
|
2019-02-23 22:10:43 +01:00
|
|
|
if validate_meta is not False and (owner is owners.default or \
|
2019-02-24 08:34:50 +01:00
|
|
|
'frozen' in option_bag.properties and 'force_metaconfig_on_freeze' in option_bag.properties):
|
2018-10-31 09:55:05 +01:00
|
|
|
moption_bag = self._get_meta(option_bag)
|
|
|
|
if moption_bag:
|
|
|
|
owner = moption_bag.config_bag.context.cfgimpl_get_values().getowner(moption_bag,
|
|
|
|
only_default=only_default)
|
2019-02-23 22:10:43 +01:00
|
|
|
elif 'force_metaconfig_on_freeze' in option_bag.properties:
|
|
|
|
return owners.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,
|
|
|
|
owner,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_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
|
|
|
|
"""
|
2018-08-01 08:37:58 +02:00
|
|
|
opt = option_bag.option
|
2017-12-04 20:05:36 +01:00
|
|
|
if opt.impl_is_symlinkoption():
|
2018-04-11 18:32:13 +02:00
|
|
|
raise ConfigError(_("can't set owner for the symlinkoption \"{}\""
|
2018-03-12 11:58:49 +01:00
|
|
|
"").format(opt.impl_get_display_name()))
|
2017-11-23 16:56:14 +01:00
|
|
|
if owner in forbidden_owners:
|
2018-09-12 21:05:14 +02:00
|
|
|
raise ValueError(_('set owner "{0}" is forbidden').format(str(owner)))
|
2013-08-21 22:21:50 +02:00
|
|
|
|
2018-08-01 08:37:58 +02:00
|
|
|
if not self._p_.hasvalue(option_bag.path):
|
2017-10-22 09:48:08 +02:00
|
|
|
raise ConfigError(_('no value for {0} cannot change owner to {1}'
|
2018-08-01 08:37:58 +02:00
|
|
|
'').format(option_bag.path, owner))
|
2018-09-06 23:06:56 +02:00
|
|
|
option_bag.config_bag.context.cfgimpl_get_settings().validate_frozen(option_bag)
|
2018-08-01 08:37:58 +02:00
|
|
|
self._p_.setowner(option_bag.path,
|
|
|
|
owner,
|
|
|
|
index=option_bag.index)
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
|
|
|
# reset
|
|
|
|
|
|
|
|
def reset(self,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag,
|
2017-12-19 23:11:45 +01:00
|
|
|
_commit=True):
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2018-09-06 23:06:56 +02:00
|
|
|
context = option_bag.config_bag.context
|
2018-08-01 08:37:58 +02:00
|
|
|
hasvalue = self._p_.hasvalue(option_bag.path)
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2018-08-18 07:51:04 +02:00
|
|
|
if hasvalue and 'validator' in option_bag.config_bag.properties:
|
2018-09-06 23:06:56 +02:00
|
|
|
fake_context = context._gen_fake_values()
|
2018-08-18 10:03:08 +02:00
|
|
|
config_bag = option_bag.config_bag.copy()
|
|
|
|
config_bag.remove_validation()
|
2018-09-06 23:06:56 +02:00
|
|
|
config_bag.context = fake_context
|
2018-08-18 10:03:08 +02:00
|
|
|
soption_bag = option_bag.copy()
|
|
|
|
soption_bag.config_bag = config_bag
|
2017-11-20 17:01:36 +01:00
|
|
|
fake_value = fake_context.cfgimpl_get_values()
|
2018-08-18 10:03:08 +02:00
|
|
|
fake_value.reset(soption_bag)
|
2018-09-06 23:06:56 +02:00
|
|
|
soption_bag.config_bag.properties = option_bag.config_bag.properties
|
|
|
|
value = fake_value.getdefaultvalue(soption_bag)
|
2018-08-01 08:37:58 +02:00
|
|
|
fake_value.setvalue_validation(value,
|
2018-09-06 23:06:56 +02:00
|
|
|
soption_bag)
|
2018-08-01 08:37:58 +02:00
|
|
|
opt = option_bag.option
|
2019-02-23 19:06:23 +01:00
|
|
|
if opt.impl_is_leader():
|
|
|
|
opt.impl_get_leadership().reset(self,
|
|
|
|
option_bag,
|
|
|
|
_commit=_commit)
|
2017-11-20 17:01:36 +01:00
|
|
|
if hasvalue:
|
2018-08-01 08:37:58 +02:00
|
|
|
if 'force_store_value' in option_bag.properties:
|
|
|
|
value = self.getdefaultvalue(option_bag)
|
|
|
|
|
|
|
|
self._setvalue(option_bag,
|
2017-11-20 17:01:36 +01:00
|
|
|
value,
|
|
|
|
owners.forced,
|
|
|
|
commit=_commit)
|
|
|
|
else:
|
2018-08-01 08:37:58 +02:00
|
|
|
self._p_.resetvalue(option_bag.path,
|
2017-11-20 17:01:36 +01:00
|
|
|
_commit)
|
2018-08-01 08:37:58 +02:00
|
|
|
context.cfgimpl_reset_cache(option_bag)
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2019-02-23 19:06:23 +01:00
|
|
|
def reset_follower(self,
|
|
|
|
option_bag,
|
|
|
|
_commit=True):
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2018-08-01 08:37:58 +02:00
|
|
|
if self._p_.hasvalue(option_bag.path, index=option_bag.index):
|
2018-09-06 23:06:56 +02:00
|
|
|
context = option_bag.config_bag.context
|
2018-08-18 07:51:04 +02:00
|
|
|
if 'validator' in option_bag.config_bag.properties:
|
2018-03-24 22:37:48 +01:00
|
|
|
fake_context = context._gen_fake_values()
|
|
|
|
fake_value = fake_context.cfgimpl_get_values()
|
2018-08-18 10:03:08 +02:00
|
|
|
config_bag = option_bag.config_bag.copy()
|
|
|
|
config_bag.remove_validation()
|
2018-09-06 23:06:56 +02:00
|
|
|
config_bag.context = fake_context
|
2018-08-18 10:03:08 +02:00
|
|
|
soption_bag = option_bag.copy()
|
|
|
|
soption_bag.config_bag = config_bag
|
2019-02-23 19:06:23 +01:00
|
|
|
fake_value.reset_follower(soption_bag)
|
2018-08-18 10:03:08 +02:00
|
|
|
value = fake_value.getdefaultvalue(soption_bag)
|
|
|
|
fake_value.setvalue_validation(value,
|
|
|
|
soption_bag)
|
2018-10-31 16:08:22 +01:00
|
|
|
self._p_.resetvalue_index(option_bag.path,
|
|
|
|
option_bag.index,
|
|
|
|
_commit)
|
2018-08-01 08:37:58 +02:00
|
|
|
context.cfgimpl_reset_cache(option_bag)
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2019-02-23 19:06:23 +01:00
|
|
|
def reset_leadership(self,
|
|
|
|
index,
|
|
|
|
option_bag,
|
|
|
|
subconfig):
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2018-08-01 08:37:58 +02:00
|
|
|
current_value = self.get_cached_value(option_bag)
|
2018-03-24 22:37:48 +01:00
|
|
|
length = len(current_value)
|
|
|
|
if index >= length:
|
2019-06-12 08:45:56 +02:00
|
|
|
raise IndexError(_('index {} is greater than the length {} '
|
2018-03-24 22:37:48 +01:00
|
|
|
'for option "{}"').format(index,
|
|
|
|
length,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag.option.impl_get_display_name()))
|
2017-11-20 17:01:36 +01:00
|
|
|
current_value.pop(index)
|
2017-12-02 22:53:57 +01:00
|
|
|
subconfig.cfgimpl_get_description().pop(self,
|
2017-11-28 22:42:30 +01:00
|
|
|
index,
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag)
|
2019-07-16 08:03:33 +02:00
|
|
|
self.setvalue(current_value,
|
|
|
|
option_bag,
|
|
|
|
_commit=True)
|
2017-12-13 22:15:34 +01:00
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
2013-09-03 10:38:28 +02:00
|
|
|
# information
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2018-09-09 22:38:03 +02:00
|
|
|
def set_information(self, key, value, path=None):
|
2013-09-03 10:38:28 +02:00
|
|
|
"""updates the information's attribute
|
|
|
|
|
|
|
|
:param key: information's key (ex: "help", "doc"
|
|
|
|
:param value: information's value (ex: "the help string")
|
|
|
|
"""
|
2018-09-09 22:38:03 +02:00
|
|
|
self._p_.set_information(path, key, value)
|
2013-09-03 10:38:28 +02:00
|
|
|
|
2018-09-09 22:38:03 +02:00
|
|
|
def get_information(self, key, default=undefined, path=None):
|
2013-09-03 10:38:28 +02:00
|
|
|
"""retrieves one information's item
|
|
|
|
|
|
|
|
:param key: the item string (ex: "help")
|
|
|
|
"""
|
2018-09-09 22:38:03 +02:00
|
|
|
return self._p_.get_information(path, key, default)
|
2013-09-03 10:38:28 +02:00
|
|
|
|
2018-09-09 22:38:03 +02:00
|
|
|
def del_information(self, key, raises=True, path=None):
|
|
|
|
self._p_.del_information(path, key, raises)
|
2016-04-28 11:31:04 +02:00
|
|
|
|
2018-10-07 10:55:52 +02:00
|
|
|
def list_information(self, path=None):
|
|
|
|
return self._p_.list_information(path)
|
|
|
|
|
2017-11-20 17:01:36 +01:00
|
|
|
#______________________________________________________________________
|
|
|
|
# mandatory warnings
|
2018-04-06 16:09:10 +02:00
|
|
|
def _mandatory_warnings(self,
|
|
|
|
context,
|
|
|
|
config_bag,
|
|
|
|
description,
|
|
|
|
currpath,
|
2018-09-26 21:30:05 +02:00
|
|
|
subconfig,
|
2018-09-08 22:51:27 +02:00
|
|
|
od_config_bag):
|
2018-04-06 16:09:10 +02:00
|
|
|
settings = context.cfgimpl_get_settings()
|
2018-11-13 22:10:01 +01:00
|
|
|
for option in description.get_children(config_bag,
|
|
|
|
context):
|
2018-04-06 16:09:10 +02:00
|
|
|
name = option.impl_getname()
|
|
|
|
path = '.'.join(currpath + [name])
|
|
|
|
|
|
|
|
if option.impl_is_optiondescription():
|
|
|
|
try:
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag = OptionBag()
|
|
|
|
option_bag.set_option(option,
|
|
|
|
path,
|
|
|
|
None,
|
2018-09-08 22:51:27 +02:00
|
|
|
od_config_bag)
|
2018-11-17 12:08:15 +01:00
|
|
|
subsubconfig = subconfig.get_subconfig(option_bag)
|
2018-04-06 16:09:10 +02:00
|
|
|
except PropertiesOptionError as err:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for path in self._mandatory_warnings(context,
|
2018-08-01 08:37:58 +02:00
|
|
|
config_bag,
|
2018-04-06 16:09:10 +02:00
|
|
|
option,
|
|
|
|
currpath + [name],
|
2018-09-26 21:30:05 +02:00
|
|
|
subsubconfig,
|
2018-09-08 22:51:27 +02:00
|
|
|
od_config_bag):
|
2018-04-06 16:09:10 +02:00
|
|
|
yield path
|
|
|
|
elif not option.impl_is_symlinkoption():
|
|
|
|
# don't verifying symlink
|
|
|
|
try:
|
2019-02-23 19:06:23 +01:00
|
|
|
if not option.impl_is_follower():
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag = OptionBag()
|
|
|
|
option_bag.set_option(option,
|
|
|
|
path,
|
|
|
|
None,
|
|
|
|
config_bag)
|
|
|
|
if 'mandatory' in option_bag.properties or 'empty' in option_bag.properties:
|
2018-09-26 21:30:05 +02:00
|
|
|
subconfig.getattr(name,
|
|
|
|
option_bag)
|
2018-04-06 16:09:10 +02:00
|
|
|
else:
|
2018-09-26 21:30:05 +02:00
|
|
|
for index in range(subconfig.cfgimpl_get_length()):
|
2018-08-01 08:37:58 +02:00
|
|
|
option_bag = OptionBag()
|
|
|
|
option_bag.set_option(option,
|
|
|
|
path,
|
|
|
|
index,
|
|
|
|
config_bag)
|
|
|
|
if 'mandatory' in option_bag.properties:
|
2018-09-26 21:30:05 +02:00
|
|
|
subconfig.getattr(name,
|
|
|
|
option_bag)
|
2018-04-06 16:09:10 +02:00
|
|
|
except PropertiesOptionError as err:
|
|
|
|
if err.proptype == ['mandatory']:
|
|
|
|
yield path
|
2019-04-17 19:13:40 +02:00
|
|
|
except RequirementError:
|
|
|
|
pass
|
2018-04-06 16:09:10 +02:00
|
|
|
except ConfigError as err:
|
|
|
|
#assume that uncalculated value is an empty value
|
|
|
|
yield path
|
2017-11-20 17:01:36 +01:00
|
|
|
|
2017-12-05 21:49:19 +01:00
|
|
|
def mandatory_warnings(self,
|
2017-12-19 23:11:45 +01:00
|
|
|
config_bag):
|
2014-03-09 20:06:44 +01:00
|
|
|
"""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
|
2014-03-09 20:06:44 +01:00
|
|
|
"""
|
2018-09-06 23:06:56 +02:00
|
|
|
context = config_bag.context
|
2017-12-13 22:15:34 +01:00
|
|
|
# copy
|
2018-08-18 07:51:04 +02:00
|
|
|
od_setting_properties = config_bag.properties - {'mandatory', 'empty'}
|
|
|
|
setting_properties = set(config_bag.properties) - {'warnings'}
|
2016-06-29 21:42:54 +02:00
|
|
|
setting_properties.update(['mandatory', 'empty'])
|
2018-08-02 22:35:40 +02:00
|
|
|
config_bag = ConfigBag(context=config_bag.context)
|
2018-08-18 07:51:04 +02:00
|
|
|
config_bag.properties = frozenset(setting_properties)
|
2018-08-18 08:06:29 +02:00
|
|
|
config_bag.set_permissive()
|
2018-09-08 22:51:27 +02:00
|
|
|
od_config_bag = ConfigBag(context=config_bag.context)
|
|
|
|
od_config_bag.properties = frozenset(od_setting_properties)
|
|
|
|
od_config_bag.set_permissive()
|
2015-10-29 09:03:13 +01:00
|
|
|
|
2017-06-16 18:25:01 +02:00
|
|
|
descr = context.cfgimpl_get_description()
|
2018-04-11 16:36:15 +02:00
|
|
|
return self._mandatory_warnings(context,
|
|
|
|
config_bag,
|
|
|
|
descr,
|
|
|
|
[],
|
|
|
|
context,
|
2018-09-08 22:51:27 +02:00
|
|
|
od_config_bag)
|
2018-09-15 10:34:15 +02:00
|
|
|
|
|
|
|
#____________________________________________________________
|
|
|
|
# default owner methods
|
|
|
|
def set_context_owner(self,
|
|
|
|
owner):
|
|
|
|
":param owner: sets the default value for owner at the Config level"
|
|
|
|
if owner in forbidden_owners:
|
|
|
|
raise ValueError(_('set owner "{0}" is forbidden').format(str(owner)))
|
|
|
|
|
|
|
|
self._p_.setowner(None,
|
|
|
|
owner,
|
|
|
|
index=None)
|
|
|
|
|
|
|
|
def get_context_owner(self):
|
|
|
|
return self._p_.getowner(None,
|
|
|
|
None)
|