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

« back to all changes in this revision

Viewing changes to MoinMoin/datastruct/backends/wiki_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 - WikiDict functions.
4
 
 
5
 
    @copyright: 2003-2007 MoinMoin:ThomasWaldmann,
6
 
                2003 by Gustavo Niemeyer
7
 
                2009 MoinMoin:DmitrijsMilajevs
8
 
    @license: GNU GPL, see COPYING for details.
9
 
"""
10
 
 
11
 
 
12
 
import re
13
 
 
14
 
from MoinMoin import caching, wikiutil
15
 
from MoinMoin.Page import Page
16
 
from MoinMoin.datastruct.backends import BaseDict, BaseDictsBackend, DictDoesNotExistError
17
 
 
18
 
 
19
 
class WikiDict(BaseDict):
20
 
    """
21
 
    Mapping of keys to values in a wiki page.
22
 
 
23
 
    A dict definition page should look like:
24
 
 
25
 
       any text ignored
26
 
        key1:: value1
27
 
        * ignored, too
28
 
        key2:: value2 containing spaces
29
 
        ...
30
 
        keyn:: ....
31
 
       any text ignored
32
 
    """
33
 
 
34
 
    def _load_dict(self):
35
 
        request = self.request
36
 
        dict_name = self.name
37
 
 
38
 
        page = Page(request, dict_name)
39
 
        if page.exists():
40
 
            arena = 'pagedicts'
41
 
            key = wikiutil.quoteWikinameFS(dict_name)
42
 
            cache = caching.CacheEntry(request, arena, key, scope='wiki', use_pickle=True)
43
 
            try:
44
 
                cache_mtime = cache.mtime()
45
 
                page_mtime = wikiutil.version2timestamp(page.mtime_usecs())
46
 
                # TODO: fix up-to-date check mtime granularity problems.
47
 
                #
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):
51
 
                    # cache is uptodate
52
 
                    return cache.content()
53
 
                else:
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()
58
 
                cache.update(d)
59
 
                return d
60
 
        else:
61
 
            raise DictDoesNotExistError(dict_name)
62
 
 
63
 
 
64
 
class WikiDicts(BaseDictsBackend):
65
 
 
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)
68
 
 
69
 
    def __contains__(self, dict_name):
70
 
        return self.is_dict_name(dict_name) and Page(self.request, dict_name).exists()
71
 
 
72
 
    def __getitem__(self, dict_name):
73
 
        return WikiDict(request=self.request, name=dict_name, backend=self)
74
 
 
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,
79
 
        # creole...).
80
 
        #
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)])
87