lf_report.py: many html corrections, adds javascript functions

- There was significant copy-paste garbage from method to method that had useless [html] tags in it.
- Footer is actually a legal html element, it doesn't need to be a div
- removed suprious and inconsistent spaces
- refactored [head] elements
- adds notes about how these methods could better be named
- copy_js method can be provided to allow clicking on elements to copy text to clipboard
- reduces heavy indentation, report html looks better in view-source

Signed-off-by: Jed Reynolds <jed@candelatech.com>
This commit is contained in:
Jed Reynolds
2021-08-26 14:28:21 -07:00
parent ab4b7e0f5c
commit 0da4aabf7d

View File

@@ -119,6 +119,7 @@ class lf_report():
def copy_css(self):
reportcss_src_file = str(self.current_path)+'/'+str(self.banner_directory)+'/report.css'
# print("copy_css: source file is: "+reportcss_src_file)
reportcss_dest_file = str(self.path_date_time)+'/report.css'
customcss_src_file = str(self.current_path)+'/'+str(self.banner_directory)+'/'+str(self.custom_css)
@@ -309,7 +310,7 @@ class lf_report():
pdfkit.from_file(self.write_output_html, self.write_output_pdf, options=options)
def get_pdf_path(self):
pdf_link_path = "{}/{}-{}".format(self.path_date_time,self.date,self.output_pdf)
pdf_link_path = "{}/{}-{}".format(self.path_date_time, self.date, self.output_pdf)
return pdf_link_path
def build_pdf_link(self,_pdf_link_name,_pdf_link_path):
@@ -317,13 +318,13 @@ class lf_report():
<!-- pdf link -->
<a href="{pdf_link_path}" target="_blank">{pdf_link_name}</a>
<br>
""".format(pdf_link_path=_pdf_link_path,pdf_link_name=_pdf_link_name)
""".format(pdf_link_path=_pdf_link_path,pdf_link_name=_pdf_link_name)
self.html += self.pdf_link_html
def build_link(self,_link_name,_link_path):
self.link = """
<!-- link -->
<a href="{link_path}" target="_blank">{link_name} </a>
<a href="{link_path}" target="_blank">{link_name}</a>
<br>
""".format(link_path=_link_path,link_name=_link_name)
self.html += self.link
@@ -339,33 +340,39 @@ class lf_report():
self.build_table()
self.end_content_div()
def get_html_head(self, title='Untitled'):
return """<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<style>
body {{ margin: 0; padding: 0; }}
</style>
<link rel='stylesheet' href='report.css' />
<link rel='stylesheet' href='custom.css' />
<title>{title}</title>
</head>""".format(title=title)
def build_banner(self):
# NOTE: {{ }} are the ESCAPED curly braces
# JBR removed deep indentation of html tag because it makes browser view-source is hard to debug
# JBR suggests rename method to start_html_doc()
self.banner_html = """<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<style>
body {{ margin: 0; padding: 0; }}
</style>
<link rel='stylesheet' href='report.css' />
<link rel='stylesheet' href='custom.css' />
<title>{title}</title>
</head>
<body>
<div id='BannerBack'>
<div id='Banner'>
<br/>
<img id='BannerLogo' align='right' src="CandelaLogo2-90dpi-200x90-trans.png" border='0'/>
<div class='HeaderStyle'>
<br>
<h1 class='TitleFontPrint' style='color:darkgreen;'> {title} </h1>
<h3 class='TitleFontPrint' style='color:darkgreen;'>{date}</h3>
</div>
</div>
</div>
<html lang='en'>
{head_tag}
<body>
<div id='BannerBack'>
<div id='Banner'>
<br/>
<img id='BannerLogo' align='right' src="CandelaLogo2-90dpi-200x90-trans.png" border='0'/>
<div class='HeaderStyle'>
<br>
<h1 class='TitleFontPrint' style='color:darkgreen;'>{title}</h1>
<h3 class='TitleFontPrint' style='color:darkgreen;'>{date}</h3>
</div>
</div>
</div>
""".format(
head_tag=self.get_html_head(title=self.title),
title=self.title,
date=self.date,
)
@@ -373,31 +380,26 @@ class lf_report():
def build_banner_left(self):
# NOTE: {{ }} are the ESCAPED curly braces
# JBR suggests rename method to start_html_doc()
# This method violates DRY, if the ID of the body/div#BannerBack/div element is actually necessary
# to specify, this needs to be made a parameter for build_banner() or start_html_doc()
self.banner_html = """<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<style>
body {{ margin: 0; padding: 0; }}
</style>
<link rel='stylesheet' href='report.css' />
<link rel='stylesheet' href='custom.css' />
<title>{title}</title>
</head>
<body>
<div id='BannerBack'>
<div id='BannerLeft'>
<br/>
<img id='BannerLogo' align='right' src="CandelaLogo2-90dpi-200x90-trans.png" border='0'/>
<div class='HeaderStyle'>
<br>
<h1 class='TitleFontPrint' style='color:darkgreen;'> {title} </h1>
<h3 class='TitleFontPrint' style='color:darkgreen;'>{date}</h3>
</div>
</div>
</div>
<html lang='en'>
{head_tag}
<body>
<div id='BannerBack'>
<div id='BannerLeft'>
<br/>
<img id='BannerLogo' align='right' src="CandelaLogo2-90dpi-200x90-trans.png" border='0'/>
<div class='HeaderStyle'>
<br>
<h1 class='TitleFontPrint' style='color:darkgreen;'>{title}</h1>
<h3 class='TitleFontPrint' style='color:darkgreen;'>{date}</h3>
</div>
</div>
</div>
""".format(
head_tag=self.get_html_head(title=self.title),
title=self.title,
date=self.date,
)
@@ -469,51 +471,63 @@ class lf_report():
self.html += setup_information
def build_footer(self):
self.footer_html = """<!DOCTYPE html>
<html lang='en'>
<footer>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<style>
body {{ margin: 0; padding: 0; }}
</style>
<link rel='stylesheet' href='report.css' />
<link rel='stylesheet' href='custom.css' />
</footer>
<body>
<div class='FooterStyle'>
<a href="https://www.candelatech.com/">
<img id='BannerLogoFooter' align='right' src="candela_swirl_small-72h.png" border='0'/>
<p>Generate by Candela Technologies LANforge network testing tool</p>
<p><a href="https://www.candelatech.com">www.candelatech.com</a><p>
</div>
</body>
"""
self.footer_html = """
<footer class='FooterStyle'>
<a href="https://www.candelatech.com/"><img
id='BannerLogoFooter' align='right' src="candela_swirl_small-72h.png" border='0'/></a>
<p>Generated by Candela Technologies LANforge network testing tool</p>
<p><a href="https://www.candelatech.com">www.candelatech.com</a><p>
</footer>
"""
self.html += self.footer_html
def build_footer_no_png(self):
self.footer_html = """<!DOCTYPE html>
<html lang='en'>
<footer>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1' />
<style>
body {{ margin: 0; padding: 0; }}
</style>
<link rel='stylesheet' href='report.css' />
<link rel='stylesheet' href='custom.css' />
</footer>
<body>
<div class='FooterStyle'>
<p>Generate by Candela Technologies LANforge network testing tool</p>
<p><a href="https://www.candelatech.com">www.candelatech.com</a><p>
</div>
</body>
"""
self.footer_html = """
<footer class='FooterStyle'>
<p>Generate by Candela Technologies LANforge network testing tool</p>
<p><a href="https://www.candelatech.com">www.candelatech.com</a><p>
</footer>"""
self.html += self.footer_html
def copy_js(self):
self.html += """
<script>
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(ele) {
var text = ele.innerHTML || '';
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function() {
console.log('Async: Copying to clipboard was successful!');
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
}
</script>
"""
def build_custom(self):
self.html += self.custom_html