changeset 4842:654fdbbf4eb5

Groups2009: _contains and _iter were removed. A bug fixed, when not defined group was mentioned in some group definition contains and iter yield exception. Tests were added for this case.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Fri, 10 Jul 2009 14:32:05 +0200
parents 14c74ab59b45
children d8093123a1bd
files MoinMoin/datastruct/backends/__init__.py MoinMoin/datastruct/backends/_tests/__init__.py
diffstat 2 files changed, 26 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/datastruct/backends/__init__.py	Thu Jul 09 13:22:18 2009 +0200
+++ b/MoinMoin/datastruct/backends/__init__.py	Fri Jul 10 14:32:05 2009 +0200
@@ -45,7 +45,7 @@
 
         return members, member_groups
 
-    def _contains(self, member, processed_groups):
+    def __contains__(self, member, processed_groups=None):
         """
         First check if <member> is part of this group and then check
         for every subgroup in this group.
@@ -56,6 +56,10 @@
         @param member: member name [unicode]
         @param processed_groups: groups which were checked for containment before [set]
         """
+
+        if not processed_groups:
+            processed_groups = set()
+
         processed_groups.add(self.name)
 
         if member in self.members or member in self.member_groups:
@@ -63,18 +67,12 @@
         else:
             groups = self.request.groups
             for group_name in self.member_groups:
-                if group_name not in processed_groups and groups[group_name]._contains(member, processed_groups):
+                if group_name not in processed_groups and group_name in groups and groups[group_name].__contains__(member, processed_groups):
                     return True
 
         return False
 
-    def __contains__(self, member):
-        """
-        Check if <member> is defined in this group. Checks also for subgroups.
-        """
-        return self._contains(member, set())
-
-    def _iter(self, yielded_members, processed_groups):
+    def __iter__(self, yielded_members=None, processed_groups=None):
         """
         Iterate first over members of this group, then over subgroups of this group.
 
@@ -86,6 +84,13 @@
         @param yielded_members: members which have been already yielded before [set]
         @param processed_groups: group names which have been iterated before [set]
         """
+
+        if not processed_groups:
+            processed_groups = set()
+
+        if not yielded_members:
+            yielded_members = set()
+
         processed_groups.add(self.name)
 
         for member in self.members:
@@ -96,14 +101,11 @@
         groups = self.request.groups
         for group_name in self.member_groups:
             if group_name not in processed_groups:
-                for member in groups[group_name]._iter(yielded_members, processed_groups):
-                    yield member
-
-    def __iter__(self):
-        """
-        Iterate over members of this group. Iterates also over subgroups if any.
-        """
-        return self._iter(set(), set())
+                if group_name in groups:
+                    for member in groups[group_name].__iter__(yielded_members, processed_groups):
+                        yield member
+                else:
+                    yield group_name
 
     def __repr__(self):
         return "<%s name=%s members=%s member_groups=%s>" % (self.__class__,
--- a/MoinMoin/datastruct/backends/_tests/__init__.py	Thu Jul 09 13:22:18 2009 +0200
+++ b/MoinMoin/datastruct/backends/_tests/__init__.py	Fri Jul 10 14:32:05 2009 +0200
@@ -22,19 +22,21 @@
                    u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
                    u'OtherGroup': [u'SomethingOther'],
                    u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
-                   u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything'],
+                   u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything', u'NotExistingGroup'],
                    u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana'],
-                   u'EmptyGroup': []}
+                   u'EmptyGroup': [],
+                   u'CheckNotExistingGroup': [u'NotExistingGroup']}
 
 
     expanded_groups = {u'EditorGroup': [u'Admin1', u'Admin2', u'John',
                                         u'JoeDoe', u'Editor1'],
                        u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
                        u'OtherGroup': [u'SomethingOther'],
-                       u'RecursiveGroup': [u'Anything', u'Something'],
-                       u'OtherRecursiveGroup': [u'Anything', u'Something'],
+                       u'RecursiveGroup': [u'Anything', u'Something', u'NotExistingGroup'],
+                       u'OtherRecursiveGroup': [u'Anything', u'Something', u'NotExistingGroup'],
                        u'ThirdRecursiveGroup': [u'Banana'],
-                       u'EmptyGroup': []}
+                       u'EmptyGroup': [],
+                       u'CheckNotExistingGroup': [u'NotExistingGroup']}
 
     def test_contains(self):
         """