lms.cli.common

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

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

def check_required_user( backend: lms.model.backend.APIBackend, config: Dict[str, Any]) -> Optional[lms.model.users.UserQuery]:
117def check_required_user(
118        backend: lms.model.backend.APIBackend,
119        config: typing.Dict[str, typing.Any],
120        ) -> typing.Union[lms.model.users.UserQuery, None]:
121    """
122    Fetch and ensure that a user is provided in the config.
123    If no user is provided, print a message and return None.
124    """
125
126    user = lms.util.parse.optional_string(config.get('user', None))
127    if (user is None):
128        print('ERROR: No user has been provided.')
129        return None
130
131    query = backend.parse_user_query(user)
132    if (query is None):
133        print('ERROR: User query is malformed.')
134        return None
135
136    return query

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

DEFAULT_STRICT_EXIT_CODE: int = 101
def strict_check(strict: bool, has_error: bool, message: str, exit_code: int = 101) -> int:
140def strict_check(
141        strict: bool,
142        has_error: bool,
143        message: str,
144        exit_code: int = DEFAULT_STRICT_EXIT_CODE,
145        ) -> int:
146    """
147    Check if strict mode is enabled and if the operation encountered an error.
148    If strict mode is triggered, print an error message and return a suggested exit status.
149    Otherwise, return 0.
150    """
151
152    if (not strict):
153        return 0
154
155    if (has_error):
156        print(f"ERROR (Strict Mode): {message}")
157        return exit_code
158
159    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.