def get_object_id(node, **kwargs): if node is None: return None for child_node in node.childNodes: if child_node.nodeName == '_object_id': from dopal.xmlutils import get_text_content return long(get_text_content(child_node)) else: return None
def get_atomic_value(self, node, **kwargs): if node is None: # The only atomic type which provides an empty response are # string types, so we will return an empty string. return '' elif isinstance(node, types.StringTypes): return node else: from dopal.xmlutils import get_text_content return get_text_content(node)
def process_xml_response(xml_node): if len(xml_node.childNodes) != 1: err = "expected one main block inside document, had %s" raise AzureusResponseXMLError, err % len(xml_node.childNodes) block_name = xml_node.firstChild.localName if block_name != 'RESPONSE': err = "expected a RESPONSE block, got %s block instead" raise AzureusResponseXMLError, err % block_name response_block = xml_node.firstChild az_dict = {} # We get an empty response block when the remote method doesn't return a # result (e.g. void), or returns a reponse which is effectively empty # (empty sequence, empty string) - or perhaps null itself. if not response_block.hasChildNodes(): return NullResponse(az_dict) # If we detect any child block with the name ERROR, then we'll raise an # error and ignore the rest of the content (it is possible for the block # to be embedded alongside other values - normally if something has gone # wrong during processing. # # XXX: Perhaps this could occur anywhere in the tree structure, what should # we do? from xml.dom import Node from dopal.xmlutils import get_text_content for child_block in response_block.childNodes: if child_block.nodeType == Node.ELEMENT_NODE and \ child_block.nodeName == 'ERROR': return ErrorResponse(az_dict, get_text_content(child_block)) if len(response_block.childNodes) == 1: node = response_block.firstChild if node.nodeType == Node.TEXT_NODE: return AtomicResponse(az_dict, get_text_content(node)) # We will have some "complex" XML structure. It may contain the definition # of one remote object, one remote object with other remote objects # branching off it, or no remote objects at all. # # We will return the XML as-is, but we will try to extract important data # so that it is more conveniently retrievable. # # Nodes which are categorised as being "important" are currently defined # as information about Azureus and any connection information. conn_node = None azureus_nodes = [] for node in response_block.childNodes: if node.nodeName.startswith('azureus_'): azureus_nodes.append(node) elif node.nodeName == '_connection_id': conn_node = node else: pass # Extract the data from the Azureus nodes. az_dict = {} for az_node in azureus_nodes: name = az_node.nodeName[8:] # (remove "azureus_" prefix) value = get_text_content(az_node) az_dict[name] = value # Extract the connection ID. if conn_node: connection_id = long(get_text_content(conn_node)) else: connection_id = None # We've got a structured definition. return StructuredResponse(az_dict, response_block, connection_id)