quizcomp.cli.parse.quiz-for-testing

Parse a quiz for the purposes of testing. The quiz will be parsed one and content will be generated for (but not output) for each of the specified format (or none if none are specified).

usage: python3 -m quizcomp.cli.parse.quiz-for-testing [-h] [--version] [--key]
                                                      [--all-questions]
                                                      [--seed SEED]
                                                      PATH [FORMAT ...]

Parse a quiz for the purposes of testing.
The quiz will be parsed one and content will be generated for (but not output)
for each of the specified format (or none if none are specified).

positional arguments:
    PATH             The path to a quiz json file.
    FORMAT           Generate (but do not output) content in this format.

options:
    -h, --help       show this help message and exit
    --version        show program's version number and exit
    --key            Generate an answer key instead of a blank quiz (default:
                     False).
    --all-questions  Include every question for each group (instead of the
                     amount listed under `pick_count`) (default: False).
    --seed SEED      The random seed to use (defaults to a random seed).
 1# pylint: disable=invalid-name
 2
 3"""
 4Parse a quiz for the purposes of testing.
 5The quiz will be parsed one and content will be generated for (but not output)
 6for each of the specified format (or none if none are specified).
 7"""
 8
 9import argparse
10import os
11import random
12import sys
13
14import quizcomp.cli.parser
15import quizcomp.converter.convert
16import quizcomp.model.constants
17import quizcomp.model.quiz
18
19def run_cli(args: argparse.Namespace) -> int:
20    """ Run the CLI. """
21
22    if (not os.path.exists(args.path)):
23        raise ValueError(f"Provided path '{args.path}' does not exist.")
24
25    if (not os.path.isfile(args.path)):
26        raise ValueError(f"Provided path '{args.path}' is not a file.")
27
28    for quiz_format in args.formats:
29        parsed_format = quizcomp.model.constants.Format(quiz_format)
30        if (parsed_format not in quizcomp.converter.convert.SUPPORTED_FORMATS):
31            raise ValueError(f"Unknown quiz format '{quiz_format}', must be one of: {quizcomp.converter.convert.SUPPORTED_FORMATS}.")
32
33    seed = args.seed
34    if (seed is None):
35        seed = random.randint(0, 2**64)
36
37    print(f"Parsing quiz: '{args.path}'.")
38
39    quiz = quizcomp.model.quiz.Quiz.from_path(args.path)
40    variant = quiz.create_variant(all_questions = args.all_questions, seed = seed)  # pylint: disable=no-member
41
42    for quiz_format in args.formats:
43        print(f"Generating quiz content for '{quiz_format}'.")
44        quizcomp.converter.convert.convert_variant(variant,
45                format = quizcomp.model.constants.Format(quiz_format),
46                constructor_args = {'answer_key': args.answer_key})
47
48    return 0
49
50def main() -> int:
51    """ Get a parser, parse the args, and call run. """
52
53    return run_cli(_get_parser().parse_args())
54
55def _get_parser() -> argparse.ArgumentParser:
56    """ Get the parser. """
57
58    parser = quizcomp.cli.parser.get_parser(__doc__.strip(),
59        include_katex = True,
60    )
61
62    parser.add_argument('path', metavar = 'PATH',
63        type = str,
64        help = 'The path to a quiz json file.')
65
66    parser.add_argument('formats', metavar = 'FORMAT',
67        type = str, nargs = '*',
68        help = 'Generate (but do not output) content in this format.')
69
70    parser.add_argument('--key', dest = 'answer_key',
71        action = 'store_true', default = False,
72        help = 'Generate an answer key instead of a blank quiz (default: %(default)s).')
73
74    parser.add_argument('--all-questions', dest = 'all_questions',
75        action = 'store_true', default = False,
76        help = 'Include every question for each group (instead of the amount listed under `pick_count`) (default: %(default)s).')
77
78    parser.add_argument('--seed', dest = 'seed',
79        action = 'store', type = int, default = None,
80        help = 'The random seed to use (defaults to a random seed).')
81
82    return parser
83
84if (__name__ == '__main__'):
85    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
20def run_cli(args: argparse.Namespace) -> int:
21    """ Run the CLI. """
22
23    if (not os.path.exists(args.path)):
24        raise ValueError(f"Provided path '{args.path}' does not exist.")
25
26    if (not os.path.isfile(args.path)):
27        raise ValueError(f"Provided path '{args.path}' is not a file.")
28
29    for quiz_format in args.formats:
30        parsed_format = quizcomp.model.constants.Format(quiz_format)
31        if (parsed_format not in quizcomp.converter.convert.SUPPORTED_FORMATS):
32            raise ValueError(f"Unknown quiz format '{quiz_format}', must be one of: {quizcomp.converter.convert.SUPPORTED_FORMATS}.")
33
34    seed = args.seed
35    if (seed is None):
36        seed = random.randint(0, 2**64)
37
38    print(f"Parsing quiz: '{args.path}'.")
39
40    quiz = quizcomp.model.quiz.Quiz.from_path(args.path)
41    variant = quiz.create_variant(all_questions = args.all_questions, seed = seed)  # pylint: disable=no-member
42
43    for quiz_format in args.formats:
44        print(f"Generating quiz content for '{quiz_format}'.")
45        quizcomp.converter.convert.convert_variant(variant,
46                format = quizcomp.model.constants.Format(quiz_format),
47                constructor_args = {'answer_key': args.answer_key})
48
49    return 0

Run the CLI.

def main() -> int:
51def main() -> int:
52    """ Get a parser, parse the args, and call run. """
53
54    return run_cli(_get_parser().parse_args())

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