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.settings
 10import edq.util.reflection
 11
 12import lms
 13import lms.model.config
 14import lms.model.constants
 15import lms.util.net
 16
 17CONFIG_FILENAME: str = 'edq-lms.json'
 18ENV_CONFIG_PREFIX: str = 'EDQLMS__'
 19DEFAULT_ENCRYPTION_KEY: str = 'EDQLMS'
 20
 21DEFAULT_SKIP_ROWS: int = 0
 22
 23_set_exchanges_clean_response_func: bool = True  # pylint: disable=invalid-name
 24"""
 25Whether to set the exchanges clean function when creating the parser.
 26This may be disabled for testing.
 27"""
 28
 29def get_parser(description: str,
 30        include_server: bool = True,
 31        include_auth: bool = True,
 32        include_output_format: bool = False,
 33        include_course: bool = False,
 34        include_assignment: bool = False,
 35        include_quiz: bool = False,
 36        include_user: bool = False,
 37        include_groupset: bool = False,
 38        include_group: bool = False,
 39        include_net: bool = True,
 40        include_skip_rows: bool = False,
 41        include_strict: bool = False,
 42        ) -> argparse.ArgumentParser:
 43    """
 44    Get an argument parser specialized for LMS Toolkit.
 45    """
 46
 47    # Set config options.
 48    edq.config.settings.set_config_filename(CONFIG_FILENAME)
 49    edq.config.settings.set_application_config_class(lms.model.config.Config)
 50    edq.config.settings.set_env_prefix(ENV_CONFIG_PREFIX)
 51    edq.config.settings.set_default_encryption_key(DEFAULT_ENCRYPTION_KEY)
 52
 53    parser = edq.core.argparser.get_default_parser(
 54            description,
 55            version = f"v{lms.__version__}",
 56            include_net = include_net,
 57    )
 58
 59    # Ensure that responses are cleaned as LMS responses.
 60    if (include_net):
 61        if (_set_exchanges_clean_response_func):
 62            edq.net.settings.set_exchanges_clean_response_func(edq.util.reflection.get_qualified_name(lms.util.net.clean_lms_response))
 63
 64    if (include_server):
 65        group = parser.add_argument_group('server options')
 66
 67        group.add_argument('--server', dest = 'server',
 68            action = 'store', type = str, default = None,
 69            help = 'The address of the LMS server to connect to.')
 70
 71        group.add_argument('--server-type', dest = 'backend_type',
 72            action = 'store', type = str,
 73            default = None,
 74            choices = [choice.value for choice in lms.model.constants.BackendType],
 75            help = 'The type of LMS being connected to (this can normally be guessed from the server address).')
 76
 77    if (include_auth):
 78        group = parser.add_argument_group('authentication options')
 79
 80        group.add_argument('--auth-user', dest = 'auth_user',
 81            action = 'store', type = str, default = None,
 82            help = 'The user to authenticate with.')
 83
 84        group.add_argument('--auth-password', dest = 'auth_password',
 85            action = 'store', type = str, default = None,
 86            help = 'The password to authenticate with.')
 87
 88        group.add_argument('--auth-token', dest = 'auth_token',
 89            action = 'store', type = str, default = None,
 90            help = 'The token to authenticate with.')
 91
 92    if (include_course):
 93        parser.add_argument('--course', dest = 'course',
 94            action = 'store', type = str, default = None,
 95            help = 'The course to target for this operation.')
 96
 97    if (include_assignment):
 98        parser.add_argument('--assignment', dest = 'assignment',
 99            action = 'store', type = str, default = None,
100            help = 'The assignment to target for this operation.')
101
102    if (include_quiz):
103        parser.add_argument('--quiz', dest = 'quiz',
104            action = 'store', type = str, default = None,
105            help = 'The quiz to target for this operation.')
106
107    if (include_user):
108        parser.add_argument('--user', dest = 'user',
109            action = 'store', type = str, default = None,
110            help = 'The user to target for this operation.')
111
112    if (include_groupset):
113        parser.add_argument('--groupset', dest = 'groupset',
114            action = 'store', type = str, default = None,
115            help = 'The group set to target for this operation.')
116
117    if (include_group):
118        parser.add_argument('--group', dest = 'group',
119            action = 'store', type = str, default = None,
120            help = 'The group to target for this operation.')
121
122    if (include_output_format):
123        group = parser.add_argument_group('output formatting options')
124
125        group.add_argument('--format', dest = 'output_format',
126            action = 'store', type = str,
127            default = lms.model.constants.OutputFormat.TEXT.value,
128            choices = [choice.value for choice in lms.model.constants.OutputFormat],
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'
ENV_CONFIG_PREFIX: str = 'EDQLMS__'
DEFAULT_ENCRYPTION_KEY: str = 'EDQLMS'
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_quiz: 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:
 30def get_parser(description: str,
 31        include_server: bool = True,
 32        include_auth: bool = True,
 33        include_output_format: bool = False,
 34        include_course: bool = False,
 35        include_assignment: bool = False,
 36        include_quiz: bool = False,
 37        include_user: bool = False,
 38        include_groupset: bool = False,
 39        include_group: bool = False,
 40        include_net: bool = True,
 41        include_skip_rows: bool = False,
 42        include_strict: bool = False,
 43        ) -> argparse.ArgumentParser:
 44    """
 45    Get an argument parser specialized for LMS Toolkit.
 46    """
 47
 48    # Set config options.
 49    edq.config.settings.set_config_filename(CONFIG_FILENAME)
 50    edq.config.settings.set_application_config_class(lms.model.config.Config)
 51    edq.config.settings.set_env_prefix(ENV_CONFIG_PREFIX)
 52    edq.config.settings.set_default_encryption_key(DEFAULT_ENCRYPTION_KEY)
 53
 54    parser = edq.core.argparser.get_default_parser(
 55            description,
 56            version = f"v{lms.__version__}",
 57            include_net = include_net,
 58    )
 59
 60    # Ensure that responses are cleaned as LMS responses.
 61    if (include_net):
 62        if (_set_exchanges_clean_response_func):
 63            edq.net.settings.set_exchanges_clean_response_func(edq.util.reflection.get_qualified_name(lms.util.net.clean_lms_response))
 64
 65    if (include_server):
 66        group = parser.add_argument_group('server options')
 67
 68        group.add_argument('--server', dest = 'server',
 69            action = 'store', type = str, default = None,
 70            help = 'The address of the LMS server to connect to.')
 71
 72        group.add_argument('--server-type', dest = 'backend_type',
 73            action = 'store', type = str,
 74            default = None,
 75            choices = [choice.value for choice in lms.model.constants.BackendType],
 76            help = 'The type of LMS being connected to (this can normally be guessed from the server address).')
 77
 78    if (include_auth):
 79        group = parser.add_argument_group('authentication options')
 80
 81        group.add_argument('--auth-user', dest = 'auth_user',
 82            action = 'store', type = str, default = None,
 83            help = 'The user to authenticate with.')
 84
 85        group.add_argument('--auth-password', dest = 'auth_password',
 86            action = 'store', type = str, default = None,
 87            help = 'The password to authenticate with.')
 88
 89        group.add_argument('--auth-token', dest = 'auth_token',
 90            action = 'store', type = str, default = None,
 91            help = 'The token to authenticate with.')
 92
 93    if (include_course):
 94        parser.add_argument('--course', dest = 'course',
 95            action = 'store', type = str, default = None,
 96            help = 'The course to target for this operation.')
 97
 98    if (include_assignment):
 99        parser.add_argument('--assignment', dest = 'assignment',
100            action = 'store', type = str, default = None,
101            help = 'The assignment to target for this operation.')
102
103    if (include_quiz):
104        parser.add_argument('--quiz', dest = 'quiz',
105            action = 'store', type = str, default = None,
106            help = 'The quiz 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.OutputFormat.TEXT.value,
129            choices = [choice.value for choice in lms.model.constants.OutputFormat],
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.