From b15ddde2a73225f3834ff4c5b9c4614c2a1b11d7 Mon Sep 17 00:00:00 2001 From: Jan Maximilian Michal <j.michal@stud.uni-goettingen.de> Date: Wed, 23 Nov 2016 03:48:08 +0000 Subject: [PATCH] Can now create new scripts from command line. --- grim.py | 43 ++++++++++++++++++++++++++++++++++--------- scripts/__init__.py | 0 2 files changed, 34 insertions(+), 9 deletions(-) delete mode 100644 scripts/__init__.py diff --git a/grim.py b/grim.py index 325ab5d..246cf3c 100755 --- a/grim.py +++ b/grim.py @@ -30,6 +30,7 @@ def file_exists(path): raise argparse.ArgumentTypeError(msg) return path + def script_is_valid(script, required): for field in required: if not hasattr(script, field): @@ -37,6 +38,7 @@ def script_is_valid(script, required): 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") @@ -100,16 +102,18 @@ def parseme(): if args.command == None: parser.print_help() + def delegator(output, script_name, instances): script = importlib.import_module(file_to_module(script_name)) handler = { - 'gap' : handle_gap_questions, - 'single choice' : handle_choice_questions, - 'multiple choice' : handle_choice_questions + 'gap': handle_gap_questions, + 'single choice': handle_choice_questions, + 'multiple choice': handle_choice_questions }[script.meta['type']] handler(output, script, instances) + def handle_gap_questions(output, script, instances): script_is_valid(script, required=['meta', 'task', 'feedback']) data = { @@ -126,7 +130,9 @@ def handle_gap_questions(output, script, instances): output = os.path.join( 'output', script.meta['title']) + '.xml' if not output else output packer.convert_and_print(data, output, instances) - info('Processed "{}" and wrote xml to "{}".'.format(script.__name__, output)) + info('Processed "{}" and wrote xml to "{}".'.format( + script.__name__, output)) + def handle_choice_questions(output, script, instances): script_is_valid(script, required=['meta', 'task', 'choices', 'feedback']) @@ -145,11 +151,30 @@ def handle_choice_questions(output, script, instances): output = os.path.join( 'output', script.meta['title']) + '.xml' if not output else output 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() + info('Processed "{}" and wrote xml to "{}".'.format( + script.__name__, output)) + + +def handle_new_script(name, qtype, author, points): + with open('scripts/' + name + '.py', 'x') as new_script: + choice = '' + if qtype in ['multi', 'single']: + choice = '\nchoices = """\n[X] A\n[ ] B\n[ ] C\n[X] D\n"""\n' + + scaffolding = ''' +meta = {{ + 'author': '{}', + 'title': '{}', + 'type': '{}', + 'points': {}, # per correct choice +}} + +task = """ decription """ +{} +feedback = """ decription """ +'''.format(author, name, qtype, points, choice).strip() + print(scaffolding, file=new_script) + info('Generated new script "{}."'.format(new_script.name)) if __name__ == '__main__': parseme() diff --git a/scripts/__init__.py b/scripts/__init__.py deleted file mode 100644 index e69de29..0000000 -- GitLab