changeset 511:ef1f74bf4776

macro DictColumns: new keyword markup defined (default is "definition list"). By setting it to "title" the column headings are defined by the titles of the pageThe text below a title becomes assigned to the column.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Fri, 19 Mar 2010 22:59:00 +0100
parents 6318c91263c6
children 31e327c450ca
files data/plugin/macro/DictColumns.py data/plugin/macro/_tests/test_DictColumns.py
diffstat 2 files changed, 58 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/DictColumns.py	Fri Mar 19 21:22:57 2010 +0100
+++ b/data/plugin/macro/DictColumns.py	Fri Mar 19 22:59:00 2010 +0100
@@ -12,11 +12,13 @@
 import re
 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
 
 Dependencies = ["pages"]
 
+
 def _csv2list(csv):
     """
     converts a string of comma separated values into a list
@@ -50,7 +52,7 @@
                  sort=u'', reverse=u'',
                  hide=u'', filter_name=u'NeverExistingDefaultFilter',
                  filter_value=u'', template_page=u'', alias_page=u'',
-                 parser=u'text_moin_wiki', search_term=None):
+                 parser=u'text_moin_wiki', markup="definition list", search_term=None):
 
         self.formatter = macro.formatter
         self.request = macro.request
@@ -67,7 +69,7 @@
         self.filter_name = filter_name
         self.filter_value = filter_value
         self.filter_key, self.filter_word = (u"", u"")
-        regex = re.compile(ur'(?P<key>\w*):: (?P<value>.*)', re.UNICODE)
+        regex = re.compile(ur'(?P<key>\w*)=(?P<value>.*)', re.UNICODE)
         try:
             self.filter_key, self.filter_word = regex.search(filter_value).groups()
         except AttributeError:
@@ -82,9 +84,32 @@
         except wikiutil.PluginMissingError:
             self.wiki_parser = None
         self.search_term = search_term
+        self.markup = markup
         if search_term is None:
             self.search_term = u'regex:title:^%s/' % self.pagename
 
+    def get_dict(self, dict_source):
+        """
+        gets the dictionary dependent of the markup
+        @param dict_source: pagename to read dict data from 
+        """
+        if self.markup == "definition list":
+            return self.request.dicts[dict_source]
+        elif self.markup == "title":
+            return self.parse_title(dict_source)
+
+    def parse_title(self, dict_source):
+        """
+        creates a dictionary based on page titles 
+        @param dict_source: pagename to read dict data from
+        """
+        body = Page(self.request, dict_source).get_raw_body()
+        parser = SlidePage(self.request, dict_source).createSlideParser()
+        ddict = {}
+        for title, bodyStart, bodyEnd in parser.parse(body):
+            ddict[title] = body[bodyStart:bodyEnd].strip()
+        return ddict
+
     def get_page_list(self):
         """
         selects the pages dependent on a search term,
@@ -117,13 +142,13 @@
             names = self.names
         # use keys from template page, no order
         elif Page(request, self.template_page).exists():
-            page_dict = request.dicts[self.template_page]
+            page_dict = self.get_dict(self.template_page)
             names = page_dict.keys()
         else:
             # fallback use the keys used on selected pages
             names = []
             for page_name in selected_pages:
-                page_dict = request.dicts[page_name]
+                page_dict = self.get_dict(page_name)
                 keys = page_dict.keys()
                 names = names + keys
         return list(set(names))
@@ -142,7 +167,7 @@
         for name in names:
             alias_dict[name] = name
         if Page(request, self.alias_page).exists():
-            alias = request.dicts[self.alias_page]
+            alias = self.get_dict(self.alias_page)
             for name in names:
                 alias_dict[name] = alias.get(name, name)
 
@@ -156,7 +181,7 @@
 
         for page_name in selected_pages:
             page = Page(request, page_name)
-            page_dict = request.dicts[page_name]
+            page_dict = self.get_dict(page_name)
             if self.filter_value and page_dict.get(self.filter_key, '') != self.filter_word:
                 continue
 
@@ -222,7 +247,8 @@
 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', search_term=None):
+                      parser=u'text_moin_wiki', markup=("definition list", "title"),
+                      search_term=None):
     """
     Creates a table by data browser widget from definition lists key value pairs.
     @param pagename: name of the page
@@ -236,6 +262,7 @@
     @param template_page: pagename of the template for setting column names
     @param alias_page: pagename of the page for setting aliases for column names
     @param parser: name of the parser used to render markup
+    @param markup: type of markup for separating key value pairs
     @param search_term: regex used to search for selecting pages
     """
     kw = locals()
--- a/data/plugin/macro/_tests/test_DictColumns.py	Fri Mar 19 21:22:57 2010 +0100
+++ b/data/plugin/macro/_tests/test_DictColumns.py	Fri Mar 19 22:59:00 2010 +0100
@@ -15,6 +15,7 @@
     pagename = "DictColumns"
     template_page = "BooksTemplate"
     alias_page = "BookAlias"
+    title_page = "BookTitleExample"
 
     page_dict = [{"book": "Gray Hat Python: Python Programming for Hackers and Reverse Engineers",
                   "author": "Justin Seitz",
@@ -73,6 +74,18 @@
 """
     alias_text = " prize:: %(prize)s"
     alias_dict = {"prize": "prize (euro)"}
+    
+    title_text = """\
+= book =
+book example
+= author =
+author example
+= prize =
+prize example
+= in stock =
+in stock example
+"""
+
 
     def setup_class(self):
         """ initializes content """
@@ -81,6 +94,7 @@
         template_page = create_page(self.request, self.template_page, template_text)
         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)
         for content in self.page_dict:
             pagename = self.pagename + '/' + content["book"]
             examples = create_page(self.request, pagename, self.text % content)
@@ -96,6 +110,15 @@
         nuke_page(self.request, self.template_page)
         nuke_page(self.request, self.alias_page)
 
+    def test_get_dict(self):
+        dict_columns = DictColumns(self.macro, pagename=self.pagename, markup="title")
+        result = dict_columns.get_dict(self.title_page)
+        expected_result = {u'author': u'author example',
+                           u'book': u'book example',
+                           u'in stock': u'in stock example',
+                           u'prize': u'prize example'}
+        assert result == expected_result
+
     def test_get_page_list(self):
         """ tests selection of pages by the default regex search_term """
         result = DictColumns(self.macro, pagename=self.pagename).get_page_list()
@@ -250,7 +273,7 @@
         selected_pages = DictColumns(self.macro, pagename=self.pagename).get_page_list()
         names = ['author', 'language', 'prize']
         # filters for this condition
-        filter_value = "prize:: 24.90"
+        filter_value = "prize=24.90"
         result = DictColumns(self.macro, pagename=self.pagename, filter_value=filter_value).dataset(names, selected_pages)
         assert len(result.columns) == len(names) + 1
         idx = names.index("prize") + 1