Back to index

moin  1.9.0~rc2
wiki_dicts.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - WikiDict functions.
00004 
00005     @copyright: 2003-2007 MoinMoin:ThomasWaldmann,
00006                 2003 by Gustavo Niemeyer
00007                 2009 MoinMoin:DmitrijsMilajevs
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 
00012 import re
00013 
00014 from MoinMoin import caching, wikiutil
00015 from MoinMoin.Page import Page
00016 from MoinMoin.datastruct.backends import BaseDict, BaseDictsBackend, DictDoesNotExistError
00017 
00018 
00019 class WikiDict(BaseDict):
00020     """
00021     Mapping of keys to values in a wiki page.
00022 
00023     A dict definition page should look like:
00024 
00025        any text ignored
00026         key1:: value1
00027         * ignored, too
00028         key2:: value2 containing spaces
00029         ...
00030         keyn:: ....
00031        any text ignored
00032     """
00033 
00034     def _load_dict(self):
00035         request = self.request
00036         dict_name = self.name
00037 
00038         page = Page(request, dict_name)
00039         if page.exists():
00040             arena = 'pagedicts'
00041             key = wikiutil.quoteWikinameFS(dict_name)
00042             cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
00043             try:
00044                 cache_mtime = cache.mtime()
00045                 page_mtime = wikiutil.version2timestamp(page.mtime_usecs())
00046                 # TODO: fix up-to-date check mtime granularity problems.
00047                 #
00048                 # cache_mtime is float while page_mtime is integer
00049                 # The comparision needs to be done on the lowest type of both
00050                 if int(cache_mtime) > int(page_mtime):
00051                     # cache is uptodate
00052                     return cache.content()
00053                 else:
00054                     raise caching.CacheError
00055             except caching.CacheError:
00056                 # either cache does not exist, is erroneous or not uptodate: recreate it
00057                 d = super(WikiDict, self)._load_dict()
00058                 cache.update(d)
00059                 return d
00060         else:
00061             raise DictDoesNotExistError(dict_name)
00062 
00063 
00064 class WikiDicts(BaseDictsBackend):
00065 
00066     # Key:: Value - ignore all but key:: value pairs, strip whitespace, exactly one space after the :: is required
00067     _dict_page_parse_regex = re.compile(ur'^ (?P<key>.+?):: (?P<val>.*?) *$', re.MULTILINE | re.UNICODE)
00068 
00069     def __contains__(self, dict_name):
00070         return self.is_dict_name(dict_name) and Page(self.request, dict_name).exists()
00071 
00072     def __getitem__(self, dict_name):
00073         return WikiDict(request=self.request, name=dict_name, backend=self)
00074 
00075     def _retrieve_items(self, dict_name):
00076         # XXX in Moin 2.0 regex should not be used instead use DOM
00077         # tree to extract dict values. Also it should be possible to
00078         # convert dict values to different markups (wiki-markup,
00079         # creole...).
00080         #
00081         # Note that formatter which extracts dictionary from a
00082         # page was developed. See
00083         # http://hg.moinmo.in/moin/1.9-groups-dmilajevs/file/982f706482e7/MoinMoin/formatter/dicts.py
00084         page = Page(self.request, dict_name)
00085         text = page.get_raw_body()
00086         return dict([match.groups() for match in self._dict_page_parse_regex.finditer(text)])
00087