lms.cli.parser

Customize an argument parser for LMS Toolkit.

  1"""
  2Customize an argument parser for LMS Toolkit.
  3"""
  4
  5import argparse
  6import typing
  7
  8import edq.core.argparser
  9import edq.net.exchange
 10import edq.util.reflection
 11
 12import lms
 13import lms.model.constants
 14import lms.util.net
 15
 16CONFIG_FILENAME: str = 'edq-lms.json'
 17
 18DEFAULT_SKIP_ROWS: int = 0
 19
 20_set_exchanges_clean_func: bool = True  # pylint: disable=invalid-name
 21"""
 22Whether to set the exchanges clean function when creating the parser.
 23This may be disabled for testing.
 24"""
 25
 26def get_parser(description: str,
 27        include_server: bool = True,
 28        include_auth: bool = True,
 29        include_output_format: bool = False,
 30        include_course: bool = False,
 31        include_assignment: bool = False,
 32        include_user: bool = False,
 33        include_groupset: bool = False,
 34        include_group: bool = False,
 35        include_net: bool = True,
 36        include_skip_rows: bool = False,
 37        include_strict: bool = False,
 38        ) -> argparse.ArgumentParser:
 39    """
 40    Get an argument parser specialized for LMS Toolkit.
 41    """
 42
 43    config_options = {
 44        'config_filename': CONFIG_FILENAME,
 45        'cli_arg_config_map': {
 46            'server': 'server',
 47            'backend_type': 'backend_type',
 48            'auth_user': 'auth_user',
 49            'auth_password': 'auth_password',
 50            'auth_token': 'auth_token',
 51            'course': 'course',
 52            'assignment': 'assignment',
 53            'user': 'user',
 54            'group': 'group',
 55            'groupset': 'groupset',
 56        },
 57    }
 58
 59    parser = edq.core.argparser.get_default_parser(
 60            description,
 61            version = f"v{lms.__version__}",
 62            include_net = include_net,
 63            config_options = config_options,
 64    )
 65
 66    # Ensure that responses are cleaned as LMS responses.
 67    if (include_net):
 68        if (_set_exchanges_clean_func):
 69            edq.net.exchange._exchanges_clean_func = edq.util.reflection.get_qualified_name(lms.util.net.clean_lms_response)
 70
 71    if (include_server):
 72        group = parser.add_argument_group('server options')
 73
 74        group.add_argument('--server', dest = 'server',
 75            action = 'store', type = str, default = None,
 76            help = 'The address of the LMS server to connect to.')
 77
 78        group.add_argument('--server-type', dest = 'backend_type',
 79            action = 'store', type = str,
 80            default = None, choices = lms.model.constants.BACKEND_TYPES,
 81            help = 'The type of LMS being connected to (this can normally be guessed from the server address).')
 82
 83    if (include_auth):
 84        group = parser.add_argument_group('authentication options')
 85
 86        group.add_argument('--auth-user', dest = 'auth_user',
 87            action = 'store', type = str, default = None,
 88            help = 'The user to authenticate with.')
 89
 90        group.add_argument('--auth-password', dest = 'auth_password',
 91            action = 'store', type = str, default = None,
 92            help = 'The password to authenticate with.')
 93
 94        group.add_argument('--auth-token', dest = 'auth_token',
 95            action = 'store', type = str, default = None,
 96            help = 'The token to authenticate with.')
 97
 98    if (include_course):
 99        parser.add_argument('--course', dest = 'course',
100            action = 'store', type = str, default = None,
101            help = 'The course to target for this operation.')
102
103    if (include_assignment):
104        parser.add_argument('--assignment', dest = 'assignment',
105            action = 'store', type = str, default = None,
106            help = 'The assignment to target for this operation.')
107
108    if (include_user):
109        parser.add_argument('--user', dest = 'user',
110            action = 'store', type = str, default = None,
111            help = 'The user to target for this operation.')
112
113    if (include_groupset):
114        parser.add_argument('--groupset', dest = 'groupset',
115            action = 'store', type = str, default = None,
116            help = 'The group set to target for this operation.')
117
118    if (include_group):
119        parser.add_argument('--group', dest = 'group',
120            action = 'store', type = str, default = None,
121            help = 'The group to target for this operation.')
122
123    if (include_output_format):
124        group = parser.add_argument_group('output formatting options')
125
126        group.add_argument('--format', dest = 'output_format',
127            action = 'store', type = str,
128            default = lms.model.constants.OUTPUT_FORMAT_TEXT, choices = lms.model.constants.OUTPUT_FORMATS,
129            help = 'The format to display the output as (default: %(default)s).')
130
131        group.add_argument('--include-extra-fields', dest = 'include_extra_fields',
132            action = 'store_true', default = False,
133            help = 'Include non-common (usually LMS-specific) fields in results (default: %(default)s).')
134
135        group.add_argument('--pretty-headers', dest = 'pretty_headers',
136            action = 'store_true', default = False,
137            help = 'When displaying headers, try to make them look "pretty" (default: %(default)s).')
138
139        group.add_argument('--skip-headers', dest = 'skip_headers',
140            action = 'store_true', default = False,
141            help = 'Skip headers when outputting results, will not apply to all formats (default: %(default)s).')
142
143    if (include_skip_rows):
144        parser.add_argument('--skip-rows', dest = 'skip_rows',
145            action = 'store', type = int, default = DEFAULT_SKIP_ROWS,
146            help = 'The number of header rows to skip (default: %(default)s).')
147
148    if (include_strict):
149        parser.add_argument('--strict', dest = 'strict',
150            action = 'store_true', default = False,
151            help = 'Enable strict mode, which is stricter about what counts as an error (default: %(default)s).')
152
153    return typing.cast(argparse.ArgumentParser, parser)
CONFIG_FILENAME: str = 'edq-lms.json'
DEFAULT_SKIP_ROWS: int = 0
def get_parser( description: str, include_server: bool = True, include_auth: bool = True, include_output_format: bool = False, include_course: bool = False, include_assignment: bool = False, include_user: bool = False, include_groupset: bool = False, include_group: bool = False, include_net: bool = True, include_skip_rows: bool = False, include_strict: bool = False) -> argparse.ArgumentParser:
 27def get_parser(description: str,
 28        include_server: bool = True,
 29        include_auth: bool = True,
 30        include_output_format: bool = False,
 31        include_course: bool = False,
 32        include_assignment: bool = False,
 33        include_user: bool = False,
 34        include_groupset: bool = False,
 35        include_group: bool = False,
 36        include_net: bool = True,
 37        include_skip_rows: bool = False,
 38        include_strict: bool = False,
 39        ) -> argparse.ArgumentParser:
 40    """
 41    Get an argument parser specialized for LMS Toolkit.
 42    """
 43
 44    config_options = {
 45        'config_filename': CONFIG_FILENAME,
 46        'cli_arg_config_map': {
 47            'server': 'server',
 48            'backend_type': 'backend_type',
 49            'auth_user': 'auth_user',
 50            'auth_password': 'auth_password',
 51            'auth_token': 'auth_token',
 52            'course': 'course',
 53            'assignment': 'assignment',
 54            'user': 'user',
 55            'group': 'group',
 56            'groupset': 'groupset',
 57        },
 58    }
 59
 60    parser = edq.core.argparser.get_default_parser(
 61            description,
 62            version = f"v{lms.__version__}",
 63            include_net = include_net,
 64            config_options = config_options,
 65    )
 66
 67    # Ensure that responses are cleaned as LMS responses.
 68    if (include_net):
 69        if (_set_exchanges_clean_func):
 70            edq.net.exchange._exchanges_clean_func = edq.util.reflection.get_qualified_name(lms.util.net.clean_lms_response)
 71
 72    if (include_server):
 73        group = parser.add_argument_group('server options')
 74
 75        group.add_argument('--server', dest = 'server',
 76            action = 'store', type = str, default = None,
 77            help = 'The address of the LMS server to connect to.')
 78
 79        group.add_argument('--server-type', dest = 'backend_type',
 80            action = 'store', type = str,
 81            default = None, choices = lms.model.constants.BACKEND_TYPES,
 82            help = 'The type of LMS being connected to (this can normally be guessed from the server address).')
 83
 84    if (include_auth):
 85        group = parser.add_argument_group('authentication options')
 86
 87        group.add_argument('--auth-user', dest = 'auth_user',
 88            action = 'store', type = str, default = None,
 89            help = 'The user to authenticate with.')
 90
 91        group.add_argument('--auth-password', dest = 'auth_password',
 92            action = 'store', type = str, default = None,
 93            help = 'The password to authenticate with.')
 94
 95        group.add_argument('--auth-token', dest = 'auth_token',
 96            action = 'store', type = str, default = None,
 97            help = 'The token to authenticate with.')
 98
 99    if (include_course):
100        parser.add_argument('--course', dest = 'course',
101            action = 'store', type = str, default = None,
102            help = 'The course to target for this operation.')
103
104    if (include_assignment):
105        parser.add_argument('--assignment', dest = 'assignment',
106            action = 'store', type = str, default = None,
107            help = 'The assignment to target for this operation.')
108
109    if (include_user):
110        parser.add_argument('--user', dest = 'user',
111            action = 'store', type = str, default = None,
112            help = 'The user to target for this operation.')
113
114    if (include_groupset):
115        parser.add_argument('--groupset', dest = 'groupset',
116            action = 'store', type = str, default = None,
117            help = 'The group set to target for this operation.')
118
119    if (include_group):
120        parser.add_argument('--group', dest = 'group',
121            action = 'store', type = str, default = None,
122            help = 'The group to target for this operation.')
123
124    if (include_output_format):
125        group = parser.add_argument_group('output formatting options')
126
127        group.add_argument('--format', dest = 'output_format',
128            action = 'store', type = str,
129            default = lms.model.constants.OUTPUT_FORMAT_TEXT, choices = lms.model.constants.OUTPUT_FORMATS,
130            help = 'The format to display the output as (default: %(default)s).')
131
132        group.add_argument('--include-extra-fields', dest = 'include_extra_fields',
133            action = 'store_true', default = False,
134            help = 'Include non-common (usually LMS-specific) fields in results (default: %(default)s).')
135
136        group.add_argument('--pretty-headers', dest = 'pretty_headers',
137            action = 'store_true', default = False,
138            help = 'When displaying headers, try to make them look "pretty" (default: %(default)s).')
139
140        group.add_argument('--skip-headers', dest = 'skip_headers',
141            action = 'store_true', default = False,
142            help = 'Skip headers when outputting results, will not apply to all formats (default: %(default)s).')
143
144    if (include_skip_rows):
145        parser.add_argument('--skip-rows', dest = 'skip_rows',
146            action = 'store', type = int, default = DEFAULT_SKIP_ROWS,
147            help = 'The number of header rows to skip (default: %(default)s).')
148
149    if (include_strict):
150        parser.add_argument('--strict', dest = 'strict',
151            action = 'store_true', default = False,
152            help = 'Enable strict mode, which is stricter about what counts as an error (default: %(default)s).')
153
154    return typing.cast(argparse.ArgumentParser, parser)

Get an argument parser specialized for LMS Toolkit.