diff --git a/grim.py b/grim.py index 325ab5deceab95c3750944aff35f959bdecf5eb7..246cf3c70ba2312806049874fbd8ddd1100d12bb 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000