From 1330ff9c1a8891913b5ef58cdfe64f8fd3751c75 Mon Sep 17 00:00:00 2001 From: jwansek Date: Tue, 26 Apr 2022 18:08:15 +0100 Subject: Changed args ready for docker, started on Dockerfile --- Smarker/mark.py | 105 ------------------------------------------- Smarker/misc_classes.py | 14 ++++++ Smarker/smarker.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 105 deletions(-) delete mode 100644 Smarker/mark.py create mode 100644 Smarker/smarker.py (limited to 'Smarker') diff --git a/Smarker/mark.py b/Smarker/mark.py deleted file mode 100644 index e8070f8..0000000 --- a/Smarker/mark.py +++ /dev/null @@ -1,105 +0,0 @@ -from dataclasses import dataclass -import jinja_helpers -import configparser -import misc_classes -import subprocess -import argparse -import tempfile -import zipfile -import reflect -import jinja2 -import shutil -import yaml -import json -import os - -def main(**kwargs): - student_no = os.path.splitext(os.path.split(args["submission"])[-1])[0] - - with misc_classes.ExtractZipToTempDir(args["submission"]) as submission_files: - with open(kwargs["assessment"], "r") as f: - assessment_struct = yaml.safe_load(f) - - with misc_classes.FileDependencies(assessment_struct): - output = reflect.gen_reflection_report(submission_files, assessment_struct, student_no, kwargs, args["submission"]) - output_file = kwargs["out"] - - if kwargs["format"] == "yaml": - strout = yaml.dump(output) - elif kwargs["format"] == "json": - strout = json.dumps(output, indent = 4) - else: - fp = os.path.join(os.path.split(__file__)[0], "templates", "%s.jinja2" % kwargs["format"]) - if kwargs["format"] in ("tex", "pdf"): - jinja_template = misc_classes.latex_jinja_env.get_template("tex.jinja2") - else: - with open(fp, "r") as f: - jinja_template = jinja2.Template(f.read()) - - strout = jinja_template.render(**output, **jinja_helpers._get_helpers(), **kwargs) - - if output_file == "stdout": - print(strout) - # input("\n\n[tempdir: %s] Press any key to close..." % tempdir) - exit() - - if output_file == "auto": - output_file = "%s_report.%s" % (student_no, kwargs["format"]) - - with open(output_file, "w") as f: - f.write(strout) - - if kwargs["format"] == "pdf": - os.environ["TEXINPUTS"] = os.path.join(os.path.split(__file__)[0], "python-latex-highlighting") + ":" - - os.rename(output_file, os.path.splitext(output_file)[0] + ".tex") - output_file = os.path.splitext(output_file)[0] + ".tex" - subprocess.run(["pdflatex", output_file]) - - os.remove(os.path.splitext(output_file)[0] + ".tex") - os.remove(os.path.splitext(output_file)[0] + ".log") - os.remove(os.path.splitext(output_file)[0] + ".aux") - - # input("\n\n[tempdir: %s] Press any key to close..." % tempdir) - -if __name__ == "__main__": - config = configparser.ConfigParser() - config.read(os.path.join(os.path.split(__file__)[0], "smarker.conf")) - - parser = argparse.ArgumentParser() - parser.add_argument( - "-a", "--assessment", - help = "Path to an assessment .yml file", - type = os.path.abspath, - required = True - ) - parser.add_argument( - "-s", "--submission", - help = "Path to a zip of a student's code", - type = os.path.abspath, - required = True - ) - parser.add_argument( - "-f", "--format", - help = "Output format type", - type = str, - choices = ["yaml", "json", "pdf"] + [os.path.splitext(f)[0] for f in os.listdir(os.path.join(os.path.split(__file__)[0], "templates"))], - default = "txt" - ) - parser.add_argument( - "-o", "--out", - help = "Path to write the output to, or, by default write to stdout. 'auto' automatically generates a file name.", - default = "stdout", - ) - - for section in config.sections(): - for option in config.options(section): - parser.add_argument( - "--%s_%s" % (section, option), - default = config.get(section, option), - help = "Optional argument inherited from config file. Read smarker.conf for details." - ) - - args = vars(parser.parse_args()) - main(**args) - \ No newline at end of file diff --git a/Smarker/misc_classes.py b/Smarker/misc_classes.py index 09c3a7d..17ad36a 100644 --- a/Smarker/misc_classes.py +++ b/Smarker/misc_classes.py @@ -1,4 +1,5 @@ from dataclasses import dataclass +import argparse import tempfile import zipfile import shutil @@ -19,6 +20,19 @@ latex_jinja_env = jinja2.Environment( loader = jinja2.FileSystemLoader(os.path.abspath(os.path.join(os.path.split(__file__)[0], "templates"))) ) +class EnvDefault(argparse.Action): + def __init__(self, envvar, required=True, default=None, **kwargs): + if not default and envvar: + if envvar in os.environ: + default = os.environ[envvar] + if required and default: + required = False + super(EnvDefault, self).__init__(default=default, required=required, + **kwargs) + + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values) + @dataclass class ExtractZipToTempDir(tempfile.TemporaryDirectory): zip_file:str diff --git a/Smarker/smarker.py b/Smarker/smarker.py new file mode 100644 index 0000000..106d426 --- /dev/null +++ b/Smarker/smarker.py @@ -0,0 +1,115 @@ +from dataclasses import dataclass +import jinja_helpers +import configparser +import misc_classes +import subprocess +import argparse +import tempfile +import zipfile +import reflect +import jinja2 +import shutil +import yaml +import json +import os + +def main(**kwargs): + student_no = os.path.splitext(os.path.split(args["submission"])[-1])[0] + + with misc_classes.ExtractZipToTempDir(args["submission"]) as submission_files: + with open(kwargs["assessment"], "r") as f: + assessment_struct = yaml.safe_load(f) + + with misc_classes.FileDependencies(assessment_struct): + output = reflect.gen_reflection_report(submission_files, assessment_struct, student_no, kwargs, args["submission"]) + output_file = kwargs["out"] + + if kwargs["format"] == "yaml": + strout = yaml.dump(output) + elif kwargs["format"] == "json": + strout = json.dumps(output, indent = 4) + else: + fp = os.path.join(os.path.split(__file__)[0], "templates", "%s.jinja2" % kwargs["format"]) + if kwargs["format"] in ("tex", "pdf"): + jinja_template = misc_classes.latex_jinja_env.get_template("tex.jinja2") + else: + with open(fp, "r") as f: + jinja_template = jinja2.Template(f.read()) + + strout = jinja_template.render(**output, **jinja_helpers._get_helpers(), **kwargs) + + if output_file == "stdout": + print(strout) + # input("\n\n[tempdir: %s] Press any key to close..." % tempdir) + exit() + + if output_file == "auto": + output_file = "%s_report.%s" % (student_no, kwargs["format"]) + + with open(output_file, "w") as f: + f.write(strout) + + if kwargs["format"] == "pdf": + os.environ["TEXINPUTS"] = os.path.join(os.path.split(__file__)[0], "python-latex-highlighting") + ":" + + os.rename(output_file, os.path.splitext(output_file)[0] + ".tex") + output_file = os.path.splitext(output_file)[0] + ".tex" + subprocess.run(["pdflatex", output_file]) + + os.remove(os.path.splitext(output_file)[0] + ".tex") + os.remove(os.path.splitext(output_file)[0] + ".log") + os.remove(os.path.splitext(output_file)[0] + ".aux") + + # input("\n\n[tempdir: %s] Press any key to close..." % tempdir) + +if __name__ == "__main__": + config = configparser.ConfigParser() + config.read(os.path.join(os.path.split(__file__)[0], "smarker.conf")) + + parser = argparse.ArgumentParser() + parser.add_argument( + "-a", "--assessment", + action = misc_classes.EnvDefault, + envvar = "assessment", + help = "Path to an assessment .yml file", + type = os.path.abspath, + required = True + ) + parser.add_argument( + "-s", "--submission", + action = misc_classes.EnvDefault, + envvar = "submission", + help = "Path to a zip of a student's code", + type = os.path.abspath, + required = True + ) + parser.add_argument( + "-f", "--format", + action = misc_classes.EnvDefault, + envvar = "format", + help = "Output format type", + type = str, + choices = ["yaml", "json", "pdf"] + [os.path.splitext(f)[0] for f in os.listdir(os.path.join(os.path.split(__file__)[0], "templates"))], + default = "txt" + ) + parser.add_argument( + "-o", "--out", + action = misc_classes.EnvDefault, + envvar = "out", + help = "Path to write the output to, or, by default write to stdout. 'auto' automatically generates a file name.", + default = "stdout", + ) + + for section in config.sections(): + for option in config.options(section): + parser.add_argument( + "--%s_%s" % (section, option), + action = misc_classes.EnvDefault, + envvar = "--%s_%s" % (section, option), + default = config.get(section, option), + help = "Optional argument inherited from config file. Read smarker.conf for details." + ) + + args = vars(parser.parse_args()) + main(**args) + \ No newline at end of file -- cgit v1.2.3