def merge(self, filename): # We change the namespace of XMLSchema to something else because # the schema processor in the bubbles library can't operate on the # XS namespace. Hack... doc = file(filename).read() doc = doc.replace(ns.XS, 'urn:metaschema') xsd = ET.fromstring(doc) self.schema = Schema(xsd)
def guess(self, filename): text = file(filename).read() text = re.sub(r'xmlns="[^"]*"', '', text) xml = ET.fromstring(text) obj = DynamicObject(xml) tns, tag = ns.split(xml.tag) root = self.complexType(tag, obj) el = Element(name=tag, type=root.name) self._addElement(self.schema, el)
def invoke(self, operation, *args, **kwargs): ''' Invoke a SOAP operation. ''' self._reqno += 1 retxml = kwargs.pop('__retxml__', self.retxml) timeout = kwargs.pop('__timeout__', self.timeout) transport_options = kwargs.pop('__transport__', {}) # Create an instance of the request message and initialize # the object from the arguments param = self.factory(operation.imsg) tmpl = param.__template__ for k,v in zip((t[0] for t in tmpl), args): param[k] = v for k,v in kwargs.items(): param[k] = v # Build the soap envelope and set the http headers payload = self.envelope(self.headers, param, operation.imsg) httphdr = { 'Content-Type': 'text/xml', 'SOAPAction': operation.action } httphdr.update(self.httphdr) # Construct and issue the request, read the response payload = ET.tostring(payload, pretty_print=True) log.debug('=== SOAP REQUEST ===\n%s', re.sub(r'password>.*?<', r'password>*****<', payload )) req = urllib2.Request(self.url, payload, httphdr) try: if self._inject: xml = ET.fromstring(self._inject.next()) else: if hasattr(self.transport, 'open'): rsp = self.transport.open(req, timeout=timeout, **transport_options) else: rsp = self.transport.urlopen(req, timeout=timeout, **transport_options) xml = ET.parse(rsp) except urllib2.HTTPError as ex: xml = ET.parse(ex) log.debug('=== SOAP RESPONSE ===\n%s', xmlstr(xml)) # Get the soap body retval = xml.find(ns.expand('soapenv:Body', self.nsmap)) if not retxml: # Does the body contain any nodes? if len(retval): # Get the first child and examine it retval = retval[0] namespace, tag = ns.split(retval.tag) # If it's a fault, convert it to an exception if tag == 'Fault': raise SoapFault(retval, self) # Otherwise, deserialize obj = self.factory(operation.omsg, retval) # If the deserialized # object has only one item, return that item, otherwise the # whole object # # This is so if the return value is a single primitive type # (like a string), you don't have to dig into an object just # to get at the single primitive return value if len(obj) == 1: obj = obj[0] retval = obj else: retval = None return retval
def invoke(self, operation, *args, **kwargs): ''' Invoke a SOAP operation. ''' self._reqno += 1 retxml = kwargs.pop('__retxml__', self.retxml) timeout = kwargs.pop('__timeout__', self.timeout) transport_options = kwargs.pop('__transport__', {}) # Create an instance of the request message and initialize # the object from the arguments param = self.factory(operation.imsg) tmpl = param.__template__ for k,v in zip((t[0] for t in tmpl), args): param[k] = v for k,v in kwargs.items(): param[k] = v # Build the soap envelope and set the http headers payload = self.envelope(self.headers, param, operation.imsg) httphdr = { 'Content-Type': 'text/xml', 'SOAPAction': operation.action } httphdr.update(self.httphdr) # Construct and issue the request, read the response payload = ET.tostring(payload, pretty_print=True) log.debug('=== SOAP REQUEST ===\n%s', payload) req = urllib2.Request(self.url, payload, httphdr) try: if self._inject: xml = ET.fromstring(self._inject.next()) else: if hasattr(self.transport, 'open'): rsp = self.transport.open(req, timeout=timeout, **transport_options) else: rsp = self.transport.urlopen(req, timeout=timeout, **transport_options) xml = ET.parse(rsp) except urllib2.HTTPError as ex: xml = ET.parse(ex) log.debug('=== SOAP RESPONSE ===\n%s', xmlstr(xml)) # Get the soap body retval = xml.find(ns.expand('soapenv:Body', self.nsmap)) if not retxml: # Does the body contain any nodes? if len(retval): # Get the first child and examine it retval = retval[0] namespace, tag = ns.split(retval.tag) # If it's a fault, convert it to an exception if tag == 'Fault': raise SoapFault(retval, self) # Otherwise, deserialize obj = self.factory(operation.omsg, retval) # If the deserialized # object has only one item, return that item, otherwise the # whole object # # This is so if the return value is a single primitive type # (like a string), you don't have to dig into an object just # to get at the single primitive return value if len(obj) == 1: obj = obj[0] retval = obj else: retval = None return retval