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

« back to all changes in this revision

Viewing changes to MoinMoin/datastruct/backends/composite_dicts.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 - dict access via various backends.
4
 
 
5
 
@copyright: 2009 DmitrijsMilajevs
6
 
@license: GPL, see COPYING for details
7
 
"""
8
 
 
9
 
from MoinMoin.datastruct.backends import BaseDictsBackend, DictDoesNotExistError
10
 
 
11
 
 
12
 
class CompositeDicts(BaseDictsBackend):
13
 
    """
14
 
    Manage several dicts backends.
15
 
    """
16
 
 
17
 
    def __init__(self, request, *backends):
18
 
        """
19
 
        @param backends: list of dict backends which are used to get
20
 
                         access to the dict definitions.
21
 
        """
22
 
        super(CompositeDicts, self).__init__(request)
23
 
        self._backends = backends
24
 
 
25
 
    def __getitem__(self, dict_name):
26
 
        """
27
 
        Get a dict by its name. First match counts.
28
 
        """
29
 
        for backend in self._backends:
30
 
            try:
31
 
                return backend[dict_name]
32
 
            except DictDoesNotExistError:
33
 
                pass
34
 
        raise DictDoesNotExistError(dict_name)
35
 
 
36
 
    def __contains__(self, dict_name):
37
 
        """
38
 
        Check if a dict called dict_name is available in any of the backends.
39
 
        """
40
 
        for backend in self._backends:
41
 
            if dict_name in backend:
42
 
                return True
43
 
        return False
44
 
 
45
 
    def __repr__(self):
46
 
        return "<%s backends=%s>" % (self.__class__, self._backends)
47