"""Takes a bunch of Rougail XML dispatched in differents folders as an input and outputs a Tiramisu's file. 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 Sample usage:: >>> from rougail import RougailConvert >>> rougail = RougailConvert() >>> tiramisu = rougail.save('tiramisu.py') The Rougail - loads the XML into an internal RougailObjSpace representation - visits/annotates the objects - dumps the object space as Tiramisu string The visit/annotation stage is a complex step that corresponds to the Rougail procedures. """ from typing import List from .i18n import _ from .config import RougailConfig from .objspace import RougailObjSpace from .xmlreflector import XMLReflector from .tiramisureflector import TiramisuReflector from .annotator import SpaceAnnotator from .error import DictConsistencyError class RougailConvert: """Rougail object """ def __init__(self, rougailconfig: RougailConfig=None, ) -> None: if rougailconfig is None: rougailconfig = RougailConfig xmlreflector = XMLReflector(rougailconfig) rougailobjspace = RougailObjSpace(xmlreflector, rougailconfig, ) self._load_dictionaries(xmlreflector, rougailobjspace, rougailconfig['variable_namespace'], rougailconfig['dictionaries_dir'], ) for namespace, extra_dir in rougailconfig['extra_dictionaries'].items(): if namespace in ['services', rougailconfig['variable_namespace']]: msg = _(f'Namespace name "{namespace}" is not allowed') raise DictConsistencyError(msg, 21, None) self._load_dictionaries(xmlreflector, rougailobjspace, namespace, extra_dir, ) functions_file = rougailconfig['functions_file'] SpaceAnnotator(rougailobjspace, functions_file, ) self.output = TiramisuReflector(rougailobjspace, functions_file, ).get_text() + '\n' @staticmethod def _load_dictionaries(xmlreflector: XMLReflector, rougailobjspace: RougailObjSpace, namespace: str, xmlfolders: List[str], ) -> List[str]: for xmlfile, document in xmlreflector.load_xml_from_folders(xmlfolders): rougailobjspace.xml_parse_document(xmlfile, document, namespace, ) def save(self, filename: str, ) -> str: """Return tiramisu object declaration as a string """ if filename: with open(filename, 'w') as tiramisu: tiramisu.write(self.output) return self.output