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::
|
|
|
|
|
|
|
|
>>> from rougail import Rougail
|
2021-02-14 10:10:48 +01:00
|
|
|
>>> rougail = Rougail()
|
|
|
|
>>> rougail.load_dictionaries(['/usr/share/rougail/dicos'])
|
|
|
|
>>> rougail.load_extra_dictionaries('extra1', ['/usr/share/rougail/extra1'])
|
2020-12-24 16:19:31 +01:00
|
|
|
>>> rougail.space_visitor('/usr/share/rougail/funcs.py')
|
|
|
|
>>> tiramisu = rougail.save()
|
|
|
|
|
|
|
|
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-14 10:10:48 +01:00
|
|
|
def __init__(self) -> None:
|
2021-02-14 17:48:50 +01:00
|
|
|
xmlreflector = XMLReflector()
|
|
|
|
rougailobjspace = RougailObjSpace(xmlreflector)
|
|
|
|
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'
|
2021-02-14 10:10:48 +01:00
|
|
|
|
|
|
|
def _load_dictionaries(self,
|
2021-02-14 17:48:50 +01:00
|
|
|
xmlreflector: XMLReflector,
|
|
|
|
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
|