def _build_xml_data_test_schema(self, custom_root): tns = 'kickass.ns' class ProductEdition(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) if custom_root: name = XmlData(Uuid) else: name = XmlData(Unicode) class Product(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) edition = ProductEdition class ExampleService(ServiceBase): @rpc(Product, _returns=Product) def say_my_uuid(ctx, product): pass app = Application([ExampleService], tns='kickass.ns', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11() ) schema = XmlSchema(app.interface) schema.build_interface_document() schema.build_validation_schema() doc = schema.get_interface_document()['tns'] print(etree.tostring(doc, pretty_print=True)) return schema
def get_schema_documents(models, default_namespace=None): '''Returns the schema documents in a dict whose keys are namespace prefixes and values are Element objects. :param models: A list of spyne.model classes that will be represented in the schema. ''' if default_namespace is None: default_namespace = models[0].get_namespace() fake_app = FakeApplication() fake_app.tns = default_namespace fake_app.services = [] interface = Interface(fake_app) for m in models: interface.add_class(m) interface.populate_interface(fake_app) document = XmlSchema(interface) document.build_interface_document() return document.get_interface_document()
def test_binary_encodings(self): class Product(ComplexModel): __namespace__ = 'some_ns' hex = ByteArray(encoding='hex') base64_1 = ByteArray(encoding='base64') base64_2 = ByteArray class SomeService(ServiceBase): @rpc(Product, _returns=Product) def echo_product(ctx, product): logging.info('edition_id: %r', product.edition_id) return product app = Application([SomeService], tns='some_ns', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11() ) _ns = {'xs': NS_XSD} pref_xs = ns.PREFMAP[NS_XSD] xs = XmlSchema(app.interface) xs.build_interface_document() elt = xs.get_interface_document()['tns'].xpath( '//xs:complexType[@name="Product"]', namespaces=_ns)[0] assert elt.xpath('//xs:element[@name="base64_1"]/@type', namespaces=_ns)[0] == '%s:base64Binary' % pref_xs assert elt.xpath('//xs:element[@name="base64_2"]/@type', namespaces=_ns)[0] == '%s:base64Binary' % pref_xs assert elt.xpath('//xs:element[@name="hex"]/@type', namespaces=_ns)[0] == '%s:hexBinary' % pref_xs
def _write_xsd(config): from lxml import etree from spyne.interface.xml_schema import XmlSchema from spyne.util.appreg import applications for (tns, name), appdata in applications.items(): xsd = XmlSchema(appdata.app.interface) xsd.build_interface_document() schemas = xsd.get_interface_document() dir_name = join(config.write_xsd, 'schema.%s' % name) try: os.makedirs(dir_name) except OSError: pass for k, v in schemas.items(): file_name = os.path.join(dir_name, "%s.xsd" % k) try: with open(file_name, 'wb') as f: etree.ElementTree(v).write(f, pretty_print=True) except Exception as e: print("Error:", e) return -1 print("written",file_name, "for ns", appdata.app.interface.nsmap[k]) return True # to force exit
def get_schema_documents(models, default_namespace=None): """Returns the schema documents in a dict whose keys are namespace prefixes and values are Element objects. :param models: A list of spyne.model classes that will be represented in the schema. """ if default_namespace is None: default_namespace = models[0].get_namespace() fake_app = FakeApplication() fake_app.tns = default_namespace fake_app.services = [] interface = Interface(fake_app) for m in models: m.resolve_namespace(m, default_namespace) interface.add_class(m) interface.populate_interface(fake_app) document = XmlSchema(interface) document.build_interface_document() return document.get_interface_document()
def _test_xml_data(self): tns = 'kickass.ns' class ProductEdition(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) name = XmlData(Unicode) class Product(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) edition = ProductEdition sample = XmlAttribute(Unicode, attribute_of='edition') class ExampleService(ServiceBase): @rpc(Product, _returns=Product) def say_my_uuid(ctx, product): pass app = Application([ExampleService], tns='kickass.ns', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11()) schema = XmlSchema(app.interface) schema.build_interface_document() doc = schema.get_interface_document()['tns'] print(etree.tostring(doc, pretty_print=True)) assert len( doc.xpath( '/xs:schema/xs:complexType[@name="Product"]' '/xs:sequence/xs:element[@name="edition"]' '/xs:complexType/xs:simpleContent/xs:extension' '/xs:attribute[@name="id"]', namespaces={'xs': ns.xsd})) == 1
def _test_xml_data(self): tns = 'kickass.ns' class ProductEdition(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) name = XmlData(Unicode) class Product(ComplexModel): __namespace__ = tns id = XmlAttribute(Uuid) edition = ProductEdition sample = XmlAttribute(Unicode, attribute_of='edition') class ExampleService(ServiceBase): @rpc(Product, _returns=Product) def say_my_uuid(ctx, product): pass app = Application([ExampleService], tns='kickass.ns', in_protocol=Soap11(validator='lxml'), out_protocol=Soap11() ) schema = XmlSchema(app.interface) schema.build_interface_document() doc = schema.get_interface_document()['tns'] print etree.tostring(doc, pretty_print=True) assert len(doc.xpath( '/xs:schema/xs:complexType[@name="Product"]' '/xs:sequence/xs:element[@name="edition"]' '/xs:complexType/xs:simpleContent/xs:extension' '/xs:attribute[@name="id"]' ,namespaces={'xs': ns.xsd})) == 1