changeset 4807:ca8fcf330aa8

Groups2009: Backend's _retrieve_members and is_group methods were introduced. _load_group became method of a BaseGroup to avoid code repetition. Test for empty group was added.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sat, 13 Jun 2009 20:44:06 +0200
parents dcbdf7e5660d
children c54a1ba60873
files MoinMoin/groups/backends/__init__.py MoinMoin/groups/backends/_tests/__init__.py MoinMoin/groups/backends/config_group.py MoinMoin/groups/backends/wiki_group.py
diffstat 4 files changed, 45 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/groups/backends/__init__.py	Sat Jun 13 20:14:27 2009 +0200
+++ b/MoinMoin/groups/backends/__init__.py	Sat Jun 13 20:44:06 2009 +0200
@@ -29,9 +29,21 @@
     def _load_group(self):
         """
         Fill in self.members, self.member_groups with data retrieved from the backend.
-        member_groups are moin group names.
         """
-        raise NotImplementedError()
+
+        request = self.request
+
+        members_final = set()
+        member_groups = set()
+
+        for member in self._backend._retrieve_members(self.name):
+            if self._backend.is_group(member):
+                member_groups.add(member)
+            else:
+                members_final.add(member)
+
+        self.members = members_final
+        self.member_groups = member_groups
 
     def _contains(self, member, processed_groups):
         """
@@ -106,6 +118,9 @@
         self.request = request
         self.page_group_regex = request.cfg.cache.page_group_regexact
 
+    def is_group(self, member):
+        return self.page_group_regex.match(member)
+
     def __contains__(self, group_name):
         """
         Check if a group called <group_name> is available in this backend.
@@ -129,3 +144,6 @@
     def __repr__(self):
         return "<%s groups=%s>" % (self.__class__, [b for b in self])
 
+    def _retrieve_members(self, group_name):
+        raise NotImplementedError()
+
--- a/MoinMoin/groups/backends/_tests/__init__.py	Sat Jun 13 20:14:27 2009 +0200
+++ b/MoinMoin/groups/backends/_tests/__init__.py	Sat Jun 13 20:44:06 2009 +0200
@@ -22,7 +22,9 @@
                    u'OtherGroup': [u'SomethingOther'],
                    u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
                    u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything'],
-                   u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana']}
+                   u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana'],
+                   u'EmptyGroup': []}
+
 
     expanded_groups = {u'EditorGroup': [u'Admin1', u'Admin2', u'John',
                                         u'JoeDoe', u'Editor1'],
@@ -30,7 +32,8 @@
                        u'OtherGroup': [u'SomethingOther'],
                        u'RecursiveGroup': [u'Anything', u'Something'],
                        u'OtherRecursiveGroup': [u'Anything', u'Something'],
-                       u'ThirdRecursiveGroup': [u'Banana']}
+                       u'ThirdRecursiveGroup': [u'Banana'],
+                       u'EmptyGroup': []}
 
     def test_contains(self):
         """
--- a/MoinMoin/groups/backends/config_group.py	Sat Jun 13 20:14:27 2009 +0200
+++ b/MoinMoin/groups/backends/config_group.py	Sat Jun 13 20:44:06 2009 +0200
@@ -12,21 +12,7 @@
 
 
 class Group(BaseGroup):
-
-    def _load_group(self):
-        request = self.request
-
-        members_final = set()
-        member_groups = set()
-
-        for member in self._backend._groups[self.name]:
-            if member in self._backend._groups:
-                member_groups.add(member)
-            else:
-                members_final.add(member)
-
-        self.members = members_final
-        self.member_groups = member_groups
+    pass
 
 
 class Backend(BaseBackend):
@@ -48,3 +34,7 @@
 
     def __getitem__(self, group_name):
         return Group(request=self.request, name=group_name, backend=self)
+
+    def _retrieve_members(self, group_name):
+        return self._groups[group_name]
+
--- a/MoinMoin/groups/backends/wiki_group.py	Sat Jun 13 20:14:27 2009 +0200
+++ b/MoinMoin/groups/backends/wiki_group.py	Sat Jun 13 20:44:06 2009 +0200
@@ -22,11 +22,6 @@
 
 class Group(BaseGroup):
 
-    # * Member - ignore all but first level list items, strip
-    # whitespace, strip free links markup. This is used for parsing
-    # pages in order to find group page members
-    group_page_parse_re = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE)
-
     def _load_group(self):
         request = self.request
         group_name = self.name
@@ -47,39 +42,32 @@
                     raise caching.CacheError
             except caching.CacheError:
                 # either cache does not exist, is erroneous or not uptodate: recreate it
-                text = page.get_raw_body()
-                self.members, self.member_groups = self._parse_page(text)
+
+                super(Group, self)._load_group()
+
                 cache.update((self.members, self. member_groups))
         else:
             raise KeyError("There is no such group page %s" % group_name)
 
-    def _parse_page(self, text):
-        """
-        Parse <text> and return members and groups defined in the <text>
-        """
-        groups = self.request.groups
-
-        text_members = (match.group('member') for match in self.group_page_parse_re.finditer(text))
-        members_final = set()
-        member_groups = set()
-
-        for member in text_members:
-            if self._backend.page_group_regex.match(member):
-                member_groups.add(member)
-            else:
-                members_final.add(member)
-
-        return members_final, member_groups
-
 
 class Backend(BaseBackend):
 
     def __contains__(self, group_name):
-        return self.page_group_regex.match(group_name) and Page(self.request, group_name).exists()
+        return self.is_group(group_name) and Page(self.request, group_name).exists()
 
     def __iter__(self):
-        return self.request.rootpage.getPageList(user='', filter=self.page_group_regex.search)
+        return iter(self.request.rootpage.getPageList(user='', filter=self.page_group_regex.search))
 
     def __getitem__(self, group_name):
         return Group(request=self.request, name=group_name, backend=self)
 
+    # * Member - ignore all but first level list items, strip
+    # whitespace, strip free links markup. This is used for parsing
+    # pages in order to find group page members.
+    _group_page_parse_regex = re.compile(ur'^ \* +(?:\[\[)?(?P<member>.+?)(?:\]\])? *$', re.MULTILINE | re.UNICODE)
+
+    def _retrieve_members(self, group_name):
+        page = Page(self.request, group_name)
+        text = page.get_raw_body()
+        return [match.group('member') for match in self._group_page_parse_regex.finditer(text)]
+