| commit | author | age | ||
| 78d798 | 1 | from string import Template |
| SP | 2 | import os |
| 3 | from . import Exceptions | |
| 4 | ||
| 5 | ||
| 6 | class TemplateEngine: | |
| 7 | def __init__(self, path=None): | |
| 8 | if path is None: | |
| cc53f3 | 9 | raise (Exceptions.TemplatePathError("No path to templates")) |
| 78d798 | 10 | |
| SP | 11 | self.template_filenames = { |
| 12 | "head": os.path.join(path, "headtemplate.tpl"), | |
| a21943 | 13 | "paper": os.path.join(path, "newpaperwithNAMEtemplate.tpl"), |
| 78d798 | 14 | "paper_sid": os.path.join(path, "newpaperwithSIDtemplate.tpl"), |
| f0812a | 15 | "problem_intro_image": os.path.join(path, "problemintrowithimage.tpl"), |
| cc53f3 | 16 | "problem_intro": os.path.join(path, "problemintrotemplate.tpl"), |
| SP | 17 | "subproblem": os.path.join(path, "subproblemtemplate.tpl"), |
| 18 | "subproblem_end": os.path.join(path, "subproblemtemplateend.tpl"), | |
| 0d81f7 | 19 | "answers": os.path.join(path,"answerstemplate.tpl") |
| 78d798 | 20 | } |
| SP | 21 | |
| 22 | self.template = {} | |
| 23 | for key, value in self.template_filenames.items(): | |
| 24 | self.template[key] = self.loadtemplate(value) | |
| 25 | ||
| 26 | def loadtemplate(self, filename): | |
| 27 | with open(filename) as fd: | |
| 28 | template = fd.read() | |
| 29 | return template | |
| 30 | ||
| 5aa5bb | 31 | def put_problem_into_template(self, problem_dict, problem_number=0): |
| 0d81f7 | 32 | retstr = Template(self.template["problem_intro"]).substitute( |
| SP | 33 | {"problem_number": problem_number, "text": problem_dict["introduction"]} |
| 34 | ) | |
| cc53f3 | 35 | if len(problem_dict["solutions"]) > 1: |
| SP | 36 | for sp, sol in zip(problem_dict["subproblems"], problem_dict["solutions"]): |
| 37 | retstr += Template(self.template["subproblem"]).substitute( | |
| 38 | { | |
| 39 | "text": sp, | |
| ce4fc9 | 40 | "ans1": sol["shuffled"][0][0].format_as_tex( |
| SP | 41 | glyph=sol["glyph"], unit=sol["unit"] |
| 42 | ), | |
| 43 | "ans2": sol["shuffled"][1][0].format_as_tex( | |
| 44 | glyph=sol["glyph"], unit=sol["unit"] | |
| 45 | ), | |
| 46 | "ans3": sol["shuffled"][2][0].format_as_tex( | |
| 47 | glyph=sol["glyph"], unit=sol["unit"] | |
| 48 | ), | |
| 49 | "ans4": sol["shuffled"][3][0].format_as_tex( | |
| 50 | glyph=sol["glyph"], unit=sol["unit"] | |
| 51 | ), | |
| cc53f3 | 52 | } |
| SP | 53 | ) |
| 54 | retstr += Template(self.template["subproblem_end"]).substitute() | |
| 55 | else: | |
| 0d81f7 | 56 | retstr += Template(self.template["answers"]).substitute( |
| cc53f3 | 57 | { |
| ce4fc9 | 58 | "ans1": problem_dict["solutions"][0]["shuffled"][0][ |
| SP | 59 | 0 |
| 60 | ].format_as_tex( | |
| 61 | glyph=problem_dict["solutions"][0]["glyph"], | |
| 62 | unit=problem_dict["solutions"][0]["unit"], | |
| 63 | ), | |
| 64 | "ans2": problem_dict["solutions"][0]["shuffled"][1][ | |
| 65 | 0 | |
| 66 | ].format_as_tex( | |
| 67 | glyph=problem_dict["solutions"][0]["glyph"], | |
| 68 | unit=problem_dict["solutions"][0]["unit"], | |
| 69 | ), | |
| 70 | "ans3": problem_dict["solutions"][0]["shuffled"][2][ | |
| 71 | 0 | |
| 72 | ].format_as_tex( | |
| 73 | glyph=problem_dict["solutions"][0]["glyph"], | |
| 74 | unit=problem_dict["solutions"][0]["unit"], | |
| 75 | ), | |
| 76 | "ans4": problem_dict["solutions"][0]["shuffled"][3][ | |
| 77 | 0 | |
| 78 | ].format_as_tex( | |
| 79 | glyph=problem_dict["solutions"][0]["glyph"], | |
| 80 | unit=problem_dict["solutions"][0]["unit"], | |
| 81 | ), | |
| cc53f3 | 82 | } |
| SP | 83 | ) |
| 78d798 | 84 | return retstr |
| a21943 | 85 | |
| 5aa5bb | 86 | def head(self, settings): |
| SP | 87 | return Template(self.template["head"]).substitute(settings) |
| a21943 | 88 | |
| SP | 89 | def tail(self): |
| ccc0bd | 90 | return """\end{document}""" |
| a21943 | 91 | |
| SP | 92 | def start_paper(self, student_id=None, student_name=None): |
| 93 | if student_id is not None: | |
| 7224af | 94 | return Template(self.template["paper"]).substitute( |
| SP | 95 | {"student_id": student_id, "student_name": student_name} |
| 96 | ) | |
| a21943 | 97 | else: |
| SP | 98 | return Template(self.template["paper_sid"]).substitute() |