rougail/src/rougail/annotator/value.py

106 lines
3.9 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-04-12 15:04:38 +02:00
from rougail.annotator.variable import Walk
2021-01-23 21:21:45 +01:00
2021-04-12 15:04:38 +02:00
from rougail.i18n import _
from rougail.error import DictConsistencyError
2021-02-20 10:41:53 +01:00
2021-04-12 15:04:38 +02:00
class Annotator(Walk): # pylint: disable=R0903
2021-01-23 21:21:45 +01:00
"""Annotate value
2021-01-23 21:15:26 +01:00
"""
2021-04-12 15:04:38 +02:00
level = 70
2021-01-23 21:15:26 +01:00
def __init__(self,
objectspace,
2021-04-12 15:04:38 +02:00
*args,
2021-01-23 21:15:26 +01:00
) -> None:
if not hasattr(objectspace.space, 'variables'):
return
self.objectspace = objectspace
self.convert_value()
2021-05-01 18:32:45 +02:00
self.add_choice_nil()
2021-01-23 21:15:26 +01:00
def convert_value(self) -> None:
2021-01-23 21:21:45 +01:00
"""convert value
"""
2021-04-14 17:34:38 +02:00
for variable in self.get_variables():
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
) -> 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]
elif variable.multi:
2021-04-14 17:34:38 +02:00
if not self.objectspace.paths.is_follower(variable.path):
variable.default = [value.name for value in variable.value]
2021-04-14 17:34:38 +02:00
if not self.objectspace.paths.is_leader(variable.path):
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:
if len(variable.value) > 1:
2021-05-01 18:32:45 +02:00
msg = _(f'the none multi variable "{variable.name}" cannot have '
'more than one value')
raise DictConsistencyError(msg, 68, variable.xmlfiles)
variable.default = variable.value[0].name
2021-01-23 21:15:26 +01:00
del variable.value
2021-05-01 18:32:45 +02:00
def add_choice_nil(self) -> None:
for variable in self.get_variables():
if variable.type != 'choice':
continue
is_none = False
for choice in variable.choice:
if choice.type == 'nil':
is_none = True
if not variable.mandatory and not is_none:
choice = self.objectspace.choice(variable.xmlfiles)
choice.name = None
choice.type = 'nil'
variable.choice.append(choice)