class IDREF(NCName): _default_qname = xsd_ns('IDREF')
class NmTokens(NmToken): _default_qname = xsd_ns('NMTOKENS')
class NCName(Name): _default_qname = xsd_ns('NCName')
class NormalizedString(String): _default_qname = xsd_ns('normalizedString')
class Language(Token): _default_qname = xsd_ns('language')
'sequence', 'group', 'choice', 'all', 'list', 'union', 'attribute', 'any', 'anyAttribute', 'attributeGroup', 'restriction', 'extension', 'notation', ]: attr = name if name not in keyword.kwlist else name + '_' setattr(tags, attr, xsd_ns(name)) class SchemaVisitor(object): """Visitor which processes XSD files and registers global elements and types in the given schema. """ def __init__(self, schema, document): self.document = document self.schema = schema self._includes = set() def process(self, node, parent): visit_func = self.visitors.get(node.tag) if not visit_func:
def clone(self, qname, min_occurs=1, max_occurs=1): return self.__class__() def parse_kwargs(self, kwargs, name, available_kwargs): if name in available_kwargs: value = kwargs[name] available_kwargs.remove(name) return {name: value} return {} def parse(self, xmlelement, schema, context=None): from zeep.xsd.schema import Schema schema = Schema(xmlelement, schema._transport) context.schemas.append(schema) return schema def parse_xmlelements(self, xmlelements, schema, name=None, context=None): if xmlelements[0].tag == self.qname: xmlelement = xmlelements.popleft() result = self.parse(xmlelement, schema, context=context) return result def resolve(self): return self default_elements = { xsd_ns('schema'): Schema(), }
class Long(Integer): _default_qname = xsd_ns('long') def pythonvalue(self, value): return long(value) if six.PY2 else int(value) # noqa
class Int(Long): _default_qname = xsd_ns('int')
class Entity(NCName): _default_qname = xsd_ns('ENTITY')
class Entities(Entity): _default_qname = xsd_ns('ENTITIES')
class IDREFS(IDREF): _default_qname = xsd_ns('IDREFS')
class AnySimpleType(AnyType): _default_qname = xsd_ns('anySimpleType') def __init__(self, qname=None, is_global=False): super(AnySimpleType, self).__init__(qname or etree.QName(self._default_qname), is_global) def __call__(self, *args, **kwargs): """Return the xmlvalue for the given value. Expects only one argument 'value'. The args, kwargs handling is done here manually so that we can return readable error messages instead of only '__call__ takes x arguments' """ num_args = len(args) + len(kwargs) if num_args != 1: raise TypeError( ('%s() takes exactly 1 argument (%d given). ' + 'Simple types expect only a single value argument') % (self.__class__.__name__, num_args)) if kwargs and 'value' not in kwargs: raise TypeError( ('%s() got an unexpected keyword argument %r. ' + 'Simple types expect only a single value argument') % (self.__class__.__name__, next(six.iterkeys(kwargs)))) value = args[0] if args else kwargs['value'] return self.xmlvalue(value) def __eq__(self, other): return (other is not None and self.__class__ == other.__class__ and self.__dict__ == other.__dict__) def __str__(self): return '%s(value)' % (self.__class__.__name__) def parse_xmlelement(self, xmlelement, schema=None, allow_none=True, context=None, schema_type=None): if xmlelement.text is None: return try: return self.pythonvalue(xmlelement.text) except (TypeError, ValueError): logger.exception("Error during xml -> python translation") return None def pythonvalue(self, xmlvalue): raise NotImplementedError('%s.pytonvalue() not implemented' % self.__class__.__name__) def render(self, parent, value, xsd_type=None, render_path=None): if value is Nil: parent.set(xsi_ns('nil'), 'true') return parent.text = self.xmlvalue(value) def signature(self, schema=None, standalone=True): return self.get_prefixed_name(schema) def validate(self, value, required=False): if required and value is None: raise ValidationError("Value is required")
class tags: schema = xsd_ns("schema") import_ = xsd_ns("import") include = xsd_ns("include") annotation = xsd_ns("annotation") element = xsd_ns("element") simpleType = xsd_ns("simpleType") complexType = xsd_ns("complexType") simpleContent = xsd_ns("simpleContent") complexContent = xsd_ns("complexContent") sequence = xsd_ns("sequence") group = xsd_ns("group") choice = xsd_ns("choice") all = xsd_ns("all") list = xsd_ns("list") union = xsd_ns("union") attribute = xsd_ns("attribute") any = xsd_ns("any") anyAttribute = xsd_ns("anyAttribute") attributeGroup = xsd_ns("attributeGroup") restriction = xsd_ns("restriction") extension = xsd_ns("extension") notation = xsd_ns("notations")
class Short(Int): _default_qname = xsd_ns("short")
class Short(Int): _default_qname = xsd_ns('short')
class AnyType(Type): _default_qname = xsd_ns('anyType') _attributes_unwrapped = [] _element = None def render(self, parent, value, xsd_type=None, render_path=None): if isinstance(value, AnyObject): if value.xsd_type is None: parent.set(xsi_ns('nil'), 'true') else: value.xsd_type.render(parent, value.value, None, render_path) parent.set(xsi_ns('type'), value.xsd_type.qname) elif hasattr(value, '_xsd_elm'): value._xsd_elm.render(parent, value, render_path) parent.set(xsi_ns('type'), value._xsd_elm.qname) else: parent.text = self.xmlvalue(value) def parse_xmlelement(self, xmlelement, schema=None, allow_none=True, context=None): xsi_type = qname_attr(xmlelement, xsi_ns('type')) xsi_nil = xmlelement.get(xsi_ns('nil')) children = list(xmlelement.getchildren()) # Handle xsi:nil attribute if xsi_nil == 'true': return None # Check if a xsi:type is defined and try to parse the xml according # to that type. if xsi_type and schema: xsd_type = schema.get_type(xsi_type, fail_silently=True) # If we were unable to resolve a type for the xsi:type (due to # buggy soap servers) then we just return the lxml element. if not xsd_type: return children # If the xsd_type is xsd:anyType then we will recurs so ignore # that. if isinstance(xsd_type, self.__class__): return xmlelement.text or None return xsd_type.parse_xmlelement(xmlelement, schema, context=context) # If no xsi:type is set and the element has children then there is # not much we can do. Just return the children elif children: return children elif xmlelement.text is not None: return self.pythonvalue(xmlelement.text) return None def resolve(self): return self def xmlvalue(self, value): return value def pythonvalue(self, value, schema=None): return value
class Byte(Short): """A signed 8-bit integer""" _default_qname = xsd_ns('byte')
class AnyType(Type): _default_qname = xsd_ns("anyType") _element = None def __call__(self, value=None): return value or "" def render( self, node: etree._Element, value: typing.Union[list, dict, CompoundValue], xsd_type: "ComplexType" = None, render_path=None, ) -> None: assert xsd_type is None if isinstance(value, AnyObject): if value.xsd_type is None: node.set(xsi_ns("nil"), "true") else: value.xsd_type.render(node, value.value, None, render_path) node.set(xsi_ns("type"), value.xsd_type.qname) elif isinstance(value, CompoundValue): value._xsd_elm.render(node, value, render_path) node.set(xsi_ns("type"), value._xsd_elm.qname) else: node.text = self.xmlvalue(value) def parse_xmlelement( self, xmlelement: etree._Element, schema: "Schema" = None, allow_none: bool = True, context: XmlParserContext = None, schema_type: "Type" = None, ) -> typing.Optional[typing.Union[str, CompoundValue, typing.List[etree._Element]]]: """Try to parse the xml element and return a value for it. There is a big chance that we cannot parse this value since it is an Any. In that case we just return the raw lxml Element nodes. :param xmlelement: XML element objects :param schema: The parent XML schema :param allow_none: Allow none :param context: Optional parsing context (for inline schemas) :param schema_type: The original type (not overriden via xsi:type) """ xsi_type = qname_attr(xmlelement, xsi_ns("type")) xsi_nil = xmlelement.get(xsi_ns("nil")) children = list(xmlelement) # Handle xsi:nil attribute if xsi_nil == "true": return None # Check if a xsi:type is defined and try to parse the xml according # to that type. if xsi_type and schema: xsd_type = schema.get_type(xsi_type, fail_silently=True) # If we were unable to resolve a type for the xsi:type (due to # buggy soap servers) then we just return the text or lxml element. if not xsd_type: logger.debug( "Unable to resolve type for %r, returning raw data", xsi_type.text) if xmlelement.text: return self.pythonvalue(xmlelement.text) return children # If the xsd_type is xsd:anyType then we will recurs so ignore # that. if isinstance(xsd_type, self.__class__): return self.pythonvalue(xmlelement.text) or None return xsd_type.parse_xmlelement(xmlelement, schema, context=context) # If no xsi:type is set and the element has children then there is # not much we can do. Just return the children elif children: return children elif xmlelement.text is not None: return self.pythonvalue(xmlelement.text) return None def resolve(self): return self def xmlvalue(self, value): """Guess the xsd:type for the value and use corresponding serializer""" from zeep.xsd.types import builtins available_types = [ builtins.String, builtins.Boolean, builtins.Decimal, builtins.Float, builtins.DateTime, builtins.Date, builtins.Time, ] for xsd_type in available_types: if isinstance(value, tuple(xsd_type.accepted_types)): return xsd_type().xmlvalue(value) return str(value) def pythonvalue(self, value, schema=None) -> typing.Optional[str]: return value if value is not None else None def signature(self, schema=None, standalone=True): return "xsd:anyType" @threaded_cached_property def _attributes_unwrapped(self): return []
class NonNegativeInteger(Integer): _default_qname = xsd_ns('nonNegativeInteger')
class Notation(_BuiltinType): accepted_types = six.string_types _default_qname = xsd_ns('NOTATION')
class UnsignedLong(NonNegativeInteger): _default_qname = xsd_ns('unsignedLong')
class Token(NormalizedString): _default_qname = xsd_ns('token')
class UnsignedInt(UnsignedLong): _default_qname = xsd_ns('unsignedInt')
class NmToken(Token): _default_qname = xsd_ns('NMTOKEN')
class UnsignedShort(UnsignedInt): _default_qname = xsd_ns('unsignedShort')
class Name(Token): _default_qname = xsd_ns('Name')
class UnsignedByte(UnsignedShort): _default_qname = xsd_ns('unsignedByte')
class ID(NCName): _default_qname = xsd_ns('ID')
class PositiveInteger(NonNegativeInteger): _default_qname = xsd_ns('positiveInteger')
class tags(object): pass for name in [ 'schema', 'import', 'include', 'annotation', 'element', 'simpleType', 'complexType', 'simpleContent', 'complexContent', 'sequence', 'group', 'choice', 'all', 'list', 'union', 'attribute', 'any', 'anyAttribute', 'attributeGroup', 'restriction', 'extension', 'notation', ]: attr = name if name not in keyword.kwlist else name + '_' setattr(tags, attr, xsd_ns(name)) class SchemaVisitor(object): """Visitor which processes XSD files and registers global elements and types in the given schema. """ def __init__(self, document, parser_context=None): self.document = document self.schema = document._schema self.parser_context = parser_context self._includes = set() def process(self, node, parent): visit_func = self.visitors.get(node.tag)