CSIT-755: Presentation and analytics layer
[csit.git] / resources / tools / report_gen / run_robot_data.py
index e2bcfa2..d5672b4 100755 (executable)
@@ -21,18 +21,28 @@ html, rst) to defined output file.
 Supported formats:
  - html
  - rst
+ - wiki
 
 :TODO:
- - wiki
  - md
 
 :Example:
 
-robot_output_parser_publish.py -i output.xml" -o "tests.rst" -f "rst" -s 3 -l 2
+run_robot_data.py -i "output.xml" -o "tests.rst" -f "rst" -s 3 -l 2
 
 The example reads the data from "output.xml", writes the output to "tests.rst"
 in rst format. It will start on the 3rd level of xml structure and the generated
 document hierarchy will start on the 2nd level.
+All test suites will be processed.
+
+:Example:
+
+run_robot_data.py -i "output.xml" -o "tests.rst" -f "rst" -r "(.*)(lisp)(.*)"
+
+The example reads the data from "output.xml", writes the output to "tests.rst"
+in rst format. It will start on the 1st level of xml structure and the generated
+document hierarchy will start on the 1st level (default values).
+Only the test suites which match the given regular expression are processed.
 """
 
 import argparse
@@ -207,11 +217,11 @@ def gen_html_table(data):
     """
 
     table = '<table width=100% border=1><tr>'
-    table += '<th width=30%>Name</th>'
-    table += '<th width=50%>Documentation</th>'
-    table += '<th width=20%>Status</th></tr>'
+    table += '<th width=30%>' + data[-1][0] + '</th>'
+    table += '<th width=50%>' + data[-1][1] + '</th>'
+    table += '<th width=20%>' + data[-1][2] + '</th></tr>'
 
-    for item in data[0:-2]:
+    for item in data[0:-1]:
         table += '<tr>'
         for element in item:
             table += '<td>' + element.replace(' |br| ', '<br>') + '</td>'
@@ -237,6 +247,11 @@ def do_rst(data, args):
     output = open(args.output, 'w')
     output.write('\n.. |br| raw:: html\n\n    <br />\n\n')
 
+    if (args.title):
+        output.write(args.title + '\n' +
+                     hdrs[shift - 1] *
+                     len(args.title) + '\n\n')
+
     for item in data:
         if int(item['level']) < start:
             continue
@@ -302,10 +317,12 @@ def gen_rst_table(data):
     table.append(template.format(*titles))
     table.append(th_separator)
     # generate table rows
-    for d in data[0:-2]:
-        table.append(template.format(*d))
+    for item in data[0:-2]:
+        desc = re.sub(r'(^ \|br\| )', r'', item[1])
+        table.append(template.format(item[0], desc, item[2]))
         table.append(separator)
-    table.append(template.format(*data[-2]))
+    desc = re.sub(r'(^ \|br\| )', r'', data[-2][1])
+    table.append(template.format(data[-2][0], desc, data[-2][2]))
     table.append(separator)
     return '\n'.join(table)
 
@@ -331,7 +348,51 @@ def do_wiki(data, args):
     :type args: ArgumentParser
     :returns: Nothing.
     """
-    raise NotImplementedError("Export to 'wiki' format is not implemented.")
+
+    shift = int(args.level)
+    start = int(args.start)
+
+    output = open(args.output, 'w')
+
+    for item in data:
+        if int(item['level']) < start:
+            continue
+        if 'ndrchk' in item['title'].lower():
+            continue
+        mark = "=" * (int(item['level']) - start + shift) + ' '
+        output.write(mark + item['title'].lower() + mark + '\n')
+        output.write(item['doc'].replace('*', "'''").replace('|br|', '\n*') +
+                     '\n')
+        try:
+            output.write(gen_wiki_table(item['tests']) + '\n\n')
+        except KeyError:
+            continue
+    output.close()
+
+
+def gen_wiki_table(data):
+    """Generates a table with TCs' names, documentation and messages / statuses
+    in wiki format.
+
+    :param data: Json data representing a table with TCs.
+    :type data: str
+    :returns: Table with TCs' names, documentation and messages / statuses in
+    wiki format.
+    :rtype: str
+    """
+
+    table = '{| class="wikitable"\n'
+    header = ""
+    for item in data[-1]:
+        header += '!{}\n'.format(item)
+    table += header
+    for item in data[0:-1]:
+        desc = re.sub(r'(^ \|br\| )', r'', item[1]).replace(' |br| ', '\n\n')
+        msg = item[2].replace(' |br| ', '\n\n')
+        table += '|-\n|{}\n|{}\n|{}\n'.format(item[0], desc, msg)
+    table += '|}\n'
+
+    return table
 
 
 def process_robot_file(args):
@@ -358,14 +419,23 @@ def process_robot_file(args):
         data = json.load(json_file)
     data.pop(-1)
 
+    if args.regex:
+        results = list()
+        regex = re.compile(args.regex)
+        for item in data:
+            if re.search(regex, item['title'].lower()):
+                results.append(item)
+    else:
+        results = data
+
     if args.formatting == 'rst':
-        do_rst(data, args)
+        do_rst(results, args)
     elif args.formatting == 'wiki':
-        do_wiki(data, args)
+        do_wiki(results, args)
     elif args.formatting == 'html':
-        do_html(data, args)
+        do_html(results, args)
     elif args.formatting == 'md':
-        do_md(data, args)
+        do_md(results, args)
 
 
 def parse_args():
@@ -398,6 +468,15 @@ def parse_args():
                         type=int,
                         default=1,
                         help="The level of the first chapter in generated file")
+    parser.add_argument("-r", "--regex",
+                        type=str,
+                        default=None,
+                        help="Regular expression used to select test suites. "
+                             "If None, all test suites are selected.")
+    parser.add_argument("-t", "--title",
+                        type=str,
+                        default=None,
+                        help="Title of the output.")
 
     return parser.parse_args()