def generate_html_report(self, report_in_js=True): self.generate_report_path() reportjs = self.report_path + '/' + 'report.js' reporttemplate = self.html_path + '/' + 'report.html' reporthtml = self.report_path + '/' + self.dict_all["project"]["name"] + '.html' if report_in_js: shutil.copy(reporttemplate, reporthtml) with open(reportjs, 'w') as f: try: d = Log.beautify_json(self.dict_all).encode('utf8').decode('utf8') f.writelines(["report = ", "\n", d, ";", "\n"]) except UnicodeDecodeError as e: print(e) else: reporthtml = self.report_path + '/' + self.dict_all["project"]["name"] + '_report.html' with open(reporttemplate, 'r') as src, open(reporthtml, 'w') as dst: for line in src: if "<script" in line and "src" in line and "report.js" in line: d = Log.beautify_json(self.dict_all) dst.writelines(["<script type='text/javascript' charset='utf-8'>", "\n"]) dst.writelines(["var report = ", "\n"]) dst.writelines([d.encode("utf8").decode("utf8"), ";", "\n"]) dst.writelines(["</script>", "\n"]) else: dst.write(line) pass
def write_to_file(thepath, thefile, content, append=False): if not content: content = {} data = Log.beautify_json(content) if type(content) is not str else content if not os.path.exists(thepath): os.makedirs(thepath) try: filepath = "%s/%s" % (thepath, thefile) file_open_mode = "a" if append else "w" with codecs.open(filepath, file_open_mode, "utf-8-sig") as f: f.write(unicode(data, "utf-8")) f.write("\r\n") except UnicodeDecodeError as e: print "UnicodeDecodeError: %s" % e except IOError as e: print "IOError: %s" % e pass