changeset 4852:65bef5bcec41

Groups2009: Experimental config_lazy_groups backend was added. GreedyGroup class was introduced for groups which store their members internally. Some header fixes and copyright notice clean up.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Fri, 17 Jul 2009 08:38:02 +0200
parents 866fe615893f
children be20907b4747
files MoinMoin/datastruct/backends/__init__.py MoinMoin/datastruct/backends/_tests/test_composite_groups.py MoinMoin/datastruct/backends/_tests/test_config_groups.py MoinMoin/datastruct/backends/_tests/test_lazy_config_groups.py MoinMoin/datastruct/backends/config_dicts.py MoinMoin/datastruct/backends/config_groups.py MoinMoin/datastruct/backends/config_lazy_groups.py MoinMoin/datastruct/backends/wiki_groups.py
diffstat 8 files changed, 190 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/datastruct/backends/__init__.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/__init__.py	Fri Jul 17 08:38:02 2009 +0200
@@ -21,17 +21,29 @@
 
 class BaseGroup(object):
 
-    def __init__(self, request, name, backend):
-        """
-        Initialize a group.
+        def __init__(self, request, name, backend):
+            """
+            Initialize a group.
 
-        @param request
-        @param name: moin group name
-        @backend: backend object which created this object
-        """
-        self.request = request
-        self.name = name
-        self._backend = backend
+            @param request
+            @param name: moin group name
+            @backend: backend object which created this object
+            """
+            self.request = request
+            self.name = name
+            self._backend = backend
+
+        def __contains__(self, member, processed_groups=None):
+            raise NotImplementedError()
+
+        def __iter__(self, yielded_members=None, processed_groups=None):
+            raise NotImplementedError()
+
+
+class GreedyGroup(BaseGroup):
+
+    def __init__(self, request, name, backend):
+        super(GreedyGroup, self).__init__(request, name, backend)
         self.members, self.member_groups = self._load_group()
 
     def _load_group(self):
@@ -40,7 +52,7 @@
         """
         members_retrieved = set(self._backend._retrieve_members(self.name))
 
-        member_groups = set(member for member in members_retrieved if self._backend.is_group(member))
+        member_groups = set(member for member in members_retrieved if self._backend.is_group_name(member))
         members = members_retrieved - member_groups
 
         return members, member_groups
@@ -120,7 +132,7 @@
         self.request = request
         self.page_group_regex = request.cfg.cache.page_group_regexact
 
-    def is_group(self, member):
+    def is_group_name(self, member):
         return self.page_group_regex.match(member)
 
     def __contains__(self, group_name):
--- a/MoinMoin/datastruct/backends/_tests/test_composite_groups.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/_tests/test_composite_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -4,7 +4,6 @@
 MoinMoin.datastruct.backends.composite_groups test
 
 @copyright: 2009 MoinMoin:DmitrijsMilajevs
-            2008 MoinMoin: MelitaMihaljevic
 @license: GPL, see COPYING for details
 """
 
@@ -75,7 +74,7 @@
         assert u'TheHacker' not in admin_group
 
     def test_iter(self):
-        all_group_names = [group_name for group_name in self.groups]
+        all_group_names = list(self.groups)
 
         assert 5 == len(all_group_names)
         # There are no duplicates
--- a/MoinMoin/datastruct/backends/_tests/test_config_groups.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/_tests/test_config_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -1,13 +1,9 @@
 # -*- coding: iso-8859-1 -*-
 """
-    MoinMoin - MoinMoin.backends.wiki_group tests
+    MoinMoin - MoinMoin.backends.config_groups tests
 
-    @copyright: 2003-2004 by Juergen Hermann <jh@web.de>,
-                2007 by MoinMoin:ThomasWaldmann
-                2008 by MoinMoin:MelitaMihaljevic
-                2009 by MoinMoin:DmitrijsMilajevs
+    @copyright: 2009 by MoinMoin:DmitrijsMilajevs
     @license: GNU GPL, see COPYING for details.
-
 """
 
 from MoinMoin.datastruct.backends._tests import GroupsBackendTest
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/datastruct/backends/_tests/test_lazy_config_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -0,0 +1,56 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - MoinMoin.backends.config_lazy_groups tests
+
+    @copyright: 2009 by MoinMoin:DmitrijsMilajevs
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.datastruct.backends._tests.test_config_groups import TestConfigGroupsBackend
+from MoinMoin.datastruct.backends._tests import GroupsBackendTest
+from MoinMoin.datastruct.backends.config_lazy_groups import LazyConfigGroups
+from MoinMoin.datastruct import ConfigGroups, CompositeGroups, GroupDoesNotExistError
+from MoinMoin._tests import wikiconfig
+
+
+class TestLazyConfigGroups(TestConfigGroupsBackend):
+
+    test_groups = {u'EditorGroup': [u'John', u'JoeDoe', u'Editor1'],
+                   u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
+                   u'OtherGroup': [u'SomethingOther'],
+                   u'EmptyGroup': [],
+                   u'CheckNotExistingGroup': [u'NotExistingGroup']}
+
+    expanded_groups = test_groups
+
+    class Config(wikiconfig.Config):
+
+        def group_manager_init(self, request):
+            groups = TestLazyConfigGroups.test_groups
+            return LazyConfigGroups(request, groups)
+
+    def test_contains_group(self):
+        pass
+
+
+class TestCompositeAndLazyConfigGroups(GroupsBackendTest):
+
+    class Config(wikiconfig.Config):
+
+        def group_manager_init(self, request):
+            config_groups = {u'EditorGroup': [u'AdminGroup', u'John', u'JoeDoe', u'Editor1', u'John'],
+                             u'RecursiveGroup': [u'Something', u'OtherRecursiveGroup'],
+                             u'OtherRecursiveGroup': [u'RecursiveGroup', u'Anything', u'NotExistingGroup'],
+                             u'ThirdRecursiveGroup': [u'ThirdRecursiveGroup', u'Banana'],
+                             u'CheckNotExistingGroup': [u'NotExistingGroup']}
+
+            lazy_groups = {u'AdminGroup': [u'Admin1', u'Admin2', u'John'],
+                           u'OtherGroup': [u'SomethingOther'],
+                           u'EmptyGroup': []}
+
+            return CompositeGroups(request,
+                                   ConfigGroups(request, config_groups),
+                                   LazyConfigGroups(request, lazy_groups))
+
+
+coverage_modules = ['MoinMoin.datastruct.backends.config_lazy_groups']
--- a/MoinMoin/datastruct/backends/config_dicts.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/config_dicts.py	Fri Jul 17 08:38:02 2009 +0200
@@ -23,7 +23,7 @@
         self._dicts = dicts
 
     def __contains__(self, dict_name):
-        return self.page_dict_regex.match(dict_name) and dict_name in self._dicts
+        return self.is_dict_name(dict_name) and dict_name in self._dicts
 
     def __iter__(self):
         return self._dicts.iterkeys()
--- a/MoinMoin/datastruct/backends/config_groups.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/config_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -8,10 +8,10 @@
 @license: GPL, see COPYING for details
 """
 
-from MoinMoin.datastruct.backends import BaseGroup, BaseGroupsBackend, GroupDoesNotExistError
+from MoinMoin.datastruct.backends import GreedyGroup, BaseGroupsBackend, GroupDoesNotExistError
 
 
-class Group(BaseGroup):
+class ConfigGroup(GreedyGroup):
     pass
 
 
@@ -27,13 +27,13 @@
         self._groups = groups
 
     def __contains__(self, group_name):
-        return self.page_group_regex.match(group_name) and group_name in self._groups
+        return group_name in self._groups
 
     def __iter__(self):
         return self._groups.iterkeys()
 
     def __getitem__(self, group_name):
-        return Group(request=self.request, name=group_name, backend=self)
+        return ConfigGroup(request=self.request, name=group_name, backend=self)
 
     def _retrieve_members(self, group_name):
         try:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/datastruct/backends/config_lazy_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -0,0 +1,99 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - config group lazy backend.
+
+    The config group backend allows defining groups in a configuration
+    file. These backends do not store members internally, but get them
+    from backend when needed in a lazy way.
+
+    NOTE, the backends are experimental and are implemented to find
+    out how different backends, for example, LDAP backend, should
+    work.
+
+    @copyright: 2009 MoinMoin:DmitrijsMilajevs
+    @license: GPL, see COPYING for details
+"""
+
+import time
+
+from MoinMoin.datastruct.backends import BaseGroup, BaseGroupsBackend, GroupDoesNotExistError
+
+
+class Proxy(object):
+    """
+    This class is done just to emulate that group definition is
+    something external.
+
+    For the LDAP backend this class not needed. __contains__ and
+    __iter__ may be defined in a Group class.
+    """
+
+    def __init__(self, groups):
+        """
+        Although, groups are passed, it doesn't look like
+        something lazy, it the best solution i can find now.
+        """
+        self._groups = groups
+
+    def __contains__(self, group_name):
+        return group_name in self._groups
+
+    def __iter__(self):
+        return self._groups.iterkeys()
+
+    def iter_group(self, group_name):
+        if group_name in self:
+            for member in self._groups[group_name]:
+                # Imitate slow connection
+                time.sleep(0.1)
+                yield member
+
+    def has_member(self, group_name, member):
+        # Imitate slow connection
+        time.sleep(0.1)
+        return group_name in self and member in self._groups[group_name]
+
+
+class LazyGroup(BaseGroup):
+
+    def __init__(self, request, name, backend):
+        super(LazyGroup, self).__init__(request, name, backend)
+
+        if name not in backend:
+            raise GroupDoesNotExistError(name)
+
+        self.proxy = backend._proxy
+
+    def __contains__(self, member, processed_groups=None):
+        return self.proxy.has_member(self.name, member)
+
+    def __iter__(self, yielded_members=None, processed_groups=None):
+        if not yielded_members:
+            yielded_members = set()
+
+        for member in self.proxy.iter_group(self.name):
+            if member not in yielded_members:
+                yielded_members.add(member)
+                yield member
+
+
+class LazyConfigGroups(BaseGroupsBackend):
+
+    def __init__(self, request, groups):
+        """
+        @param groups: Dictionary of groups where key is a group name,
+        and value is a list of members of that group.
+        """
+        super(LazyConfigGroups, self).__init__(request)
+
+        self._proxy = Proxy(groups)
+
+    def __contains__(self, group_name):
+        return group_name in self._proxy
+
+    def __iter__(self):
+        return self._proxy.__iter__()
+
+    def __getitem__(self, group_name):
+        return LazyGroup(request=self.request, name=group_name, backend=self)
+
--- a/MoinMoin/datastruct/backends/wiki_groups.py	Tue Jul 14 12:51:03 2009 +0200
+++ b/MoinMoin/datastruct/backends/wiki_groups.py	Fri Jul 17 08:38:02 2009 +0200
@@ -15,11 +15,11 @@
 
 from MoinMoin import caching, wikiutil
 from MoinMoin.Page import Page
-from MoinMoin.datastruct.backends import BaseGroup, BaseGroupsBackend, GroupDoesNotExistError
+from MoinMoin.datastruct.backends import GreedyGroup, BaseGroupsBackend, GroupDoesNotExistError
 from MoinMoin.formatter.groups import Formatter
 
 
-class WikiGroup(BaseGroup):
+class WikiGroup(GreedyGroup):
 
     def _load_group(self):
         request = self.request
@@ -51,7 +51,7 @@
 class WikiGroups(BaseGroupsBackend):
 
     def __contains__(self, group_name):
-        return self.is_group(group_name) and Page(self.request, group_name).exists()
+        return self.is_group_name(group_name) and Page(self.request, group_name).exists()
 
     def __iter__(self):
         return iter(self.request.rootpage.getPageList(user='', filter=self.page_group_regex.search))