From eb5b1d2889aa5e408309f5d89989768ff19bc7f4 Mon Sep 17 00:00:00 2001 From: jwansek Date: Mon, 17 Jan 2022 21:25:29 +0000 Subject: proved argparsing --- mark.py | 77 +++++++++++++++++++++++++++++++++++++++--------------------- reflect.py | 28 +++++++++++++++------- smarker.conf | 7 ++++-- 3 files changed, 75 insertions(+), 37 deletions(-) diff --git a/mark.py b/mark.py index 93909a7..c9f0dbd 100644 --- a/mark.py +++ b/mark.py @@ -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 diff --git a/reflect.py b/reflect.py index 90fa5b6..0ce886f 100644 --- a/reflect.py +++ b/reflect.py @@ -76,7 +76,8 @@ class Reflect: i[0]: ( i[1], {"comments": inspect.getcomments(i[1]), "doc": inspect.getdoc(i[1])}, - str(inspect.signature(i[1])) + str(inspect.signature(i[1])), + inspect.getsource(i[1]) ) for i in inspect.getmembers( self.get_classes(module_name)[class_name][0], @@ -89,7 +90,8 @@ class Reflect: i[0]: ( i[1], {"comments": inspect.getcomments(i[1]), "doc": inspect.getdoc(i[1])}, - str(inspect.signature(i[1])) + str(inspect.signature(i[1])), + inspect.getsource(i[1]) ) for i in inspect.getmembers(self.imported_modules[module_name]) if inspect.isfunction(i[1]) @@ -141,6 +143,10 @@ class Reflect: # return inspect.getclasstree(classes) return tree + def get_source_code(self, file_, line_start, line_end): + with open(file=file_, mode="r") as f: + return f.readlines()[line_start:line_end] + def gen_reflection_report(client_code_path, assessment_struct): reflection = Reflect(client_code_path) present_module_names = [i.name for i in reflection.client_modules] @@ -195,8 +201,10 @@ def gen_reflection_report(client_code_path, assessment_struct): out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["present"] = False continue - out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["arguments"] = present_methods[method_name][-1] - out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["documentation"] = present_methods[method_name][-2] + out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["arguments"] = present_methods[method_name][-2] + out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["minimum_arguments"] = present_methods[method_name][-2].count(",") + 1 + out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["documentation"] = present_methods[method_name][-3] + out["files"][i][required_file]["classes"][j][class_name]["methods"][k][required_method]["source_code"] = present_methods[method_name][-1] if "functions" in required_files_features.keys(): present_functions = reflection.get_functions(module_name) @@ -210,8 +218,10 @@ def gen_reflection_report(client_code_path, assessment_struct): out["files"][i][required_file]["functions"][j][required_function]["present"] = False continue - out["files"][i][required_file]["functions"][j][required_function]["documentation"] = present_functions[function_name][-2] - out["files"][i][required_file]["functions"][j][required_function]["arguments"] = present_functions[function_name][-1] + out["files"][i][required_file]["functions"][j][required_function]["documentation"] = present_functions[function_name][-3] + out["files"][i][required_file]["functions"][j][required_function]["arguments"] = present_functions[function_name][-2] + out["files"][i][required_file]["functions"][j][required_function]["minimum_arguments"] = present_functions[function_name][-2].count(",") + 1 + out["files"][i][required_file]["functions"][j][required_function]["source_code"] = present_functions[function_name][-2] out["class_tree"] = reflection.get_class_tree() return out @@ -221,5 +231,7 @@ if __name__ == "__main__": reflect = Reflect(user_code_path) reflect.import_module("pjtool") - for c, v in reflect.get_classes(("pjtool")).items(): - print(c, v) + # for c, v in reflect.get_classes(("pjtool")).items(): + # print(c, v) + for k, v in reflect.get_functions("pjtool").items(): + print(k, v) \ No newline at end of file diff --git a/smarker.conf b/smarker.conf index bcb7922..598ffa5 100644 --- a/smarker.conf +++ b/smarker.conf @@ -1,7 +1,10 @@ [mysql] host = 192.168.1.92 +port = 3306 user = smarker passwd = smarkerPassword -[report] -show_full_docs = True \ No newline at end of file +[.md] +show_full_docs = True +show_source = True +show_numlines = True \ No newline at end of file -- cgit v1.2.3