From 1c24a4bc50a7b7e79609d039641fdb58da9cdb24 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 23 Jan 2025 11:43:53 +0100 Subject: [PATCH] optimize detection of xml-data --- document.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/document.py b/document.py index fdac302..bfd6e30 100644 --- a/document.py +++ b/document.py @@ -1041,6 +1041,30 @@ class Incoming(metaclass=PoolMeta): msg='cannot convert %(val)s' % { 'val': date_string})) + def _facturx_get_level(self, xml_data): + """ detect level and flavour of factur-x file, + + Args: + xml_data (xml-tree): xml-tree + + Raises: + UserError: if unexpected error message from facturx + + Returns: + tuple: (flavour, level) + """ + try: + fx_flavour = facturx.get_flavor(xml_data) + fx_level = facturx.get_level(xml_data) + except ValueError as e1: + fx_flavour = fx_level = None + if not str(e1).startswith( + 'This XML is not a Factur-X nor Order-X XML'): + raise UserError(gettext( + 'document_incoming_invoice_xml.msg_convert_error', + msg=str(e1))) + return (fx_flavour, fx_level) + def _facturx_detect_content(self): """ check xml-data against xsd of XRechnung and FacturX, begin with extended, goto minimal, then xrechnung @@ -1066,14 +1090,18 @@ class Incoming(metaclass=PoolMeta): msg='content-type "%(mime)s" not supported' % { 'mime': self.mime_type})) - fx_flavour = facturx.get_flavor(xml_data) - fx_level = facturx.get_level(xml_data) - + (fx_flavour, fx_level) = self._facturx_get_level(xml_data) for xsdpath, xsdtype, funcname in xml_types: + # skip factur-x validation if library was not able to detect + # level and flavour of xml-file + if xsdtype.lower().startswith('factur-x') and ( + not fx_flavour) and (not fx_level): + continue + + # fast forward to known xml-type if fx_flavour == 'factur-x' and fx_level: if not (xsdtype.lower().startswith('factur-x') and xsdtype.lower().endswith(fx_level)): - # skip check if xml-content is already known continue fname = os.path.join(*[os.path.split(__file__)[0]] + xsdpath)