Back to index

moin  1.9.0~rc2
composite_dicts.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - dict access via various backends.
00004 
00005 @copyright: 2009 DmitrijsMilajevs
00006 @license: GPL, see COPYING for details
00007 """
00008 
00009 from MoinMoin.datastruct.backends import BaseDictsBackend, DictDoesNotExistError
00010 
00011 
00012 class CompositeDicts(BaseDictsBackend):
00013     """
00014     Manage several dicts backends.
00015     """
00016 
00017     def __init__(self, request, *backends):
00018         """
00019         @param backends: list of dict backends which are used to get
00020                          access to the dict definitions.
00021         """
00022         super(CompositeDicts, self).__init__(request)
00023         self._backends = backends
00024 
00025     def __getitem__(self, dict_name):
00026         """
00027         Get a dict by its name. First match counts.
00028         """
00029         for backend in self._backends:
00030             try:
00031                 return backend[dict_name]
00032             except DictDoesNotExistError:
00033                 pass
00034         raise DictDoesNotExistError(dict_name)
00035 
00036     def __contains__(self, dict_name):
00037         """
00038         Check if a dict called dict_name is available in any of the backends.
00039         """
00040         for backend in self._backends:
00041             if dict_name in backend:
00042                 return True
00043         return False
00044 
00045     def __repr__(self):
00046         return "<%s backends=%s>" % (self.__class__, self._backends)
00047