"""Param annotator 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 """ import tiramisu from .variable import CONVERT_OPTION from ..i18n import _ from ..error import DictConsistencyError class ParamAnnotator: def valid_type_validation(self, obj, ) -> None: return None def convert_param(self, objects) -> None: """ valid and convert param """ for obj in objects: if not hasattr(obj, 'param'): continue param_to_delete = [] variable_type = self.valid_type_validation(obj) for param_idx, param in enumerate(obj.param): if hasattr(param, 'text'): if param.type in ['suffix', 'index']: msg = _(f'"{param.type}" parameter must not have a value') raise DictConsistencyError(msg, 28, obj.xmlfiles) elif param.type == 'nil': if param.text is not None: msg = _(f'"{param.type}" parameter must not have a value') raise DictConsistencyError(msg, 40, obj.xmlfiles) elif param.type == 'variable': try: path, suffix = self.objectspace.paths.get_variable_path(param.text, obj.namespace, ) param.text = self.objectspace.paths.get_variable(path) if variable_type and param.text.type != variable_type: msg = _(f'"{obj.name}" has type "{variable_type}" but param has type "{param.text.type}"') raise DictConsistencyError(msg, 26, param.xmlfiles) if suffix: param.suffix = suffix family_path = self.objectspace.paths.get_variable_family_path(path) param.family = self.objectspace.paths.get_family(family_path, param.text.namespace, ) except DictConsistencyError as err: if err.errno != 42 or not param.optional: raise err param_to_delete.append(param_idx) elif variable_type: self._convert_with_variable_type(variable_type, param) continue # no param.text if param.type == 'suffix': for target in obj.target: if not self.objectspace.paths.variable_is_dynamic(target.name.path): msg = _(f'"{param.type}" parameter cannot be set with target "{target.name}"' f' which is not a dynamic variable') raise DictConsistencyError(msg, 53, obj.xmlfiles) elif param.type == 'index': for target in obj.target: if not self.objectspace.paths.is_follower(target.name.path): msg = _(f'"{param.type}" parameter cannot be set with target "{target.name.name}"' f' which is not a follower variable') raise DictConsistencyError(msg, 60, obj.xmlfiles) pass elif param.type == 'nil': param.text = None elif param.type == 'string': param.text = '' if variable_type: self._convert_with_variable_type(variable_type, param) else: msg = _(f'"{param.type}" parameter must have a value') raise DictConsistencyError(msg, 27, obj.xmlfiles) param_to_delete.sort(reverse=True) for param_idx in param_to_delete: obj.param.pop(param_idx) def _convert_with_variable_type(self, variable_type: str, param: 'self.objectspace.param', ) -> None: if 'type' in vars(param) and variable_type != param.type: msg = _(f'parameter has incompatible type "{param.type}" ' f'with type "{variable_type}"') raise DictConsistencyError(msg, 7, param.xmlfiles) try: option = CONVERT_OPTION[variable_type] param.text = option.get('func', str)(param.text) getattr(tiramisu, option['opttype'])('test', 'Object to valid value', param.text, **option.get('initkwargs', {})) except ValueError as err: msg = _(f'unable to change type of value "{param.text}" ' f'is not a valid "{variable_type}"') raise DictConsistencyError(msg, 13, param.xmlfiles) from err param.type = variable_type