#!/usr/bin/env python3 import sys import os import time import datetime import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np import pandas as pd import pdfkit import math # internal candela references included during intial phases, to be deleted at future date # https://candelatech.atlassian.net/wiki/spaces/LANFORGE/pages/372703360/Scripting+Data+Collection+March+2021 # base report class class lf_report(): def __init__(self, _report_path = 'lanforge', _output_format = 'html', # pass in on the write functionality _dataframe="", _title="LANForge Test Run Heading", _table_title="LANForge Table Heading", _graph_title="LANForge Graph Title", _obj = "", _obj_title = "", _date="1/1/2-21 00:00(UTC)", _output_html="outfile.html", _output_pdf="outfile.pdf"): self.dataframe=_dataframe self.title=_title self.table_title=_table_title self.graph_title=_graph_title self.date=_date self.output_html=_output_html self.output_pdf=_output_pdf self.banner_html = "" self.graph_titles="" self.graph_image="" self.html = "" self.custom_html = "" self.objective = _obj self.obj_title = _obj_title self.systeminfopath = "" self.report_path = "" if _report_path == 'lanforge': new_file_path = str(datetime.datetime.now().strftime("%Y-%m-%d-%H-h-%m-m-%S-s")).replace(':','-') try: path = os.path.join('/home/lanforge/report-data/', new_file_path) os.mkdir(path) except: curr_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(curr_dir_path, new_file_path) os.mkdir(path) self.report_path = str(path) elif _report_path == 'local': curr_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(curr_dir_path, new_file_path) self.report_path = str(path) else: # for now just put local curr_dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) path = os.path.join(curr_dir_path, new_file_path) os.mkdir(path) self.report_path = str(path) # move the banners and candela images to report path def set_title(self,_title): self.title = _title def set_table_title(self,_table_title): self.table_title = _table_title def set_graph_title(self,_graph_title): self.graph_title = _graph_title def set_date(self,_date): self.date = _date def set_dataframe(self,_dataframe): self.dataframe = _dataframe def set_custom_html(self,_custom_html): self.custom_html = _custom_html def set_obj_html(self,_obj_title, _obj ): self.objective = _obj self.obj_title = _obj_title def set_graph_image(self,_graph_image): self.graph_image = _graph_image def get_path(self): return self.report_path def write_html(self): self.output_html = str(self.report_path)+'/'+ self.output_html test_file = open(self.output_html, "w") test_file.write(self.html) test_file.close() return self.html def write_pdf(self): # write to pdf # write logic to generate pdf here # wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb # sudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.deb options = {"enable-local-file-access" : None} # prevent eerror Blocked access to file self.output_pdf = str(self.report_path)+'/'+ self.output_pdf pdfkit.from_file(self.output_html, self.output_pdf, options=options) def generate_report(self): self.write_html() self.write_pdf() # only use is pass all data in constructor, no graph output def build_all(self): self.build_banner() self.build_table_title() self.build_table() def build_banner(self): self.banner_html = """