aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/testopia_to_json_script.py
blob: c644292e2f69fc7249d8eeb5df3a892a16e8c38a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#Script to automate converting Testopia test case(s) to predefined json output
import csv
import json 
import re
import html

class CsvToJsonSpecialConvertor(object):
    def __init__(self, csv_file_read):
        super(CsvToJsonSpecialConvertor, self).__init__()
        self.csv_file_read = csv_file_read
        self.TAG_RE = re.compile(r'<(?!br)(?!/p).*?>')

    def remove_tags(self,text):
        """
        This module basically removes tags
        and the other unwanted strings and other strings 
        """
        return self.TAG_RE.sub('', text).replace("<br>"," ").replace("</p>"," ").replace("\r","").replace("nbsp"," ").replace("&;","").replace("<\t>"," ")
      
    def dataformator(self,row):
        """
        for creating data in required format and
        storing it in formated ways .
        """
        data = dict()
        data["@alias"] = row[8].split("(")[0].strip().replace(" ","_")+"."+row[8].split("(")[0].strip().replace(" ","_")+"."+row[2].strip().strip(".").replace(" ","_")
        data["summary"] = row[2].replace("(auto)","").strip().replace(" ","_")
        data["author"] = {
          "email": row[3],
          "name": row[3]
        },
        regular_expression_for_expected = re.compile('<br>]\\n\d\.\s|<br>\d\.\s')
        regular_expression_for_action = re.compile('<br>\\n\d\.\s|<br>\d\.\s|\d\.\s')
        # html_unescape_string = html.unescape(html_string)
        row_22 =  html.unescape(row[22])
        row_23 =  html.unescape(row[23])
        row_22 =  row_22.replace("\u00a0","") 
        row_23 =  row_23.replace("\u00a0","")
        row_22 =  row_22.replace("\u201d","")
        row_22 =  row_22.replace("\u201c","")
        row_22 =  row_22.replace("\u2013","")
        row_23 =  row_23.replace("\u201d","")
        row_23 =  row_23.replace("\u201c","")
        row_23 =  row_23.replace("\u2013","")
        row_23 =  row_23.replace("\u2013","")
        actions_column_data = re.split(regular_expression_for_action,row_22)
        exepected_result_column_data = re.split(regular_expression_for_expected,row_23)
        data_for_excution_raw =  list(map(self.remove_tags, actions_column_data))
        #this is for removing empty string from list
        data_for_excution  = []
        for data_in_exe in data_for_excution_raw:
          if data_in_exe:
            data_for_excution.append(data_in_exe)
        expected_results_raw = list(map(self.remove_tags, exepected_result_column_data))
        
        excution_list ={}
        list_len = len(data_for_excution)
        final_regular_ex_data = re.compile('<br>]\\n\d\.\s|\d\.\s')
        meta_final_expected_result  = re.split(final_regular_ex_data, expected_results_raw[0])
        # expected_results for removing empty string
        new_map=[]
        for data_in in meta_final_expected_result:
          if data_in:
            new_map.append(data_in)
        #for finding the point like 1.,2.  to convert it in list     
        ms = re.findall('[0-9]\.\s',expected_results_raw[0] if meta_final_expected_result else "")
        #for creating dict with the same {"1. ":"run the commands"} etc for mapping the stesps and 
        #results
        data_in_dict = dict(zip(ms,new_map)) if ms else {"last":new_map[0] if new_map else "" }        
        count_data_for_last = len(data_for_excution)
        ms = len(meta_final_expected_result)
        new_count = 1
        for i, n in enumerate(data_for_excution):
            new_data = {
                   new_count :{
                      "action":n,
                      "expected_results": data_in_dict.get("last")  if ms == 1 and (i+1 == count_data_for_last)  else data_in_dict.get(str(i+1)+". ","")         
                    }
              }
            new_count= new_count+1   
            excution_list.update(new_data)
        data["execution"]= excution_list
        return data

    def master(self):
        csvfile = open(self.csv_file_read, 'rU')
        jsonfile = open(self.csv_file_read.split(".")[0]+".json", 'w')
        reader = csv.reader(csvfile,delimiter=',',)
        final_list = []
        count= 0
        for row in reader:
            if count:
                data = self.dataformator(row)
                final_list.append({"test":data})
            count = count+1
        out= json.dumps(final_list, sort_keys=True, indent=4)
        jsonfile.write(out)        
if __name__ == "__main__":
    obje = CsvToJsonSpecialConvertor("MANUAL_BSPQEMU_core-image-sato-sdk_Genericx86-64_MTURBOT.csv")
    obje.master()