From: Marius Crisan <49843191+cmcproject@users.noreply.github.com> Date: Thu, 15 Jun 2023 20:02:20 +0000 (+0300) Subject: Update ZUGFeRD XML generation (#8) X-Git-Tag: 2.4.0~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=71bcefb020211895cd0126ccc535cf3b521266a4;p=thirdparty%2Fpython-drafthorse.git Update ZUGFeRD XML generation (#8) Co-authored-by: Raphael Michel --- diff --git a/drafthorse/models/elements.py b/drafthorse/models/elements.py index 2c60626..2d24b29 100644 --- a/drafthorse/models/elements.py +++ b/drafthorse/models/elements.py @@ -67,10 +67,14 @@ class Element(metaclass=BaseElementMeta): node.append(el) def serialize(self, schema="FACTUR-X_BASIC"): - xml = b'' + ET.tostring( - self.to_etree(), "utf-8" - ) - return validate_xml(xmlout=xml, schema=schema) + """ + Create XML from ZUGFeRD data model + :param schema: XML schema + :return: ZUGFeRD XML + """ + xml = ET.tostring(self.to_etree(), "utf-8") + + return validate_xml(xml, schema) def __setattr__(self, key, value): if ( diff --git a/drafthorse/utils.py b/drafthorse/utils.py index e2b474e..7c9d8ee 100644 --- a/drafthorse/utils.py +++ b/drafthorse/utils.py @@ -1,35 +1,9 @@ import logging import os -from xml.dom import minidom logger = logging.getLogger("drafthorse") -def minify(xml): - try: - from lxml import etree - - return b'' + etree.tostring( - etree.fromstring(xml) - ) - except ImportError: - logger.warning("Could not minify output as LXML is not installed.") - return xml - - -def prettify(xml, **kwargs): - try: - from lxml import etree - except ImportError: - reparsed = minidom.parseString(xml) - return reparsed.toprettyxml(indent="\t") - else: - parser = etree.XMLParser(remove_blank_text=True, **kwargs) - return b'' + etree.tostring( - etree.fromstring(xml, parser), pretty_print=True - ) - - def validate_xml(xmlout, schema): try: from lxml import etree @@ -42,6 +16,7 @@ def validate_xml(xmlout, schema): ) parser = etree.XMLParser(schema=schema) xml_root = etree.fromstring(xmlout, parser) - return b'' + etree.tostring( - xml_root, pretty_print=True + + return etree.tostring( + xml_root, pretty_print=True, xml_declaration=True, encoding="UTF-8" ) diff --git a/tests/test_roundtrip.py b/tests/test_roundtrip.py index c6e9783..2b98e13 100644 --- a/tests/test_roundtrip.py +++ b/tests/test_roundtrip.py @@ -2,9 +2,10 @@ import lxml.etree import os import pytest from difflib import unified_diff +from xml.dom import minidom from drafthorse.models.document import Document -from drafthorse.utils import prettify, validate_xml +from drafthorse.utils import validate_xml samples = [ f @@ -13,11 +14,22 @@ samples = [ ] -def _diff_xml(a, b): +def diff_xml(a, b): for line in unified_diff(a.splitlines(), b.splitlines()): print(line) +def prettify(xml, **kwargs): + try: + from lxml import etree + except ImportError: + reparsed = minidom.parseString(xml) + return reparsed.toprettyxml(indent="\t") + else: + parser = etree.XMLParser(remove_blank_text=True, **kwargs) + return etree.tostring(etree.fromstring(xml, parser), pretty_print=True) + + @pytest.mark.parametrize("filename", samples) def test_sample_roundtrip(filename): origxml = prettify( @@ -41,11 +53,11 @@ def test_sample_roundtrip(filename): try: generatedxml = prettify(doc.serialize(schema)) generatedxml = b"\n".join(generatedxml.split(b"\n")[1:]).decode().strip() - _diff_xml(origxml, generatedxml) + diff_xml(origxml, generatedxml) except lxml.etree.XMLSyntaxError: generatedxml = prettify(doc.serialize(None)) generatedxml = b"\n".join(generatedxml.split(b"\n")[1:]).decode().strip() - _diff_xml(origxml, generatedxml) + diff_xml(origxml, generatedxml) raise # Compare output XML