~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/datastruct/backends/composite_groups.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: iso-8859-1 -*-
2
 
"""
3
 
MoinMoin - group access via various backends.
4
 
 
5
 
The composite_groups is a backend that does not have direct storage,
6
 
but composes other backends to a new one, so group definitions are
7
 
retrieved from several backends. This allows to mix different
8
 
backends.
9
 
 
10
 
@copyright: 2009 DmitrijsMilajevs
11
 
@license: GPL, see COPYING for details
12
 
"""
13
 
 
14
 
from MoinMoin.datastruct.backends import BaseGroupsBackend, GroupDoesNotExistError
15
 
 
16
 
 
17
 
class CompositeGroups(BaseGroupsBackend):
18
 
    """
19
 
    Manage several group backends.
20
 
    """
21
 
 
22
 
    def __init__(self, request, *backends):
23
 
        """
24
 
        @param backends: list of group backends which are used to get
25
 
                         access to the group definitions.
26
 
        """
27
 
        super(CompositeGroups, self).__init__(request)
28
 
        self._backends = backends
29
 
 
30
 
    def __getitem__(self, group_name):
31
 
        """
32
 
        Get a group by its name. First match counts.
33
 
        """
34
 
        for backend in self._backends:
35
 
            try:
36
 
                return backend[group_name]
37
 
            except GroupDoesNotExistError:
38
 
                pass
39
 
        raise GroupDoesNotExistError(group_name)
40
 
 
41
 
    def __iter__(self):
42
 
        """
43
 
        Iterate over group names in all backends (filtering duplicates).
44
 
 
45
 
        If a group with same name is defined in several backends, the
46
 
        composite_groups backend yields only backend which is listed
47
 
        earlier in self._backends.
48
 
        """
49
 
        yielded_groups = set()
50
 
 
51
 
        for backend in self._backends:
52
 
            for group_name in backend:
53
 
                if group_name not in yielded_groups:
54
 
                    yield group_name
55
 
                    yielded_groups.add(group_name)
56
 
 
57
 
    def __contains__(self, group_name):
58
 
        """
59
 
        Check if a group called group_name is available in any of the backends.
60
 
 
61
 
        @param group_name: name of the group [unicode]
62
 
        """
63
 
        for backend in self._backends:
64
 
            if group_name in backend:
65
 
                return True
66
 
        return False
67
 
 
68
 
    def __repr__(self):
69
 
        return "<%s backends=%s>" % (self.__class__, self._backends)
70