comparison MoinMoin/groups/backends/__init__.py @ 4796:7dd5d15de911

Groups2009: BaseGroup and BaseBackend initial import. wiki_group.Group and wiki_group.Backend inherit from those classes. TestWikiGroupNameMapping is added to check group name mapping routines.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Tue, 09 Jun 2009 23:35:01 +0200
parents 3f9147c23d9c
children 4539a7956eac
comparison
equal deleted inserted replaced
4795:64ff4ea0b597 4796:7dd5d15de911
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - base classes for group backends.
4
5 @copyright: 2009 MoinMoin:DmitrijsMilajevs
6 @license: GPL, see COPYING for details
7 """
8
9 class BaseGroup(object):
10
11 def __init__(self, request, name, backend):
12 """
13 Initialize a group.
14
15 @param request
16 @param name: moin group name
17 @backend: backend object which created this object
18
19 """
20 self.request = request
21 self.name = name
22 self.backend = backend
23
24 self.to_backend_name = backend.to_backend_name
25 self.to_group_name = backend.to_group_name
26
27 self._load_group()
28
29 def _load_group(self):
30 """
31 Fill in self.members, self.member_groups with data retrieved from the backend.
32 member_groups are moin group names.
33 """
34 raise NotImplementedError()
35
36 def _contains(self, member, processed_groups):
37 """
38 First check if <member> is part of this group and then check
39 for every subgroup in this group.
40
41 <processed_groups> is needed to avoid infinite recursion, if
42 groups are defined recursively.
43
44 @param member: member name [unicode]
45 @param processed_groups: groups which were checked for containment before [set]
46 """
47 processed_groups.add(self.name)
48
49 if member in self.members:
50 return True
51 else:
52 groups = self.request.groups
53 for group_name in self.member_groups:
54 if group_name not in processed_groups and groups[group_name]._contains(member, processed_groups):
55 return True
56
57 return False
58
59 def __contains__(self, member):
60 """
61 Check if <member> is defined in this group. Checks also for subgroups.
62 """
63 return self._contains(member, set())
64
65 def _iter(self, yielded_members, processed_groups):
66 """
67 Iterate first over members of this group, then over subgroups of this group.
68
69 <yielded_members> and <processed_groups> are needed to avoid infinite recursion.
70 This can happen if there are two groups like these:
71 OneGroup: Something, OtherGroup
72 OtherGroup: OneGroup, SomethingOther
73
74 @param yielded_members: members which have been already yielded before [set]
75 @param processed_groups: group names which have been iterated before [set]
76 """
77 processed_groups.add(self.name)
78
79 for member in self.members:
80 if member not in yielded_members:
81 yield member
82 yielded_members.add(member)
83
84 groups = self.request.groups
85 for group_name in self.member_groups:
86 if group_name not in processed_groups:
87 for member in groups[group_name]._iter(yielded_members, processed_groups):
88 if member not in yielded_members:
89 yield member
90 yielded_members.add(member)
91
92
93 def __iter__(self):
94 """
95 Iterate over members of this group. Iterates also over subgroups if any.
96 """
97 return self._iter(set(), set())
98
99 def __repr__(self):
100 return "<%s name=%s members=%s member_groups=%s>" % (self.__class__,
101 self.name,
102 self.members,
103 self.member_groups)
104
105
106 class BaseBackend(object):
107
108 def __init__(self, request):
109 self.request = request
110
111 def __contains__(self, group_name):
112 """
113 Check if a group called <group_name> is available in this backend.
114 """
115 return self._exists_group(group_name)
116
117 def __iter__(self):
118 """
119 Iterate over group names of the groups defined in this backend.
120
121 @return: moin group names
122 """
123 backend_group_names = self._get_group_names()
124 return (self.to_group_name(backend_group_name) for backend_group_name in backend_group_names)
125
126 def __getitem__(self, group_name):
127 """
128 Get a group by its moin group name.
129 """
130 return self._get_group(group_name)
131
132 # These methods should be overridden in children classes.
133 def to_backend_name(self, group_name):
134 """
135 A function mapping the moin group name to the backend group name.
136 """
137 return group_name
138
139 def to_group_name(self, backend_group_name):
140 """
141 A function mapping the backend group name to the moin group name.
142 """
143 return backend_group_name
144
145 def _exists_group(self, group_name):
146 """
147 Check if the group <group_name> is defined in the backend.
148 """
149 raise NotImplementedError()
150
151 def _get_group(self, group_name):
152 """
153 Retrieve a <group_name> group members from the backend.
154
155 @return: a group with retrieved members. [Group]
156 """
157 raise NotImplementedError()
158
159 def _get_group_names(self):
160 """
161 Retrieve moin group names stored in the backend.
162 """
163 raise NotImplementedError()
164