rougail/src/rougail/rougail.py

72 lines
2.6 KiB
Python

"""
Takes a bunch of Rougail XML dispatched in differents folders
as an input and outputs a Tiramisu's file
Sample usage::
>>> from rougail import Rougail
>>> rougail = Rougail('/usr/share/rougail/rougail.dtd')
>>> rougail.create_or_populate_from_xml(['/usr/share/rougail/dicos'])
>>> rougail.create_or_populate_from_xml(['/usr/share/rougail/extra1'], 'extra1')
>>> 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
from .objspace import RougailObjSpace
from .xmlreflector import XMLReflector
from .tiramisureflector import TiramisuReflector
from .annotator import SpaceAnnotator
class Rougail:
"""Rougail object
"""
def __init__(self,
dtdfilename: str,
) -> None:
self.xmlreflector = XMLReflector()
self.xmlreflector.parse_dtd(dtdfilename)
self.rougailobjspace = RougailObjSpace(self.xmlreflector)
self.funcs_path = None
def create_or_populate_from_xml(self,
xmlfolders: List[str],
namespace: str=None,
) -> List[str]:
"""Parses a bunch of XML files and populates the RougailObjSpace
"""
for xmlfile in self.xmlreflector.load_xml_from_folders(xmlfolders):
document = self.xmlreflector.parse_xmlfile(xmlfile)
self.rougailobjspace.redefine_variables = []
self.rougailobjspace.xml_parse_document(xmlfile,
document,
self.rougailobjspace.space,
namespace,
)
def space_visitor(self,
eosfunc_file: str,
) -> None:
"""All XML are loader, now annotate content
"""
self.funcs_path = eosfunc_file
SpaceAnnotator(self.rougailobjspace, eosfunc_file)
def save(self) -> str:
"""Return tiramisu object declaration as a string
"""
tiramisu_objects = TiramisuReflector(self.rougailobjspace.space,
self.funcs_path,
)
return tiramisu_objects.get_text() + '\n'