1
+ """grouper-python.group - functions to interact with group objects.
2
+
3
+ These are "helper" functions that most likely will not be called directly.
4
+ Instead, a GrouperClient class should be created, then from there use that
5
+ GrouperClient's methods to find and create objects, and use those objects' methods.
6
+ These helper functions are used by those objects, but can be called
7
+ directly if needed.
8
+ """
9
+
1
10
from __future__ import annotations
2
11
from typing import TYPE_CHECKING , Any
3
12
4
13
if TYPE_CHECKING : # pragma: no cover
5
14
from .objects .group import CreateGroup , Group
6
- from .objects .client import Client
15
+ from .objects .client import GrouperClient
7
16
from .objects .subject import Subject
8
17
from .objects .exceptions import (
9
18
GrouperGroupNotFoundException ,
15
24
16
25
def find_group_by_name (
17
26
group_name : str ,
18
- client : Client ,
27
+ client : GrouperClient ,
19
28
stem : str | None = None ,
20
29
act_as_subject : Subject | None = None ,
21
30
) -> list [Group ]:
31
+ """Find a group or groups by approximate name.
32
+
33
+ :param group_name: The group name to search for
34
+ :type group_name: str
35
+ :param client: The GrouperClient to use
36
+ :type client: GrouperClient
37
+ :param stem: Optional stem to limit the search to, defaults to None
38
+ :type stem: str | None, optional
39
+ :param act_as_subject: Optional subject to act as, defaults to None
40
+ :type act_as_subject: Subject | None, optional
41
+ :raises GrouperStemNotFoundException: The specified stem cannot be found
42
+ :raises GrouperSuccessException: An otherwise unhandled issue with the result
43
+ :return: List of found groups, will be an empty list if no groups are found
44
+ :rtype: list[Group]
45
+ """
22
46
from .objects .group import Group
23
47
24
48
body = {
@@ -45,10 +69,10 @@ def find_group_by_name(
45
69
raise GrouperStemNotFoundException (str (stem ), r )
46
70
else : # pragma: no cover
47
71
# Some other issue, so pass the failure through
48
- raise
72
+ raise err
49
73
if "groupResults" in r ["WsFindGroupsResults" ]:
50
74
return [
51
- Group . from_results (client , grp )
75
+ Group (client , grp )
52
76
for grp in r ["WsFindGroupsResults" ]["groupResults" ]
53
77
]
54
78
else :
@@ -57,9 +81,20 @@ def find_group_by_name(
57
81
58
82
def create_groups (
59
83
groups : list [CreateGroup ],
60
- client : Client ,
84
+ client : GrouperClient ,
61
85
act_as_subject : Subject | None = None ,
62
86
) -> list [Group ]:
87
+ """Create groups.
88
+
89
+ :param groups: List of groups to create
90
+ :type groups: list[CreateGroup]
91
+ :param client: The GrouperClient to use
92
+ :type client: GrouperClient
93
+ :param act_as_subject: Optional subject to act as, defaults to None
94
+ :type act_as_subject: Subject | None, optional
95
+ :return: Group objects representing the created groups
96
+ :rtype: list[Group]
97
+ """
63
98
from .objects .group import Group
64
99
65
100
groups_to_save = []
@@ -87,16 +122,29 @@ def create_groups(
87
122
act_as_subject = act_as_subject ,
88
123
)
89
124
return [
90
- Group . from_results (client , result ["wsGroup" ])
125
+ Group (client , result ["wsGroup" ])
91
126
for result in r ["WsGroupSaveResults" ]["results" ]
92
127
]
93
128
94
129
95
130
def delete_groups (
96
131
group_names : list [str ],
97
- client : Client ,
132
+ client : GrouperClient ,
98
133
act_as_subject : Subject | None = None ,
99
134
) -> None :
135
+ """Delete the given groups.
136
+
137
+ :param group_names: The names of groups to delete
138
+ :type group_names: list[str]
139
+ :param client: The GrouperClient to use
140
+ :type client: GrouperClient
141
+ :param act_as_subject: Optional subject to act as, defaults to None
142
+ :type act_as_subject: Subject | None, optional
143
+ :raises GrouperPermissionDenied: Permission denied to complete the operation
144
+ :raises GrouperGroupNotFoundException: A group with the given name cannot
145
+ be found
146
+ :raises GrouperSuccessException: An otherwise unhandled issue with the result
147
+ """
100
148
group_lookup = [{"groupName" : group } for group in group_names ]
101
149
body = {
102
150
"WsRestGroupDeleteRequest" : {
@@ -139,10 +187,25 @@ def delete_groups(
139
187
140
188
def get_groups_by_parent (
141
189
parent_name : str ,
142
- client : Client ,
190
+ client : GrouperClient ,
143
191
recursive : bool = False ,
144
192
act_as_subject : Subject | None = None ,
145
193
) -> list [Group ]:
194
+ """Get Groups within the given parent stem.
195
+
196
+ :param parent_name: The parent stem to look in
197
+ :type parent_name: str
198
+ :param client: The GrouperClient to use
199
+ :type client: GrouperClient
200
+ :param recursive: Whether to look recursively through the entire subtree (True),
201
+ or only one level in the given parent (False), defaults to False
202
+ :type recursive: bool, optional
203
+ :param act_as_subject: Optional subject to act as, defaults to None
204
+ :type act_as_subject: Subject | None, optional
205
+ :raises GrouperSuccessException: An otherwise unhandled issue with the result
206
+ :return: The list of Groups found
207
+ :rtype: list[Group]
208
+ """
146
209
from .objects .group import Group
147
210
148
211
body = {
@@ -162,7 +225,7 @@ def get_groups_by_parent(
162
225
)
163
226
if "groupResults" in r ["WsFindGroupsResults" ]:
164
227
return [
165
- Group . from_results (client , grp )
228
+ Group (client , grp )
166
229
for grp in r ["WsFindGroupsResults" ]["groupResults" ]
167
230
]
168
231
else :
@@ -171,9 +234,23 @@ def get_groups_by_parent(
171
234
172
235
def get_group_by_name (
173
236
group_name : str ,
174
- client : Client ,
237
+ client : GrouperClient ,
175
238
act_as_subject : Subject | None = None ,
176
239
) -> Group :
240
+ """Get a group with the given name.
241
+
242
+ :param group_name: The name of the group to get
243
+ :type group_name: str
244
+ :param client: The GrouperClient to use
245
+ :type client: GrouperClient
246
+ :param act_as_subject: Optional subject to act as, defaults to None
247
+ :type act_as_subject: Subject | None, optional
248
+ :raises GrouperGroupNotFoundException: A group with the given name cannot
249
+ be found
250
+ :raises GrouperSuccessException: An otherwise unhandled issue with the result
251
+ :return: The group with the given name
252
+ :rtype: Group
253
+ """
177
254
from .objects .group import Group
178
255
179
256
body = {
@@ -186,4 +263,4 @@ def get_group_by_name(
186
263
r = client ._call_grouper ("/groups" , body , act_as_subject = act_as_subject )
187
264
if "groupResults" not in r ["WsFindGroupsResults" ]:
188
265
raise GrouperGroupNotFoundException (group_name , r )
189
- return Group . from_results (client , r ["WsFindGroupsResults" ]["groupResults" ][0 ])
266
+ return Group (client , r ["WsFindGroupsResults" ]["groupResults" ][0 ])
0 commit comments