rougail/src/rougail/annotator/value.py

99 lines
3.8 KiB
Python
Raw Normal View History

2021-01-23 21:21:45 +01:00
"""Annotate value
2021-01-30 08:15:26 +01:00
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
2021-01-23 21:21:45 +01:00
"""
2021-02-12 07:48:27 +01:00
from .variable import Walk
2021-01-23 21:21:45 +01:00
2021-02-20 10:41:53 +01:00
from ..i18n import _
from ..error import DictConsistencyError
2021-02-12 07:48:27 +01:00
class ValueAnnotator(Walk): # pylint: disable=R0903
2021-01-23 21:21:45 +01:00
"""Annotate value
2021-01-23 21:15:26 +01:00
"""
def __init__(self,
objectspace,
) -> None:
if not hasattr(objectspace.space, 'variables'):
return
self.objectspace = objectspace
self.convert_value()
def convert_value(self) -> None:
2021-01-23 21:21:45 +01:00
"""convert value
"""
2021-02-12 07:48:27 +01:00
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)
2021-01-23 21:15:26 +01:00
2021-01-26 13:33:54 +01:00
def _convert_value(self,
variable,
2021-01-23 21:15:26 +01:00
variable_type: str=None,
) -> None:
2021-01-26 13:33:54 +01:00
# 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]
2021-02-18 17:00:12 +01:00
# if the variable is mandatory and doesn't have any value
# then the variable's mode is set to 'basic'
2021-01-26 13:33:54 +01:00
# 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
2021-02-23 21:36:26 +01:00
if has_value and 'mandatory' not in vars(variable):
2021-01-26 13:33:54 +01:00
# if has value without any calculation
variable.mandatory = True
2021-01-23 21:15:26 +01:00
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
2021-01-23 21:15:26 +01:00
else:
2021-02-20 10:41:53 +01:00
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)
2021-01-23 21:15:26 +01:00
variable.default = variable.value[0].name
del variable.value