def add_missing_elements_for_methods(self): def missing_methods(): for service in self.interface.services: for method in service.public_methods.values(): if method.aux is None: yield method pref_tns = self.interface.prefmap[self.interface.tns] elements = self.get_schema_info(pref_tns).elements schema_root = self.schema_dict[pref_tns] for method in missing_methods(): name = method.in_message.Attributes.sub_name if name is None: name = method.in_message.get_type_name() if not name in elements: element = etree.Element(ns.XSD('element')) element.set('name', name) element.set('type', method.in_message.get_type_name_ns(self.interface)) elements[name] = element schema_root.append(element) if method.out_message is not None: name = method.out_message.Attributes.sub_name if name is None: name = method.out_message.get_type_name() if not name in elements: element = etree.Element(ns.XSD('element')) element.set('name', name) element.set('type', method.out_message \ .get_type_name_ns(self.interface)) elements[name] = element schema_root.append(element)
def get_schema_node(self, pref): """Return schema node for the given namespace prefix.""" if not (pref in self.schema_dict): schema = etree.Element(ns.XSD('schema'), nsmap=self.interface.nsmap) schema.set("targetNamespace", self.interface.nsmap[pref]) schema.set("elementFormDefault", "qualified") self.schema_dict[pref] = schema else: schema = self.schema_dict[pref] return schema
def test_add_to_schema(self): class CM(ComplexModel): i = Integer s = String a = XmlAttribute(String) app = FakeApp() app.tns = 'tns' CM.resolve_namespace(CM, app.tns) interface = Interface(app) interface.add_class(CM) wsdl = Wsdl11(interface) wsdl.build_interface_document('http://a-aaaa.com') pref = CM.get_namespace_prefix(interface) type_def = wsdl.get_schema_info(pref).types[CM.get_type_name()] attribute_def = type_def.find(xml.XSD('attribute')) print(etree.tostring(type_def, pretty_print=True)) self.assertIsNotNone(attribute_def) self.assertEqual(attribute_def.get('name'), 'a') self.assertEqual(attribute_def.get('type'), CM.a.type.get_type_name_ns(interface))
def build_schema_nodes(self, with_schema_location=False): self.schema_dict = {} tags = set() for cls in chain.from_iterable(toposort2(self.interface.deps)): self.add(cls, tags) for pref in self.namespaces: schema = self.get_schema_node(pref) # append import tags for namespace in self.interface.imports[ self.interface.nsmap[pref]]: import_ = etree.SubElement(schema, ns.XSD('import')) import_.set("namespace", namespace) import_pref = self.interface.get_namespace_prefix(namespace) if with_schema_location and \ self.namespaces.get(import_pref, False): import_.set('schemaLocation', "%s.xsd" % import_pref) sl = ns.schema_location.get(namespace, None) if not (sl is None): import_.set('schemaLocation', sl) # append simpleType and complexType tags for node in self.namespaces[pref].types.values(): schema.append(node) # append element tags for node in self.namespaces[pref].elements.values(): schema.append(node) self.add_missing_elements_for_methods() self.event_manager.fire_event('document_built', self) self.event_manager.fire_event('xml_document_built', self)