Loading translation/translate/ChoiceMapping.py +10 −9 Changes for translation/translate/ChoiceMapping.py: 10 added lines, 9 removed lines. Original line number Original line Diff line number Diff line Loading @@ -10,23 +10,24 @@ from .ComplexTypeMapping import ComplexTypeMapping log = logging.getLogger() log = logging.getLogger() class ChoiceMapping(ComplexTypeMapping): class ChoiceMapping(ComplexTypeMapping): @classmethod @classmethod def process_choice(cls, choice: XsdGroup, current_ns: str): def process_choice(cls, choice: XsdGroup, current_ns: str): if choice.model != 'choice': if choice.model != "choice": raise Exception(f"Wrong group type: {c.model}") raise Exception(f"Wrong group type: {c.model}") oneOf = [] oneOf = [] for c in choice.iter_model(): for c in choice.iter_model(): if not (type(c) is XsdElement): if not (type(c) is XsdElement): raise Exception(f"Non-element {c} encountered in choice {choice}") raise Exception(f"Non-element {c} encountered in choice {choice}") t = TypeMapping.get_type_from_elem(c, current_ns) t = TypeMapping.get_type_from_elem(c, current_ns) oneOf.append({ oneOf.append( { "type": "object", "type": "object", "properties" : { "properties": {c.local_name: t}, c.local_name : t "required": [c.local_name], }, } "required" : [c.local_name] ) }) return oneOf return oneOf def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): Loading @@ -36,7 +37,7 @@ class ChoiceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") log.debug("Not a complex type, giving up") return None return None content = xst.content content = xst.content if (content.model != 'choice'): if content.model != "choice": log.debug("Not a choice, giving up") log.debug("Not a choice, giving up") return None return None return { 'oneOf' : ChoiceMapping.process_choice(content, xst.namespaces[''])} return {"oneOf": ChoiceMapping.process_choice(content, xst.namespaces[""])} translation/translate/ComplexTypeMapping.py +2 −3 Changes for translation/translate/ComplexTypeMapping.py: 2 added lines, 3 removed lines. Original line number Original line Diff line number Diff line Loading @@ -2,10 +2,9 @@ from xmlschema.validators.complex_types import * from .TypeMapping import TypeMapping from .TypeMapping import TypeMapping class ComplexTypeMapping(TypeMapping): class ComplexTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): if not (type(xst) is XsdComplexType): if not (type(xst) is XsdComplexType): return None return None return { return {"type": "object"} "type" : "object" } translation/translate/SequenceMapping.py +26 −28 Changes for translation/translate/SequenceMapping.py: 26 added lines, 28 removed lines. Original line number Original line Diff line number Diff line Loading @@ -20,24 +20,17 @@ class SequenceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") log.debug("Not a complex type, giving up") return None return None content = xst.content content = xst.content if (content.model != 'sequence'): if content.model != "sequence": log.debug("Not a sequence, giving up") log.debug("Not a sequence, giving up") return None return None mapped_type = { mapped_type = {"type": "object", "properties": {}, "required": []} 'type' : 'object', 'properties' : {}, 'required' : [] } # Not going to try and do all of this automatically for now # Not going to try and do all of this automatically for now # Only make insert the xsiType parameter # Only make insert the xsiType parameter if (xst.base_type): if xst.base_type: # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) mapped_type['properties']['xsiType'] = { mapped_type["properties"]["xsiType"] = {"type": "string", "enum": xst.name} "type" : "string", mapped_type["required"].append("xsiType") "enum" : xst.name } mapped_type['required'].append('xsiType') # if xst.abstract: # if xst.abstract: # mapped_type['__ABSTRACT__'] = True # mapped_type['__ABSTRACT__'] = True # pass # pass Loading @@ -47,34 +40,39 @@ class SequenceMapping(ComplexTypeMapping): log.debug(f"Processing model item {c}") log.debug(f"Processing model item {c}") if type(c) is XsdElement: if type(c) is XsdElement: if c.effective_max_occurs != 1: if c.effective_max_occurs != 1: mapped_type['properties'][c.local_name] = { mapped_type["properties"][c.local_name] = { "type": "array", "type": "array", "items" : TypeMapping.get_type_from_elem(c, xst.namespaces['']) "items": TypeMapping.get_type_from_elem(c, xst.namespaces[""]), } } if c.effective_max_occurs: if c.effective_max_occurs: mapped_type['properties'][c.local_name]['maxItems'] = c.effective_max_occurs mapped_type["properties"][c.local_name][ "maxItems" ] = c.effective_max_occurs if c.effective_min_occurs > 0: if c.effective_min_occurs > 0: mapped_type['properties'][c.local_name]['minItems'] = c.effective_min_occurs mapped_type["properties"][c.local_name][ "minItems" ] = c.effective_min_occurs else: else: mapped_type['properties'][c.local_name] = TypeMapping.get_type_from_elem(c, xst.namespaces['']) mapped_type["properties"][ c.local_name ] = TypeMapping.get_type_from_elem(c, xst.namespaces[""]) if c.effective_min_occurs == 1: if c.effective_min_occurs == 1: mapped_type['required'].append(c.local_name) mapped_type["required"].append(c.local_name) elif type(c) is XsdGroup: elif type(c) is XsdGroup: if inner_choice: if inner_choice: raise Exception (f"Second group '{c.local_name}' encountered in {xst}") raise Exception( f"Second group '{c.local_name}' encountered in {xst}" ) if c.model != "choice": if c.model != "choice": raise Exception (f"Don't know what to do with inner group {c} in {xst} - not a choice") raise Exception( inner_choice = ChoiceMapping.process_choice(c, xst.namespaces['']) f"Don't know what to do with inner group {c} in {xst} - not a choice" ) inner_choice = ChoiceMapping.process_choice(c, xst.namespaces[""]) elif type(c) is XsdAnyElement: elif type(c) is XsdAnyElement: mapped_type = {} mapped_type = {} else: else: raise Exception(f"Unknown element type {c}") raise Exception(f"Unknown element type {c}") if (inner_choice): if inner_choice: return { return {"allOf": [mapped_type, {"oneOf": inner_choice}]} 'allOf' : [ mapped_type, {'oneOf' : inner_choice} ] } else: else: return mapped_type return mapped_type translation/translate/SimpleTypeMapping.py +2 −3 Changes for translation/translate/SimpleTypeMapping.py: 2 added lines, 3 removed lines. Original line number Original line Diff line number Diff line Loading @@ -7,12 +7,11 @@ from .TypeMapping import TypeMapping log = logging.getLogger() log = logging.getLogger() class SimpleTypeMapping(TypeMapping): class SimpleTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): log.debug(f"Attempting mapping of {xst} to simple type") log.debug(f"Attempting mapping of {xst} to simple type") if not (type(xst) is XsdAtomicRestriction): if not (type(xst) is XsdAtomicRestriction): log.debug("Type is not an XsdAtomicRestriction, giving up") log.debug("Type is not an XsdAtomicRestriction, giving up") return None return None return { return {"$ref": xst.base_type.name} "$ref" : xst.base_type.name } No newline at end of file translation/translate/TypeMapping.py +4 −15 Changes for translation/translate/TypeMapping.py: 4 added lines, 15 removed lines. Original line number Original line Diff line number Diff line Loading @@ -8,6 +8,7 @@ from xmlschema.validators.facets import * log = logging.getLogger() log = logging.getLogger() class TypeMapping(ABC): class TypeMapping(ABC): ns_to_id_map = {} ns_to_id_map = {} Loading @@ -19,16 +20,12 @@ class TypeMapping(ABC): "dateTime": {"type": "string"}, "dateTime": {"type": "string"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "anyURI": {"type": "string"}, "anyURI": {"type": "string"}, "integer": {"type": "integer"}, "integer": {"type": "integer"}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "boolean": {"type": "boolean"}, "boolean": {"type": "boolean"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "anyType": {}, "anyType" : {} } } @abstractmethod @abstractmethod Loading @@ -37,7 +34,7 @@ class TypeMapping(ABC): @classmethod @classmethod def extract_namespace(cls, qname: str): def extract_namespace(cls, qname: str): match = re.search(r'^\{([^\{\}]+)\}(([^\{\}]+))$', qname) match = re.search(r"^\{([^\{\}]+)\}(([^\{\}]+))$", qname) if match is None: if match is None: return None return None return match.group(1) return match.group(1) Loading @@ -54,16 +51,8 @@ class TypeMapping(ABC): @classmethod @classmethod def get_type_from_elem(cls, elem: XsdElement, current_ns: str): def get_type_from_elem(cls, elem: XsdElement, current_ns: str): ns = cls.extract_namespace(elem.type.name) ns = cls.extract_namespace(elem.type.name) if (ns == TypeMapping.XSD_NS): if ns == TypeMapping.XSD_NS: # this should be an XSD primitive type # this should be an XSD primitive type return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) else: else: return cls.get_ref_for(elem.type, current_ns) return cls.get_ref_for(elem.type, current_ns) Loading
translation/translate/ChoiceMapping.py +10 −9 Changes for translation/translate/ChoiceMapping.py: 10 added lines, 9 removed lines. Original line number Original line Diff line number Diff line Loading @@ -10,23 +10,24 @@ from .ComplexTypeMapping import ComplexTypeMapping log = logging.getLogger() log = logging.getLogger() class ChoiceMapping(ComplexTypeMapping): class ChoiceMapping(ComplexTypeMapping): @classmethod @classmethod def process_choice(cls, choice: XsdGroup, current_ns: str): def process_choice(cls, choice: XsdGroup, current_ns: str): if choice.model != 'choice': if choice.model != "choice": raise Exception(f"Wrong group type: {c.model}") raise Exception(f"Wrong group type: {c.model}") oneOf = [] oneOf = [] for c in choice.iter_model(): for c in choice.iter_model(): if not (type(c) is XsdElement): if not (type(c) is XsdElement): raise Exception(f"Non-element {c} encountered in choice {choice}") raise Exception(f"Non-element {c} encountered in choice {choice}") t = TypeMapping.get_type_from_elem(c, current_ns) t = TypeMapping.get_type_from_elem(c, current_ns) oneOf.append({ oneOf.append( { "type": "object", "type": "object", "properties" : { "properties": {c.local_name: t}, c.local_name : t "required": [c.local_name], }, } "required" : [c.local_name] ) }) return oneOf return oneOf def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): Loading @@ -36,7 +37,7 @@ class ChoiceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") log.debug("Not a complex type, giving up") return None return None content = xst.content content = xst.content if (content.model != 'choice'): if content.model != "choice": log.debug("Not a choice, giving up") log.debug("Not a choice, giving up") return None return None return { 'oneOf' : ChoiceMapping.process_choice(content, xst.namespaces[''])} return {"oneOf": ChoiceMapping.process_choice(content, xst.namespaces[""])}
translation/translate/ComplexTypeMapping.py +2 −3 Changes for translation/translate/ComplexTypeMapping.py: 2 added lines, 3 removed lines. Original line number Original line Diff line number Diff line Loading @@ -2,10 +2,9 @@ from xmlschema.validators.complex_types import * from .TypeMapping import TypeMapping from .TypeMapping import TypeMapping class ComplexTypeMapping(TypeMapping): class ComplexTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): if not (type(xst) is XsdComplexType): if not (type(xst) is XsdComplexType): return None return None return { return {"type": "object"} "type" : "object" }
translation/translate/SequenceMapping.py +26 −28 Changes for translation/translate/SequenceMapping.py: 26 added lines, 28 removed lines. Original line number Original line Diff line number Diff line Loading @@ -20,24 +20,17 @@ class SequenceMapping(ComplexTypeMapping): log.debug("Not a complex type, giving up") log.debug("Not a complex type, giving up") return None return None content = xst.content content = xst.content if (content.model != 'sequence'): if content.model != "sequence": log.debug("Not a sequence, giving up") log.debug("Not a sequence, giving up") return None return None mapped_type = { mapped_type = {"type": "object", "properties": {}, "required": []} 'type' : 'object', 'properties' : {}, 'required' : [] } # Not going to try and do all of this automatically for now # Not going to try and do all of this automatically for now # Only make insert the xsiType parameter # Only make insert the xsiType parameter if (xst.base_type): if xst.base_type: # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) # mapped_type['__DESCENDENT_OF__'] = TypeMapping.get_ref_for(xst.base_type, xst.namespaces['']) mapped_type['properties']['xsiType'] = { mapped_type["properties"]["xsiType"] = {"type": "string", "enum": xst.name} "type" : "string", mapped_type["required"].append("xsiType") "enum" : xst.name } mapped_type['required'].append('xsiType') # if xst.abstract: # if xst.abstract: # mapped_type['__ABSTRACT__'] = True # mapped_type['__ABSTRACT__'] = True # pass # pass Loading @@ -47,34 +40,39 @@ class SequenceMapping(ComplexTypeMapping): log.debug(f"Processing model item {c}") log.debug(f"Processing model item {c}") if type(c) is XsdElement: if type(c) is XsdElement: if c.effective_max_occurs != 1: if c.effective_max_occurs != 1: mapped_type['properties'][c.local_name] = { mapped_type["properties"][c.local_name] = { "type": "array", "type": "array", "items" : TypeMapping.get_type_from_elem(c, xst.namespaces['']) "items": TypeMapping.get_type_from_elem(c, xst.namespaces[""]), } } if c.effective_max_occurs: if c.effective_max_occurs: mapped_type['properties'][c.local_name]['maxItems'] = c.effective_max_occurs mapped_type["properties"][c.local_name][ "maxItems" ] = c.effective_max_occurs if c.effective_min_occurs > 0: if c.effective_min_occurs > 0: mapped_type['properties'][c.local_name]['minItems'] = c.effective_min_occurs mapped_type["properties"][c.local_name][ "minItems" ] = c.effective_min_occurs else: else: mapped_type['properties'][c.local_name] = TypeMapping.get_type_from_elem(c, xst.namespaces['']) mapped_type["properties"][ c.local_name ] = TypeMapping.get_type_from_elem(c, xst.namespaces[""]) if c.effective_min_occurs == 1: if c.effective_min_occurs == 1: mapped_type['required'].append(c.local_name) mapped_type["required"].append(c.local_name) elif type(c) is XsdGroup: elif type(c) is XsdGroup: if inner_choice: if inner_choice: raise Exception (f"Second group '{c.local_name}' encountered in {xst}") raise Exception( f"Second group '{c.local_name}' encountered in {xst}" ) if c.model != "choice": if c.model != "choice": raise Exception (f"Don't know what to do with inner group {c} in {xst} - not a choice") raise Exception( inner_choice = ChoiceMapping.process_choice(c, xst.namespaces['']) f"Don't know what to do with inner group {c} in {xst} - not a choice" ) inner_choice = ChoiceMapping.process_choice(c, xst.namespaces[""]) elif type(c) is XsdAnyElement: elif type(c) is XsdAnyElement: mapped_type = {} mapped_type = {} else: else: raise Exception(f"Unknown element type {c}") raise Exception(f"Unknown element type {c}") if (inner_choice): if inner_choice: return { return {"allOf": [mapped_type, {"oneOf": inner_choice}]} 'allOf' : [ mapped_type, {'oneOf' : inner_choice} ] } else: else: return mapped_type return mapped_type
translation/translate/SimpleTypeMapping.py +2 −3 Changes for translation/translate/SimpleTypeMapping.py: 2 added lines, 3 removed lines. Original line number Original line Diff line number Diff line Loading @@ -7,12 +7,11 @@ from .TypeMapping import TypeMapping log = logging.getLogger() log = logging.getLogger() class SimpleTypeMapping(TypeMapping): class SimpleTypeMapping(TypeMapping): def map(self, xst: BaseXsdType): def map(self, xst: BaseXsdType): log.debug(f"Attempting mapping of {xst} to simple type") log.debug(f"Attempting mapping of {xst} to simple type") if not (type(xst) is XsdAtomicRestriction): if not (type(xst) is XsdAtomicRestriction): log.debug("Type is not an XsdAtomicRestriction, giving up") log.debug("Type is not an XsdAtomicRestriction, giving up") return None return None return { return {"$ref": xst.base_type.name} "$ref" : xst.base_type.name } No newline at end of file
translation/translate/TypeMapping.py +4 −15 Changes for translation/translate/TypeMapping.py: 4 added lines, 15 removed lines. Original line number Original line Diff line number Diff line Loading @@ -8,6 +8,7 @@ from xmlschema.validators.facets import * log = logging.getLogger() log = logging.getLogger() class TypeMapping(ABC): class TypeMapping(ABC): ns_to_id_map = {} ns_to_id_map = {} Loading @@ -19,16 +20,12 @@ class TypeMapping(ABC): "dateTime": {"type": "string"}, "dateTime": {"type": "string"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "token": {"type": "string", "pattern": "^[^\r\n\t]*$"}, "anyURI": {"type": "string"}, "anyURI": {"type": "string"}, "integer": {"type": "integer"}, "integer": {"type": "integer"}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "nonNegativeInteger": {"type": "integer", "minimum": 0}, "boolean": {"type": "boolean"}, "boolean": {"type": "boolean"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "hexBinary": {"type": "string", "pattern": "^([a-fA-F0-9]{2})*$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "base64Binary": {"type": "string", "pattern": "^[-A-Za-z0-9+/]*={0,3}$"}, "anyType": {}, "anyType" : {} } } @abstractmethod @abstractmethod Loading @@ -37,7 +34,7 @@ class TypeMapping(ABC): @classmethod @classmethod def extract_namespace(cls, qname: str): def extract_namespace(cls, qname: str): match = re.search(r'^\{([^\{\}]+)\}(([^\{\}]+))$', qname) match = re.search(r"^\{([^\{\}]+)\}(([^\{\}]+))$", qname) if match is None: if match is None: return None return None return match.group(1) return match.group(1) Loading @@ -54,16 +51,8 @@ class TypeMapping(ABC): @classmethod @classmethod def get_type_from_elem(cls, elem: XsdElement, current_ns: str): def get_type_from_elem(cls, elem: XsdElement, current_ns: str): ns = cls.extract_namespace(elem.type.name) ns = cls.extract_namespace(elem.type.name) if (ns == TypeMapping.XSD_NS): if ns == TypeMapping.XSD_NS: # this should be an XSD primitive type # this should be an XSD primitive type return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) return dict(TypeMapping.XSD_TYPE_MAP[elem.type.local_name]) else: else: return cls.get_ref_for(elem.type, current_ns) return cls.get_ref_for(elem.type, current_ns)