create a new Rougail object

This commit is contained in:
Emmanuel Garette 2020-12-24 16:19:31 +01:00
parent 9feb45e165
commit d395f4a17b
4 changed files with 72 additions and 72 deletions

View file

@ -1,5 +1,5 @@
#from .loader import load #from .loader import load
from .objspace import RougailObjSpace from .rougail import Rougail
from .annotator import modes from .annotator import modes
__ALL__ = ('RougailObjSpace', 'modes') __ALL__ = ('RougailObjSpace', 'modes')

View file

@ -1,33 +1,10 @@
"""
Takes a bunch of Rougail XML dispatched in differents folders
as an input and outputs a Tiramisu's file
Sample usage::
>>> from rougail.objspace import RougailObjSpace
>>> eolobj = RougailObjSpace('/usr/share/rougail/rougail.dtd')
>>> eolobj.create_or_populate_from_xml('rougail', ['/usr/share/rougail/dicos'])
>>> eolobj.space_visitor('/usr/share/rougail/funcs.py')
>>> tiramisu = eolobj.save()
The RougailObjSpace
- 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 typing import List
from .i18n import _ from .i18n import _
from .xmlreflector import XMLReflector from .xmlreflector import XMLReflector
from .annotator import SpaceAnnotator
from .tiramisureflector import TiramisuReflector
from .utils import normalize_family from .utils import normalize_family
from .error import OperationError, SpaceObjShallNotBeUpdated, DictConsistencyError from .error import SpaceObjShallNotBeUpdated, DictConsistencyError
from .path import Path from .path import Path
from .config import Config from .config import Config
@ -75,12 +52,13 @@ class RougailObjSpace:
"""Rougail ObjectSpace is an object's reflexion of the XML elements """Rougail ObjectSpace is an object's reflexion of the XML elements
""" """
def __init__(self, dtdfilename): def __init__(self,
xmlreflector: XMLReflector,
) -> None:
self.index = 0 self.index = 0
self.xmlreflector = xmlreflector
self.space = ObjSpace() self.space = ObjSpace()
self.paths = Path() self.paths = Path()
self.xmlreflector = XMLReflector()
self.xmlreflector.parse_dtd(dtdfilename)
self.redefine_variables = None self.redefine_variables = None
self.forced_text_elts = set() self.forced_text_elts = set()
@ -142,21 +120,6 @@ class RougailObjSpace:
return '"' + xmlfiles[0] + '"' return '"' + xmlfiles[0] + '"'
return '"' + '", "'.join(xmlfiles[:-1]) + '"' + ' and ' + '"' + xmlfiles[-1] + '"' return '"' + '", "'.join(xmlfiles[:-1]) + '"' + ' and ' + '"' + xmlfiles[-1] + '"'
def create_or_populate_from_xml(self,
namespace: str,
xmlfolders: List[str],
) -> 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.redefine_variables = []
self.xml_parse_document(xmlfile,
document,
self.space,
namespace,
)
def xml_parse_document(self, def xml_parse_document(self,
xmlfile, xmlfile,
document, document,
@ -457,13 +420,3 @@ class RougailObjSpace:
getattr(space, child.tag).append(variableobj) getattr(space, child.tag).append(variableobj)
else: else:
setattr(space, child.tag, variableobj) setattr(space, child.tag, variableobj)
def space_visitor(self, eosfunc_file):
self.funcs_path = eosfunc_file
SpaceAnnotator(self, eosfunc_file)
def save(self):
tiramisu_objects = TiramisuReflector(self.space,
self.funcs_path,
)
return tiramisu_objects.get_text() + '\n'

63
src/rougail/rougail.py Normal file
View file

@ -0,0 +1,63 @@
"""
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('rougail', ['/usr/share/rougail/dicos'])
>>> 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:
def __init__(self,
dtdfilename: str,
) -> None:
self.xmlreflector = XMLReflector()
self.xmlreflector.parse_dtd(dtdfilename)
self.rougailobjspace = RougailObjSpace(self.xmlreflector)
def create_or_populate_from_xml(self,
namespace: str,
xmlfolders: List[str],
) -> 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:
self.funcs_path = eosfunc_file
SpaceAnnotator(self.rougailobjspace, eosfunc_file)
def save(self) -> str:
tiramisu_objects = TiramisuReflector(self.rougailobjspace.space,
self.funcs_path,
)
return tiramisu_objects.get_text() + '\n'

View file

@ -5,7 +5,7 @@ from pytest import fixture, raises
from os import listdir from os import listdir
from json import load from json import load
from rougail import objspace, annotator from rougail import Rougail, annotator
from rougail.error import DictConsistencyError from rougail.error import DictConsistencyError
from rougail.config import Config from rougail.config import Config
@ -54,7 +54,7 @@ def test_dir_error(request):
def launch_flattener(test_dir, test_ok=False): def launch_flattener(test_dir, test_ok=False):
eolobj = objspace.RougailObjSpace(Config['dtdfilename']) eolobj = Rougail(Config['dtdfilename'])
dirs = [test_dir] dirs = [test_dir]
subfolder = join(test_dir, 'subfolder') subfolder = join(test_dir, 'subfolder')
if isdir(subfolder): if isdir(subfolder):
@ -81,22 +81,6 @@ def launch_flattener(test_dir, test_ok=False):
assert tiramisu_objects == tiramisu_objects_ori assert tiramisu_objects == tiramisu_objects_ori
def fake_traduc(txt):
return txt
def setup_module(module):
module.traduc_ori = objspace._
objspace._ = fake_traduc
annotator._ = fake_traduc
objspace.ServiceAnnotator = getattr(annotator, 'ServiceAnnotator')
def teardown_module(module):
objspace._ = module.traduc_ori
annotator._ = module.traduc_ori
def test_dictionary(test_dir): def test_dictionary(test_dir):
assert getcwd() == ORI_DIR assert getcwd() == ORI_DIR
test_dir = join(dico_dirs, test_dir) test_dir = join(dico_dirs, test_dir)
@ -121,4 +105,4 @@ def test_error_dictionary(test_dir_error):
def test_no_dtd(): def test_no_dtd():
with raises(IOError): with raises(IOError):
eolobj = objspace.RougailObjSpace('notexists.dtd') eolobj = Rougail('notexists.dtd')