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

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]:
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

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]:
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

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]:
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

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: Dict[str, Any]) -> Optional[lms.model.users.UserQuery]:
 95def check_required_user(
 96        backend: lms.model.backend.APIBackend,
 97        config: typing.Dict[str, typing.Any],
 98        ) -> typing.Union[lms.model.users.UserQuery, None]:
 99    """
100    Fetch and ensure that a user is provided in the config.
101    If no user is provided, print a message and return None.
102    """
103
104    user = lms.util.parse.optional_string(config.get('user', None))
105    if (user is None):
106        print('ERROR: No user has been provided.')
107        return None
108
109    query = backend.parse_user_query(user)
110    if (query is None):
111        print('ERROR: User query is malformed.')
112        return None
113
114    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:
118def strict_check(
119        strict: bool,
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 strict):
131        return 0
132
133    if (has_error):
134        print(f"ERROR (Strict Mode): {message}")
135        return exit_code
136
137    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.