From bf975393e2374d2ef1512331510570679e9071ca Mon Sep 17 00:00:00 2001 From: Jan Maximilian Michal <j.michal@stud.uni-goettingen.de> Date: Thu, 3 Nov 2016 11:37:34 +0000 Subject: [PATCH] Scripts are tested for required fields --- grim.py | 20 +++++++++++++++++--- hallgrim/IliasXMLCreator/gap.py | 15 +++++++++++++++ hallgrim/messages.py | 11 ++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 hallgrim/IliasXMLCreator/gap.py diff --git a/grim.py b/grim.py index 910056a..941ca09 100755 --- a/grim.py +++ b/grim.py @@ -21,20 +21,31 @@ def type_selector(type): if 'single' in type: return 'SINGLE CHOICE QUESTION' + def file_exists(path): if not os.path.exists(path): msg = 'The script "{}" does not exist.'.format(path) raise argparse.ArgumentTypeError(msg) return path +def script_is_valid(script): + required = ['meta', 'task', 'choices', 'feedback'] + for field in required: + if not hasattr(script, field): + error("script does not export '{}' field.".format(field)) + if any(not hasattr(script, field) for field in required): + abort("Script is invalid (see above)") + def parseme(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest="command") - parser_new = subparsers.add_parser("new", help="The utility the generate new scripts.") + parser_new = subparsers.add_parser( + "new", help="The utility the generate new scripts.") parser_new.add_argument( "name", - help="The name of the new script" + help="The name of the new script", + metavar='NAME' ) parser_new.add_argument( "-t", @@ -58,7 +69,8 @@ def parseme(): metavar='POINTS', ) - parser_gen = subparsers.add_parser("gen", help="Subcommand to convert from script to xml.") + parser_gen = subparsers.add_parser( + "gen", help="Subcommand to convert from script to xml.") parser_gen.add_argument( '-o', '--out', @@ -90,6 +102,7 @@ def parseme(): def handle_choice_questions(output, script_name, instances): script = importlib.import_module(file_to_module(script_name)) + script_is_valid(script) data = { 'type': type_selector(script.meta['type']), 'description': "_description", @@ -107,6 +120,7 @@ def handle_choice_questions(output, script_name, instances): packer.convert_and_print(data, output, instances) info('Processed "{}" and wrote xml to "{}".'.format(script_name, output)) + def handle_new_script(name, type, author, points): raise NotImplementedError() diff --git a/hallgrim/IliasXMLCreator/gap.py b/hallgrim/IliasXMLCreator/gap.py new file mode 100644 index 0000000..2c8e83e --- /dev/null +++ b/hallgrim/IliasXMLCreator/gap.py @@ -0,0 +1,15 @@ +import xml.etree.ElementTree as et + +from hallgrim.IliasXMLCreator.xmlBuildingBlocks import * + +class GapQuestion: + """docstring for GapQuestion""" + def __init__(self, type, description, question_text, author, title, questions, feedback, gapLength): + self.type = type + self.description = description + self.question_text = question_text + self.author = author + self.title = title + self.questions = questions + self.feedback = feedback + self.gapLength = gapLength diff --git a/hallgrim/messages.py b/hallgrim/messages.py index 9f66c17..e698bfd 100644 --- a/hallgrim/messages.py +++ b/hallgrim/messages.py @@ -1,3 +1,5 @@ +import sys + def warn(msg): print('[WARN]', msg) @@ -5,4 +7,11 @@ def debug(msg): print('[DEBUG]', msg) def info(msg): - print('[INFO]', msg) \ No newline at end of file + print('[INFO]', msg) + +def error(msg): + print('[ERROR]', msg) + +def abort(msg): + print('[FATAL]', msg) + sys.exit('exiting...') -- GitLab