Skip to content
Snippets Groups Projects
Commit bf975393 authored by Jan Maximilian Michal's avatar Jan Maximilian Michal
Browse files

Scripts are tested for required fields

parent 4f61b4ba
No related branches found
No related tags found
No related merge requests found
...@@ -21,20 +21,31 @@ def type_selector(type): ...@@ -21,20 +21,31 @@ def type_selector(type):
if 'single' in type: if 'single' in type:
return 'SINGLE CHOICE QUESTION' return 'SINGLE CHOICE QUESTION'
def file_exists(path): def file_exists(path):
if not os.path.exists(path): if not os.path.exists(path):
msg = 'The script "{}" does not exist.'.format(path) msg = 'The script "{}" does not exist.'.format(path)
raise argparse.ArgumentTypeError(msg) raise argparse.ArgumentTypeError(msg)
return path 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(): def parseme():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command") 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( parser_new.add_argument(
"name", "name",
help="The name of the new script" help="The name of the new script",
metavar='NAME'
) )
parser_new.add_argument( parser_new.add_argument(
"-t", "-t",
...@@ -58,7 +69,8 @@ def parseme(): ...@@ -58,7 +69,8 @@ def parseme():
metavar='POINTS', 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( parser_gen.add_argument(
'-o', '-o',
'--out', '--out',
...@@ -90,6 +102,7 @@ def parseme(): ...@@ -90,6 +102,7 @@ def parseme():
def handle_choice_questions(output, script_name, instances): def handle_choice_questions(output, script_name, instances):
script = importlib.import_module(file_to_module(script_name)) script = importlib.import_module(file_to_module(script_name))
script_is_valid(script)
data = { data = {
'type': type_selector(script.meta['type']), 'type': type_selector(script.meta['type']),
'description': "_description", 'description': "_description",
...@@ -107,6 +120,7 @@ def handle_choice_questions(output, script_name, instances): ...@@ -107,6 +120,7 @@ def handle_choice_questions(output, script_name, instances):
packer.convert_and_print(data, output, instances) 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_new_script(name, type, author, points): def handle_new_script(name, type, author, points):
raise NotImplementedError() raise NotImplementedError()
......
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
import sys
def warn(msg): def warn(msg):
print('[WARN]', msg) print('[WARN]', msg)
...@@ -5,4 +7,11 @@ def debug(msg): ...@@ -5,4 +7,11 @@ def debug(msg):
print('[DEBUG]', msg) print('[DEBUG]', msg)
def info(msg): def info(msg):
print('[INFO]', msg) print('[INFO]', msg)
\ No newline at end of file
def error(msg):
print('[ERROR]', msg)
def abort(msg):
print('[FATAL]', msg)
sys.exit('exiting...')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment