comparison data/plugin/macro/DictColumns.py @ 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 b7a9843c5730
children 31e327c450ca
comparison
equal deleted inserted replaced
510:6318c91263c6 511:ef1f74bf4776
10 @license: GNU GPL, see COPYING for details. 10 @license: GNU GPL, see COPYING for details.
11 """ 11 """
12 import re 12 import re
13 from MoinMoin import wikiutil, search 13 from MoinMoin import wikiutil, search
14 from MoinMoin.Page import Page 14 from MoinMoin.Page import Page
15 from MoinMoin.action.SlideShow import SlidePage
15 from MoinMoin.util.dataset import TupleDataset, Column 16 from MoinMoin.util.dataset import TupleDataset, Column
16 from MoinMoin.widget.browser import DataBrowserWidget 17 from MoinMoin.widget.browser import DataBrowserWidget
17 18
18 Dependencies = ["pages"] 19 Dependencies = ["pages"]
20
19 21
20 def _csv2list(csv): 22 def _csv2list(csv):
21 """ 23 """
22 converts a string of comma separated values into a list 24 converts a string of comma separated values into a list
23 @param csv: string of comma separated values 25 @param csv: string of comma separated values
48 """ 50 """
49 def __init__(self, macro, pagename=u'', title=u'', names=u'', 51 def __init__(self, macro, pagename=u'', title=u'', names=u'',
50 sort=u'', reverse=u'', 52 sort=u'', reverse=u'',
51 hide=u'', filter_name=u'NeverExistingDefaultFilter', 53 hide=u'', filter_name=u'NeverExistingDefaultFilter',
52 filter_value=u'', template_page=u'', alias_page=u'', 54 filter_value=u'', template_page=u'', alias_page=u'',
53 parser=u'text_moin_wiki', search_term=None): 55 parser=u'text_moin_wiki', markup="definition list", search_term=None):
54 56
55 self.formatter = macro.formatter 57 self.formatter = macro.formatter
56 self.request = macro.request 58 self.request = macro.request
57 self.pagename = pagename 59 self.pagename = pagename
58 if not pagename: 60 if not pagename:
65 self.reverse = reverse 67 self.reverse = reverse
66 self.hide = hide 68 self.hide = hide
67 self.filter_name = filter_name 69 self.filter_name = filter_name
68 self.filter_value = filter_value 70 self.filter_value = filter_value
69 self.filter_key, self.filter_word = (u"", u"") 71 self.filter_key, self.filter_word = (u"", u"")
70 regex = re.compile(ur'(?P<key>\w*):: (?P<value>.*)', re.UNICODE) 72 regex = re.compile(ur'(?P<key>\w*)=(?P<value>.*)', re.UNICODE)
71 try: 73 try:
72 self.filter_key, self.filter_word = regex.search(filter_value).groups() 74 self.filter_key, self.filter_word = regex.search(filter_value).groups()
73 except AttributeError: 75 except AttributeError:
74 # Don't filter if syntax was wrong 76 # Don't filter if syntax was wrong
75 self.filter_value = u"" 77 self.filter_value = u""
80 self.request.cfg, "parser", 82 self.request.cfg, "parser",
81 parser, function="Parser") 83 parser, function="Parser")
82 except wikiutil.PluginMissingError: 84 except wikiutil.PluginMissingError:
83 self.wiki_parser = None 85 self.wiki_parser = None
84 self.search_term = search_term 86 self.search_term = search_term
87 self.markup = markup
85 if search_term is None: 88 if search_term is None:
86 self.search_term = u'regex:title:^%s/' % self.pagename 89 self.search_term = u'regex:title:^%s/' % self.pagename
90
91 def get_dict(self, dict_source):
92 """
93 gets the dictionary dependent of the markup
94 @param dict_source: pagename to read dict data from
95 """
96 if self.markup == "definition list":
97 return self.request.dicts[dict_source]
98 elif self.markup == "title":
99 return self.parse_title(dict_source)
100
101 def parse_title(self, dict_source):
102 """
103 creates a dictionary based on page titles
104 @param dict_source: pagename to read dict data from
105 """
106 body = Page(self.request, dict_source).get_raw_body()
107 parser = SlidePage(self.request, dict_source).createSlideParser()
108 ddict = {}
109 for title, bodyStart, bodyEnd in parser.parse(body):
110 ddict[title] = body[bodyStart:bodyEnd].strip()
111 return ddict
87 112
88 def get_page_list(self): 113 def get_page_list(self):
89 """ 114 """
90 selects the pages dependent on a search term, 115 selects the pages dependent on a search term,
91 without listing of template, dictionary pages and 116 without listing of template, dictionary pages and
115 # use selection and order 140 # use selection and order
116 if self.names: 141 if self.names:
117 names = self.names 142 names = self.names
118 # use keys from template page, no order 143 # use keys from template page, no order
119 elif Page(request, self.template_page).exists(): 144 elif Page(request, self.template_page).exists():
120 page_dict = request.dicts[self.template_page] 145 page_dict = self.get_dict(self.template_page)
121 names = page_dict.keys() 146 names = page_dict.keys()
122 else: 147 else:
123 # fallback use the keys used on selected pages 148 # fallback use the keys used on selected pages
124 names = [] 149 names = []
125 for page_name in selected_pages: 150 for page_name in selected_pages:
126 page_dict = request.dicts[page_name] 151 page_dict = self.get_dict(page_name)
127 keys = page_dict.keys() 152 keys = page_dict.keys()
128 names = names + keys 153 names = names + keys
129 return list(set(names)) 154 return list(set(names))
130 155
131 def dataset(self, names, selected_pages): 156 def dataset(self, names, selected_pages):
140 # default alias 165 # default alias
141 alias_dict = {} 166 alias_dict = {}
142 for name in names: 167 for name in names:
143 alias_dict[name] = name 168 alias_dict[name] = name
144 if Page(request, self.alias_page).exists(): 169 if Page(request, self.alias_page).exists():
145 alias = request.dicts[self.alias_page] 170 alias = self.get_dict(self.alias_page)
146 for name in names: 171 for name in names:
147 alias_dict[name] = alias.get(name, name) 172 alias_dict[name] = alias.get(name, name)
148 173
149 col = Column(self.title, label=self.title) 174 col = Column(self.title, label=self.title)
150 if self.title in hide_columns: 175 if self.title in hide_columns:
154 data.columns = [] 179 data.columns = []
155 data.columns.extend([col]) 180 data.columns.extend([col])
156 181
157 for page_name in selected_pages: 182 for page_name in selected_pages:
158 page = Page(request, page_name) 183 page = Page(request, page_name)
159 page_dict = request.dicts[page_name] 184 page_dict = self.get_dict(page_name)
160 if self.filter_value and page_dict.get(self.filter_key, '') != self.filter_word: 185 if self.filter_value and page_dict.get(self.filter_key, '') != self.filter_word:
161 continue 186 continue
162 187
163 row = [] 188 row = []
164 for name in names: 189 for name in names:
220 return html 245 return html
221 246
222 def macro_DictColumns(macro, pagename=unicode, title=u'', names=u'', sort=u'', reverse=u'', 247 def macro_DictColumns(macro, pagename=unicode, title=u'', names=u'', sort=u'', reverse=u'',
223 hide=u'', filter_name=u'NeverExistingDefaultFilter', 248 hide=u'', filter_name=u'NeverExistingDefaultFilter',
224 filter_value=u'', template_page=u'', alias_page=u'', 249 filter_value=u'', template_page=u'', alias_page=u'',
225 parser=u'text_moin_wiki', search_term=None): 250 parser=u'text_moin_wiki', markup=("definition list", "title"),
251 search_term=None):
226 """ 252 """
227 Creates a table by data browser widget from definition lists key value pairs. 253 Creates a table by data browser widget from definition lists key value pairs.
228 @param pagename: name of the page 254 @param pagename: name of the page
229 @param title: entry in upper left corner of the table 255 @param title: entry in upper left corner of the table
230 @param name: names of columns, key name of definition list (comma separated) 256 @param name: names of columns, key name of definition list (comma separated)
234 @param filter_name: name of columns to filter by autofilter 260 @param filter_name: name of columns to filter by autofilter
235 @param filter_value: dict definition for value of column to filter by 261 @param filter_value: dict definition for value of column to filter by
236 @param template_page: pagename of the template for setting column names 262 @param template_page: pagename of the template for setting column names
237 @param alias_page: pagename of the page for setting aliases for column names 263 @param alias_page: pagename of the page for setting aliases for column names
238 @param parser: name of the parser used to render markup 264 @param parser: name of the parser used to render markup
265 @param markup: type of markup for separating key value pairs
239 @param search_term: regex used to search for selecting pages 266 @param search_term: regex used to search for selecting pages
240 """ 267 """
241 kw = locals() 268 kw = locals()
242 # wiki input can be a string with comma separated values. 269 # wiki input can be a string with comma separated values.
243 kw["names"] = _csv2list(kw["names"]) 270 kw["names"] = _csv2list(kw["names"])