Skip to content

Commit

Permalink
<Enhancement>[]: <Make writer only write V11>
Browse files Browse the repository at this point in the history
[]

[#42]
  • Loading branch information
Humberto Sanchez II committed Sep 19, 2023
1 parent c03d85f commit 5b28710
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
21 changes: 12 additions & 9 deletions oglio/Writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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)
26 changes: 18 additions & 8 deletions oglio/toXmlV11/OglToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,43 @@
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:
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:
"""
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)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ codeallybasic==0.5.2
codeallyadvanced==0.5.1
pyutmodel==1.5.0
ogl==0.90.0
untanglepyut==1.2.8
untanglepyut==1.2.9
9 changes: 7 additions & 2 deletions tests/oglio/TestWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions tests/resources/testdata/SimpleMultipleDocumentV11.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='iso-8859-1'?>
<PyutProject version="11" CodePath="/tmp/bogus/Ozzee.py">
<PyutDocument type="CLASS_DIAGRAM" title="Diagram 1" scrollPositionX="0" scrollPositionY="0" pixelsPerUnitX="20" pixelsPerUnitY="20">
<OglClass width="79" height="20" x="375" y="150">
<PyutClass id="1" name="ClassName1" stereotype="noStereotype" displayMethods="True" displayParameters="Unspecified" displayFields="True" displayStereotype="True" description="" />
</OglClass>
</PyutDocument>
<PyutDocument type="CLASS_DIAGRAM" title="Diagram 2" scrollPositionX="0" scrollPositionY="0" pixelsPerUnitX="20" pixelsPerUnitY="20">
<OglClass width="112" height="20" x="375" y="100">
<PyutClass id="2" name="SimpleClassName2" stereotype="noStereotype" displayMethods="True" displayParameters="Unspecified" displayFields="True" displayStereotype="True" description="" />
</OglClass>
</PyutDocument>
</PyutProject>

0 comments on commit 5b28710

Please sign in to comment.