def complex_add(interface, cls): if cls.get_type_name() is ModelBase.Empty: (child, ) = cls._type_info.values() cls.__type_name__ = '%sArray' % child.get_type_name() if not interface.has_class(cls): extends = getattr(cls, '__extends__', None) if not (extends is None): interface.add(extends) complex_type = etree.Element("{%s}complexType" % namespace.xsd) complex_type.set('name', cls.get_type_name()) if cls.Annotations.doc != '' or cls.Annotations.appinfo != None: annotation = etree.SubElement(complex_type, "{%s}annotation" % namespace.xsd) if cls.Annotations.doc != '': doc = etree.SubElement(annotation, "{%s}documentation" % namespace.xsd) doc.text = cls.Annotations.doc _ai = cls.Annotations.appinfo; if _ai != None: appinfo = etree.SubElement(annotation, "{%s}appinfo" % namespace.xsd) if isinstance(_ai, dict): dict_to_etree(_ai, appinfo) elif isinstance(_ai, str) or isinstance(_ai, unicode): appinfo.text = _ai elif isinstance(_ai, etree._Element): appinfo.append(_ai) else: from rpclib.util.xml import get_object_as_xml appinfo.append(get_object_as_xml(_ai)) sequence_parent = complex_type if not (extends is None): complex_content = etree.SubElement(complex_type, "{%s}complexContent" % namespace.xsd) extension = etree.SubElement(complex_content, "{%s}extension" % namespace.xsd) extension.set('base', extends.get_type_name_ns(interface)) sequence_parent = extension sequence = etree.SubElement(sequence_parent, '{%s}sequence' % namespace.xsd) for k, v in cls._type_info.items(): if isinstance(v, XmlAttribute): attribute = etree.SubElement(complex_type, '{%s}attribute' % namespace.xsd) v.describe(k, attribute, interface) continue if v != cls: interface.add(v) member = etree.SubElement(sequence, '{%s}element' % namespace.xsd) member.set('name', k) member.set('type', v.get_type_name_ns(interface)) if v.Attributes.min_occurs != 1: # 1 is the xml schema default member.set('minOccurs', str(v.Attributes.min_occurs)) if v.Attributes.max_occurs != 1: # 1 is the xml schema default member.set('maxOccurs', str(v.Attributes.max_occurs)) if bool(v.Attributes.nillable) == True: member.set('nillable', 'true') #else: # member.set('nillable', 'false') interface.add_complex_type(cls, complex_type) # simple node element = etree.Element('{%s}element' % namespace.xsd) element.set('name', cls.get_type_name()) element.set('type', cls.get_type_name_ns(interface)) interface.add_element(cls, element)
__namespace__ = "some_other_namespace" a = String b = Integer c = Decimal d = DateTime e = XmlAttribute(Integer) docs = get_schema_documents([Punk, Foo]) pprint(docs) print() # the default ns prefix is always tns print("the default namespace %r:" % docs["tns"].attrib["targetNamespace"]) print(etree.tostring(docs["tns"], pretty_print=True)) print() # Namespace prefixes are assigned like s0, s1, s2, etc... print("the other namespace %r:" % docs["s0"].attrib["targetNamespace"]) print(etree.tostring(docs["s0"], pretty_print=True)) foo = Foo(a="a", b=1, c=3.4, d=datetime(2011, 02, 20), e=5) doc = get_object_as_xml(foo) print(etree.tostring(doc, pretty_print=True)) print(get_xml_as_object(Foo, doc)) # See http://lxml.de/validation.html to see what this could be used for. print(get_validation_schema([Punk, Foo]))
def complex_add(document, cls): complex_type = etree.Element("{%s}complexType" % namespace.xsd) complex_type.set('name', cls.get_type_name()) if cls.Annotations.doc != '' or cls.Annotations.appinfo != None: annotation = etree.SubElement(complex_type, "{%s}annotation" % namespace.xsd) if cls.Annotations.doc != '': doc = etree.SubElement(annotation, "{%s}documentation" % namespace.xsd) doc.text = cls.Annotations.doc _ai = cls.Annotations.appinfo; if _ai != None: appinfo = etree.SubElement(annotation, "{%s}appinfo" % namespace.xsd) if isinstance(_ai, dict): dict_to_etree(_ai, appinfo) elif isinstance(_ai, str) or isinstance(_ai, unicode): appinfo.text = _ai elif isinstance(_ai, etree._Element): appinfo.append(_ai) else: from rpclib.util.xml import get_object_as_xml appinfo.append(get_object_as_xml(_ai)) sequence_parent = complex_type extends = getattr(cls, '__extends__', None) if not (extends is None): if (extends.get_type_name() == cls.get_type_name() and extends.get_namespace() == cls.get_namespace()): raise Exception("%r can't extend %r because they are all '{%s}%s'" % (cls, extends, cls.get_type_name(), cls.get_namespace())) else: complex_content = etree.SubElement(complex_type, "{%s}complexContent" % namespace.xsd) extension = etree.SubElement(complex_content, "{%s}extension" % namespace.xsd) extension.set('base', extends.get_type_name_ns(document.interface)) sequence_parent = extension sequence = etree.SubElement(sequence_parent, '{%s}sequence' % namespace.xsd) for k, v in cls._type_info.items(): if issubclass(v, XmlAttribute): attribute = etree.SubElement(complex_type, '{%s}attribute' % namespace.xsd) v.describe(k, attribute, document) continue if v != cls: document.add(v) member = etree.SubElement(sequence, v.Attributes.schema_tag) if v.Attributes.schema_tag == '{%s}element' % namespace.xsd: member.set('name', k) member.set('type', v.get_type_name_ns(document.interface)) elif v.Attributes.schema_tag == '{%s}any' % namespace.xsd and \ (issubclass(v, AnyXml)): if v.Attributes.namespace is not None: member.set('namespace', v.Attributes.namespace) if v.Attributes.process_contents is not None: member.set('processContents', v.Attributes.process_contents) else: raise ValueError("Unhandled schema_tag / type combination. %r %r" % (v, v.Attributes.schema_tag)) if v.Attributes.min_occurs != 1: # 1 is the xml schema default member.set('minOccurs', str(v.Attributes.min_occurs)) if v.Attributes.max_occurs != 1: # 1 is the xml schema default val = v.Attributes.max_occurs if val == float('inf'): val = 'unbounded' else: val = str(val) member.set('maxOccurs', val) if bool(v.Attributes.nillable) != False: # False is the xml schema default member.set('nillable', 'true') document.add_complex_type(cls, complex_type) # simple node element = etree.Element('{%s}element' % namespace.xsd) element.set('name', cls.get_type_name()) element.set('type', cls.get_type_name_ns(document.interface)) document.add_element(cls, element)
d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = String b = Integer c = Decimal d = DateTime docs = get_schema_documents([Punk, Foo]) pprint(docs) print # the default ns prefix is always tns print "the default namespace %r:" % docs['tns'].attrib['targetNamespace'] print etree.tostring(docs['tns'], pretty_print=True) print # Namespace prefixes are assigned like s0, s1, s2, etc... print "the other namespace %r:" % docs['s0'].attrib['targetNamespace'] print etree.tostring(docs['s0'], pretty_print=True) foo = Foo(a='a', b=1, c=3.4, d=datetime(2011,02,20)) print etree.tostring(get_object_as_xml(foo),pretty_print=True) # See http://lxml.de/validation.html to see what this could be used for. print get_validation_schema([Punk, Foo])
d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = String b = Integer c = Decimal d = DateTime docs = get_schema_documents([Punk, Foo]) pprint(docs) print() # the default ns prefix is always tns print("the default namespace %r:" % docs['tns'].attrib['targetNamespace']) print(etree.tostring(docs['tns'], pretty_print=True)) print() # Namespace prefixes are assigned like s0, s1, s2, etc... print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) print(etree.tostring(docs['s0'], pretty_print=True)) foo = Foo(a='a', b=1, c=3.4, d=datetime(2011, 02, 20)) print(etree.tostring(get_object_as_xml(foo), pretty_print=True)) # See http://lxml.de/validation.html to see what this could be used for. print(get_validation_schema([Punk, Foo]))