From ebeb7d02dc1a0667d4bea45c6168fb001d4de9ae Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 11 Mar 2022 10:54:19 +0100
Subject: [PATCH 6/9] utils/xml: import 'qnames' for xmlschema 1.0 (#62631)

---
 passerelle/utils/xml.py | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/passerelle/utils/xml.py b/passerelle/utils/xml.py
index 7a32cc3f..113f11f4 100644
--- a/passerelle/utils/xml.py
+++ b/passerelle/utils/xml.py
@@ -19,16 +19,21 @@ from collections import OrderedDict
 
 import jsonschema
 
 from .logging import ignore_loggers
 
 with ignore_loggers('xmlschema'):
     import xmlschema
 
+    try:
+        import xmlschema.names as names  # xmschema >= 1.1
+    except ModuleNotFoundError:
+        import xmlschema.qnames as names  # xmschema < 1.1
+
 
 def text_content(node):
     """Extract text content from node and all its children. Equivalent to
     xmlNodeGetContent from libxml."""
 
     if node is None:
         return ''
 
@@ -122,25 +127,25 @@ class TransformConverter(xmlschema.UnorderedConverter):
                 mapped = self.transformer.type_map[xsd_element.type.name]
                 if hasattr(self.transformer, 'encode_%s' % mapped):
                     obj = getattr(self.transformer, 'encode_%s' % mapped)(obj)
         return super(TransformConverter, self).element_encode(obj, xsd_element, level=level)
 
 
 class JSONSchemaFromXMLSchema(object):
     SIMPLE_TYPE_MAPPING = {
-        xmlschema.qnames.XSD_STRING: 'string',
-        xmlschema.qnames.XSD_INTEGER: 'integer',
-        xmlschema.qnames.XSD_INT: 'integer',
-        xmlschema.qnames.XSD_POSITIVE_INTEGER: 'integer',
-        xmlschema.qnames.XSD_UNSIGNED_INT: 'integer',
-        xmlschema.qnames.XSD_NON_NEGATIVE_INTEGER: 'integer',
-        xmlschema.qnames.XSD_BOOLEAN: 'boolean',
-        xmlschema.qnames.XSD_DOUBLE: 'number',
-        xmlschema.qnames.XSD_DECIMAL: 'number',
+        names.XSD_STRING: 'string',
+        names.XSD_INTEGER: 'integer',
+        names.XSD_INT: 'integer',
+        names.XSD_POSITIVE_INTEGER: 'integer',
+        names.XSD_UNSIGNED_INT: 'integer',
+        names.XSD_NON_NEGATIVE_INTEGER: 'integer',
+        names.XSD_BOOLEAN: 'boolean',
+        names.XSD_DOUBLE: 'number',
+        names.XSD_DECIMAL: 'number',
     }
 
     def __init__(self, xml_schema, root_element):
         if not isinstance(xml_schema, xmlschema.XMLSchema):
             with ignore_loggers('xmlschema'):
                 xml_schema = xmlschema.XMLSchema(xml_schema, converter=TransformConverter(transformer=self))
         self.xml_schema = xml_schema
         self.root_element = root_element
@@ -188,42 +193,42 @@ class JSONSchemaFromXMLSchema(object):
             if simple_type.white_space not in ('collapse', 'preserve'):
                 raise NotImplementedError(simple_type)
             schema = OrderedDict(cls.simpletype_to_jsonschema(simple_type.base_type))
             for validator in simple_type.validators:
                 if isinstance(validator, xmlschema.validators.XsdEnumerationFacets):
                     schema['enum'] = validator.enumeration
                 elif (
                     isinstance(validator, xmlschema.validators.XsdMinLengthFacet)
-                    and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
+                    and simple_type.base_type.name == names.XSD_STRING
                 ):
                     schema['minLength'] = validator.value
                 elif (
                     isinstance(validator, xmlschema.validators.XsdMaxLengthFacet)
-                    and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
+                    and simple_type.base_type.name == names.XSD_STRING
                 ):
                     schema['maxLength'] = validator.value
                 elif (
                     isinstance(validator, xmlschema.validators.XsdLengthFacet)
-                    and simple_type.base_type.name == xmlschema.qnames.XSD_STRING
+                    and simple_type.base_type.name == names.XSD_STRING
                 ):
                     schema['minLength'] = validator.value
                     schema['maxLength'] = validator.value
                 elif isinstance(validator, xmlschema.validators.XsdMinInclusiveFacet):
                     schema['minimum'] = validator.value
                 elif isinstance(validator, xmlschema.validators.XsdMaxInclusiveFacet):
                     schema['maximum'] = validator.value
                 elif (
                     isinstance(validator, xmlschema.validators.XsdTotalDigitsFacet)
-                    and simple_type.base_type.name == xmlschema.qnames.XSD_DECIMAL
+                    and simple_type.base_type.name == names.XSD_DECIMAL
                 ):
                     schema['exclusiveMaximum'] = 10**validator.value
                 elif (
                     isinstance(validator, xmlschema.validators.XsdFractionDigitsFacet)
-                    and simple_type.base_type.name == xmlschema.qnames.XSD_DECIMAL
+                    and simple_type.base_type.name == names.XSD_DECIMAL
                 ):
                     schema['multipleOf'] = 1 / 10.0**validator.value
                 else:
                     raise NotImplementedError(validator)
             add_patterns()
             return schema
 
         if isinstance(simple_type, xmlschema.validators.XsdUnion):
@@ -331,17 +336,17 @@ class JSONSchemaFromXMLSchema(object):
             base_schema = cls.type_to_jsonschema(xmltype.base_type)
             if not xmltype.attributes:
                 schema = base_schema
             else:
                 cls.attributegroup_to_jsonschema(xmltype.attributes)
                 schema['properties']['$'] = base_schema
             return schema
         else:
-            if xmltype.has_mixed_content() or xmltype.name == xmlschema.qnames.XSD_ANY_TYPE:
+            if xmltype.has_mixed_content() or xmltype.name == names.XSD_ANY_TYPE:
                 raise NotImplementedError(xmltype)
 
             schema = OrderedDict({'type': 'object'})
             schema['additionalProperties'] = False
             if xmltype.attributes:
                 cls.attributegroup_to_jsonschema(xmltype.attributes, schema)
             cls.group_to_jsonschema(xmltype.content_type, schema)
             return schema
-- 
2.30.2

