lms .cli .courses .quizzes .questions .write
Write quiz questions to disk in the Quiz Composer format.
usage: python3 -m lms.cli.courses.quizzes.questions.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]
[QUESTION_QUERY ...]
Write quiz questions to disk in the Quiz Composer format.
positional arguments:
QUESTION_QUERY A query for the questions to get, or don't specify for
all questions.
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 questions
(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 questions 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 quiz_query = lms.cli.common.check_required_quiz(backend, config) 26 if (quiz_query is None): 27 return 2 28 29 questions = [] 30 if (len(args.questions) == 0): 31 questions = backend.courses_quizzes_questions_resolve_and_list(course_query, quiz_query, fetch_resources = True) 32 else: 33 queries = backend.parse_quiz_question_queries(args.questions) 34 questions = backend.courses_quizzes_questions_get(course_query, quiz_query, queries, fetch_resources = True) 35 36 base_dir = os.path.abspath(args.out_dir) 37 for question in questions: 38 path = question.write(base_dir, force = args.force) 39 print(f"Wrote question '{question.name}' to '{path}'.") 40 41 print(f"{len(questions)} questions 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 questions (default: %(default)s).") 64 65 parser.add_argument('questions', metavar = 'QUESTION_QUERY', 66 type = str, nargs = '*', 67 help = "A query for the questions to get, or don't specify for all questions.") 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 quiz_query = lms.cli.common.check_required_quiz(backend, config) 27 if (quiz_query is None): 28 return 2 29 30 questions = [] 31 if (len(args.questions) == 0): 32 questions = backend.courses_quizzes_questions_resolve_and_list(course_query, quiz_query, fetch_resources = True) 33 else: 34 queries = backend.parse_quiz_question_queries(args.questions) 35 questions = backend.courses_quizzes_questions_get(course_query, quiz_query, queries, fetch_resources = True) 36 37 base_dir = os.path.abspath(args.out_dir) 38 for question in questions: 39 path = question.write(base_dir, force = args.force) 40 print(f"Wrote question '{question.name}' to '{path}'.") 41 42 print(f"{len(questions)} questions 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.