summaryrefslogtreecommitdiffstats
path: root/jinja_helpers.py
blob: a9e0e2eb7a23ff9047e5d96c3bacde2adfca80b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""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())

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 = {}
    for s in struct:
        key = list(s.keys())[0]
        out[key] = s[key]
    return out

def get_required_num_args(funcname):
    return int(re.findall(r"(?<=\()(\d+)(?=\))", funcname)[0])

def bool_to_yesno(b:bool):
    if b:
        return "YES"
    else:
        return "NO"

def bool_to_checkbox(b:bool):
    if b:
        return "[x]"
    else:
        return "[ ]"

def len_documentation(comments, docs):
    """This function isn't in jinja"""
    if comments == "None":
        commentlen = 0
    else:
        commentlen = len(comments)

    if docs == "None":
        docslen = 0
    else:
        docslen = len(docs)

    return commentlen + docslen

def len_(obj):
    return len(obj)

def get_source_numlines(source):
    return "%d lines (%d characters)" % (source.count("\n"), len(source))

#https://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates
def tex_escape(text):
    conv = {
        '&': r'\&',
        '%': r'\%',
        '$': r'\$',
        '#': r'\#',
        '_': r'\_',
        '{': r'\{',
        '}': r'\}',
        '~': r'\textasciitilde{}',
        '^': r'\^{}',
        '\\': r'\textbackslash{}',
        '<': r'\textless{}',
        '>': r'\textgreater{}',
    }
    regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key = lambda item: - len(item))))
    # print(text, regex.sub(lambda match: conv[match.group()], text))
    return regex.sub(lambda match: conv[match.group()], text)

def _get_helpers():
    import reflect
    import os

    r = reflect.Reflect(os.getcwd())
    r.import_module("jinja_helpers")
    return {k: v[0] for k, v in r.get_functions("jinja_helpers").items()}

if __name__ == "__main__":
    # import json
    # with open("100301654_report.json", "r") as f:
    #     init_struct = json.load(f)["files"]

    # print(flatten_struct(flatten_struct(init_struct)["example.py"]["functions"]))

    print(get_required_num_args("aFunctionThatIsntThere(2)"))