lms.cli.common

  1import typing
  2
  3import lms.model.assignments
  4import lms.model.backend
  5import lms.model.config
  6import lms.model.courses
  7import lms.model.groupsets
  8import lms.model.users
  9import lms.util.parse
 10
 11DEFAULT_STRICT_EXIT_CODE: int = 101
 12
 13def check_required_assignment(
 14        backend: lms.model.backend.APIBackend,
 15        config: lms.model.config.Config,
 16        ) -> typing.Union[lms.model.assignments.AssignmentQuery, None]:
 17    """
 18    Fetch and ensure that an assignment is provided in the config.
 19    If no assignment is provided, print a message and return None.
 20    """
 21
 22    assignment = lms.util.parse.optional_string(config.assignment)
 23    if (assignment is None):
 24        print('ERROR: No assignment has been provided.')
 25        return None
 26
 27    query = backend.parse_assignment_query(assignment)
 28    if (query is None):
 29        print('ERROR: Assignment query is malformed.')
 30        return None
 31
 32    return query
 33
 34def check_required_course(
 35        backend: lms.model.backend.APIBackend,
 36        config: lms.model.config.Config,
 37        ) -> typing.Union[lms.model.courses.CourseQuery, None]:
 38    """
 39    Fetch and ensure that a course is provided in the config.
 40    If no course is provided, print a message and return None.
 41    """
 42
 43    course = lms.util.parse.optional_string(config.course)
 44    if (course is None):
 45        print('ERROR: No course has been provided.')
 46        return None
 47
 48    query = backend.parse_course_query(course)
 49    if (query is None):
 50        print('ERROR: Course query is malformed.')
 51        return None
 52
 53    return query
 54
 55def check_required_groupset(
 56        backend: lms.model.backend.APIBackend,
 57        config: lms.model.config.Config,
 58        ) -> typing.Union[lms.model.groupsets.GroupSetQuery, None]:
 59    """
 60    Fetch and ensure that a group set is provided in the config.
 61    If no group set is provided, print a message and return None.
 62    """
 63
 64    groupset = lms.util.parse.optional_string(config.groupset)
 65    if (groupset is None):
 66        print('ERROR: No group set has been provided.')
 67        return None
 68
 69    query = backend.parse_groupset_query(groupset)
 70    if (query is None):
 71        print('ERROR: Group set query is malformed.')
 72        return None
 73
 74    return query
 75
 76def check_required_group(
 77        backend: lms.model.backend.APIBackend,
 78        config: lms.model.config.Config,
 79        ) -> typing.Union[lms.model.groups.GroupQuery, None]:
 80    """
 81    Fetch and ensure that a group is provided in the config.
 82    If no group is provided, print a message and return None.
 83    """
 84
 85    group = lms.util.parse.optional_string(config.group)
 86    if (group is None):
 87        print('ERROR: No group has been provided.')
 88        return None
 89
 90    query = backend.parse_group_query(group)
 91    if (query is None):
 92        print('ERROR: Group query is malformed.')
 93        return None
 94
 95    return query
 96
 97def check_required_user(
 98        backend: lms.model.backend.APIBackend,
 99        config: lms.model.config.Config,
100        ) -> typing.Union[lms.model.users.UserQuery, None]:
101    """
102    Fetch and ensure that a user is provided in the config.
103    If no user is provided, print a message and return None.
104    """
105
106    user = lms.util.parse.optional_string(config.user)
107    if (user is None):
108        print('ERROR: No user has been provided.')
109        return None
110
111    query = backend.parse_user_query(user)
112    if (query is None):
113        print('ERROR: User query is malformed.')
114        return None
115
116    return query
117
118def strict_check(
119        config: lms.model.config.Config,
120        has_error: bool,
121        message: str,
122        exit_code: int = DEFAULT_STRICT_EXIT_CODE,
123        ) -> int:
124    """
125    Check if strict mode is enabled and if the operation encountered an error.
126    If strict mode is triggered, print an error message and return a suggested exit status.
127    Otherwise, return 0.
128    """
129
130    if (not config.strict):
131        return 0
132
133    if (has_error):
134        print(f"ERROR (Strict Mode): {message}")
135        return exit_code
136
137    return 0
DEFAULT_STRICT_EXIT_CODE: int = 101
def check_required_assignment( backend: lms.model.backend.APIBackend, config: lms.model.config.Config) -> Optional[lms.model.assignments.AssignmentQuery]:
14def check_required_assignment(
15        backend: lms.model.backend.APIBackend,
16        config: lms.model.config.Config,
17        ) -> typing.Union[lms.model.assignments.AssignmentQuery, None]:
18    """
19    Fetch and ensure that an assignment is provided in the config.
20    If no assignment is provided, print a message and return None.
21    """
22
23    assignment = lms.util.parse.optional_string(config.assignment)
24    if (assignment is None):
25        print('ERROR: No assignment has been provided.')
26        return None
27
28    query = backend.parse_assignment_query(assignment)
29    if (query is None):
30        print('ERROR: Assignment query is malformed.')
31        return None
32
33    return query

Fetch and ensure that an assignment is provided in the config. If no assignment is provided, print a message and return None.

def check_required_course( backend: lms.model.backend.APIBackend, config: lms.model.config.Config) -> Optional[lms.model.courses.CourseQuery]:
35def check_required_course(
36        backend: lms.model.backend.APIBackend,
37        config: lms.model.config.Config,
38        ) -> typing.Union[lms.model.courses.CourseQuery, None]:
39    """
40    Fetch and ensure that a course is provided in the config.
41    If no course is provided, print a message and return None.
42    """
43
44    course = lms.util.parse.optional_string(config.course)
45    if (course is None):
46        print('ERROR: No course has been provided.')
47        return None
48
49    query = backend.parse_course_query(course)
50    if (query is None):
51        print('ERROR: Course query is malformed.')
52        return None
53
54    return query

Fetch and ensure that a course is provided in the config. If no course is provided, print a message and return None.

def check_required_groupset( backend: lms.model.backend.APIBackend, config: lms.model.config.Config) -> Optional[lms.model.groupsets.GroupSetQuery]:
56def check_required_groupset(
57        backend: lms.model.backend.APIBackend,
58        config: lms.model.config.Config,
59        ) -> typing.Union[lms.model.groupsets.GroupSetQuery, None]:
60    """
61    Fetch and ensure that a group set is provided in the config.
62    If no group set is provided, print a message and return None.
63    """
64
65    groupset = lms.util.parse.optional_string(config.groupset)
66    if (groupset is None):
67        print('ERROR: No group set has been provided.')
68        return None
69
70    query = backend.parse_groupset_query(groupset)
71    if (query is None):
72        print('ERROR: Group set query is malformed.')
73        return None
74
75    return query

Fetch and ensure that a group set is provided in the config. If no group set is provided, print a message and return None.

def check_required_group( backend: lms.model.backend.APIBackend, config: lms.model.config.Config) -> Optional[lms.model.groups.GroupQuery]:
77def check_required_group(
78        backend: lms.model.backend.APIBackend,
79        config: lms.model.config.Config,
80        ) -> typing.Union[lms.model.groups.GroupQuery, None]:
81    """
82    Fetch and ensure that a group is provided in the config.
83    If no group is provided, print a message and return None.
84    """
85
86    group = lms.util.parse.optional_string(config.group)
87    if (group is None):
88        print('ERROR: No group has been provided.')
89        return None
90
91    query = backend.parse_group_query(group)
92    if (query is None):
93        print('ERROR: Group query is malformed.')
94        return None
95
96    return query

Fetch and ensure that a group is provided in the config. If no group is provided, print a message and return None.

def check_required_user( backend: lms.model.backend.APIBackend, config: lms.model.config.Config) -> Optional[lms.model.users.UserQuery]:
 98def check_required_user(
 99        backend: lms.model.backend.APIBackend,
100        config: lms.model.config.Config,
101        ) -> typing.Union[lms.model.users.UserQuery, None]:
102    """
103    Fetch and ensure that a user is provided in the config.
104    If no user is provided, print a message and return None.
105    """
106
107    user = lms.util.parse.optional_string(config.user)
108    if (user is None):
109        print('ERROR: No user has been provided.')
110        return None
111
112    query = backend.parse_user_query(user)
113    if (query is None):
114        print('ERROR: User query is malformed.')
115        return None
116
117    return query

Fetch and ensure that a user is provided in the config. If no user is provided, print a message and return None.

def strict_check( config: lms.model.config.Config, has_error: bool, message: str, exit_code: int = 101) -> int:
119def strict_check(
120        config: lms.model.config.Config,
121        has_error: bool,
122        message: str,
123        exit_code: int = DEFAULT_STRICT_EXIT_CODE,
124        ) -> int:
125    """
126    Check if strict mode is enabled and if the operation encountered an error.
127    If strict mode is triggered, print an error message and return a suggested exit status.
128    Otherwise, return 0.
129    """
130
131    if (not config.strict):
132        return 0
133
134    if (has_error):
135        print(f"ERROR (Strict Mode): {message}")
136        return exit_code
137
138    return 0

Check if strict mode is enabled and if the operation encountered an error. If strict mode is triggered, print an error message and return a suggested exit status. Otherwise, return 0.