changeset 545:946737f9eb1b

DictColumns: new parser for multiline definition list (markup=mdl) introduced, Books:: Einstieg in Python 3 Books:: Python Cookbook Books:: Gray Hat Python The parser returns a defaultdict(list). This list is rendered line by line in the table cell.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Tue, 15 Feb 2011 22:56:50 +0100
parents 02470d83aab9
children ca0f831afd76
files data/plugin/macro/DictColumns.py data/plugin/macro/_tests/test_DictColumns.py
diffstat 2 files changed, 48 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/DictColumns.py	Fri Jan 07 22:03:20 2011 +0100
+++ b/data/plugin/macro/DictColumns.py	Tue Feb 15 22:56:50 2011 +0100
@@ -6,15 +6,18 @@
     <<DictColumns(search_term=regex:title:^Examplepage/)>
 
     @copyright: 2006 by michael cohen <scudette@users.sourceforge.net> (PageDicts)
-    @copyright: 2008-2010 by MoinMoin:ReimarBauer (completly rewritten)
+    @copyright: 2008-2011 by MoinMoin:ReimarBauer (completly rewritten)
     @license: GNU GPL, see COPYING for details.
 """
 import re
+
+from collections import defaultdict
 from MoinMoin import wikiutil, search
 from MoinMoin.Page import Page
 from MoinMoin.action.SlideShow import SlidePage
 from MoinMoin.util.dataset import TupleDataset, Column
 from MoinMoin.widget.browser import DataBrowserWidget
+from MoinMoin.datastruct.backends.wiki_dicts import WikiDicts
 
 Dependencies = ["pages"]
 
@@ -94,11 +97,32 @@
         gets the dictionary dependent of the markup
         @param dict_source: pagename to read dict data from
         """
-        if self.markup == "definition list":
+        if self.markup in ("definition list", "dl"):
             return self.request.dicts[dict_source]
-        elif self.markup == "title":
+        elif self.markup in ("multiline definition list", "mdl"):
+            return self.parse_multiline_dict(dict_source)
+        elif self.markup in ("title", "t"):
             return self.parse_title(dict_source)
 
+    def parse_multiline_dict(self, dict_source):
+        """
+        creates a dictionary based on a definition list with multiple entries of the same key. 
+        The type of the value is a list
+         a:: 1
+         b:: 1
+         b:: 2
+         b:: 3
+         c:: 4
+        @param dict_source: pagename to read dict data from
+        """
+        body = Page(self.request, dict_source).get_raw_body()
+        ddict = defaultdict(list)
+
+        for match in WikiDicts._dict_page_parse_regex.finditer(body):
+            key, value = match.groups()
+            ddict[key].append(value)
+        return ddict
+    
     def parse_title(self, dict_source):
         """
         creates a dictionary based on page titles
@@ -190,8 +214,10 @@
             for name in names:
                 if name in page_dict.keys():
                     value = page_dict.get(name, '')
-                    value = value.replace('attachment:', 'attachment:%s/' % page_name)
-                    value = value.replace('<<Image(', '<<Image(%s/' % page_name)
+                    if isinstance(value, list) and len(value) > 1:
+                        value = '<<BR>>'.join(value)
+                    elif isinstance(value, list):
+                        value = value[0]
 
                     if self.wiki_parser:
                         row.append((wikiutil.renderText(request, self.wiki_parser, value),
@@ -250,7 +276,9 @@
 def macro_DictColumns(macro, pagename=unicode, title=u'', names=u'', sort=u'', reverse=u'',
                       hide=u'', filter_name=u'NeverExistingDefaultFilter',
                       filter_value=u'', template_page=u'', alias_page=u'',
-                      parser=u'text_moin_wiki', markup=("definition list", "title"),
+                      parser=u'text_moin_wiki', markup=("definition list", "title",
+                                                        "multiline definition list",
+                                                        "dl", "mdl", "t"),
                       search_term=None):
     """
     Creates a table by data browser widget from definition lists key value pairs.
--- a/data/plugin/macro/_tests/test_DictColumns.py	Fri Jan 07 22:03:20 2011 +0100
+++ b/data/plugin/macro/_tests/test_DictColumns.py	Tue Feb 15 22:56:50 2011 +0100
@@ -16,6 +16,7 @@
     template_page = "BooksTemplate"
     alias_page = "BookAlias"
     title_page = "BookTitleExample"
+    multiline_page = "MyBooks"
 
     page_dict = [{"book": "Gray Hat Python: Python Programming for Hackers and Reverse Engineers",
                   "author": "Justin Seitz",
@@ -86,6 +87,11 @@
 in stock example
 """
 
+    multiline_text = """
+ Buch:: Einstieg in Python 3
+ Buch:: Python Cookbook
+ Buch:: Gray Hat Python
+"""
 
     def setup_class(self):
         """ initializes content """
@@ -95,6 +101,7 @@
         alias_text = self.alias_text % self.alias_dict
         alias_page = create_page(self.request, self.alias_page, alias_text)
         title_page = create_page(self.request, self.title_page, self.title_text)
+        multiline_page = create_page(self.request, self.multiline_page, self.multiline_text)
         for content in self.page_dict:
             pagename = self.pagename + '/' + content["book"]
             examples = create_page(self.request, pagename, self.text % content)
@@ -109,6 +116,7 @@
         nuke_page(self.request, self.pagename)
         nuke_page(self.request, self.template_page)
         nuke_page(self.request, self.alias_page)
+        nuke_page(self.request, self.multiline_page)
 
     def test_get_dict(self):
         dict_columns = DictColumns(self.macro, pagename=self.pagename, markup="title")
@@ -118,6 +126,12 @@
                            u'in stock': u'in stock example',
                            u'prize': u'prize example'}
         assert result == expected_result
+    
+    def test_get_multiline_dict(self):
+        dict_columns = DictColumns(self.macro, pagename=self.pagename, markup="mdl")
+        result = dict_columns.get_dict(self.multiline_page)
+        expected_result = {"Buch": ["Einstieg in Python 3", "Python Cookbook", "Gray Hat Python"]}
+        assert result == expected_result
 
     def test_get_page_list(self):
         """ tests selection of pages by the default regex search_term """