#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("
"," ").replace("

"," ").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('
]\\n\d\.\s|
\d\.\s') regular_expression_for_action = re.compile('
\\n\d\.\s|
\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('
]\\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()