"""Annotate value Created by: EOLE (http://eole.orion.education.fr) Copyright (C) 2005-2018 Forked by: Cadoles (http://www.cadoles.com) Copyright (C) 2019-2021 distribued with GPL-2 or later license This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """ from .variable import Walk from ..i18n import _ from ..error import DictConsistencyError class ValueAnnotator(Walk): # pylint: disable=R0903 """Annotate value """ def __init__(self, objectspace, ) -> None: if not hasattr(objectspace.space, 'variables'): return self.objectspace = objectspace self.convert_value() def convert_value(self) -> None: """convert value """ for variable in self.get_variables(with_leadership=True): if isinstance(variable, self.objectspace.leadership): variable_type = 'leader' for follower in variable.variable: self._convert_value(follower, variable_type, ) variable_type = 'follower' else: self._convert_value(variable) def _convert_value(self, variable, variable_type: str=None, ) -> None: # a boolean must have value, the default value is "True" if not hasattr(variable, 'value') and variable.type == 'boolean': new_value = self.objectspace.value(variable.xmlfiles) new_value.name = True new_value.type = 'boolean' variable.value = [new_value] # if the variable is mandatory and doesn't have any value # then the variable's mode is set to 'basic' # variable with default value is mandatory if hasattr(variable, 'value') and variable.value: has_value = True for value in variable.value: if value.type == 'calculation': has_value = False break if has_value and 'mandatory' not in vars(variable): # if has value without any calculation variable.mandatory = True if not hasattr(variable, 'value'): return if variable.value[0].type == 'calculation': variable.default = variable.value[0] else: if variable.multi: if variable_type != 'follower': variable.default = [value.name for value in variable.value] if variable_type != 'leader': if variable.multi == 'submulti': variable.default_multi = [value.name for value in variable.value] else: variable.default_multi = variable.value[0].name else: if len(variable.value) > 1: msg = _(f'the non multi variable "{variable.name}" cannot have ' 'more than one value') raise DictConsistencyError(msg, 68, variable.xmlfiles) variable.default = variable.value[0].name del variable.value