diff options
Diffstat (limited to 'mark.py')
-rw-r--r-- | mark.py | 77 |
1 files changed, 50 insertions, 27 deletions
@@ -1,3 +1,4 @@ +import configparser import argparse import tempfile import zipfile @@ -6,19 +7,45 @@ import yaml import json import os -def main(assessment_path, submission_path, student_no, output_format): - # print(student_no) +def main(**kwargs): + student_no = os.path.splitext(os.path.split(args["submission"])[-1])[0] - with open(assessment_path, "r") as f: - assessment_struct = yaml.safe_load(f) + with tempfile.TemporaryDirectory() as tempdir: + with zipfile.ZipFile(args["submission"]) as z: + z.extractall(tempdir) + + # some zipping applications make a folder inside the zip with the files in that folder. + # try to deal with this here. + submission_files = tempdir + if os.path.isdir( + os.path.join(submission_files, os.listdir(submission_files)[0]) + ) and len(os.listdir(submission_files)) == 1: + submission_files = os.path.join(submission_files, os.listdir(submission_files)[0]) + + with open(kwargs["assessment"], "r") as f: + assessment_struct = yaml.safe_load(f) - output = reflect.gen_reflection_report(submission_path, assessment_struct) - if output_format == "yaml": - print(yaml.dump(output)) - elif output_format == "json": - print(json.dumps(output, indent = 4)) + output = reflect.gen_reflection_report(submission_files, assessment_struct) + output_file = kwargs["out"] + if kwargs["format"] == "yaml": + strout = yaml.dump(output) + elif kwargs["format"] == "json": + strout = json.dumps(output, indent = 4) + + if output_file == "stdout": + print(strout) + 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 __name__ == "__main__": + config = configparser.ConfigParser() + config.read("smarker.conf") + parser = argparse.ArgumentParser() parser.add_argument( "-a", "--assessment", @@ -39,24 +66,20 @@ if __name__ == "__main__": choices = ["yaml", "json"], required = True ) - args = vars(parser.parse_args()) + 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", + ) - with tempfile.TemporaryDirectory() as tempdir: - with zipfile.ZipFile(args["submission"]) as z: - z.extractall(tempdir) + 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." + ) - # some zipping applications make a folder inside the zip with the files in that folder. - # try to deal with this here. - submission_files = tempdir - if os.path.isdir( - os.path.join(submission_files, os.listdir(submission_files)[0]) - ) and len(os.listdir(submission_files)) == 1: - submission_files = os.path.join(submission_files, os.listdir(submission_files)[0]) - - main( - args["assessment"], - submission_files, - os.path.splitext(os.path.split(args["submission"])[-1])[0], - args["format"] - ) + args = vars(parser.parse_args()) + main(**args)
\ No newline at end of file |