Attachment 'PageDicts.xapianenabled.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     If you prefix the Bug0 with an !-mark, it will search for all user
  15     readable pages wich contain Bug0 (allowing for searching a categorie)
  16     Also note, that 'open' can be any valid regular expression (include pipes)
  17 
  18     @copyright: 2006 by michael cohen <scudette@users.sourceforge.net>
  19     @license: GNU GPL, see COPYING for details.
  20     History:
  21     0.4: made more usefull by allowing categories and catching some errors
  22          by Remco Boerma
  23     0.3: Fix typo, allow the "transpose" keyword to change the table layout
  24     0.2: original version
  25 """
  26 import re
  27 from MoinMoin import wikiutil, wikidicts, search
  28 from MoinMoin.Page import Page
  29 from MoinMoin.util.dataset import TupleDataset, Column
  30 from MoinMoin.widget.browser import DataBrowserWidget
  31 
  32 Dependencies = ["pages"]
  33 
  34 def parse_condition(arg):
  35     """ arg is a string which will be parsed into a condition dict.
  36 
  37     A condition is a way of enforcing a requirement on a dict d. For example:
  38 
  39     ## This will match if regex matches d['column_name']
  40     column_name ~= regex
  41 
  42     ## This will not match if regexp matches (inverse of above)
  43     column_name !~= regex
  44     """
  45     if '!~=' in arg:
  46         tmp = arg.split("!~=",1)
  47         result=dict(
  48             column= tmp[0].strip(),
  49             regex= re.compile(tmp[1].strip(), re.IGNORECASE),
  50             func = lambda condition,dictionary: not condition['regex'].search(dictionary[condition['column']])
  51             )
  52 
  53         return result
  54     elif '~=' in arg:
  55         tmp = arg.split("~=",1)
  56         result = dict(
  57             column = tmp[0].strip(),
  58             regex = re.compile(tmp[1].strip(), re.IGNORECASE),
  59             func = lambda condition,dictionary: condition['regex'].search(dictionary[condition['column']])
  60             )
  61 
  62         return result
  63 
  64 def match_conditions(conditions, dictionary):
  65     """ Returns true if all conditions match, false if any fail to match """
  66     for condition in conditions:
  67         try:
  68 	    if not condition['func'](condition,dictionary):
  69                return False
  70 	except KeyError:
  71 	    # Remco: prolly it's a template linking or anything, and this value is not available
  72 	    return False
  73 
  74     return True
  75 
  76 def execute(macro, args):
  77     request = macro.request
  78     args = [ arg.strip() for arg in args.split(',') ]
  79     conditions = []
  80     transpose = 0 ## if we transpose the results
  81     needle = args[0]
  82     if not needle:
  83         return macro.formatter.text("It is probably not a good idea to parse all pages as dicts? You must specify a title filter")
  84 
  85     try:
  86         args = args[1:]
  87     except IndexError:
  88         args = []
  89 
  90     ## Remove the args which are conditions and look for transpose flag
  91     for i in range(len(args)):
  92         condition = parse_condition(args[i])
  93         if condition:
  94             conditions.append(condition) # remco change
  95             args[i]=None
  96         elif 'transpose' in args[i]:
  97             transpose = 1
  98             args[i]=None
  99 
 100 
 101     # RemcoBoerma -- use internal search for finding the page: allows all contstructs to perform
 102     # the search... 
 103     results = [p.page_name for p in search.searchPages(request, needle, sort='page_name').hits]
 104     #results.sort()
 105    
 106     table = TupleDataset()
 107     table.addRow( [ '', ] + [ arg for arg in args if arg ])
 108 
 109     ## Look at all the pages that matched our string
 110     for page in results:
 111         dictionary = wikidicts.Dict(request, page)
 112         
 113         ## Check to make sure that the conditons are matched
 114         if not match_conditions(conditions, dictionary):
 115             continue
 116 
 117         ## Add the row to the table
 118         row = [ Page(request, page).link_to(request) ]
 119 	cols = [ dictionary.get(column,'') for column in args if column ]
 120         
 121 	## look if we have something in a column at all
 122 	found = [ 1 for col in cols if col ]
 123 
 124 	## only print the row if it has something
 125 	if found:
 126         	table.addRow(row + cols)
 127 
 128     if transpose:
 129         ## transpose table (make column rows and rows columns)
 130         table.data = map(None, zip(*table.data))
 131     ## create the top columns
 132     table.columns = [ Column(col, label=col, align='right') for col in table.data[0] ]
 133     ## remove the row extracted as the column
 134     table.data = table.data[1:]
 135     tmp = DataBrowserWidget(request)
 136     tmp.setData(table)
 137     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.
  • [get | view] (2008-02-26 15:42:28, 5.2 KB) [[attachment:GraphViz.py]]
  • [get | view] (2009-06-09 11:32:14, 4.7 KB) [[attachment:PageDicts.xapianenabled.py]]
  • [get | view] (2008-04-23 13:09:50, 41.7 KB) [[attachment:Wiki Cheat Sheet V0.1.odg]]
  • [get | view] (2008-04-23 13:09:36, 129.9 KB) [[attachment:Wiki Cheat Sheet V0.1.pdf]]
  • [get | view] (2008-04-23 13:11:00, 36.6 KB) [[attachment:Wiki Cheat Sheet V0.1.png]]
  • [get | view] (2008-04-24 12:55:13, 67.7 KB) [[attachment:Wiki Cheat Sheet V1.0 ENG.odg]]
  • [get | view] (2008-04-24 12:54:59, 165.0 KB) [[attachment:Wiki Cheat Sheet V1.0 ENG.pdf]]
  • [get | view] (2008-04-24 12:57:34, 18.6 KB) [[attachment:Wiki Cheat Sheet V1.0.png]]
  • [get | view] (2010-03-02 19:36:44, 1.4 KB) [[attachment:pytidy.patch]]
 All files | Selected Files: delete move to page copy to page

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