7from .mediator_class
import UserGroupMediator
17 return ", ".join(items)
25 mediator.AddUser(
"Stephen");
26 mediator.AddUser(
"Gladys");
27 mediator.AddUser(
"Marvin");
28 mediator.AddUser(
"Arthur");
39 mediator.AddGroup(
"admins");
40 mediator.AddGroup(
"Users");
41 mediator.AddGroup(
"Power Users");
44 mediator.AddUserToGroup(
"Marvin",
"admins");
45 mediator.AddUserToGroup(
"Arthur",
"admins");
46 mediator.AddUserToGroup(
"Stephen",
"Users");
47 mediator.AddUserToGroup(
"Gladys",
"Users");
48 mediator.AddUserToGroup(
"Arthur",
"Power Users");
49 mediator.AddUserToGroup(
"Marvin",
"Power Users");
66 print(
"Mediator Exercise")
75 print(
" Operation 1: Show all groups")
76 print(
" All groups: {0}".format(
_ListToString(mediator.GetAllGroups())))
80 print(
" Operation 2: Show all users");
81 print(
" All users : {0}".format(
_ListToString(mediator.GetAllUsers())))
85 print(
" Operation 3: Determine if a user is a member of a specific group.");
88 print(
" Is user '{0}' in the '{1}' group?".format(userName, groupName), end=
"")
89 print(
" {0}".format(
"Yes" if mediator.IsUserInGroup(userName, groupName)
else "No"))
93 print(
" Operation 4: Show all users in a specific group.");
95 userNames = mediator.GetUsersInGroup(groupName);
96 print(
" All users in '{0}' group: {1}".format(groupName,
_ListToString(userNames)))
100 print(
" Operation 5: Show all groups containing a specific user.");
102 groupNames = mediator.GetGroupsWithUser(userName);
103 print(
" All groups with user '{0}': {1}".format(userName,
_ListToString(groupNames)))
107 print(
" Operation 6: Remove a user from a group.");
109 groupName =
"Power Users";
110 mediator.RemoveUserFromGroup(userName, groupName);
111 print(
" Removed user '{0}' from group '{1}'".format(userName, groupName))
112 groupNames = mediator.GetGroupsWithUser(userName);
113 print(
" All groups with user '{0}': {1}".format(userName,
_ListToString(groupNames)))
117 print(
" Operation 7: Add a user to a group.");
119 print(
" Adding user '{0}' to group '{1}'.".format(userName, groupName))
120 mediator.AddUserToGroup(userName, groupName);
121 groupNames = mediator.GetGroupsWithUser(userName);
122 print(
" All groups with user '{0}': {1}".format(userName,
_ListToString(groupNames)))
126 print(
" Operation 8: Remove a user from all groups.");
128 groupNames = mediator.GetGroupsWithUser(userName);
129 print(
" Removing user '{0}' from all groups.".format(userName))
130 print(
" Start: all groups with user '{0}': {1}".format(userName,
_ListToString(groupNames)))
131 print(
" Removing...");
132 mediator.RemoveUserFromAllGroups(userName);
133 groupNames = mediator.GetGroupsWithUser(userName);
134 print(
" End: all groups with user '{0}': {1}".format(userName,
_ListToString(groupNames)))
138 print(
" Operation 9: Remove a user (also removes the user from all groups).");
140 print(
" Removing user '{0}'.".format(userName))
141 mediator.RemoveUser(userName);
142 print(
" All users : {0}".format(
_ListToString(mediator.GetAllUsers())))
143 groupNames = mediator.GetAllGroups();
144 for name
in groupNames:
145 userNames = mediator.GetUsersInGroup(name);
146 print(
" Users in group '{0}': {1}".format(name,
_ListToString(userNames)))