quizcomp.cli.parse.question

Parse a single quiz question (JSON file) and output the result in the specified format.

usage: python3 -m quizcomp.cli.parse.question [-h] [--version]
                                              [--format {canvas,html,json,md,tex,qti}]
                                              [--key]
                                              PATH

Parse a single quiz question (JSON file) and output the result in the specified format.

positional arguments:
    PATH                The path to a quiz question json file.

options:
    -h, --help          show this help message and exit
    --version           show program's version number and exit
    --format {canvas,html,json,md,tex,qti}
                        Output the parsed document in this format (default:
                        Format.JSON).
    --key               Generate an answer key instead of a blank quiz
                        (default: False).
 1"""
 2Parse a single quiz question (JSON file) and output the result in the specified format.
 3"""
 4
 5import argparse
 6import os
 7import sys
 8
 9import quizcomp.cli.parser
10import quizcomp.converter.convert
11import quizcomp.model.constants
12import quizcomp.model.question
13
14def run_cli(args: argparse.Namespace) -> int:
15    """ Run the CLI. """
16
17    if (not os.path.exists(args.path)):
18        raise ValueError(f"Provided question path '{args.path}' does not exist.")
19
20    if (not os.path.isfile(args.path)):
21        raise ValueError(f"Provided question path '{args.path}' is not a file.")
22
23    question = quizcomp.model.question.Question.from_path(args.path)
24
25    content = quizcomp.converter.convert.convert_question(
26        question,
27        format = quizcomp.model.constants.Format(args.format),
28        constructor_args = {'answer_key': args.answer_key},
29    )
30
31    print(content)
32
33    return 0
34
35def main() -> int:
36    """ Get a parser, parse the args, and call run. """
37
38    return run_cli(_get_parser().parse_args())
39
40def _get_parser() -> argparse.ArgumentParser:
41    """ Get the parser. """
42
43    parser = quizcomp.cli.parser.get_parser(__doc__.strip(),
44        include_katex = True,
45    )
46
47    parser.add_argument('path', metavar = 'PATH',
48        type = str,
49        help = 'The path to a quiz question json file.')
50
51    parser.add_argument('--format',
52        action = 'store', type = str, default = quizcomp.model.constants.Format.JSON,
53        choices = [choice.value for choice in quizcomp.converter.convert.SUPPORTED_FORMATS],
54        help = 'Output the parsed document in this format (default: %(default)s).')
55
56    parser.add_argument('--key', dest = 'answer_key',
57        action = 'store_true', default = False,
58        help = 'Generate an answer key instead of a blank quiz (default: %(default)s).')
59
60    return parser
61
62if (__name__ == '__main__'):
63    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
15def run_cli(args: argparse.Namespace) -> int:
16    """ Run the CLI. """
17
18    if (not os.path.exists(args.path)):
19        raise ValueError(f"Provided question path '{args.path}' does not exist.")
20
21    if (not os.path.isfile(args.path)):
22        raise ValueError(f"Provided question path '{args.path}' is not a file.")
23
24    question = quizcomp.model.question.Question.from_path(args.path)
25
26    content = quizcomp.converter.convert.convert_question(
27        question,
28        format = quizcomp.model.constants.Format(args.format),
29        constructor_args = {'answer_key': args.answer_key},
30    )
31
32    print(content)
33
34    return 0

Run the CLI.

def main() -> int:
36def main() -> int:
37    """ Get a parser, parse the args, and call run. """
38
39    return run_cli(_get_parser().parse_args())

Get a parser, parse the args, and call run.