Newer
Older
#!/usr/local/bin/python3

Jan Maximilian Michal
committed
import importlib
import argparse
import os
import sys
# local import

Jan Maximilian Michal
committed
from hallgrim.IliasXMLCreator import packer

Jan Maximilian Michal
committed
from hallgrim.messages import *
from hallgrim.parser import *

Jan Maximilian Michal
committed

Jan Maximilian Michal
committed
def file_to_module(name):

Jan Maximilian Michal
committed
return name.rstrip('.py').replace('/', '.')
def type_selector(type):
if 'multiple' in type:

Jan Maximilian Michal
committed
return 'MULTIPLE CHOICE QUESTION'
if 'single' in type:

Jan Maximilian Michal
committed
return 'SINGLE CHOICE QUESTION'

Jan Maximilian Michal
committed
if 'gap' in type:
return 'CLOZE QUESTION'

Jan Maximilian Michal
committed
def file_exists(path):
if not os.path.exists(path):
msg = 'The script "{}" does not exist.'.format(path)
raise argparse.ArgumentTypeError(msg)
return path

Jan Maximilian Michal
committed
def script_is_valid(script, required):
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)")

Jan Maximilian Michal
committed
def parseme():
parser = argparse.ArgumentParser()

Jan Maximilian Michal
committed
subparsers = parser.add_subparsers(dest="command")
parser_new = subparsers.add_parser(
"new", help="The utility the generate new scripts.")

Jan Maximilian Michal
committed
parser_new.add_argument(
"name",
help="The name of the new script",
metavar='NAME'

Jan Maximilian Michal
committed
)
parser_new.add_argument(
"-t",
"--type",
choices=['multi', 'single', 'gap', 'alignment'],
default='multi',
metavar='TYPE'
)
parser_new.add_argument(
"-a",
"--author",
help="Name of the scripts author",
default='ILIAS Author',
metavar='AUTHOR'
)
parser_new.add_argument(
"-p",
"--points",
help='Points given for correct answer (different behavior for different questions)',
type=float,
metavar='POINTS',
)
parser_gen = subparsers.add_parser(
"gen", help="Subcommand to convert from script to xml.")

Jan Maximilian Michal
committed
parser_gen.add_argument(

Jan Maximilian Michal
committed
'-o',
'--out',

Jan Maximilian Michal
committed
help='''Specify different output file. If no argument is given the Name
of the script is used.''',

Jan Maximilian Michal
committed
metavar='FILE')

Jan Maximilian Michal
committed
parser_gen.add_argument(

Jan Maximilian Michal
committed
help='Script to execute',

Jan Maximilian Michal
committed
type=file_exists,

Jan Maximilian Michal
committed
metavar='FILE')

Jan Maximilian Michal
committed
parser_gen.add_argument(
'-i',
'--instances',
help='How many instances should be produced (Only for parametrized questions).',
type=int,
default=1,
metavar='COUNT')

Jan Maximilian Michal
committed
args = parser.parse_args()

Jan Maximilian Michal
committed
if args.command == 'gen':

Jan Maximilian Michal
committed
delegator(args.out, args.input, args.instances)

Jan Maximilian Michal
committed
if args.command == 'new':
handle_new_script(args.name, args.type, args.author, args.points)
if args.command == None:
parser.print_help()

Jan Maximilian Michal
committed
def delegator(output, script_name, instances):

Jan Maximilian Michal
committed
script = importlib.import_module(file_to_module(script_name))

Jan Maximilian Michal
committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
handler = {
'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 = {
'type': type_selector(script.meta['type']),
'description': "_description",
'gap_list': gap_parser(script.task),
'author': script.meta['author'],
'title': script.meta['title'],
'shuffle': script.meta['shuffle'] if 'shuffle' in script.meta else True,
'feedback': markdown(script.feedback),
'gap_length': script.meta['gap_length'] if 'gap_length' in script.meta else 20,
}
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_choice_questions(output, script, instances):
script_is_valid(script, required=['meta', 'task', 'choices', 'feedback'])

Jan Maximilian Michal
committed
data = {

Jan Maximilian Michal
committed
'type': type_selector(script.meta['type']),

Jan Maximilian Michal
committed
'description': "_description",
'question_text': markdown(script.task),

Jan Maximilian Michal
committed
'author': script.meta['author'],
'title': script.meta['title'],
'maxattempts': '0',

Jan Maximilian Michal
committed
'shuffle': script.meta['shuffle'] if 'shuffle' in script.meta else True,
'questions': choice_parser(script.choices, script.meta['points']),

Jan Maximilian Michal
committed
'feedback': markdown(script.feedback),

Jan Maximilian Michal
committed
}
output = os.path.join(
'output', script.meta['title']) + '.xml' if not output else output

Jan Maximilian Michal
committed
packer.convert_and_print(data, output, instances)

Jan Maximilian Michal
committed
info('Processed "{}" and wrote xml to "{}".'.format(script.__name__, output))

Jan Maximilian Michal
committed

Jan Maximilian Michal
committed
def handle_new_script(name, type, author, points):
raise NotImplementedError()

Jan Maximilian Michal
committed
if __name__ == '__main__':