changeset 4851:866fe615893f

Groups2009: The wiki_dicts backend uses regexp to retrieve dict definition from a page. In 2.0 this should be changed. dicts.py formatter was removed.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Tue, 14 Jul 2009 12:51:03 +0200
parents 6241e92270b6
children 65bef5bcec41
files MoinMoin/datastruct/backends/wiki_dicts.py MoinMoin/formatter/_tests/test_dicts.py MoinMoin/formatter/dicts.py
diffstat 3 files changed, 14 insertions(+), 134 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/datastruct/backends/wiki_dicts.py	Tue Jul 14 11:52:15 2009 +0200
+++ b/MoinMoin/datastruct/backends/wiki_dicts.py	Tue Jul 14 12:51:03 2009 +0200
@@ -14,7 +14,6 @@
 from MoinMoin import caching, wikiutil
 from MoinMoin.Page import Page
 from MoinMoin.datastruct.backends import BaseDict, BaseDictsBackend, DictDoesNotExistError
-from MoinMoin.formatter.dicts import Formatter
 
 
 class WikiDict(BaseDict):
@@ -61,6 +60,9 @@
 
 class WikiDicts(BaseDictsBackend):
 
+    # Key:: Value - ignore all but key:: value pairs, strip whitespace, exactly one space after the :: is required
+    _dict_page_parse_regex = re.compile(ur'^ (?P<key>.+?):: (?P<val>.*?) *$', re.MULTILINE | re.UNICODE)
+
     def __contains__(self, dict_name):
         return self.is_dict_name(dict_name) and Page(self.request, dict_name).exists()
 
@@ -68,9 +70,15 @@
         return WikiDict(request=self.request, name=dict_name, backend=self)
 
     def _retrieve_items(self, dict_name):
-        formatter = Formatter(self.request)
-        page = Page(self.request, dict_name, formatter=formatter)
-        page.send_page(content_only=True)
+        # XXX in Moin 2.0 regex should not be used instead use DOM
+        # tree to extract dict values. Also it should be possible to
+        # convert dict values to different markups (wiki-markup,
+        # creole...).
+        #
+        # Note that formatter which extracts dictionary from a
+        # page was developed. See
+        # http://hg.moinmo.in/moin/1.9-groups-dmilajevs/file/982f706482e7/MoinMoin/formatter/dicts.py
+        page = Page(self.request, dict_name)
+        text = page.get_raw_body()
+        return dict([match.groups() for match in self._dict_page_parse_regex.finditer(text)])
 
-        return formatter.dict
-
--- a/MoinMoin/formatter/_tests/test_dicts.py	Tue Jul 14 11:52:15 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - MoinMoin.formatter.dicts Tests
-
-    @copyright: 2009 by MoinMoin:DmitrijsMilajevs
-    @license: GNU GPL, see COPYING for details.
-"""
-
-
-from  MoinMoin.formatter.dicts import Formatter
-from MoinMoin.Page import Page
-from MoinMoin._tests import become_trusted, create_page, nuke_page
-
-
-class TestDictFormatter(object):
-
-    def get_dict(self, text):
-        request = self.request
-        formatter = Formatter(self.request)
-
-        become_trusted(request)
-        create_page(request, u'TestPageDict', text)
-        page = Page(request, 'TestPageDict', formatter=formatter)
-        page.send_page(content_only=True)
-        nuke_page(request, u'TestPageDict')
-
-        return formatter.dict
-
-    def test_simple(self):
-        text = """
- One:: 1
- Two:: 2
-"""
-        dict = self.get_dict(text)
-        assert len(dict) == 2
-        assert dict['One'] == '1'
-        assert dict['Two'] == '2'
-
-    def test_complex(self):
-        text = '''
-Text ignored
- * list items ignored
-  * Second level list ignored
- First:: first item
- text with spaces:: second item
-
-Empty lines ignored, so is this text
-Next line has key with empty value
- Empty string::
- Last:: last item
-'''
-        dict = self.get_dict(text)
-        assert len(dict) == 4
-        assert dict['First'] == 'first item'
-        assert dict['text with spaces'] == 'second item'
-        assert dict['Empty string'] == ''
-        assert dict['Last'] == 'last item'
-
-
-coverage_modules = ['MoinMoin.formtter.dicts']
-
--- a/MoinMoin/formatter/dicts.py	Tue Jul 14 11:52:15 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - MoinMoin.formatter.dicts
-
-    @copyright: 2005 MoinMoin:NirSoffer
-                2009 MoinMoin:DmitrijsMilajevs
-    @license: GNU GPL, see COPYING for details.
-"""
-
-
-from MoinMoin.formatter import FormatterBase
-from MoinMoin import wikiutil
-
-
-class Formatter(FormatterBase):
-    """
-    Collect definition lists and format nothing
-    """
-
-    def __init__(self, request, **kw):
-        FormatterBase.__init__(self, request, **kw)
-        self.dict = {}
-        self.term = []
-        self.description = []
-        self.current = None
-
-    def definition_term(self, on):
-        if on:
-            self.term = []
-            self.current = 'term'
-        else:
-            self.current = None
-        return self.null()
-
-    def definition_desc(self, on):
-        if on:
-            self.description = []
-            self.current = 'description'
-        else:
-            term = ' '.join(self.term)
-            description = ' '.join(self.description)
-            self.dict[term] = description
-            self.current = None
-        return self.null()
-
-    def text(self, text):
-        if self.current:
-            text = text.strip()
-            if text:
-                attr = getattr(self, self.current)
-                attr.append(text)
-        return self.null()
-
-    def null(self, *args, **kw):
-        return ''
-
-    # All these must be overriden here because they raise
-    # NotImplementedError!@#! or return html?! in the base class.
-    set_highlight_re = rawHTML = url = image = smiley = null
-    strong = emphasis = underline = highlight = sup = sub = strike = null
-    code = preformatted = small = big = code_area = code_line = null
-    code_token = linebreak = paragraph = rule = icon = null
-    number_list = definition_list = bullet_list = listitem = null
-    heading = table = pagelink = null
-    table_row = table_cell = attachment_link = attachment_image = attachment_drawing = null
-    transclusion = transclusion_param = null
-