From 5b2871049c30d2e18917519c6b800d68eaebaff3 Mon Sep 17 00:00:00 2001 From: Humberto Sanchez II Date: Mon, 18 Sep 2023 19:22:24 -0500 Subject: [PATCH] []: [] [https://github.com/hasii2011/oglio/issues/42] --- oglio/Writer.py | 21 ++++++++------- oglio/toXmlV11/OglToXml.py | 26 +++++++++++++------ requirements.txt | 2 +- tests/oglio/TestWriter.py | 9 +++++-- .../testdata/SimpleMultipleDocumentV11.xml | 13 ++++++++++ 5 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 tests/resources/testdata/SimpleMultipleDocumentV11.xml diff --git a/oglio/Writer.py b/oglio/Writer.py index b798c21..5f1c5ec 100644 --- a/oglio/Writer.py +++ b/oglio/Writer.py @@ -7,14 +7,17 @@ from oglio.Types import OglProject -from oglio.toXmlV10.OglToDom import OglToDom as OglToMiniDomV10 +from oglio.toXmlV11.OglToXml import OglToXml class Writer: """ - A shim on top of the OGL serialization layer; Allows me to one day replace + A shim on top of the OGL serialization layer; + Allowed me to replace the heavy-duty Python core xml minidom implementation - Or even replace XML with JSON + with Python Xml Elment Tree + + The write only writes the latest XML version """ def __init__(self): @@ -32,12 +35,12 @@ def writeFile(self, oglProject: OglProject, fqFileName: str): if fqFileName.endswith('.put') is False: fqFileName = f'{fqFileName}.put' - oglToMiniDom: OglToMiniDomV10 = OglToMiniDomV10(projectVersion=oglProject.version, projectCodePath=oglProject.codePath) + oglToXml: OglToXml = OglToXml(projectCodePath=oglProject.codePath) for oglDocument in oglProject.oglDocuments.values(): - oglToMiniDom.serialize(oglDocument=oglDocument) + oglToXml.serialize(oglDocument=oglDocument) - rawXml: str = oglToMiniDom.xml + rawXml: str = oglToXml.xml self.logger.info(f'{ZLIB_VERSION=}') byteText: bytes = rawXml.encode() @@ -57,9 +60,9 @@ def writeXmlFile(self, oglProject: OglProject, fqFileName: str, prettyXml: bool if fqFileName.endswith('.xml') is False: fqFileName = f'{fqFileName}.xml' - oglToMiniDom: OglToMiniDomV10 = OglToMiniDomV10(projectVersion=oglProject.version, projectCodePath=oglProject.codePath) + oglToXml: OglToXml = OglToXml(projectCodePath=oglProject.codePath) for oglDocument in oglProject.oglDocuments.values(): - oglToMiniDom.serialize(oglDocument=oglDocument) + oglToXml.serialize(oglDocument=oglDocument) - oglToMiniDom.writeXml(fqFileName=fqFileName, prettyXml=prettyXml) + oglToXml.writeXml(fqFileName=fqFileName) diff --git a/oglio/toXmlV11/OglToXml.py b/oglio/toXmlV11/OglToXml.py index 5653947..f2f4e59 100644 --- a/oglio/toXmlV11/OglToXml.py +++ b/oglio/toXmlV11/OglToXml.py @@ -23,7 +23,7 @@ XML_VERSION: str = '11' INDENT_SPACES: str = ' ' # TODO: Make this configurable -PRETTY_PRINT: bool = True # TODO: Make this configurable +PRETTY_PRINT: bool = True class OglToXml: @@ -31,16 +31,26 @@ def __init__(self, projectCodePath: str): self.logger: Logger = getLogger(__name__) - self._projectCodePath: str = projectCodePath + self._projectCodePath: str = projectCodePath + topElement: Element = Element(XmlConstants.TOP_LEVEL_ELEMENT) - topElement: Element = Element(XmlConstants.TOP_LEVEL_ELEMENT) topElement.set(XmlConstants.ATTR_VERSION, XML_VERSION) topElement.set(XmlConstants.ATTR_CODE_PATH, projectCodePath) tree: ElementTree = ElementTree(topElement) indent(tree, space=' ') - self._topElement: Element = topElement - self._tree: ElementTree = tree + + self._topElement: Element = topElement + self._tree: ElementTree = tree + self._prettyPrint: bool = PRETTY_PRINT + + @property + def prettyPrint(self) -> bool: + return self._prettyPrint + + @prettyPrint.setter + def prettyPrint(self, newValue: bool): + self._prettyPrint = newValue @property def xml(self) -> str: @@ -48,8 +58,8 @@ def xml(self) -> str: Returns: The serialized XML in pretty print format """ - if PRETTY_PRINT is True: - return self._prettyPrint(self._topElement) + if self.prettyPrint is True: + return self._toPrettyString(self._topElement) else: return self._toString(self._topElement) @@ -96,7 +106,7 @@ def _oglDocumentToXml(self, oglDocument: OglDocument) -> Element: return documentTop - def _prettyPrint(self, originalElement: Element): + def _toPrettyString(self, originalElement: Element): """ Create a copy of the input originalElement Convert to string, then parse again diff --git a/requirements.txt b/requirements.txt index 3f4a23f..31ea2dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,4 @@ codeallybasic==0.5.2 codeallyadvanced==0.5.1 pyutmodel==1.5.0 ogl==0.90.0 -untanglepyut==1.2.8 \ No newline at end of file +untanglepyut==1.2.9 \ No newline at end of file diff --git a/tests/oglio/TestWriter.py b/tests/oglio/TestWriter.py index 8604807..fdd588d 100644 --- a/tests/oglio/TestWriter.py +++ b/tests/oglio/TestWriter.py @@ -35,6 +35,8 @@ class TestWriter(TestBase): MULTI_DOCUMENT_FILENAME: str = 'SimpleMultipleDocument.xml' TEST_COMPRESSED_PROJECT: str = 'TestCompressedProject.put' + EXPECTED_MULTI_DOCUMENT_FILENAME: str = 'SimpleMultipleDocumentV11.xml' + def setUp(self): super().setUp() @@ -44,18 +46,21 @@ def tearDown(self): def testSimpleWrite(self): oglProject: OglProject = self._getTestOglProject() - generatedFileName: str = TestBase.constructGeneratedName(TestWriter.MULTI_DOCUMENT_FILENAME) + generatedFileName: str = TestBase.constructGeneratedName(TestWriter.EXPECTED_MULTI_DOCUMENT_FILENAME) writer: Writer = Writer() writer.writeXmlFile(oglProject=oglProject, fqFileName=generatedFileName) - TestBase.cleanupGenerated(TestWriter.MULTI_DOCUMENT_FILENAME) + self._runDiff(TestWriter.EXPECTED_MULTI_DOCUMENT_FILENAME) + + TestBase.cleanupGenerated(TestWriter.EXPECTED_MULTI_DOCUMENT_FILENAME) def testWriteCompressedFile(self): """ Manually run pyut2xml and manually diff the files """ + TestBase.keep = True oglProject: OglProject = self._getTestOglProject() generatedFileName: str = TestWriter.constructGeneratedName(TestWriter.TEST_COMPRESSED_PROJECT) writer: Writer = Writer() diff --git a/tests/resources/testdata/SimpleMultipleDocumentV11.xml b/tests/resources/testdata/SimpleMultipleDocumentV11.xml new file mode 100644 index 0000000..571978f --- /dev/null +++ b/tests/resources/testdata/SimpleMultipleDocumentV11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file