Attachment 'PageDicts-0.4.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.4: Fix for moinmoin v1.6.3
  19 	0.3: Fix typo, allow the "transpose" keyword to change the table layout
  20     0.2: original version
  21 """
  22 import re
  23 from MoinMoin import wikiutil, wikidicts
  24 from MoinMoin.Page import Page
  25 from MoinMoin.util.dataset import TupleDataset, Column
  26 from MoinMoin.widget.browser import DataBrowserWidget
  27 
  28 Dependencies = ["pages"]
  29 
  30 def parse_condition(arg):
  31     """ arg is a string which will be parsed into a condition dict.
  32 
  33     A condition is a way of enforcing a requirement on a dict d. For example:
  34 
  35     ## This will match if regex matches d['column_name']
  36     column_name ~= regex
  37 
  38     ## This will not match if regexp matches (inverse of above)
  39     column_name !~= regex
  40     """
  41     if '!~=' in arg:
  42         tmp = arg.split("!~=",1)
  43         result=dict(
  44             column= tmp[0].strip(),
  45             regex= re.compile(tmp[1].strip(), re.IGNORECASE),
  46             func = lambda condition,dictionary: not condition['regex'].search(dictionary[condition['column']])
  47             )
  48 
  49         return result
  50     elif '~=' in arg:
  51         tmp = arg.split("~=",1)
  52         result = dict(
  53             column = tmp[0].strip(),
  54             regex = re.compile(tmp[1].strip(), re.IGNORECASE),
  55             func = lambda condition,dictionary: condition['regex'].search(dictionary[condition['column']])
  56             )
  57 
  58         return result
  59 
  60 def match_conditions(conditions, dictionary):
  61     """ Returns true if all conditions match, false if any fail to match """
  62     for condition in conditions:
  63         if not condition['func'](condition,dictionary):
  64             return False
  65 
  66     return True
  67 
  68 def execute(macro, args):
  69     request = macro.request
  70     args = [ arg.strip() for arg in args.split(',') ]
  71     conditions = []
  72     transpose = 0 ## if we transpose the results
  73     needle = args[0]
  74     if not needle:
  75         return macro.formatter.text("It is probably not a good idea to parse all pages as dicts? You must specify a title filter")
  76 
  77     try:
  78         args = args[1:]
  79     except IndexError:
  80         args = []
  81 
  82     ## Remove the args which are conditions and look for transpose flag
  83     for i in range(len(args)):
  84         condition = parse_condition(args[i])
  85         if condition:
  86             conditions.append(condition)
  87             args[i]=None
  88         elif 'transpose' in args[i]:
  89             transpose = 1
  90             args[i]=None
  91 
  92     results = request.rootpage.getPageList(filter=re.compile(needle).search)
  93     results.sort()
  94 
  95     table = TupleDataset()
  96     table.addRow( [ '', ] + [ arg for arg in args if arg ])
  97 
  98     ## Look at all the pages that matched our string
  99     for page in results:
 100         dictionary = wikidicts.Dict(request, page)
 101 
 102         ## Check to make sure that the conditons are matched
 103         if not match_conditions(conditions, dictionary):
 104             continue
 105 
 106         ## Add the row to the table
 107         row = [ Page(request, page).link_to(request) ]
 108         cols = [ dictionary.get(column,'') for column in args if column ]
 109 
 110         ## look if we have something in a column at all
 111         found = [ 1 for col in cols if col ]
 112 
 113         ## only print the row if it has something
 114         if found:
 115                 table.addRow(row + cols)
 116 
 117     if transpose:
 118         ## transpose table (make column rows and rows columns)
 119         table.data = map(None, zip(*table.data))
 120     ## create the top columns
 121     table.columns = [ Column(col, label=col, align='right') for col in table.data[0] ]
 122     ## remove the row extracted as the column
 123     table.data = table.data[1:]
 124     tmp = DataBrowserWidget(request)
 125     tmp.setData(table)
 126     return tmp.toHTML()

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.
  • [get | view] (2006-09-03 14:07:57, 3.4 KB) [[attachment:PageDicts-0.2.py]]
  • [get | view] (2006-12-22 22:23:43, 4.0 KB) [[attachment:PageDicts-0.3.py]]
  • [get | view] (2008-06-06 11:04:38, 4.1 KB) [[attachment:PageDicts-0.4.py]]
  • [get | view] (2008-10-06 21:25:15, 4.2 KB) [[attachment:PageDicts-0.5.py]]
  • [get | view] (2011-07-19 11:57:52, 4.7 KB) [[attachment:PageDicts-0.6.py]]
  • [get | view] (2009-02-09 21:23:53, 28.9 KB) [[attachment:PageDicts-traceback.html]]
  • [get | view] (2006-09-03 13:19:03, 3.2 KB) [[attachment:PageDicts.py]]
  • [get | view] (2008-06-06 11:02:01, 25.4 KB) [[attachment:traceback.html]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.