1
# -*- coding: iso-8859-1 -*-
3
MoinMoin - WikiDict functions.
5
@copyright: 2003-2007 MoinMoin:ThomasWaldmann,
6
2003 by Gustavo Niemeyer
7
2009 MoinMoin:DmitrijsMilajevs
8
@license: GNU GPL, see COPYING for details.
14
from MoinMoin import caching, wikiutil
15
from MoinMoin.Page import Page
16
from MoinMoin.datastruct.backends import BaseDict, BaseDictsBackend, DictDoesNotExistError
19
class WikiDict(BaseDict):
21
Mapping of keys to values in a wiki page.
23
A dict definition page should look like:
28
key2:: value2 containing spaces
35
request = self.request
38
page = Page(request, dict_name)
41
key = wikiutil.quoteWikinameFS(dict_name)
42
cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
44
cache_mtime = cache.mtime()
45
page_mtime = wikiutil.version2timestamp(page.mtime_usecs())
46
# TODO: fix up-to-date check mtime granularity problems.
48
# cache_mtime is float while page_mtime is integer
49
# The comparision needs to be done on the lowest type of both
50
if int(cache_mtime) > int(page_mtime):
52
return cache.content()
54
raise caching.CacheError
55
except caching.CacheError:
56
# either cache does not exist, is erroneous or not uptodate: recreate it
57
d = super(WikiDict, self)._load_dict()
61
raise DictDoesNotExistError(dict_name)
64
class WikiDicts(BaseDictsBackend):
66
# Key:: Value - ignore all but key:: value pairs, strip whitespace, exactly one space after the :: is required
67
_dict_page_parse_regex = re.compile(ur'^ (?P<key>.+?):: (?P<val>.*?) *$', re.MULTILINE | re.UNICODE)
69
def __contains__(self, dict_name):
70
return self.is_dict_name(dict_name) and Page(self.request, dict_name).exists()
72
def __getitem__(self, dict_name):
73
return WikiDict(request=self.request, name=dict_name, backend=self)
75
def _retrieve_items(self, dict_name):
76
# XXX in Moin 2.0 regex should not be used instead use DOM
77
# tree to extract dict values. Also it should be possible to
78
# convert dict values to different markups (wiki-markup,
81
# Note that formatter which extracts dictionary from a
82
# page was developed. See
83
# http://hg.moinmo.in/moin/1.9-groups-dmilajevs/file/982f706482e7/MoinMoin/formatter/dicts.py
84
page = Page(self.request, dict_name)
85
text = page.get_raw_body()
86
return dict([match.groups() for match in self._dict_page_parse_regex.finditer(text)])