diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2022-03-03 18:13:24 +0000 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2022-03-03 18:13:24 +0000 |
commit | 92f710554dac6fb75e0c3b4bcf4a0305ee4df4c3 (patch) | |
tree | caf1f528de60c96dfa4f346e93b314bb8afa76a3 /jinja_helpers.py | |
parent | 9d1668c5e4820213406d18278116aad98fe83331 (diff) | |
download | Smarker-92f710554dac6fb75e0c3b4bcf4a0305ee4df4c3.tar.gz Smarker-92f710554dac6fb75e0c3b4bcf4a0305ee4df4c3.zip |
started working on rendering junitxml's nicely to pdfs
Diffstat (limited to 'jinja_helpers.py')
-rw-r--r-- | jinja_helpers.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/jinja_helpers.py b/jinja_helpers.py index e72db35..ca0748d 100644 --- a/jinja_helpers.py +++ b/jinja_helpers.py @@ -1,7 +1,14 @@ """Functions in this module will be avaliable to call in jinja templates""" +import subprocess +import lxml.html import datetime +import tempfile +import shutil +import pdfkit import yaml +import json import re +import os def get_datetime(): return str(datetime.datetime.now()) @@ -9,6 +16,38 @@ def get_datetime(): def recurse_class_tree_text(tree, indent = 4): return yaml.dump(tree, indent = indent).replace(": {}", "") +def recurse_class_tree_forest(tree): + return re.sub(r"\"|:|\{\}|,", "", json.dumps(tree, indent=4)).replace("{", "[").replace("}", "]") + +def junit_xml_to_html(junit_xml, student_id): + # setup tempfiles for the junit xml and html + with tempfile.NamedTemporaryFile(suffix = ".xml", mode = "w", delete = False) as xml_f: + xml_f.write(junit_xml) + html_path = os.path.join(tempfile.mkdtemp(), "junit.html") + + # convert the junit xml to html + subprocess.run(["junit2html", xml_f.name, html_path]) + + # remove the html elements we don't like + root = lxml.html.parse(html_path) + for toremove in root.xpath("/html/body/h1"): + toremove.getparent().remove(toremove) + for toremove in root.xpath("/html/body/table"): + toremove.getparent().remove(toremove) + for toremove in root.xpath("/html/body/p"): + toremove.getparent().remove(toremove) + + # convert the html to pdf + out_fname = "%s_test_report.pdf" % student_id + pdfkit.from_string(lxml.etree.tostring(root).decode(), out_fname) + + # remove the tempfiles + input("%s continue..." % html_path) + shutil.rmtree(os.path.split(html_path)[0]) + os.remove(xml_f.name) + + return out_fname + def flatten_struct(struct): # print("Attempting to flatten: ", struct) out = {} |