[Fuego] [PATCH 2/3] ftc: gen-report: Add excel formatter

Hoang Van Tuyen tuyen.hoangvan at toshiba-tsdv.com
Fri Mar 2 03:36:29 UTC 2018


Add a formatter for excel format. The report file is stored in
${report_dir}. Currently, We always use pts_set_style function
for making the size of column proper. In the future, several
other fields can be added to the report, we can disable using
the pts_set_style function if it is needed

Signed-off-by: Hoang Van Tuyen <tuyen.hoangvan at toshiba-tsdv.com>
---
  engine/scripts/ftc | 68 
+++++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 67 insertions(+), 1 deletion(-)

diff --git a/engine/scripts/ftc b/engine/scripts/ftc
index 0b99f94..e0ed13d 100755
--- a/engine/scripts/ftc
+++ b/engine/scripts/ftc
@@ -2077,6 +2077,69 @@ def gen_pdf_report(header_data, report_data, 
report_dir):
      elements.append(table)
      doc.build(elements, onFirstPage=add_page_number, 
onLaterPages=add_page_number)

+# support generating the excel report format
+
+# adjust column width size
+# the size of data in fields and header_fields is quite small,
+# so, always use pts_set_style now
+def pts_set_style(ws):
+    dims ={}
+    for row in ws.rows:
+        for cell in row:
+            if cell.value:
+                dims[cell.column] = max((dims.get(cell.column, 0), 
len(str(cell.value)) + 2))
+    for col, value in dims.items():
+        ws.column_dimensions[col].width = value
+
+# return number to Excel-style column name
+def excel_column_name(n):
+    name = ""
+    while n > 0:
+        n, r = divmod (n - 1, 26)
+        name = chr(r + ord('A')) + name
+    return name
+
+def gen_excel_report(header_data, report_data, report_dir):
+    from openpyxl import Workbook
+    from openpyxl.style import Fill, Color
+
+    # create an excel file and add worksheets
+    workbook = Workbook()
+    header_sheet = workbook.create_sheet(title="Header")
+    data_sheet = workbook.create_sheet(title="Data")
+
+    # generate header
+    header_sheet.append(["Fuego Test Report"])
+    for field,val_str in header_data:
+        header_sheet.append(["%s: %s" % (field, val_str)])
+    # in future, if we don't want this, use a flag for checking using 
pts_set_style
+    pts_set_style(header_sheet)
+
+    # generate lines for this report
+    for item in report_data:
+        data_sheet.append(item)
+    # in future, we can disable this by using a flag
+    pts_set_style(data_sheet)
+
+    # format header row to sheet
+    head_fill = Fill()
+    head_fill.start_color = Color('FFC0C0C0')
+    head_fill.end_color = Color('FFC0C0C0')
+    head_fill.fill_type = Fill.FILL_SOLID
+    for j in range(0, data_sheet.get_highest_column()):
+        data_sheet.cell(row = 0, column = j).style.fill = head_fill
+
+    # add auto filter
+    data_sheet.auto_filter = "A1:" + 
excel_column_name(data_sheet.get_highest_column()) + 
str(data_sheet.get_highest_row())
+
+    # if we have added sheets, remove the default one ("Sheet")
+    sheets = workbook.get_sheet_names()
+    if len(sheets) > 1:
+ workbook.remove_sheet(workbook.get_sheet_by_name("Sheet"))
+
+    # save the report
+    workbook.save(report_dir + "report.xlsx")
+
  # generate a report from run results
  def do_gen_report(conf, options):
      global quiet, verbose
@@ -2098,7 +2161,7 @@ def do_gen_report(conf, options):
      fmt="txt"
      if "--format" in options:
          fmt = options[options.index("--format")+1]
-        if fmt not in ["txt","html","pdf"]:
+        if fmt not in ["txt","html","pdf","excel"]:
              error_out("Unsupported format '%s' specified" % fmt)

      if "--layout" in options:
@@ -2129,6 +2192,9 @@ def do_gen_report(conf, options):
      if fmt=="pdf":
          gen_pdf_report(header_data, report_data, report_dir)
          sys.exit(0)
+    if fmt=="excel":
+        gen_excel_report(header_data, report_data, report_dir)
+        sys.exit(0)

      print report

-- 
2.1.4


-- 
================================================================
Hoang Van Tuyen (Mr.)
TOSHIBA SOFTWARE DEVELOPMENT (VIETNAM) CO., LTD.
16th Floor, VIT Building, 519 Kim Ma Str., Ba Dinh Dist., Hanoi, Vietnam
Tel: 84-4-22208801 (Company) - Ext.251
Fax: 84-4-22208802 (Company)
Email: tuyen.hoangvan at toshiba-tsdv.com
================================================================


-- 
This mail was scanned by BitDefender
For more information please visit http://www.bitdefender.com



More information about the Fuego mailing list