Example #1
0
  def DownloadCsvReport(self, report_job_id, report_xml=''):
    """Download and return report data in CSV format.

    Args:
      report_job_id: str ID of the report job.
      report_xml: str Report in XML format. Used for testing and debugging.

        Ex:
          report_job_id = '1234567890'
          report_xml = '<report><table><columns>...</columns></table></report>'

    Returns:
      str report data if all data is in ASCII.
      unicode report data if report contains non-ASCII characters.
      None if report failed.
    """
    # Get XML report data.
    if not report_xml:
      report_xml = self.DownloadXmlReport(report_job_id)
    if report_xml is None:
      return None

    # Prepare XML for DOM construction.
    #
    # Report consists of two parts: table and totals. Table contains column
    # names and rows of data. The outgoing CSV will contain only columns and
    # data rows. The totals are not included, but can be easily calculated
    # from the CSV data.
    report_xml = report_xml.replace('<?xml version="1.0" standalone="yes"?>',
                                    '')

    items = []
    try:
      # Construct DOM object.
      dom = minidom.parseString(report_xml)

      # Get data columns.
      columns = []
      column_dom = dom.getElementsByTagName('column')
      for column_item in column_dom:
        if column_item.hasAttributes():
          columns.append(column_item.attributes.item(0).value)

      # Get data rows.
      rows = []
      row_dom = dom.getElementsByTagName('row')
      for row_item in row_dom:
        if row_item.hasAttributes():
          attrs = row_item.attributes
          row = {}
          for index in range(attrs.length):
            row[attrs.item(index).name] = attrs.item(index).value
          rows.append(row)

      # Combine columns and rows into CSV format.
      for column in columns:
        items.append('%s,' % column)
      items = [''.join(items).rstrip(','), '\n']
      for row in rows:
        sub_items = []
        for column in columns:
          sub_items.append('%s,' % row[column])
        items.append('%s\n' %  ''.join(sub_items).rstrip(','))
    except:
      msg = ('Unable to parse report\'s data. Please, file a bug at '
             'http://code.google.com/p/google-api-adwords-python-lib/'
             'issues/list.')
      raise Error(msg)

    # A workaround for reports that include non-ASCII characters (i.e. ø).
    try:
      report_csv = str(''.join(items))
    except (UnicodeEncodeError, Exception):
      report_csv = unicode(''.join(items))
    return report_csv
Example #2
0
def build_results(result, buffer):
    # Builds a results.xml by parsing xml logs in QTestLib xml format and 
    # combining their output into check xml format.


    
    # Then add those to the doc structure
    
    #testElement.setAttributeNS(checkns,"result","Success")

    #x = 0 
    #for line  in buffer.split("\n"):
    #    print "LINE " + str(x) + ": " + line
    #    x=x+1
    
    tmpdoc = minidom.parseString(buffer)

    e_testcase = tmpdoc.getElementsByTagName("TestCase")
    title =  e_testcase[0].attributes.item(0).nodeValue

    suiteElement = doc.createElementNS(checkns,"suite")
    titleElement = doc.createElementNS(checkns,"title")


    node = doc.createTextNode(title)
    titleElement.appendChild(node)

    rootElement.appendChild(suiteElement)
    suiteElement.appendChild(titleElement)


    suites = tmpdoc.getElementsByTagName("TestFunction")

    for elem in tmpdoc.getElementsByTagName("TestFunction"):
        testElement = doc.createElementNS(checkns,"test")
    
        pathElement = doc.createElementNS(checkns,"path")
        fnElement = doc.createElementNS(checkns,"fn")
        idElement = doc.createElementNS(checkns,"id")
        descriptionElement = doc.createElementNS(checkns,"description")
        messageElement = doc.createElementNS(checkns,"message")

        node = doc.createTextNode( elem.attributes.item(0).nodeValue )
        idElement.appendChild(node)

        node = doc.createTextNode(".")
        pathElement.appendChild(node)

        testElement.appendChild(pathElement)
        testElement.appendChild(fnElement)
        testElement.appendChild(idElement)
        testElement.appendChild(descriptionElement)
        testElement.appendChild(messageElement)
    
        suiteElement.appendChild(testElement)
        
        incident = elem.getElementsByTagName("Incident")
        if incident != None:
            incident_type = incident[0].getAttribute("type")
            incident_file = incident[0].getAttribute("file")
            incident_row  = incident[0].getAttribute("line")

            if (incident_type == "pass"):
                testElement.setAttributeNS(checkns,"result","success")
            elif (incident_type == "fail"):            
                testElement.setAttributeNS(checkns,"result","failure")
            else:
                testElement.setAttributeNS(checkns,"result","error")

            if incident_file != None and incident_file != "":
                node = doc.createTextNode(incident_file + ":" + str(incident_row))
            else:
                node = doc.createTextNode("n/a:0")
            
            fnElement.appendChild(node)


            description = incident[0].getElementsByTagName("DataTag")
            if description != None and description.length>0 and description[0].firstChild != None:
                node = doc.createTextNode(str(description[0].firstChild.data)+" ")
                descriptionElement.appendChild(node)

            description = incident[0].getElementsByTagName("Description")
            if description != None and description.length>0 and description[0].firstChild != None:
                node = doc.createTextNode(str(description[0].firstChild.data)+" ")
                descriptionElement.appendChild(node)

    #for binary in binariestorun:
    #    filename = "/tmp/" + binary + ".log"
    #    file = open(filename,"r")
    #    buffer = file.read()
    #    print binary
    
    return
Example #3
0
  def DownloadCsvReport(self, report_job_id=-1, report_xml=''):
    """Download and return report data in CSV format.

    Report consists of two parts: table and totals. Table contains column
    names and rows of data. The outgoing CSV will contain only columns and
    data rows. The totals are not included, but can be easily calculated from
    the CSV data.

    Args:
      [optional]
      report_job_id: str ID of the report job.
      report_xml: str Report in XML format. Used for testing and debugging.

    Returns:
      str Report data if all data is in ASCII.
      unicode Report data if report contains non-ASCII characters.
      None If report failed.
    """
    # Get XML report data.
    if not report_xml and report_job_id > -1:
      report_xml = self.DownloadXmlReport(report_job_id)
    if report_xml is None:
      return None

    # Prepare XML for DOM construction.
    report_xml = report_xml.replace('<?xml version="1.0" standalone="yes"?>',
                                    '')
    csv_rows = []
    try:
      # Construct DOM object.
      dom = minidom.parseString(report_xml)

      # Get data columns.
      columns = []
      column_dom = dom.getElementsByTagName('column')
      for column_item in column_dom:
        if column_item.hasAttributes():
          columns.append(column_item.attributes.item(0).value)

      # Get data rows.
      rows = []
      row_dom = dom.getElementsByTagName('row')
      for row_item in row_dom:
        if row_item.hasAttributes():
          attrs = row_item.attributes
          row = {}
          for index in range(attrs.length):
            row[attrs.item(index).name] = attrs.item(index).value
          rows.append(row)

      # Combine columns and rows into CSV format.
      csv_rows = [[column for column in columns]]
      for row in rows: csv_rows.append([row[column] for column in columns])
    except:
      msg = ('Unable to parse report\'s data. Please, file a bug at '
             'http://code.google.com/p/google-api-adwords-python-lib/'
             'issues/list.')
      raise Error(msg)

    buffer = cStringIO.StringIO()
    # A workaround for reports that include non-ASCII characters (i.e. ø).
    try:
      csv.writer(buffer).writerows(csv_rows)
    except (UnicodeEncodeError, Exception):
      unicode_csv_rows = []
      for row in csv_rows:
        unicode_csv_rows.append(
            ','.join([Utils.CsvEscape(item) for item in row]))
      return unicode('\n'.join(unicode_csv_rows))
    return buffer.getvalue().rstrip()
Example #4
0
    def DownloadCsvReport(self, report_job_id, report_xml=''):
        """Download and return report data in CSV format.

    Args:
      report_job_id: str ID of the report job.
      report_xml: str Report in XML format. Used for testing and debugging.

        Ex:
          report_job_id = '1234567890'
          report_xml = '<report><table><columns>...</columns></table></report>'

    Returns:
      str report data if all data is in ASCII.
      unicode report data if report contains non-ASCII characters.
      None if report failed.
    """
        # Get XML report data.
        if not report_xml:
            report_xml = self.DownloadXmlReport(report_job_id)
        if report_xml is None:
            return None

        # Prepare XML for DOM construction.
        #
        # Report consists of two parts: table and totals. Table contains column
        # names and rows of data. The outgoing CSV will contain only columns and
        # data rows. The totals are not included, but can be easily calculated
        # from the CSV data.
        report_xml = report_xml.replace(
            '<?xml version="1.0" standalone="yes"?>', '')

        items = []
        try:
            # Construct DOM object.
            dom = minidom.parseString(report_xml)

            # Get data columns.
            columns = []
            column_dom = dom.getElementsByTagName('column')
            for column_item in column_dom:
                if column_item.hasAttributes():
                    columns.append(column_item.attributes.item(0).value)

            # Get data rows.
            rows = []
            row_dom = dom.getElementsByTagName('row')
            for row_item in row_dom:
                if row_item.hasAttributes():
                    attrs = row_item.attributes
                    row = {}
                    for index in range(attrs.length):
                        row[attrs.item(index).name] = attrs.item(index).value
                    rows.append(row)

            # Combine columns and rows into CSV format.
            for column in columns:
                items.append('%s,' % column)
            items = [''.join(items).rstrip(','), '\n']
            for row in rows:
                sub_items = []
                for column in columns:
                    sub_items.append('%s,' % row[column])
                items.append('%s\n' % ''.join(sub_items).rstrip(','))
        except:
            msg = ('Unable to parse report\'s data. Please, file a bug at '
                   'http://code.google.com/p/google-api-adwords-python-lib/'
                   'issues/list.')
            raise Error(msg)

        # A workaround for reports that include non-ASCII characters (i.e. ø).
        try:
            report_csv = str(''.join(items))
        except (UnicodeEncodeError, Exception):
            report_csv = unicode(''.join(items))
        return report_csv
    def DownloadCsvReport(self, report_job_id=-1, report_xml=''):
        """Download and return report data in CSV format.

    Report consists of two parts: table and totals. Table contains column
    names and rows of data. The outgoing CSV will contain only columns and
    data rows. The totals are not included, but can be easily calculated from
    the CSV data.

    Args:
      [optional]
      report_job_id: str ID of the report job.
      report_xml: str Report in XML format. Used for testing and debugging.

    Returns:
      str Report data if all data is in ASCII.
      unicode Report data if report contains non-ASCII characters.
      None If report failed.
    """
        # Get XML report data.
        if not report_xml and report_job_id > -1:
            report_xml = self.DownloadXmlReport(report_job_id)
        if report_xml is None:
            return None

        # Prepare XML for DOM construction.
        report_xml = report_xml.replace(
            '<?xml version="1.0" standalone="yes"?>', '')
        csv_rows = []
        try:
            # Construct DOM object.
            dom = minidom.parseString(report_xml)

            # Get data columns.
            columns = []
            column_dom = dom.getElementsByTagName('column')
            for column_item in column_dom:
                if column_item.hasAttributes():
                    columns.append(column_item.attributes.item(0).value)

            # Get data rows.
            rows = []
            row_dom = dom.getElementsByTagName('row')
            for row_item in row_dom:
                if row_item.hasAttributes():
                    attrs = row_item.attributes
                    row = {}
                    for index in range(attrs.length):
                        row[attrs.item(index).name] = attrs.item(index).value
                    rows.append(row)

            # Combine columns and rows into CSV format.
            csv_rows = [[column for column in columns]]
            for row in rows:
                csv_rows.append([row[column] for column in columns])
        except:
            msg = ('Unable to parse report\'s data. Please, file a bug at '
                   'http://code.google.com/p/google-api-adwords-python-lib/'
                   'issues/list.')
            raise Error(msg)

        buffer = cStringIO.StringIO()
        # A workaround for reports that include non-ASCII characters (i.e. ø).
        try:
            csv.writer(buffer).writerows(csv_rows)
        except (UnicodeEncodeError, Exception):
            unicode_csv_rows = []
            for row in csv_rows:
                unicode_csv_rows.append(','.join(
                    [Utils.CsvEscape(item) for item in row]))
            return unicode('\n'.join(unicode_csv_rows))
        return buffer.getvalue().rstrip()