quizcomp.cli.parse.quiz

Parse a single quiz and output the results of the parse.

usage: python3 -m quizcomp.cli.parse.quiz [-h] [--version]
                                          [--format {canvas,html,json,md,tex,qti}]
                                          [--key] [--all-questions]
                                          [--no-shuffle] [--seed SEED]
                                          PATH

Parse a single quiz and output the results of the parse.

positional arguments:
    PATH                The path to a quiz 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).
    --all-questions     Include every question for each group (instead of the
                        amount listed under `pick_count`) (default: False).
    --no-shuffle        Don't shuffle any question answers. (default: False).
    --seed SEED         The random seed to use (defaults to a random seed).
 1"""
 2Parse a single quiz and output the results of the parse.
 3"""
 4
 5import argparse
 6import os
 7import random
 8import sys
 9
10import quizcomp.cli.parser
11import quizcomp.converter.convert
12import quizcomp.model.constants
13import quizcomp.model.quiz
14
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 quiz path '{args.path}' does not exist.")
20
21    if (not os.path.isfile(args.path)):
22        raise ValueError(f"Provided quiz path '{args.path}' is not a file.")
23
24    seed = args.seed
25    if (seed is None):
26        seed = random.randint(0, 2**64)
27
28    quiz = quizcomp.model.quiz.Quiz.from_path(args.path)
29    if (args.no_shuffle):
30        quiz.attributes['shuffle_answers'] = False  # pylint: disable=no-member
31
32    variant = quiz.create_variant(all_questions = args.all_questions, seed = seed)  # pylint: disable=no-member
33    content = quizcomp.converter.convert.convert_variant(
34            variant,
35            format = quizcomp.model.constants.Format(args.format),
36            constructor_args = {'answer_key': args.answer_key})
37
38    print(content)
39
40    return 0
41
42def main() -> int:
43    """ Get a parser, parse the args, and call run. """
44
45    return run_cli(_get_parser().parse_args())
46
47def _get_parser() -> argparse.ArgumentParser:
48    """ Get the parser. """
49
50    parser = quizcomp.cli.parser.get_parser(__doc__.strip(),
51        include_katex = True,
52    )
53
54    parser.add_argument('path', metavar = 'PATH',
55        type = str,
56        help = 'The path to a quiz json file.')
57
58    parser.add_argument('--format',
59        action = 'store', type = str, default = quizcomp.model.constants.Format.JSON,
60        choices = [choice.value for choice in quizcomp.converter.convert.SUPPORTED_FORMATS],
61        help = 'Output the parsed document in this format (default: %(default)s).')
62
63    parser.add_argument('--key', dest = 'answer_key',
64        action = 'store_true', default = False,
65        help = 'Generate an answer key instead of a blank quiz (default: %(default)s).')
66
67    parser.add_argument('--all-questions', dest = 'all_questions',
68        action = 'store_true', default = False,
69        help = 'Include every question for each group (instead of the amount listed under `pick_count`) (default: %(default)s).')
70
71    parser.add_argument('--no-shuffle', dest = 'no_shuffle',
72        action = 'store_true', default = False,
73        help = "Don't shuffle any question answers. (default: %(default)s).")
74
75    parser.add_argument('--seed', dest = 'seed',
76        action = 'store', type = int, default = None,
77        help = 'The random seed to use (defaults to a random seed).')
78
79    return parser
80
81if (__name__ == '__main__'):
82    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
16def run_cli(args: argparse.Namespace) -> int:
17    """ Run the CLI. """
18
19    if (not os.path.exists(args.path)):
20        raise ValueError(f"Provided quiz path '{args.path}' does not exist.")
21
22    if (not os.path.isfile(args.path)):
23        raise ValueError(f"Provided quiz path '{args.path}' is not a file.")
24
25    seed = args.seed
26    if (seed is None):
27        seed = random.randint(0, 2**64)
28
29    quiz = quizcomp.model.quiz.Quiz.from_path(args.path)
30    if (args.no_shuffle):
31        quiz.attributes['shuffle_answers'] = False  # pylint: disable=no-member
32
33    variant = quiz.create_variant(all_questions = args.all_questions, seed = seed)  # pylint: disable=no-member
34    content = quizcomp.converter.convert.convert_variant(
35            variant,
36            format = quizcomp.model.constants.Format(args.format),
37            constructor_args = {'answer_key': args.answer_key})
38
39    print(content)
40
41    return 0

Run the CLI.

def main() -> int:
43def main() -> int:
44    """ Get a parser, parse the args, and call run. """
45
46    return run_cli(_get_parser().parse_args())

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