lms.cli.courses.quizzes.write

Write quiz to disk in the Quiz Composer format.

usage: python3 -m lms.cli.courses.quizzes.write [-h] [--version]
                                                [--server SERVER]
                                                [--server-type {blackboard,canvas,moodle}]
                                                [--auth-user AUTH_USER]
                                                [--auth-password AUTH_PASSWORD]
                                                [--auth-token AUTH_TOKEN]
                                                [--course COURSE]
                                                [--quiz QUIZ]
                                                [--out-dir OUT_DIR] [--force]
                                                [QUIZ_QUERY ...]

Write quiz to disk in the Quiz Composer format.

positional arguments:
  QUIZ_QUERY            A query for a quiz to get, or don't specify for all
                        quizzes.

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --course COURSE       The course to target for this operation.
  --quiz QUIZ           The quiz to target for this operation.
  --out-dir OUT_DIR     Where the output will be written (default: .).
  --force               Delete any existing files when writing the quizzes
                        (default: False).

server options:
  --server SERVER       The address of the LMS server to connect to.
  --server-type {blackboard,canvas,moodle}
                        The type of LMS being connected to (this can normally
                        be guessed from the server address).

authentication options:
  --auth-user AUTH_USER
                        The user to authenticate with.
  --auth-password AUTH_PASSWORD
                        The password to authenticate with.
  --auth-token AUTH_TOKEN
                        The token to authenticate with.
 1"""
 2Write quiz to disk in the Quiz Composer format.
 3"""
 4
 5import argparse
 6import os
 7import sys
 8
 9import lms.backend.instance
10import lms.cli.common
11import lms.cli.parser
12import lms.model.base
13
14def run_cli(args: argparse.Namespace) -> int:
15    """ Run the CLI. """
16
17    config = args._config
18
19    backend = lms.backend.instance.get_backend(**config)
20
21    course_query = lms.cli.common.check_required_course(backend, config)
22    if (course_query is None):
23        return 1
24
25    quizzes = []
26    if (len(args.quizzes) == 0):
27        quizzes = backend.courses_quizzes_resolve_and_list(course_query)
28    else:
29        queries = backend.parse_quiz_queries(args.quizzes)
30        quizzes = backend.courses_quizzes_get(course_query, queries)
31
32    base_dir = os.path.abspath(args.out_dir)
33    for quiz in quizzes:
34        # Get the groups and questions for this quiz.
35        groups = backend.courses_quizzes_groups_resolve_and_list(course_query, quiz.to_query())
36        questions = backend.courses_quizzes_questions_resolve_and_list(course_query, quiz.to_query(), fetch_resources = True)
37
38        path = quiz.write(base_dir, groups, questions, force = args.force)
39        print(f"Wrote quiz '{quiz.name}' to '{path}'.")
40
41    print(f"{len(quizzes)} quizzes written.")
42
43    return 0
44
45def main() -> int:
46    """ Get a parser, parse the args, and call run. """
47    return run_cli(_get_parser().parse_args())
48
49def _get_parser() -> argparse.ArgumentParser:
50    """ Get the parser. """
51
52    parser = lms.cli.parser.get_parser(__doc__.strip(),
53            include_course = True,
54            include_quiz = True,
55    )
56
57    parser.add_argument('--out-dir', dest = 'out_dir',
58        action = 'store', type = str, default = '.',
59        help = "Where the output will be written (default: %(default)s).")
60
61    parser.add_argument('--force', dest = 'force',
62        action = 'store_true', default = False,
63        help = "Delete any existing files when writing the quizzes (default: %(default)s).")
64
65    parser.add_argument('quizzes', metavar = 'QUIZ_QUERY',
66        type = str, nargs = '*',
67        help = "A query for a quiz to get, or don't specify for all quizzes.")
68
69    return parser
70
71if (__name__ == '__main__'):
72    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
15def run_cli(args: argparse.Namespace) -> int:
16    """ Run the CLI. """
17
18    config = args._config
19
20    backend = lms.backend.instance.get_backend(**config)
21
22    course_query = lms.cli.common.check_required_course(backend, config)
23    if (course_query is None):
24        return 1
25
26    quizzes = []
27    if (len(args.quizzes) == 0):
28        quizzes = backend.courses_quizzes_resolve_and_list(course_query)
29    else:
30        queries = backend.parse_quiz_queries(args.quizzes)
31        quizzes = backend.courses_quizzes_get(course_query, queries)
32
33    base_dir = os.path.abspath(args.out_dir)
34    for quiz in quizzes:
35        # Get the groups and questions for this quiz.
36        groups = backend.courses_quizzes_groups_resolve_and_list(course_query, quiz.to_query())
37        questions = backend.courses_quizzes_questions_resolve_and_list(course_query, quiz.to_query(), fetch_resources = True)
38
39        path = quiz.write(base_dir, groups, questions, force = args.force)
40        print(f"Wrote quiz '{quiz.name}' to '{path}'.")
41
42    print(f"{len(quizzes)} quizzes written.")
43
44    return 0

Run the CLI.

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

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