From d395f4a17b6949d694bf79e60e91187df2e541c3 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 24 Dec 2020 16:19:31 +0100 Subject: [PATCH] create a new Rougail object --- src/rougail/__init__.py | 2 +- src/rougail/objspace.py | 57 ++++------------------------------- src/rougail/rougail.py | 63 +++++++++++++++++++++++++++++++++++++++ tests/test_1_flattener.py | 22 ++------------ 4 files changed, 72 insertions(+), 72 deletions(-) create mode 100644 src/rougail/rougail.py diff --git a/src/rougail/__init__.py b/src/rougail/__init__.py index 6fa753fc..bf8873fb 100644 --- a/src/rougail/__init__.py +++ b/src/rougail/__init__.py @@ -1,5 +1,5 @@ #from .loader import load -from .objspace import RougailObjSpace +from .rougail import Rougail from .annotator import modes __ALL__ = ('RougailObjSpace', 'modes') diff --git a/src/rougail/objspace.py b/src/rougail/objspace.py index 94e39c61..94c76231 100644 --- a/src/rougail/objspace.py +++ b/src/rougail/objspace.py @@ -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 .i18n import _ from .xmlreflector import XMLReflector -from .annotator import SpaceAnnotator -from .tiramisureflector import TiramisuReflector from .utils import normalize_family -from .error import OperationError, SpaceObjShallNotBeUpdated, DictConsistencyError +from .error import SpaceObjShallNotBeUpdated, DictConsistencyError from .path import Path from .config import Config @@ -75,12 +52,13 @@ class RougailObjSpace: """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.xmlreflector = xmlreflector self.space = ObjSpace() self.paths = Path() - self.xmlreflector = XMLReflector() - self.xmlreflector.parse_dtd(dtdfilename) self.redefine_variables = None self.forced_text_elts = set() @@ -142,21 +120,6 @@ class RougailObjSpace: return '"' + xmlfiles[0] + '"' 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, xmlfile, document, @@ -457,13 +420,3 @@ class RougailObjSpace: getattr(space, child.tag).append(variableobj) else: 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' diff --git a/src/rougail/rougail.py b/src/rougail/rougail.py new file mode 100644 index 00000000..71880435 --- /dev/null +++ b/src/rougail/rougail.py @@ -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' diff --git a/tests/test_1_flattener.py b/tests/test_1_flattener.py index 3d3e8c0c..5e112c69 100644 --- a/tests/test_1_flattener.py +++ b/tests/test_1_flattener.py @@ -5,7 +5,7 @@ from pytest import fixture, raises from os import listdir from json import load -from rougail import objspace, annotator +from rougail import Rougail, annotator from rougail.error import DictConsistencyError from rougail.config import Config @@ -54,7 +54,7 @@ def test_dir_error(request): def launch_flattener(test_dir, test_ok=False): - eolobj = objspace.RougailObjSpace(Config['dtdfilename']) + eolobj = Rougail(Config['dtdfilename']) dirs = [test_dir] subfolder = join(test_dir, 'subfolder') if isdir(subfolder): @@ -81,22 +81,6 @@ def launch_flattener(test_dir, test_ok=False): 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): assert getcwd() == ORI_DIR test_dir = join(dico_dirs, test_dir) @@ -121,4 +105,4 @@ def test_error_dictionary(test_dir_error): def test_no_dtd(): with raises(IOError): - eolobj = objspace.RougailObjSpace('notexists.dtd') + eolobj = Rougail('notexists.dtd')