changeset 4846:982f706482e7

Groups2009: MoinMoin.formatter.dicts formatter was introduced. It collects dict items defined on a page. MoinMoin.datastruct.backends.wiki_dicts uses this formatter to get items from a dict page. _dict_page_parse_regex was killed.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sat, 11 Jul 2009 15:17:13 +0200
parents 916c28ef9026
children f0bcf3e1725d
files MoinMoin/datastruct/backends/wiki_dicts.py MoinMoin/formatter/_tests/test_dicts.py MoinMoin/formatter/dicts.py MoinMoin/formatter/groups.py
diffstat 4 files changed, 135 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/datastruct/backends/wiki_dicts.py	Sat Jul 11 12:33:57 2009 +0200
+++ b/MoinMoin/datastruct/backends/wiki_dicts.py	Sat Jul 11 15:17:13 2009 +0200
@@ -14,6 +14,7 @@
 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):
@@ -59,16 +60,6 @@
 
 
 class WikiDicts(BaseDictsBackend):
-    """
-    A dictionary of Dict objects
-
-    Config:
-       cfg.page_dict_regex
-       Default: ".*Dict$"  Defs$ Vars$ ???????????????????
-    """
-
-    # 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()
@@ -77,7 +68,9 @@
         return WikiDict(request=self.request, name=dict_name, backend=self)
 
     def _retrieve_items(self, dict_name):
-        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)])
+        formatter = Formatter(self.request)
+        page = Page(self.request, dict_name, formatter=formatter)
+        page.send_page(content_only=True)
 
+        return formatter.dict
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/formatter/_tests/test_dicts.py	Sat Jul 11 15:17:13 2009 +0200
@@ -0,0 +1,61 @@
+# -*- 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']
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/formatter/dicts.py	Sat Jul 11 15:17:13 2009 +0200
@@ -0,0 +1,67 @@
+# -*- 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
+
--- a/MoinMoin/formatter/groups.py	Sat Jul 11 12:33:57 2009 +0200
+++ b/MoinMoin/formatter/groups.py	Sat Jul 11 15:17:13 2009 +0200
@@ -1,6 +1,6 @@
 # -*- coding: iso-8859-1 -*-
 """
-    MoinMoin - MoinMoin.formatter.groups datastruct (dicts and groups) formatters
+    MoinMoin - MoinMoin.formatter.groups
 
     @copyright: 2009 MoinMoin:DmitrijsMilajevs
     @license: GNU GPL, see COPYING for details.