Attachment 'PageDicts.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 config, wikiutil, search, 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 c,d: not c['regex'].search(d[c['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 c,d: c['regex'].search(d[c['column']])
  52             )
  53 
  54         return result
  55 
  56 def match_conditions(conditions, d):
  57     """ Returns true if all conditions match, false if any fail to match """
  58     for c in conditions:
  59         if not c['func'](c,d):
  60             return False
  61 
  62     return True
  63 
  64 def execute(macro, args):
  65     request = macro.request
  66     args = [a.strip() for a 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         c = parse_condition(args[i])
  80         if c:
  81             conditions.append(c)
  82             args[i]=None
  83         
  84     query = search.QueryParser(titlesearch=1).parse_query(needle)
  85     results = search.searchPages(request, query)
  86     results.sortByPagename()
  87 
  88     table = TupleDataset()
  89     table.columns = [  Column('', label='', align='right'), ] +[ 
  90         Column(x, label=x, align='right') for x in args if x ]
  91 
  92     ## Look at all the hits from our title filter
  93     for x in results.hits:
  94         d = wikidicts.Dict(request, x.page_name)
  95         
  96         ## Check to make sure that the conditons are matched
  97         if not match_conditions(conditions, d): continue
  98 
  99         ## Add the row to the table
 100         r = [ Page(request, x.page_name).link_to(request) ] + [ d.get(y,'') for y in args if y ]
 101         table.addRow(r)
 102 
 103     tmp = DataBrowserWidget(request)
 104     tmp.setData(table)
 105     tmp.render()
 106 
 107     return ''

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.