Design Pattern Examples
Overview of object-oriented design patterns
mediator_class.py
Go to the documentation of this file.
8
9
10from .mediator_user_classes import UserList
11from .mediator_group_classes import GroupList
12
13
14
21
22
23 def __init__(self) -> None:
24 self.Users = UserList()
26
27
31
32
33
35
36
37
40
41
42 def __init__(self) -> None:
44
45
49
50
51
56 def AddUser(self, name : str) -> None:
57 self._userGroupsContainer.Users.AddUser(name)
58
59
60
68 def RemoveUser(self, name : str) -> None:
70 self._userGroupsContainer.Users.RemoveUser(name)
71
72
73
78 def AddGroup(self, name : str) -> None:
79 self._userGroupsContainer.Groups.AddGroup(name);
80
81
82
87 def RemoveGroup(self, name : str) -> None:
88 self._userGroupsContainer.Groups.RemoveGroup(name);
89
90
91
101 def AddUserToGroup(self, userName : str, groupName : str) -> None:
102 # As mediator, we must verify the user exists because the group
103 # has no way to do this (groups have no knowledge of how users
104 # are stored, by design).
105 if not self._userGroupsContainer.Users.FindUser(userName):
106 message = "User '{0}' does not exist. Cannot add to group '{1}'!".format(userName, groupName)
107 raise ValueError(message)
108
109 foundGroup = self._userGroupsContainer.Groups.FindGroup(groupName)
110 if not foundGroup:
111 message = "Cannot add user '{0}' to group '{1}' as that group does not exist!".format(userName, groupName)
112 raise ValueError(message)
113
114 foundGroup.AddUser(userName)
115
116
117
126 def RemoveUserFromGroup(self, userName : str, groupName : str) -> None:
127 # As mediator, we must verify the user exists because the group
128 # has no way to do this (groups have no knowledge of how users
129 # are stored, by design).
130 if not self._userGroupsContainer.Users.FindUser(userName):
131 message = "User '{0}' does not exist. Cannot remove from group '{1}'!".format(userName, groupName)
132 raise ValueError(message)
133
134 foundGroup = self._userGroupsContainer.Groups.FindGroup(groupName)
135 if not foundGroup:
136 message = "Cannot remove user '{0}' from group '{1}' as that group does not exist!".format(userName, groupName)
137 raise ValueError(message)
138
139 foundGroup.RemoveUser(userName);
140
141
142
149 def RemoveUserFromAllGroups(self, userName : str) -> None:
150 # As mediator, we must verify the user exists because the group
151 # has no way to do this (groups have no knowledge of how users
152 # are stored, by design).
153 if not self._userGroupsContainer.Users.FindUser(userName):
154 message = "User '{0}' does not exist. Cannot remove from all groups'!".format(userName)
155 raise ValueError(message)
156
157 for groupName in self._userGroupsContainer.Groups.GroupNames:
158 group = self._userGroupsContainer.Groups.FindGroup(groupName)
159 if group:
160 if group.ContainsUser(userName):
161 group.RemoveUser(userName)
162
163
164
175 def IsUserInGroup(self, userName : str, groupName : str) -> bool:
176 userInGroup = False
177
178 if not self._userGroupsContainer.Users.FindUser(userName):
179 message = "User '{0}' does not exist. Cannot determine if user is in group '{1}'!".format(userName, groupName)
180 raise ValueError(message)
181
182 foundGroup = self._userGroupsContainer.Groups.FindGroup(groupName)
183 if not foundGroup:
184 message = "Cannot determine if user '{0}' is in group '{1}' as that group does not exist!".format(userName, groupName)
185 raise ValueError(message)
186
187 userInGroup = foundGroup.ContainsUser(userName)
188
189 return userInGroup
190
191
192
202 def GetGroupsWithUser(self, userName : str) -> list[str]:
203 if not self._userGroupsContainer.Users.FindUser(userName):
204 message = "User '{0}' does not exist. Cannot get groups container user!".format(userName)
205 raise ValueError(message)
206
207 groupNames = []
208 for groupName in self._userGroupsContainer.Groups.GroupNames:
209 group = self._userGroupsContainer.Groups.FindGroup(groupName)
210 if group:
211 if group.ContainsUser(userName):
212 groupNames.append(groupName)
213
214 return groupNames
215
216
217
227 def GetUsersInGroup(self, groupName : str) -> list[str]:
228 foundGroup = self._userGroupsContainer.Groups.FindGroup(groupName)
229 if not foundGroup:
230 message = "Cannot determine get users in group '{0}' as that group does not exist!".format(groupName)
231 raise ValueError(message)
232
233 return foundGroup.Users
234
235
236
240 def GetAllGroups(self) -> list[str]:
241 return self._userGroupsContainer.Groups.GroupNames
242
243
244
248 def GetAllUsers(self) -> list[str]:
249 return self._userGroupsContainer.Users.UserNames
Represents the mediator between caller, users, and groups.
None AddUser(self, str name)
Add a user to the list of known users.
list[str] GetUsersInGroup(self, str groupName)
Retrieve a list of users in the specified group.
None AddUserToGroup(self, str userName, str groupName)
Add the specified user to the specified group.
list[str] GetGroupsWithUser(self, str userName)
Retrieve a list of all groups that contain the specified user.
bool IsUserInGroup(self, str userName, str groupName)
Determine if the specified user is in the specified group.
list[str] GetAllGroups(self)
Retrieve a list of all known groups.
list[str] GetAllUsers(self)
Retrieve a list of all known users.
None RemoveUser(self, str name)
Removes the specified user from the list of known users, if the user exists.
None RemoveGroup(self, str name)
Remove the specified group from the list of known groups if the group exists.
None AddGroup(self, str name)
Add a group to the list of known groups.
None RemoveUserFromAllGroups(self, str userName)
Remove the specified user from all existing groups.
None RemoveUserFromGroup(self, str userName, str groupName)
Remove the specified user from the specified group.