comparison MoinMoin/datastruct/backends/__init__.py @ 4832:f83b7b0b59c4

Groups2009: groups and dicts were merged inside of the datastruct module.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Fri, 03 Jul 2009 16:36:47 +0100
parents MoinMoin/datastruct/groups/backends/__init__.py@069aac5ce3e4
children 50732c861ac2
comparison
equal deleted inserted replaced
4831:af155b846cea 4832:f83b7b0b59c4
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - base classes for datastruct backends.
4
5 @copyright: 2009 MoinMoin:DmitrijsMilajevs
6 @license: GPL, see COPYING for details
7 """
8
9
10 class GroupDoesNotExistError(Exception):
11 """
12 Raised when a group name is not found in the backend.
13 """
14
15
16 class DictDoesNotExistError(Exception):
17 """
18 Raised when a dict name is not found in the backend.
19 """
20
21
22 class BaseGroup(object):
23
24 def __init__(self, request, name, backend):
25 """
26 Initialize a group.
27
28 @param request
29 @param name: moin group name
30 @backend: backend object which created this object
31
32 """
33 self.request = request
34 self.name = name
35 self._backend = backend
36 self.members, self.member_groups = self._load_group()
37
38 def _load_group(self):
39 """
40 Retrieve group data from the backend and filter it to members and group_members.
41 """
42 members_retrieved = set(self._backend._retrieve_members(self.name))
43
44 member_groups = set(member for member in members_retrieved if self._backend.is_group(member))
45 members = members_retrieved - member_groups
46
47 return members, member_groups
48
49 def _contains(self, member, processed_groups):
50 """
51 First check if <member> is part of this group and then check
52 for every subgroup in this group.
53
54 <processed_groups> is needed to avoid infinite recursion, if
55 groups are defined recursively.
56
57 @param member: member name [unicode]
58 @param processed_groups: groups which were checked for containment before [set]
59 """
60 processed_groups.add(self.name)
61
62 if member in self.members or member in self.member_groups:
63 return True
64 else:
65 groups = self.request.groups
66 for group_name in self.member_groups:
67 if group_name not in processed_groups and groups[group_name]._contains(member, processed_groups):
68 return True
69
70 return False
71
72 def __contains__(self, member):
73 """
74 Check if <member> is defined in this group. Checks also for subgroups.
75 """
76 return self._contains(member, set())
77
78 def _iter(self, yielded_members, processed_groups):
79 """
80 Iterate first over members of this group, then over subgroups of this group.
81
82 <yielded_members> and <processed_groups> are needed to avoid infinite recursion.
83 This can happen if there are two groups like these:
84 OneGroup: Something, OtherGroup
85 OtherGroup: OneGroup, SomethingOther
86
87 @param yielded_members: members which have been already yielded before [set]
88 @param processed_groups: group names which have been iterated before [set]
89 """
90 processed_groups.add(self.name)
91
92 for member in self.members:
93 if member not in yielded_members:
94 yielded_members.add(member)
95 yield member
96
97 groups = self.request.groups
98 for group_name in self.member_groups:
99 if group_name not in processed_groups:
100 for member in groups[group_name]._iter(yielded_members, processed_groups):
101 yield member
102
103 def __iter__(self):
104 """
105 Iterate over members of this group. Iterates also over subgroups if any.
106 """
107 return self._iter(set(), set())
108
109 def __repr__(self):
110 return "<%s name=%s members=%s member_groups=%s>" % (self.__class__,
111 self.name,
112 self.members,
113 self.member_groups)
114
115
116 class BaseGroupBackend(object):
117
118 def __init__(self, request):
119 self.request = request
120 self.page_group_regex = request.cfg.cache.page_group_regexact
121
122 def is_group(self, member):
123 return self.page_group_regex.match(member)
124
125 def __contains__(self, group_name):
126 """
127 Check if a group called <group_name> is available in this backend.
128 """
129 raise NotImplementedError()
130
131 def __iter__(self):
132 """
133 Iterate over moin group names of the groups defined in this backend.
134
135 @return: moin group names
136 """
137 raise NotImplementedError()
138
139 def __getitem__(self, group_name):
140 """
141 Get a group by its moin group name.
142 """
143 raise NotImplementedError()
144
145 def __repr__(self):
146 return "<%s groups=%s>" % (self.__class__, list(self))
147
148 def _retrieve_members(self, group_name):
149 raise NotImplementedError()
150
151 def groups_with_member(self, member):
152 """
153 List all group names of groups containing <member>.
154
155 @param member: member name [unicode]
156 @return: list of group names [unicode]
157 """
158 for group_name in self:
159 try:
160 if member in self[group_name]:
161 yield group_name
162 except GroupDoesNotExistError:
163 pass
164
165 def get(self, key, default=None):
166 if key in self:
167 return self[key]
168 else:
169 return default
170
171
172 class BaseDict(object):
173
174 def __init__(self, request, name, backend):
175 """
176 Initialize a dict.
177
178 @param request
179 @param name: moin dict name
180 @backend: backend object which created this object
181
182 """
183 self.request = request
184 self.name = name
185 self._backend = backend
186 self._dict = self._load_dict()
187
188 def __iter__(self):
189 return self._dict.__iter__()
190
191 def __len__(self):
192 return self._dict.__len__()
193
194 def __getitem__(self, key):
195 return self._dict[key]
196
197 def get(self, key, default=None):
198 return self._dict.get(key, default)
199
200 def _load_dict(self):
201 """
202 Retrieve dict data from the backend.
203 """
204 return self._backend._retrieve_members(self.name)
205
206 def __repr__(self):
207 return "<%r name=%r items=%r>" % (self.__class__, self.name, self._dict.items())
208
209
210 class BaseDictBackend(object):
211
212 def __init__(self, request):
213 self.request = request
214 self.page_dict_regex = request.cfg.cache.page_dict_regexact
215
216 def is_dict_name(self, name):
217 return self.page_dict_regex.match(name)
218
219 def __contains__(self, dict_name):
220 """
221 Check if a dict called <dict_name> is available in this backend.
222 """
223 raise NotImplementedError()
224
225 def __getitem__(self, dict_name):
226 """
227 Get a dict by its moin dict name.
228 """
229 raise NotImplementedError()
230
231 def _retrieve_members(self, dict_name):
232 raise NotImplementedError()
233