autograder.cli.courses.users.enroll-file

Enroll users from a TSV file into a course.

usage: python3 -m autograder.cli.courses.users.enroll-file [-h] [--version]
                                                           [--server SERVER]
                                                           [--course COURSE]
                                                           [--user USER]
                                                           [--pass PASS]
                                                           [--dry-run]
                                                           [--skip-inserts]
                                                           [--skip-updates]
                                                           [--send-emails]
                                                           [--testing-mode]
                                                           PATH

Enroll users from a TSV file into a course.

positional arguments:
    PATH             Path to a TSV file where each line contains up to four
                     columns: [email, name, course-role, lms-id]. Only the
                     email is required. Leading and trailing whitespace is
                     stripped from all fields.

options:
    -h, --help       show this help message and exit
    --version        show program's version number and exit

command-specific options:
    --server SERVER  The URL of the autograder server to communicate with.
    --course COURSE  The ID of the course to make this request to.
    --user USER      The email of the user making this request.
    --pass PASS      The password of the user making this request.
    --dry-run        Do not commit/finalize the operation, just do all the
                     steps and state what the result would look like.
    --skip-inserts   Skip insert operations.
    --skip-updates   Skip update operations.
    --send-emails    Send any relevant emails to users affected by this
                     operation (e.g., a user being enrolled in a course).

testing options:
    --testing-mode   Run as if a test is being run (default: False).
  1# pylint: disable=invalid-name
  2
  3"""
  4Enroll users from a TSV file into a course.
  5"""
  6
  7import argparse
  8import sys
  9import typing
 10
 11import edq.util.json
 12
 13import autograder.api.config
 14import autograder.api.courses.users.enroll
 15import autograder.cli.parser
 16import autograder.util.load
 17
 18API_PARAMS: typing.List[autograder.api.config.APIParam] = [
 19    autograder.api.config.PARAM_SERVER,
 20    autograder.api.config.PARAM_COURSE,
 21    autograder.api.config.PARAM_USER_EMAIL,
 22    autograder.api.config.PARAM_USER_PASS,
 23
 24    autograder.api.config.PARAM_DRY_RUN,
 25    autograder.api.config.PARAM_SKIP_INSERTS,
 26    autograder.api.config.PARAM_SKIP_UPDATES,
 27
 28    autograder.api.config.PARAM_SEND_EMAILS,
 29
 30    autograder.api.config.PARAM_RAW_COURSE_USERS,
 31]
 32
 33def run_cli(args: argparse.Namespace) -> int:
 34    """ Run the CLI. """
 35
 36    config = args._config_info.application_config
 37    config.raw_course_users = _load_users(args.path)
 38
 39    result = autograder.api.courses.users.enroll.send(config, exit_on_error = True)
 40    print(edq.util.json.dumps(result, indent = 4))
 41
 42    return 0
 43
 44def _load_users(path: str) -> typing.List[typing.Dict[str, str]]:
 45    """ Load raw user entries from a TSV file. """
 46
 47    users = []
 48
 49    rows = autograder.util.load.load_tsv(path, 4)
 50    for (lineno, row) in enumerate(rows):
 51        email = row.pop(0)
 52
 53        name = ''
 54        if (len(row) > 0):
 55            name = row.pop(0)
 56
 57        course_role = ''
 58        if (len(row) > 0):
 59            course_role = row.pop(0)
 60            course_role = course_role.lower()
 61
 62        if (course_role == ''):
 63            course_role = 'unknown'
 64
 65        if (not autograder.model.user.CourseRole.has_value(course_role)):
 66            raise ValueError(f"File ('{path}') line ({lineno}) has an invalid course role '{course_role}'.")
 67
 68        course_lms_id = ''
 69        if (len(row) > 0):
 70            course_lms_id = row.pop(0)
 71
 72        users.append({
 73            'email': email,
 74            'name': name,
 75            'course-role': course_role,
 76            'course-lms-id': course_lms_id,
 77        })
 78
 79    return users
 80
 81def main() -> int:
 82    """ Get a parser, parse the args, and call run. """
 83
 84    return run_cli(_get_parser().parse_args())
 85
 86def _get_parser() -> argparse.ArgumentParser:
 87    """ Get a parser for this operation. """
 88
 89    parser = autograder.cli.parser.get_parser(
 90        __doc__.strip(),
 91        API_PARAMS)
 92
 93    parser.add_argument('path', metavar = 'PATH',
 94        action = 'store', type = str,
 95        help = 'Path to a TSV file where each line contains up to four columns:'
 96                + ' [email, name, course-role, lms-id].'
 97                + ' Only the email is required. Leading and trailing whitespace is stripped'
 98                + ' from all fields.')
 99
100    return parser
101
102if (__name__ == '__main__'):
103    sys.exit(main())
def run_cli(args: argparse.Namespace) -> int:
34def run_cli(args: argparse.Namespace) -> int:
35    """ Run the CLI. """
36
37    config = args._config_info.application_config
38    config.raw_course_users = _load_users(args.path)
39
40    result = autograder.api.courses.users.enroll.send(config, exit_on_error = True)
41    print(edq.util.json.dumps(result, indent = 4))
42
43    return 0

Run the CLI.

def main() -> int:
82def main() -> int:
83    """ Get a parser, parse the args, and call run. """
84
85    return run_cli(_get_parser().parse_args())

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