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