lms.cli.courses.quizzes.download

Download a quiz and write it in the Quiz Composer format.

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

Download a quiz and write it in the Quiz Composer format.

positional arguments:
    QUIZ_QUERY          A query for a quiz to get, or leave empty to download
                        all quizzes for the course.

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.
    --out-dir OUT_DIR   Where the output will be written (default: .).
    --force             Delete an existing quiz output directory before
                        writing the new content (default: False).
    --skip-fetch-images
                        Skip fetching images embedded into the quiz text
                        (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"""
 2Download a quiz and write it in the Quiz Composer format.
 3"""
 4
 5import argparse
 6import os
 7import sys
 8import typing
 9
10import edq.util.dirent
11
12import lms.backend.instance
13import lms.cli.common
14import lms.cli.parser
15import lms.model.assignments
16import lms.model.base
17
18def run_cli(args: argparse.Namespace) -> int:
19    """ Run the CLI. """
20
21    config = args._config_info.application_config
22    backend = lms.backend.instance.get_backend(config)
23
24    course_query = lms.cli.common.check_required_course(backend, config)
25    if (course_query is None):
26        return 1
27
28    quiz_queries: typing.List[lms.model.assignments.AssignmentQuery] = []
29    if (len(args.quizzes) == 0):
30        quiz_queries = [quiz.to_query() for quiz in backend.courses_quizzes_resolve_and_list(course_query)]
31    else:
32        quiz_queries = backend.parse_assignment_queries(args.quizzes)
33
34    if (len(quiz_queries) == 0):
35        print("Found no quizzes to download.")
36        return 0
37
38    base_dir = os.path.abspath(args.out_dir)
39    for quiz_query in quiz_queries:
40        quiz = backend.courses_quizzes_resolve_and_download(course_query, quiz_query)
41
42        path = os.path.join(base_dir, quiz.get_name())
43        if (os.path.exists(path)):
44            if (args.force):
45                edq.util.dirent.remove(path)
46            else:
47                print(f"Directory for quiz ('{quiz.name}') already exists, skipping write: '{path}'.")
48                continue
49
50        quiz.to_dir(path, fetch_images = (args.skip_fetch_images is False))
51
52        print(f"Wrote quiz '{quiz.name}' to '{path}'.")
53
54    print(f"{len(quiz_queries)} quizzes written.")
55
56    return 0
57
58def main() -> int:
59    """ Get a parser, parse the args, and call run. """
60    return run_cli(_get_parser().parse_args())
61
62def _get_parser() -> argparse.ArgumentParser:
63    """ Get the parser. """
64
65    parser = lms.cli.parser.get_parser(__doc__.strip(),
66        include_course = True,
67    )
68
69    parser.add_argument('--out-dir', dest = 'out_dir',
70        action = 'store', type = str, default = '.',
71        help = "Where the output will be written (default: %(default)s).")
72
73    parser.add_argument('--force', dest = 'force',
74        action = 'store_true', default = False,
75        help = "Delete an existing quiz output directory before writing the new content (default: %(default)s).")
76
77    parser.add_argument('--skip-fetch-images', dest = 'skip_fetch_images',
78        action = 'store_true', default = False,
79        help = "Skip fetching images embedded into the quiz text (default: %(default)s).")
80
81    parser.add_argument('quizzes', metavar = 'QUIZ_QUERY',
82        type = str, nargs = '*',
83        help = "A query for a quiz to get, or leave empty to download all quizzes for the course.")
84
85    return parser
86
87if (__name__ == '__main__'):
88    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
19def run_cli(args: argparse.Namespace) -> int:
20    """ Run the CLI. """
21
22    config = args._config_info.application_config
23    backend = lms.backend.instance.get_backend(config)
24
25    course_query = lms.cli.common.check_required_course(backend, config)
26    if (course_query is None):
27        return 1
28
29    quiz_queries: typing.List[lms.model.assignments.AssignmentQuery] = []
30    if (len(args.quizzes) == 0):
31        quiz_queries = [quiz.to_query() for quiz in backend.courses_quizzes_resolve_and_list(course_query)]
32    else:
33        quiz_queries = backend.parse_assignment_queries(args.quizzes)
34
35    if (len(quiz_queries) == 0):
36        print("Found no quizzes to download.")
37        return 0
38
39    base_dir = os.path.abspath(args.out_dir)
40    for quiz_query in quiz_queries:
41        quiz = backend.courses_quizzes_resolve_and_download(course_query, quiz_query)
42
43        path = os.path.join(base_dir, quiz.get_name())
44        if (os.path.exists(path)):
45            if (args.force):
46                edq.util.dirent.remove(path)
47            else:
48                print(f"Directory for quiz ('{quiz.name}') already exists, skipping write: '{path}'.")
49                continue
50
51        quiz.to_dir(path, fetch_images = (args.skip_fetch_images is False))
52
53        print(f"Wrote quiz '{quiz.name}' to '{path}'.")
54
55    print(f"{len(quiz_queries)} quizzes written.")
56
57    return 0

Run the CLI.

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

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