Attachment 'PageDicts-0.5.py'
Download 1 """
2 MoinMoin - PageDicts
3
4 This macro uses the wikidicts mechanism for treating pages as dicts to
5 then display a selection of pages in a table.
6
7 Use it like this:
8 [[PageDicts(Bug0, Description, Status, Status ~= open)]]
9
10 This will draw a table with the columns pages, Description and
11 Status for all pages whos titles match Bug0 with Status matching
12 open. This can be used in many different scenarios, with
13 appropriate templates.
14
15 @copyright: 2006 by michael cohen <scudette@users.sourceforge.net>
16 @license: GNU GPL, see COPYING for details.
17 History:
18 0.5: Changed from return tmp.toHtml to return tmp.render()
19 0.4: Changed 'conditions.append(c) to conditions.append(condition)
20 0.3: Fix typo, allow the "transpose" keyword to change the table layout
21 0.2: original version
22 """
23 import re
24 from MoinMoin import wikiutil, wikidicts
25 from MoinMoin.Page import Page
26 from MoinMoin.util.dataset import TupleDataset, Column
27 from MoinMoin.widget.browser import DataBrowserWidget
28
29 Dependencies = ["pages"]
30 #Dependencies = []
31
32 def parse_condition(arg):
33 """ arg is a string which will be parsed into a condition dict.
34
35 A condition is a way of enforcing a requirement on a dict d. For example:
36
37 ## This will match if regex matches d['column_name']
38 column_name ~= regex
39
40 ## This will not match if regexp matches (inverse of above)
41 column_name !~= regex
42 """
43 if '!~=' in arg:
44 tmp = arg.split("!~=",1)
45 result=dict(
46 column= tmp[0].strip(),
47 regex= re.compile(tmp[1].strip(), re.IGNORECASE),
48 func = lambda condition,dictionary: not condition['regex'].search(dictionary[condition['column']])
49 )
50
51 return result
52 elif '~=' in arg:
53 tmp = arg.split("~=",1)
54 result = dict(
55 column = tmp[0].strip(),
56 regex = re.compile(tmp[1].strip(), re.IGNORECASE),
57 func = lambda condition,dictionary: condition['regex'].search(dictionary[condition['column']])
58 )
59
60 return result
61
62 def match_conditions(conditions, dictionary):
63 """ Returns true if all conditions match, false if any fail to match """
64 for condition in conditions:
65 if not condition['func'](condition,dictionary):
66 return False
67
68 return True
69
70 def execute(macro, args):
71 request = macro.request
72 args = [ arg.strip() for arg in args.split(',') ]
73 conditions = []
74 transpose = 0 ## if we transpose the results
75 needle = args[0]
76 if not needle:
77 return macro.formatter.text("It is probably not a good idea to parse all pages as dicts? You must specify a title filter")
78
79 try:
80 args = args[1:]
81 except IndexError:
82 args = []
83
84 ## Remove the args which are conditions and look for transpose flag
85 for i in range(len(args)):
86 condition = parse_condition(args[i])
87 if condition:
88 conditions.append(condition)
89 args[i]=None
90 elif 'transpose' in args[i]:
91 transpose = 1
92 args[i]=None
93
94 results = request.rootpage.getPageList(filter=re.compile(needle).search)
95 results.sort()
96
97 table = TupleDataset()
98 table.addRow( [ 'Name', ] + [ arg for arg in args if arg ])
99
100 ## Look at all the pages that matched our string
101 for page in results:
102 dictionary = wikidicts.Dict(request, page)
103
104 ## Check to make sure that the conditons are matched
105 if not match_conditions(conditions, dictionary):
106 continue
107
108 ## Add the row to the table
109 row = [ Page(request, page).link_to(request) ]
110 cols = [ dictionary.get(column,'') for column in args if column ]
111
112 ## look if we have something in a column at all
113 found = [ 1 for col in cols if col ]
114
115 ## only print the row if it has something
116 if found:
117 table.addRow(row + cols)
118
119 if transpose:
120 ## transpose table (make column rows and rows columns)
121 table.data = map(None, zip(*table.data))
122 ## create the top columns
123 table.columns = [ Column(col, label=col, align='right') for col in table.data[0] ]
124 ## remove the row extracted as the column
125 table.data = table.data[1:]
126 tmp = DataBrowserWidget(request)
127 tmp.setData(table)
128 return tmp.render()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.