def test_subs(self): from lxml import etree from spyne.util.xml import get_schema_documents xpath = lambda o, x: o.xpath(x, namespaces={"xs": ns.xsd}) m = { "s0": "aa", "s2": "cc", "s3": "dd", } class C(ComplexModel): __namespace__ = "aa" a = Integer b = Integer(sub_name="bb") c = Integer(sub_ns="cc") d = Integer(sub_ns="dd", sub_name="dd") elt = get_schema_documents([C], "aa")['tns'] print etree.tostring(elt, pretty_print=True) seq, = xpath(elt, "xs:complexType/xs:sequence") assert len(seq) == 4 assert len(xpath(seq, 'xs:element[@name="a"]')) == 1 assert len(xpath(seq, 'xs:element[@name="bb"]')) == 1 # TODO: this doesn't feel right. # check the spec to see whether it should it be prefixed. assert len(xpath(seq, 'xs:element[@name="{cc}c"]')) == 1 assert len(xpath(seq, 'xs:element[@name="{dd}dd"]')) == 1
def test_choice_tag(self): class SomeObject(ComplexModel): __namespace__ = "badass_ns" one = Integer(xml_choice_group="numbers") two = Integer(xml_choice_group="numbers") punk = Unicode class KickassService(ServiceBase): @rpc(_returns=SomeObject) def wooo(ctx): return SomeObject() Application([KickassService], tns='kickass.ns', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11() ) docs = get_schema_documents([SomeObject]) doc = docs['tns'] print(etree.tostring(doc, pretty_print=True)) assert len(doc.xpath('/xs:schema/xs:complexType[@name="SomeObject"]' '/xs:sequence/xs:element[@name="punk"]', namespaces={'xs': NS_XSD})) > 0 assert len(doc.xpath('/xs:schema/xs:complexType[@name="SomeObject"]' '/xs:sequence/xs:choice/xs:element[@name="one"]', namespaces={'xs': NS_XSD})) > 0
def test_subs(self): from lxml import etree from spyne.util.xml import get_schema_documents xpath = lambda o, x: o.xpath(x, namespaces={"xs": NS_XSD}) m = { "s0": "aa", "s2": "cc", "s3": "dd", } class C(ComplexModel): __namespace__ = "aa" a = Integer b = Integer(sub_name="bb") c = Integer(sub_ns="cc") d = Integer(sub_ns="dd", sub_name="dd") elt = get_schema_documents([C], "aa")['tns'] print(etree.tostring(elt, pretty_print=True)) seq, = xpath(elt, "xs:complexType/xs:sequence") assert len(seq) == 4 assert len(xpath(seq, 'xs:element[@name="a"]')) == 1 assert len(xpath(seq, 'xs:element[@name="bb"]')) == 1
def test_deserialize(self): class Punk(ComplexModel): __namespace__ = 'some_namespace' a = Unicode b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = Unicode b = Integer c = Decimal d = DateTime e = XmlAttribute(Integer) def __eq__(self, other): # remember that this is a test object assert ( self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e ) return True docs = get_schema_documents([Punk, Foo]) pprint(docs) assert docs['s0'].tag == '{http://www.w3.org/2001/XMLSchema}schema' assert docs['tns'].tag == '{http://www.w3.org/2001/XMLSchema}schema' print() print("the other namespace %r:" % docs['tns'].attrib['targetNamespace']) assert docs['tns'].attrib['targetNamespace'] == 'some_namespace' print(etree.tostring(docs['tns'], pretty_print=True)) print() print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) assert docs['s0'].attrib['targetNamespace'] == 'some_other_namespace' print(etree.tostring(docs['s0'], pretty_print=True)) print() foo = Foo(a=u'a', b=1, c=decimal.Decimal('3.4'), d=datetime(2011,02,20,tzinfo=pytz.utc), e=5) doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) foo_back = get_xml_as_object(doc, Foo) assert foo_back == foo # as long as it doesn't fail, it's ok. get_validation_schema([Punk, Foo])
def test_deserialize(self): class Punk(ComplexModel): __namespace__ = 'some_namespace' a = Unicode b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __namespace__ = 'some_other_namespace' a = Unicode b = Integer c = Decimal d = DateTime e = XmlAttribute(Integer) def __eq__(self, other): # remember that this is a test object assert ( self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e ) return True docs = get_schema_documents([Punk, Foo]) pprint(docs) assert docs['s0'].tag == '{http://www.w3.org/2001/XMLSchema}schema' assert docs['tns'].tag == '{http://www.w3.org/2001/XMLSchema}schema' print() print("the other namespace %r:" % docs['tns'].attrib['targetNamespace']) assert docs['tns'].attrib['targetNamespace'] == 'some_namespace' print(etree.tostring(docs['tns'], pretty_print=True)) print() print("the other namespace %r:" % docs['s0'].attrib['targetNamespace']) assert docs['s0'].attrib['targetNamespace'] == 'some_other_namespace' print(etree.tostring(docs['s0'], pretty_print=True)) print() foo = Foo(a=u'a', b=1, c=decimal.Decimal('3.4'), d=datetime(2011,2,20,tzinfo=pytz.utc), e=5) doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) foo_back = get_xml_as_object(doc, Foo) assert foo_back == foo # as long as it doesn't fail, it's ok. get_validation_schema([Punk, Foo])
def test_annotation(self): tns = 'some_ns' doc = "Some Doc" class SomeClass(ComplexModel): __namespace__ = tns some_attr = Unicode(doc=doc) schema = get_schema_documents([SomeClass], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) assert schema.xpath("//xs:documentation/text()", namespaces={'xs': NS_XSD}) == [doc]
def test_mandatory(self): xpath = lambda o, x: o.xpath(x, namespaces={"xs": NS_XSD}) class C(ComplexModel): __namespace__ = "aa" foo = XmlAttribute(M(Unicode)) elt = get_schema_documents([C])['tns'] print(etree.tostring(elt, pretty_print=True)) foo, = xpath(elt, 'xs:complexType/xs:attribute[@name="foo"]') attrs = foo.attrib assert 'use' in attrs and attrs['use'] == 'required'
def test_boolean_default(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns bald = Boolean(default=True) schema = get_schema_documents([SomeGuy], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['bald'].Attributes.default == True
def test_attribute(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns name = XmlAttribute(Unicode) schema = get_schema_documents([SomeGuy], tns)['tns'] print etree.tostring(schema, pretty_print=True) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['name'].type is Unicode
def test_attribute(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns name = XmlAttribute(Unicode) schema = get_schema_documents([SomeGuy], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['name'].type is Unicode
def test_attribute_with_customized_type(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns name = XmlAttribute(Unicode(default="aa")) schema = get_schema_documents([SomeGuy], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['name'].type.__orig__ is Unicode assert NewGuy._type_info['name'].type.Attributes.default == "aa"
def test_any_tag(self): logging.basicConfig(level=logging.DEBUG) class SomeType(ComplexModel): __namespace__ = "zo" anything = AnyXml(schema_tag='{%s}any' % NS_XSD, namespace='##other', process_contents='lax') docs = get_schema_documents([SomeType]) print(etree.tostring(docs['tns'], pretty_print=True)) _any = docs['tns'].xpath('//xsd:any', namespaces={'xsd': NS_XSD}) assert len(_any) == 1 assert _any[0].attrib['namespace'] == '##other' assert _any[0].attrib['processContents'] == 'lax'
def test_attribute_with_customized_type(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns name = XmlAttribute(Unicode(default="aa")) schema = get_schema_documents([SomeGuy], tns)['tns'] print etree.tostring(schema, pretty_print=True) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['name'].type.__orig__ is Unicode assert NewGuy._type_info['name'].type.Attributes.default == "aa"
def test_customized_unicode(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = tns name = Unicode(max_len=10, pattern="a", min_len=5, default="aa") schema = get_schema_documents([SomeGuy], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects['some_ns'].types["SomeGuy"] assert NewGuy._type_info['name'].Attributes.max_len == 10 assert NewGuy._type_info['name'].Attributes.min_len == 5 assert NewGuy._type_info['name'].Attributes.pattern == "a" assert NewGuy._type_info['name'].Attributes.default == "aa"
def test_simple(self): tns = 'some_ns' class SomeGuy(ComplexModel): __namespace__ = 'some_ns' id = Integer schema = get_schema_documents([SomeGuy], tns)['tns'] print(etree.tostring(schema, pretty_print=True)) objects = parse_schema_element(schema) pprint(objects[tns].types) NewGuy = objects[tns].types["SomeGuy"] assert NewGuy.get_type_name() == SomeGuy.get_type_name() assert NewGuy.get_namespace() == SomeGuy.get_namespace() assert dict(NewGuy._type_info) == dict(SomeGuy._type_info)
from spyne.util.xml import parse_schema_string # Define the object class SomeObject(ComplexModel): i = Integer s = Unicode d = DateTime __repr__ = own_repr # Instantiate the object instance = SomeObject(i=5, s="str", d=datetime.now()) # Generate schema documents schema_elts = get_schema_documents([SomeObject], 'some_ns') # Serialize the xml schema document for object schema = etree.tostring(schema_elts['tns'], pretty_print=True) # Serialize the object to XML instance_elt = get_object_as_xml(instance, SomeObject) # Serialize the element tree to string data = etree.tostring(instance_elt, pretty_print=True) print instance print print schema print data
b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __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))
from spyne.util.xml import get_xml_as_object from spyne.util.xml import parse_schema_string # Define the object class SomeObject(ComplexModel): i = Integer s = Unicode d = DateTime __repr__ = hier_repr # Instantiate the object instance = SomeObject(i=5, s="str", d=datetime.now()) # Generate schema documents schema_elts = get_schema_documents([SomeObject], 'some_ns') # Serialize the xml schema document for object schema = etree.tostring(schema_elts['tns'], pretty_print=True) # Serialize the object to XML instance_elt = get_object_as_xml(instance, SomeObject) # Serialize the element tree to string data = etree.tostring(instance_elt, pretty_print=True) print(instance) print() print(schema) print(data)
def _get_schema(self, *args): schema_doc = get_schema_documents(args)['tns'] return parse_schema_element(schema_doc)
class ProductEdition(ComplexModel): __namespace__ = 'kickass_namespace' id = XmlAttribute(Uuid) name = XmlData(Unicode) class Product(ComplexModel): __namespace__ = 'kickass_namespace' id = XmlAttribute(Uuid) edition = ProductEdition docs = get_schema_documents([Punk, Foo, Product]) pprint(docs) print() pprint({k: v.attrib['targetNamespace'] for k,v in docs.items()}) # 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)) print()
b = Integer c = Decimal d = DateTime class Foo(ComplexModel): __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)
class ProductEdition(ComplexModel): __namespace__ = 'kickass_namespace' id = XmlAttribute(Uuid) name = XmlData(Unicode) class Product(ComplexModel): __namespace__ = 'kickass_namespace' id = XmlAttribute(Uuid) edition = ProductEdition docs = get_schema_documents([Punk, Foo, Product]) pprint(docs) print() pprint({k: v.attrib['targetNamespace'] for k, v in docs.items()}) # 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)) print()