# -*- coding: utf-8 -*- "takes care of the option's values and multi values" # Copyright (C) 2013-2018 Team tiramisu (see AUTHORS for all contributors) # # 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. # # 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. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . # ____________________________________________________________ import weakref from .error import ConfigError, PropertiesOptionError from .setting import owners, expires_time, undefined, forbidden_owners, OptionBag, ConfigBag from .autolib import carry_out_calculation from .i18n import _ class Values(object): """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. """ __slots__ = ('context', '_p_', '__weakref__') def __init__(self, context, storage): """ Initializes the values's dict. :param context: the context is the home config's values :param storage: where values or owners are stored """ self.context = weakref.ref(context) # store the storage self._p_ = storage #______________________________________________________________________ # get context def _getcontext(self): """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() if context is None: # pragma: no cover raise ConfigError(_('the context does not exist anymore')) return context #______________________________________________________________________ # get value def get_cached_value(self, option_bag): """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 """ # try to retrive value in cache setting_properties = option_bag.config_bag.setting_properties is_cached, value = self._p_.getcache(option_bag.path, expires_time, option_bag.index, setting_properties, option_bag.properties, 'value') if not is_cached: # no cached value so get value value = self.getvalue(option_bag) # validate value context = self._getcontext() opt = option_bag.option opt.impl_validate(value, option_bag, context=context, check_error=True) if setting_properties and 'warnings' in setting_properties: opt.impl_validate(value, option_bag, context=context, check_error=False) # store value in cache if not is_cached: self._p_.setcache(option_bag.path, option_bag.index, value, setting_properties, option_bag.properties) if isinstance(value, list): # return a copy, so value cannot be modified return value.copy() # and return it return value def getvalue(self, option_bag): """actually retrieves the value :param path: the path of the `Option` :param index: index for a slave `Option` :returns: value """ # get owner and value from store # index allowed only for slave index = option_bag.index is_slave = option_bag.option.impl_is_master_slaves('slave') if index is None or not is_slave: _index = None else: _index = index owner, value = self._p_.getowner(option_bag.path, owners.default, index=_index, with_value=True) 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 if not ('frozen' in option_bag.properties and \ 'force_default_on_freeze' in option_bag.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 return self.getdefaultvalue(option_bag) def getdefaultvalue(self, option_bag): """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 """ context = self._getcontext() config_bag = option_bag.config_bag opt = option_bag.option index = option_bag.index def _reset_cache(_value): is_cache, cache_value = self._p_.getcache(option_bag.path, expires_time, index, config_bag.setting_properties, option_bag.properties, 'value') if is_cache and cache_value == _value: # calculation return same value as previous value, # so do not invalidate cache return # calculated value is a new value, so reset cache context.cfgimpl_reset_cache(option_bag) if opt.impl_is_master_slaves('slave'): index_ = index else: index_ = None if option_bag.index != index_: moption_bag = OptionBag() moption_bag.set_option(opt, option_bag.path, index_, config_bag) moption_bag.fromconsistency = option_bag.fromconsistency.copy() else: moption_bag = option_bag if self._is_meta(moption_bag): meta = context.cfgimpl_get_meta() # retrieved value from meta config try: # FIXME could have different property! value = meta.getattr(moption_bag.path, moption_bag) except PropertiesOptionError: # if properties error, return an other default value # unexpected error, should not happened pass else: return value if opt.impl_has_callback(): # if value has callback, calculate value callback, callback_params = opt.impl_get_callback() value = carry_out_calculation(opt, context=context, callback=callback, callback_params=callback_params, index=index, option_bag=option_bag) if isinstance(value, list) and index is not None: # 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(value) return value if len(value) > index: # return the value for specified index if found _reset_cache(value[index]) return value[index] # there is no calculate value for this index, # so return an other default value else: if opt.impl_is_submulti(): if isinstance(value, list): # value is a list, but no index specified if (value != [] and not isinstance(value[0], list)): # if submulti, return a list of value value = [value] elif index is not None: # if submulti, return a list of value value = [value] else: # return a list of list for a submulti value = [[value]] elif opt.impl_is_multi() and not isinstance(value, list) and index is None: # return a list for a multi value = [value] _reset_cache(value) return value # 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: # if index, must return good value for this index if len(value) > index: value = value[index] else: # no value for this index, retrieve default multi value # default_multi is already a list for submulti value = opt.impl_getdefault_multi() return value def isempty(self, opt, value, force_allow_empty_list=False, index=None): "convenience method to know if an option is empty" empty = opt._empty 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: 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 #______________________________________________________________________ # set value def setvalue(self, value, option_bag, _commit): context = self._getcontext() owner = context.cfgimpl_get_settings().getowner() if option_bag.config_bag.validate: if option_bag.index is not None or option_bag.option._has_consistencies(context): # set value to a fake config when option has dependency # validation will be complet in this case (consistency, ...) tested_context = context._gen_fake_values() #sconfig_bag = config_bag.copy() ori_validate = option_bag.config_bag.validate if ori_validate is True: option_bag.config_bag.validate = False try: tested_context.cfgimpl_get_values().setvalue(value, option_bag, True) option_bag.config_bag.validate = True tested_context.getattr(option_bag.path, option_bag) except Exception as exc: if ori_validate is True: option_bag.config_bag.validate = True raise exc else: self.setvalue_validation(value, option_bag) self._setvalue(option_bag, value, owner, commit=_commit) def setvalue_validation(self, value, option_bag): context = self._getcontext() settings = context.cfgimpl_get_settings() # First validate properties with this value opt = option_bag.option settings.validate_frozen(option_bag) settings.validate_mandatory(value, option_bag) # Value must be valid for option opt.impl_validate(value, option_bag, context, check_error=True) if option_bag.config_bag.setting_properties and \ 'warnings' in option_bag.config_bag.setting_properties: # No error found so emit warnings opt.impl_validate(value, option_bag, context, check_error=False) def _setvalue(self, option_bag, value, owner, commit=True): self._getcontext().cfgimpl_reset_cache(option_bag) if isinstance(value, list): # copy value = value.copy() self._p_.setvalue(option_bag.path, value, owner, option_bag.index, commit) def _is_meta(self, option_bag, force_owner_is_default=False): if not force_owner_is_default and self._p_.hasvalue(option_bag.path, index=option_bag.index): # has already a value, so not meta return False context = self._getcontext() meta = context.cfgimpl_get_meta() if meta is None: return False opt = option_bag.option if opt.impl_is_master_slaves('slave'): master = opt.impl_get_master_slaves().getmaster() masterp = master.impl_getpath(context) # slave could be a "meta" only if master hasn't value if self._p_.hasvalue(masterp, index=None): return False return not meta.cfgimpl_get_values().is_default_owner(option_bag) #______________________________________________________________________ # owner def is_default_owner(self, option_bag, validate_meta=undefined): return self.getowner(option_bag, validate_meta=validate_meta, only_default=True) == owners.default def getowner(self, option_bag, validate_meta=True, only_default=False): """ retrieves the option's owner :param opt: the `option.Option` object :param force_permissive: behaves as if the permissive property was present :returns: a `setting.owners.Owner` object """ context = self._getcontext() opt = option_bag.option if opt.impl_is_symlinkoption(): option_bag.ori_option = opt opt = opt.impl_getopt() option_bag.option = opt option_bag.path = opt.impl_getpath(context) self_properties = option_bag.properties settings = context.cfgimpl_get_settings() if option_bag.config_bag.setting_properties is not None: settings.validate_properties(option_bag) if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties: return owners.default if only_default: if self._p_.hasvalue(option_bag.path, option_bag.index): owner = undefined else: owner = owners.default else: owner = self._p_.getowner(option_bag.path, owners.default, index=option_bag.index) if owner is owners.default and validate_meta is not False: meta = context.cfgimpl_get_meta() if meta is not None and self._is_meta(option_bag): owner = meta.cfgimpl_get_values().getowner(option_bag, only_default=only_default) return owner def setowner(self, owner, option_bag): """ sets a owner to an option :param opt: the `option.Option` object :param owner: a valid owner, that is a `setting.owners.Owner` object """ opt = option_bag.option if opt.impl_is_symlinkoption(): raise ConfigError(_("can't set owner for the symlinkoption \"{}\"" "").format(opt.impl_get_display_name())) if owner in forbidden_owners: raise ConfigError(_('set owner "{0}" is forbidden').format(str(owner))) if not self._p_.hasvalue(option_bag.path): raise ConfigError(_('no value for {0} cannot change owner to {1}' '').format(option_bag.path, owner)) self._getcontext().cfgimpl_get_settings().validate_frozen(option_bag) self._p_.setowner(option_bag.path, owner, index=option_bag.index) #______________________________________________________________________ # reset def reset(self, option_bag, _commit=True): context = self._getcontext() setting = context.cfgimpl_get_settings() hasvalue = self._p_.hasvalue(option_bag.path) if hasvalue and option_bag.config_bag.validate: ori_validate = option_bag.config_bag.validate if ori_validate is True: option_bag.config_bag.validate = False fake_context = context._gen_fake_values() fake_value = fake_context.cfgimpl_get_values() fake_value.reset(option_bag) if ori_validate is True: option_bag.config_bag.validate = True value = fake_value.getdefaultvalue(option_bag) fake_value.setvalue_validation(value, option_bag) opt = option_bag.option if opt.impl_is_master_slaves('master'): opt.impl_get_master_slaves().reset(self, option_bag, _commit=_commit) if hasvalue: if 'force_store_value' in option_bag.properties: value = self.getdefaultvalue(option_bag) self._setvalue(option_bag, value, owners.forced, commit=_commit) else: self._p_.resetvalue(option_bag.path, _commit) if not opt.impl_is_master_slaves('master'): # if master, already reset behind pass context.cfgimpl_reset_cache(option_bag) def reset_slave(self, option_bag): if self._p_.hasvalue(option_bag.path, index=option_bag.index): context = self._getcontext() if option_bag.config_bag.validate: fake_context = context._gen_fake_values() fake_value = fake_context.cfgimpl_get_values() ori_validate = option_bag.config_bag.validate option_bag.config_bag.validate = False try: fake_value.reset_slave(option_bag) value = fake_value.getdefaultvalue(option_bag) fake_value.setvalue_validation(value, option_bag) option_bag.config_bag.validate = ori_validate except Exception as err: option_bag.config_bag.validate = ori_validate raise err self._p_.resetvalue_index(option_bag.path, option_bag.index) context.cfgimpl_reset_cache(option_bag) def reset_master(self, index, option_bag, subconfig): current_value = self.get_cached_value(option_bag) length = len(current_value) if index >= length: raise IndexError(_('index "{}" is higher than the length "{}" ' 'for option "{}"').format(index, length, option_bag.option.impl_get_display_name())) current_value.pop(index) self.setvalue(current_value, option_bag, _commit=True) subconfig.cfgimpl_get_description().pop(self, index, option_bag) #______________________________________________________________________ # information 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") """ return self._p_.get_information(key, default) def del_information(self, key, raises=True): self._p_.del_information(key, raises) #______________________________________________________________________ # mandatory warnings def _mandatory_warnings(self, context, config_bag, description, currpath, config, od_setting_properties): settings = context.cfgimpl_get_settings() # for option in config.cfgimpl_get_children(self.config_bag): for option in description.impl_getchildren(config_bag, context): name = option.impl_getname() path = '.'.join(currpath + [name]) if option.impl_is_optiondescription(): ori_setting_properties = config_bag._setting_properties config_bag._setting_properties = od_setting_properties try: option_bag = OptionBag() option_bag.set_option(option, path, None, config_bag) subconfig = config.get_subconfig(name, option_bag) except PropertiesOptionError as err: pass else: config_bag._setting_properties = ori_setting_properties for path in self._mandatory_warnings(context, config_bag, option, currpath + [name], subconfig, od_setting_properties): yield path elif not option.impl_is_symlinkoption(): # don't verifying symlink try: if not option.impl_is_master_slaves('slave'): option_bag = OptionBag() option_bag.set_option(option, path, None, config_bag) if 'mandatory' in option_bag.properties or 'empty' in option_bag.properties: config.getattr(name, option_bag) else: for index in range(config.cfgimpl_get_length()): option_bag = OptionBag() option_bag.set_option(option, path, index, config_bag) if 'mandatory' in option_bag.properties: config.getattr(name, option_bag) except PropertiesOptionError as err: if err.proptype == ['mandatory']: yield path except ConfigError as err: #assume that uncalculated value is an empty value yield path def mandatory_warnings(self, config_bag): """convenience function to trace Options that are mandatory and where no value has been set :returns: generator of mandatory Option's path """ context = self._getcontext() # copy od_setting_properties = config_bag.setting_properties - {'mandatory', 'empty'} setting_properties = set(config_bag.setting_properties) - {'warnings'} setting_properties.update(['mandatory', 'empty']) config_bag = ConfigBag(config=config_bag.config) config_bag._setting_properties = frozenset(setting_properties) config_bag.force_permissive = True descr = context.cfgimpl_get_description() return self._mandatory_warnings(context, config_bag, descr, [], context, od_setting_properties)