changeset 4767:3b34bbd60dca

Groups2009: Group name mapping from moin to backend and vice versa
author Dmitrijs Milajevs <dimazest@gmail.com>
date Tue, 26 May 2009 16:33:52 +0200
parents c198eff7b345
children 235bb30c9bd6
files MoinMoin/groups/__init__.py MoinMoin/groups/_test/test_backend_manager.py
diffstat 2 files changed, 74 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/groups/__init__.py	Tue May 26 13:24:44 2009 +0200
+++ b/MoinMoin/groups/__init__.py	Tue May 26 16:33:52 2009 +0200
@@ -16,16 +16,42 @@
     group name. It provides access to groups of specific backend.
     """
 
-    def __init__(self, backend):
+    def __init__(self, backend, mapper_to_backend=None, mapper_from_backend=None):
         """
         Creates backend manager object.
 
+        XXX Decorators can be used for group name mapping.
+
         @type backend: group backend object.
         @param backend: the backend which provides access to the group
         definitions.
+
+        @type mapper_to_backend: function which takes one string as an
+        argument and returns a string
+        @param mapper_to_backend: function which maps moin group
+        name to the backend group name
+
+        @type mapper_from_backend: function which takes one string as an
+        argument and returns a string
+        @param mapper_from_backend: function which maps backend group
+        name to the moin group name
         """
         self._backend = backend
 
+        # XXX Should we check that *two* mapper functions are passed,
+        # and if not throw an exception?
+        if mapper_to_backend is not None:
+            self.mapper_to_backend = mapper_to_backend
+        else:
+            # Nothing to map, just return unmodified string
+            self.mapper_to_backend = lambda x: x
+
+        if mapper_from_backend is not None:
+            self.mapper_from_backend = mapper_from_backend
+        else:
+            # Nothing to map, just return unmodified string
+            self.mapper_from_backend = lambda x: x
+
     def __getitem__(self, group_name):
         """
         Selection of a group by its name.
@@ -33,13 +59,13 @@
         @type group_name: unicode string.
         @param group_name: name of the group which object to select.
         """
-        return self._backend[group_name]
+        return self._backend[self.mapper_to_backend(group_name)]
 
     def __iter__(self):
         """
         Iteration over group names.
         """
-        return iter(self._backend)
+        return (self.mapper_from_backend(group_name) for group_name in self._backend)
 
     def __contains__(self, group_name):
         """
@@ -48,7 +74,7 @@
         @type group_name: unicode string.
         @param group_name: name of the group which is checked for an containment.
         """
-        return group_name in self._backend
+        return self.mapper_to_backend(group_name) in self._backend
 
     def membergroups(self, member):
         """
@@ -57,7 +83,7 @@
         @return: list of group names in which member takes part in
         """
         return [group_name for group_name in self
-                         if member in self[group_name]]
+                if member in self[group_name]]
 
 
 class GroupManager(object):
--- a/MoinMoin/groups/_test/test_backend_manager.py	Tue May 26 13:24:44 2009 +0200
+++ b/MoinMoin/groups/_test/test_backend_manager.py	Tue May 26 16:33:52 2009 +0200
@@ -11,10 +11,12 @@
 
 from MoinMoin.groups import BackendManager
 
+
 class TestBackendManagerAPI(object):
     """
     This tastcase test Backend manager API
     """
+
     def setup_method(self, method):
         self.admin_group = frozenset([u'Admin', u'JohnDoe'])
         self.editor_group = frozenset([u'MainEditor', u'JohnDoe'])
@@ -63,4 +65,45 @@
         assert u'FruitGroup' not in john_doe_groups
 
 
+class TestManagerMapping(object):
+    """
+    This class tests mapping of the group names from a backend to the
+    moin and from the moin to a backend.
+
+    Here the simplest situation is considered. Moin expect groups to
+    be named as *Group, but backend stores group names without this prefix.
+
+    When group names are passed or retrieved from the backend they
+    should be mapped.
+    """
+
+    def setup_class(self):
+        self.admin_group = frozenset([u'Admin', u'JohnDoe'])
+        self.editor_group = frozenset([u'MainEditor', u'JohnDoe'])
+
+        # Group names here do not follow moin convention: they do not
+        # have group prefix.
+        groups = {u'Admin': self.admin_group,
+                  u'Editor': self.editor_group}
+
+        # Simply drop last five letters, what is length of word "Group"
+        mapper_to_backend = lambda group_name: group_name[:-5]
+        # Add "Group" postfix for every group name received from a backend
+        mapper_from_backend = lambda group_name: "%sGroup" % group_name
+
+        self.group_backend = BackendManager(backend=groups,
+                                            mapper_to_backend=mapper_to_backend,
+                                            mapper_from_backend=mapper_from_backend)
+
+
+    def test_getitem(self):
+        admin_group = self.group_backend[u'AdminGroup']
+        assert self.admin_group == admin_group
+
+    def test_contains(self):
+        assert u'AdminGroup' in self.group_backend
+
+    def test_membersgroups(self):
+        assert u'AdminGroup' in self.group_backend.membergroups(u'JohnDoe')
+        
 coverage_modules = ['MoinMoin.groups']