from string import Template
|
import os
|
from . import Exceptions
|
|
|
class TemplateEngine:
|
def __init__(self, path=None):
|
if path is None:
|
raise (Exceptions.TemplatePathError("No path to templates"))
|
|
self.template_filenames = {
|
"head": os.path.join(path, "headtemplate.tpl"),
|
"paper": os.path.join(path, "newpaperwithNAMEtemplate.tpl"),
|
"paper_sid": os.path.join(path, "newpaperwithSIDtemplate.tpl"),
|
"problem_intro_image": os.path.join(path, "problemintrowithimage.tpl"),
|
"problem_intro": os.path.join(path, "problemintrotemplate.tpl"),
|
"subproblem": os.path.join(path, "subproblemtemplate.tpl"),
|
"subproblem_end": os.path.join(path, "subproblemtemplateend.tpl"),
|
"answers": os.path.join(path,"answerstemplate.tpl")
|
}
|
|
self.template = {}
|
for key, value in self.template_filenames.items():
|
self.template[key] = self.loadtemplate(value)
|
|
def loadtemplate(self, filename):
|
with open(filename) as fd:
|
template = fd.read()
|
return template
|
|
def put_problem_into_template(self, problem_dict, problem_number=0):
|
retstr = Template(self.template["problem_intro"]).substitute(
|
{"problem_number": problem_number, "text": problem_dict["introduction"]}
|
)
|
if len(problem_dict["solutions"]) > 1:
|
for sp, sol in zip(problem_dict["subproblems"], problem_dict["solutions"]):
|
retstr += Template(self.template["subproblem"]).substitute(
|
{
|
"text": sp,
|
"ans1": sol["shuffled"][0][0].format_as_tex(
|
glyph=sol["glyph"], unit=sol["unit"]
|
),
|
"ans2": sol["shuffled"][1][0].format_as_tex(
|
glyph=sol["glyph"], unit=sol["unit"]
|
),
|
"ans3": sol["shuffled"][2][0].format_as_tex(
|
glyph=sol["glyph"], unit=sol["unit"]
|
),
|
"ans4": sol["shuffled"][3][0].format_as_tex(
|
glyph=sol["glyph"], unit=sol["unit"]
|
),
|
}
|
)
|
retstr += Template(self.template["subproblem_end"]).substitute()
|
else:
|
retstr += Template(self.template["answers"]).substitute(
|
{
|
"ans1": problem_dict["solutions"][0]["shuffled"][0][
|
0
|
].format_as_tex(
|
glyph=problem_dict["solutions"][0]["glyph"],
|
unit=problem_dict["solutions"][0]["unit"],
|
),
|
"ans2": problem_dict["solutions"][0]["shuffled"][1][
|
0
|
].format_as_tex(
|
glyph=problem_dict["solutions"][0]["glyph"],
|
unit=problem_dict["solutions"][0]["unit"],
|
),
|
"ans3": problem_dict["solutions"][0]["shuffled"][2][
|
0
|
].format_as_tex(
|
glyph=problem_dict["solutions"][0]["glyph"],
|
unit=problem_dict["solutions"][0]["unit"],
|
),
|
"ans4": problem_dict["solutions"][0]["shuffled"][3][
|
0
|
].format_as_tex(
|
glyph=problem_dict["solutions"][0]["glyph"],
|
unit=problem_dict["solutions"][0]["unit"],
|
),
|
}
|
)
|
return retstr
|
|
def head(self, settings):
|
return Template(self.template["head"]).substitute(settings)
|
|
def tail(self):
|
return """\end{document}"""
|
|
def start_paper(self, student_id=None, student_name=None):
|
if student_id is not None:
|
return Template(self.template["paper"]).substitute(
|
{"student_id": student_id, "student_name": student_name}
|
)
|
else:
|
return Template(self.template["paper_sid"]).substitute()
|