Back to index

moin  1.9.0~rc2
composite_groups.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - group access via various backends.
00004 
00005 The composite_groups is a backend that does not have direct storage,
00006 but composes other backends to a new one, so group definitions are
00007 retrieved from several backends. This allows to mix different
00008 backends.
00009 
00010 @copyright: 2009 DmitrijsMilajevs
00011 @license: GPL, see COPYING for details
00012 """
00013 
00014 from MoinMoin.datastruct.backends import BaseGroupsBackend, GroupDoesNotExistError
00015 
00016 
00017 class CompositeGroups(BaseGroupsBackend):
00018     """
00019     Manage several group backends.
00020     """
00021 
00022     def __init__(self, request, *backends):
00023         """
00024         @param backends: list of group backends which are used to get
00025                          access to the group definitions.
00026         """
00027         super(CompositeGroups, self).__init__(request)
00028         self._backends = backends
00029 
00030     def __getitem__(self, group_name):
00031         """
00032         Get a group by its name. First match counts.
00033         """
00034         for backend in self._backends:
00035             try:
00036                 return backend[group_name]
00037             except GroupDoesNotExistError:
00038                 pass
00039         raise GroupDoesNotExistError(group_name)
00040 
00041     def __iter__(self):
00042         """
00043         Iterate over group names in all backends (filtering duplicates).
00044 
00045         If a group with same name is defined in several backends, the
00046         composite_groups backend yields only backend which is listed
00047         earlier in self._backends.
00048         """
00049         yielded_groups = set()
00050 
00051         for backend in self._backends:
00052             for group_name in backend:
00053                 if group_name not in yielded_groups:
00054                     yield group_name
00055                     yielded_groups.add(group_name)
00056 
00057     def __contains__(self, group_name):
00058         """
00059         Check if a group called group_name is available in any of the backends.
00060 
00061         @param group_name: name of the group [unicode]
00062         """
00063         for backend in self._backends:
00064             if group_name in backend:
00065                 return True
00066         return False
00067 
00068     def __repr__(self):
00069         return "<%s backends=%s>" % (self.__class__, self._backends)
00070