def __init__(self, xml_parser=None, pretty_xml=False):
        """Inits SoapBuffer.

    Args:
      xml_parser: str XML parser to use.
      pretty_xml: bool Indicator for whether to prettify XML.
    """
        super(SoapBuffer, self).__init__()

        self._buffer = ''
        self.__dump = {}
        self.__xml_parser = xml_parser
        # Pick a default XML parser, if none was set.
        if not self.__xml_parser:
            for parser, status in [(PYXML, PYXML_LIB), (ETREE, ETREE_LIB)]:
                if status: self.__xml_parser = parser
        # Validate current state of the chosen XML parser.
        if self.__xml_parser == PYXML and not PYXML_LIB:
            msg = 'PyXML v%s or newer is required.' % MIN_PYXML_VERSION
            raise MissingPackageError(msg)
        elif self.__xml_parser == ETREE and not ETREE_LIB:
            msg = 'ElementTree v%s or newer is required.' % MIN_ETREE_VERSION
            raise MissingPackageError(msg)
        elif self.__xml_parser != PYXML and self.__xml_parser != ETREE:
            msg = ('Invalid input for XML parser, expecting one of %s.' %
                   sorted(set(PYXML + ETREE)))
            raise InvalidInputError(msg)
        # Set XML parser signature.
        if self.__xml_parser == PYXML:
            self.__xml_parser_sig = '%s v%s' % (PYXML_NAME, MIN_PYXML_VERSION)
        elif self.__xml_parser == ETREE:
            self.__xml_parser_sig = '%s v%s' % (ETREE_NAME, ETREE_VERSION)
        self.__pretty_xml = pretty_xml
    def InjectXml(self, xml_in):
        """Hook into the SoapBuffer to test local SOAP XML.

    Prepares dump dict with a given input.

    Args:
      xml_in: str SOAP XML request, response, or both.
    """
        # Prepare string for use with regular expression.
        xml_in = xml_in.replace('\n', '%newline%')

        try:
            pattern = re.compile(
                '(<SOAP-ENV:Envelope:Envelope.*</SOAP-ENV:Envelope>|'
                '<ns0:Envelope.*RequestHeader.*?</.*?:Envelope>)')
            req = pattern.findall(xml_in)
            pattern = re.compile(
                '(<soap.*?:Envelope.*</soap.*?:Envelope>|'
                '<ns0:Envelope.*ResponseHeader.*?</.*?:Envelope>)')
            res = pattern.findall(xml_in)

            # Do we have a SOAP XML request?
            if req:
                # Rebuild original formatting of the string and dump it.
                req = req[0].replace('%newline%', '\n')
                self.__dump['dumpSoapOut'] = (
                    '%s Outgoing SOAP %s\n'
                    '<?xml version="1.0" encoding="UTF-8"?>\n%s\n'
                    '%s' % ('*' * 3, '*' * 54, req, '*' * 72))

            # Do we have a SOAP XML response?
            if res:
                # Rebuild original formatting of the string and dump it.
                res = res[0].replace('%newline%', '\n')
                self.__dump['dumpSoapIn'] = (
                    '%s Incoming SOAP %s\n'
                    '<?xml version="1.0" encoding="UTF-8"?>\n%s\n'
                    '%s' % ('*' * 3, '*' * 54, res.lstrip('\n'), '*' * 72))
        except Exception:
            msg = 'Invalid input, expecting SOAP XML request, response, or both.'
            raise InvalidInputError(msg)
Beispiel #3
0
    auth[header] = raw_input(prompt_msg).rstrip('\r')
    if header in old_auth:
      if auth[header] == 'none':
        auth[header] = ''
      elif not auth[header]:
        auth[header] = old_auth[header]
    else:
      if auth[header] == 'none':
        auth[header] = ''
  elif source == 'config':
    # Prompt user to update configuration values.
    if header == 'xml_parser':
      res = raw_input('%s: ' % prompt_msg).rstrip('\r')
      if not SanityCheck.IsConfigUserInputValid(res, ['1', '2']):
        msg = 'Possible values are \'1\' or \'2\'.'
        raise InvalidInputError(msg)
    elif header == 'app_name':
      res = raw_input('%s: ' % prompt_msg).rstrip('\r')
    else:
      res = raw_input('%s [y/n]: ' % prompt_msg).rstrip('\r')
      if not SanityCheck.IsConfigUserInputValid(res, ['y', 'n']):
        msg = 'Possible values are \'y\' or \'n\'.'
        raise InvalidInputError(msg)
    config[header] = res

# Load new authentication credentials into dfa_api_auth.pkl.
try:
  fh = open(AUTH_PKL, 'w')
  try:
    pickle.dump(auth, fh)
  finally: