Attachment 'SimpleTable.py'

Download

   1 """
   2     MoinMoin - Processor for turning long lists of data into simple
   3     tables for a more compact display.  Loosly based on CSV processor.
   4 
   5     Data is considered to be line separated cells that contain wiki
   6     markup.  Each line will become a cell in a wiki table.  Lines may
   7     contain table markup (e.g. <:> for centered).
   8 
   9     Arguments are the following in any order:
  10 
  11     integer      : number of columns for the table
  12     top-bottom   : fill rows from top to bottom
  13     bottom-top   : fill rows from bottom to top
  14     right-left   : fill rows from right to left
  15     left-right   : fill rows from left to right
  16     row-major    : fill rows first, columns second
  17     column-major : fill columns first, rows second
  18     sort         : alphanumerically sort the data before
  19                    filling the table
  20 
  21     Mutually exclusive items are top-bottom/bottom-top,
  22     right-left/left-right and row-major/column-major.
  23 
  24     Default is: 2 top-bottom left-right row-major
  25 
  26     Examples:
  27 
  28     {{{#!SimpleTable
  29     A
  30     B
  31     C
  32     D
  33     E
  34     }}}
  35 
  36     A B
  37     C D
  38     E
  39 
  40     {{{#!SimpleTable 3 column-major
  41     ...
  42 
  43     A C E
  44     B D
  45 
  46     {{{#!SimpleTable right-left bottom-top
  47     ...
  48 
  49       E
  50     D C
  51     B A
  52 
  53     @copyright: 2002 Robert Kleemann <robertk@oz.net>
  54     @copyright: Updated for MoinMoin 1.1 by Michael Geary <Mike@Geary.com>
  55     @copyright: Updated for MoinMoin 1.2 by Thomas Waldmann
  56     @license: GPL, see COPYING for defaults
  57 """
  58 
  59 from MoinMoin.parser import wiki
  60 
  61 # c'mon Guido, give us C's ternary operator
  62 IF = lambda a,b,c:(a and [b] or [c])[0]
  63     
  64 def process(request, formatter, lines):
  65     # parse bangpath for arguments
  66     yo = "top-bottom"
  67     xo = "left-right"
  68     first = "row-major"
  69     sort = False
  70     cols = 2
  71     for arg in lines[0].split()[1:]:
  72         if arg=="top-bottom" or arg=="bottom-top":
  73             yo=arg
  74         elif arg=="right-left" or arg=="left-right":
  75             xo=arg
  76         elif arg=="row-major" or arg=="column-major":
  77             first = arg
  78         elif arg=="sort":
  79             sort = True
  80         else:
  81             try:
  82                 cols = int(arg)
  83             except ValueError:
  84                 pass
  85 
  86     # remove bang path
  87     del lines[0]
  88 
  89     if sort:
  90         lines.sort()
  91 
  92     # make the new matrix in the correct order
  93     rows = (len(lines)-1) / cols + 1
  94     size = rows*cols
  95     matrix = [" "]*size
  96 
  97     if yo=="top-bottom":
  98         if xo=="left-right":
  99             i = 0
 100             if first=="row-major":
 101                 inc = lambda i, rows, cols, size: i+1
 102             else:
 103                 inc = lambda i, rows, cols, size: IF(i/cols==rows-1, i+1-(size-cols), i+cols)
 104         else:
 105             i = cols-1
 106             if first=="row-major":
 107                 inc = lambda i, rows, cols, size: IF(i%cols==0, i+cols*2-1, i-1)
 108             else:
 109                 inc = lambda i, rows, cols, size: IF(i/cols==rows-1, i-1-(size-cols), i+cols)
 110     else:
 111         if xo=="left-right":
 112             i = size-rows
 113             if first=="row-major":
 114                 inc = lambda i, rows, cols, size: IF(i%cols==cols-1, i-cols*2+1, i+1)
 115             else:
 116                 inc = lambda i, rows, cols, size: IF(i/cols==0, i+1+(size-cols), i-cols)
 117         else:
 118             i = size-1
 119             if first=="row-major":
 120                 inc = lambda i, rows, cols, size: i-1
 121             else:
 122                 inc = lambda i, rows, cols, size: IF(i/cols==0, i-1+(size-cols), i-cols)
 123     
 124     for line in lines:
 125         matrix[i] = line
 126         i = inc(i, rows, cols, size)
 127 
 128     # create output list
 129     output = []
 130     for r in range(rows):
 131         row_of_cells = matrix[r*cols:(r+1)*cols]
 132         output.append("||%s||" % "||".join(row_of_cells))
 133     wikiizer = wiki.Parser("\n".join(output), request)
 134     wikiizer.format(formatter)

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] (2004-05-05 21:34:20, 10.3 KB) [[attachment:AbcMusic.py]]
  • [get | view] (2004-04-15 07:19:38, 3.4 KB) [[attachment:DataLanguage.py]]
  • [get | view] (2004-04-15 20:23:33, 2.5 KB) [[attachment:GANTT-1.2.1.py]]
  • [get | view] (2003-12-07 18:15:55, 2.4 KB) [[attachment:GANTT.py]]
  • [get | view] (2004-07-29 15:04:28, 12.1 KB) [[attachment:IndentTable.py]]
  • [get | view] (2004-10-05 13:12:16, 10.6 KB) [[attachment:MySQL.py]]
  • [get | view] (2004-03-27 18:55:57, 3.7 KB) [[attachment:SimpleTable.py]]
  • [get | view] (2003-12-07 18:15:55, 1.5 KB) [[attachment:TextOnRight.py]]
  • [get | view] (2004-09-17 06:53:46, 3.0 KB) [[attachment:awktable-1.2.3.py]]
  • [get | view] (2004-10-28 13:55:04, 1.0 KB) [[attachment:colorer.py]]
  • [get | view] (2004-11-04 00:26:37, 1.4 KB) [[attachment:csv_python_module.diff]]
  • [get | view] (2004-04-21 18:29:38, 0.2 KB) [[attachment:html.py]]
  • [get | view] (2004-08-16 10:59:24, 4.8 KB) [[attachment:latex-1.2.3.py]]
  • [get | view] (2004-08-07 07:36:59, 3.9 KB) [[attachment:latex-cygwin.py]]
  • [get | view] (2004-04-09 17:05:33, 2.9 KB) [[attachment:latex.1.2.1.py]]
  • [get | view] (2004-02-29 16:50:13, 2.1 KB) [[attachment:latex.py]]
 All files | Selected Files: delete move to page copy to page

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