optimize detection of xml-data

This commit is contained in:
Frederik Jaeckel 2025-01-23 11:43:53 +01:00
parent f7769624cc
commit 1c24a4bc50

View file

@ -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)