Exemplo n.º 1
0
    def IsHandshakeComplete(self):
        """Return state of the handshake.

    A successful handshake occurs when a SOAP request is sent and a SOAP
    response is recieved.

    Returns:
      bool True if successful handshake, False otherwise.
    """
        if (self.GetHeadersOut() and self.GetSoapOut() and self.GetHeadersIn()
                and self.GetSoapIn() and not Utils.IsHtml(self.GetSoapIn())):
            return True
        return False
Exemplo n.º 2
0
    def __PrettyPrintXml(self, doc, level=0):
        """Return a pretty-printed version of the XML document.

    Args:
      doc: str XML document.
      level: int Level of prettiness, defaults to 0. If -1, remove prettiness.

    Returns:
      str Pretty-printed version of the XML document.
    """
        # Make sure we have a valid doc to work with.
        if Utils.IsHtml(doc):
            return doc

        try:
            if self.__xml_parser == PYXML:
                dom = minidom.parseString(doc)
                pretty_doc = dom.toprettyxml(indent=' ', encoding='UTF-8')
            elif self.__xml_parser == ETREE:
                tree = etree.fromstring(doc)
                self.__Indent(tree)
                pretty_doc = etree.tostring(tree, 'UTF-8')
        except (ExpatError, SyntaxError):
            # If there was a problem with loading XML message into a DOM, return
            # original XML message.
            return doc

        # Adjust prettiness of data values in the XML document.
        #
        # Before:  <operations>
        #            0
        #          </operations>
        #
        # After:   <operations>0</operations>
        pattern = re.compile('\n\s+\n')
        pretty_doc = pattern.sub('\n', pretty_doc)
        groups = re.findall('>(\n\s+(.*?)\n\s+)</', pretty_doc, re.M)
        for old, new in groups:
            if old and new and (new.find('<') > -1 or new.find('>') > -1):
                continue
            pretty_doc = pretty_doc.replace(old, new)

        if level == -1:
            pattern = re.compile('>\s+<')
            pretty_doc = pattern.sub('><', pretty_doc)
        return pretty_doc.strip('\n')