def test_subs(self): from lxml import etree from spyne.util.xml import get_xml_as_object from spyne.util.xml import get_object_as_xml 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_object_as_xml(C(a=1, b=2, c=3, d=4), C) print etree.tostring(elt, pretty_print=True) assert elt.xpath("s0:a/text()", namespaces=m) == ["1"] assert elt.xpath("s0:bb/text()", namespaces=m) == ["2"] assert elt.xpath("s2:c/text()", namespaces=m) == ["3"] assert elt.xpath("s3:dd/text()", namespaces=m) == ["4"] c = get_xml_as_object(elt, C) print c assert c.a == 1 assert c.b == 2 assert c.c == 3 assert c.d == 4
def test_sub_attributes(self): from lxml import etree from spyne.util.xml import get_xml_as_object from spyne.util.xml import get_object_as_xml m = { "s0": "aa", "s2": "cc", "s3": "dd", } class C(ComplexModel): __namespace__ = "aa" a = XmlAttribute(Integer) b = XmlAttribute(Integer(sub_name="bb")) c = XmlAttribute(Integer(sub_ns="cc")) d = XmlAttribute(Integer(sub_ns="dd", sub_name="dd")) elt = get_object_as_xml(C(a=1, b=2, c=3, d=4), C) print(etree.tostring(elt, pretty_print=True)) assert elt.xpath("//*/@a") == ["1"] assert elt.xpath("//*/@bb") == ["2"] assert elt.xpath("//*/@s2:c", namespaces=m) == ["3"] assert elt.xpath("//*/@s3:dd", namespaces=m) == ["4"] c = get_xml_as_object(elt, C) print(c) assert c.a == 1 assert c.b == 2 assert c.c == 3 assert c.d == 4
def test_datetime_usec(self): fs = etree.fromstring d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.123456</d>'), DateTime) assert d.microsecond == 123456 # rounds up d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234567</d>'), DateTime) assert d.microsecond == 123457 # rounds down d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234564</d>'), DateTime) assert d.microsecond == 123456 # rounds up as well d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234565</d>'), DateTime) assert d.microsecond == 123457
def test_empty_string(self): class a(ComplexModel): b = Unicode elt = etree.fromstring('<a><b/></a>') o = get_xml_as_object(elt, a) assert o.b == ''
def test_dates(self): d = Date xml_dates = [etree.fromstring('<d>2013-04-05</d>'), etree.fromstring('<d>2013-04-05+02:00</d>'), etree.fromstring('<d>2013-04-05-02:00</d>'), etree.fromstring('<d>2013-04-05Z</d>')] for xml_date in xml_dates: c = get_xml_as_object(xml_date, d) assert isinstance(c, datetime.date) == True assert c.year == 2013 assert c.month == 4 assert c.day == 5
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_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_datetime_usec(self): fs = etree.fromstring d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.123456</d>"), DateTime) assert d.microsecond == 123456 # rounds up d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234567</d>"), DateTime) assert d.microsecond == 123457 # rounds down d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234564</d>"), DateTime) assert d.microsecond == 123456 # rounds up as well d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234565</d>"), DateTime) # FIXME: this is very interesting. why? if six.PY3: assert d.microsecond == 123456 else: assert d.microsecond == 123457
def test_datetime_usec(self): fs = etree.fromstring d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.123456</d>'), DateTime) assert d.microsecond == 123456 # rounds up d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234567</d>'), DateTime) assert d.microsecond == 123457 # rounds down d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234564</d>'), DateTime) assert d.microsecond == 123456 # rounds up as well d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234565</d>'), DateTime) # FIXME: this is very interesting. why? if not six.PY2: assert d.microsecond == 123456 else: assert d.microsecond == 123457
def test(xml): params = get_xml_as_object(xml, AnyDict) params = clear_list(params) method = get_method(params) c = wic_client() if not hasattr(c, method): raise Exception, 'Not implemented method: %s' % method kwargs = params[method] kwargs = getattr(c, method)(**kwargs) params[method] = kwargs return params
def call(xml): #Decrypt request xml = subprocess.check_output(['java', '-jar', '/usr/share/openstack-dashboard/webservices/java/Encryptor.jar', '-decrypt', xml, ENCRYPT_PASSWORD]).strip() xml = AnyXml.from_string(xml) #Process params from request params = get_xml_as_object(xml, AnyDict) params = clear_list(params) #Handle request id, description = handle_request(params) res = '''<?xml version="1.0" encoding="UTF-8"?> <result> <id>%s</id> <description>%s</description> </result>''' % (id, description, ) return res.decode('utf8')
def process(value): if value is not None: return get_xml_as_object(etree.fromstring(value), self.cls)
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)) 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]))
__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]))
# 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 # parse the schema document parsed_schema = parse_schema_string(schema)['some_ns'] # Get SomeObject definition from the parsed schema document NewObject = parsed_schema.types['SomeObject'] # We print an empty instance just to see the parsed fields. print NewObject() # Deserialize the xml document using the definition from the schema. new_instance = get_xml_as_object(etree.fromstring(data), NewObject) print new_instance assert new_instance.s == instance.s assert new_instance.i == instance.i assert new_instance.d == instance.d
# 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() print("the other namespace %r:" % docs['s2'].attrib['targetNamespace']) print(etree.tostring(docs['s2'], pretty_print=True)) print() # Object serialization and deserialization foo = Foo(a='a', b=1, c=3.4, d=datetime(2011, 02, 20), e=5, f='f') doc = get_object_as_xml(foo, Foo) print(etree.tostring(doc, pretty_print=True)) print(get_xml_as_object(doc, Foo)) print() # XmlData example. print("Product output (illustrates XmlData):") product = Product(id=uuid.uuid4(), edition=ProductEdition(id=uuid.uuid4(), name='My edition')) print(etree.tostring(get_object_as_xml(product, Product), pretty_print=True)) # See http://lxml.de/validation.html to see what this could be used for. print(get_validation_schema([Punk, Foo]))
# 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) # parse the schema document parsed_schema = parse_schema_string(schema)['some_ns'] # Get SomeObject definition from the parsed schema document NewObject = parsed_schema.types['SomeObject'] # We print an empty instance just to see the parsed fields. print(NewObject()) # Deserialize the xml document using the definition from the schema. new_instance = get_xml_as_object(etree.fromstring(data), NewObject) print(new_instance) assert new_instance.s == instance.s assert new_instance.i == instance.i assert new_instance.d == instance.d
def test_parse(self): package = get_xml_as_object(etree.fromstring(SAMPLE_OPF_DOC), Package)
# # * Neither the name of pyubl nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # from lxml import etree from spyne.interface.xml_schema import parser from spyne.util.xml import get_xml_as_object from spyne.util.xml import get_object_as_xml from ubl.const.schema.parsed import xmldsig from ubl.const.schema.parsed import NS data = open('example.xml', 'rb').read() data_elt = etree.fromstring(data, parser=parser.PARSER) sig_elt = data_elt.xpath('ds:Signature', namespaces={'ds': NS.XMLDSIG})[0] print(get_xml_as_object(sig_elt, xmldsig.SignatureType))