rougail/src/rougail/convert.py

113 lines
4.1 KiB
Python
Raw Normal View History

2021-01-30 08:15:26 +01:00
"""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
2020-12-24 16:19:31 +01:00
Sample usage::
2021-02-22 19:28:51 +01:00
>>> from rougail import RougailConvert
>>> rougail = RougailConvert()
>>> tiramisu = rougail.save('tiramisu.py')
2020-12-24 16:19:31 +01:00
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
2021-02-14 10:10:48 +01:00
from .i18n import _
from .config import RougailConfig
2020-12-24 16:19:31 +01:00
from .objspace import RougailObjSpace
from .xmlreflector import XMLReflector
from .tiramisureflector import TiramisuReflector
from .annotator import SpaceAnnotator
2021-02-14 10:10:48 +01:00
from .error import DictConsistencyError
2020-12-24 16:19:31 +01:00
2021-02-14 10:10:48 +01:00
class RougailConvert:
2021-01-18 17:46:21 +01:00
"""Rougail object
"""
2021-02-16 12:08:45 +01:00
def __init__(self,
rougailconfig: RougailConfig=None,
) -> None:
if rougailconfig is None:
rougailconfig = RougailConfig
xmlreflector = XMLReflector(rougailconfig)
rougailobjspace = RougailObjSpace(xmlreflector,
rougailconfig,
)
2021-02-14 17:48:50 +01:00
self._load_dictionaries(xmlreflector,
rougailobjspace,
2021-02-16 12:08:45 +01:00
rougailconfig['variable_namespace'],
rougailconfig['dictionaries_dir'],
2021-02-14 17:48:50 +01:00
)
2021-02-16 12:08:45 +01:00
for namespace, extra_dir in rougailconfig['extra_dictionaries'].items():
if namespace in ['services', rougailconfig['variable_namespace']]:
2021-02-14 17:48:50 +01:00
msg = _(f'Namespace name "{namespace}" is not allowed')
raise DictConsistencyError(msg, 21, None)
self._load_dictionaries(xmlreflector,
rougailobjspace,
namespace,
extra_dir,
)
2021-02-16 12:08:45 +01:00
functions_file = rougailconfig['functions_file']
if not isinstance(functions_file, list):
functions_file = [functions_file]
2021-02-14 17:48:50 +01:00
SpaceAnnotator(rougailobjspace,
functions_file,
)
self.output = TiramisuReflector(rougailobjspace,
functions_file,
2021-12-10 23:35:44 +01:00
rougailconfig['internal_functions'],
2021-02-14 17:48:50 +01:00
).get_text() + '\n'
2021-02-14 10:10:48 +01:00
2021-02-18 17:00:12 +01:00
@staticmethod
def _load_dictionaries(xmlreflector: XMLReflector,
2021-02-14 17:48:50 +01:00
rougailobjspace: RougailObjSpace,
2021-02-14 10:10:48 +01:00
namespace: str,
xmlfolders: List[str],
) -> List[str]:
2021-02-14 17:48:50 +01:00
for xmlfile, document in xmlreflector.load_xml_from_folders(xmlfolders):
rougailobjspace.xml_parse_document(xmlfile,
document,
namespace,
)
2020-12-24 16:19:31 +01:00
2021-02-14 10:10:48 +01:00
def save(self,
filename: str,
) -> str:
2021-01-18 17:46:21 +01:00
"""Return tiramisu object declaration as a string
"""
2021-02-14 10:10:48 +01:00
if filename:
with open(filename, 'w') as tiramisu:
2021-02-14 17:48:50 +01:00
tiramisu.write(self.output)
return self.output