Attachment 'FlowTable.py'

Download

   1 """
   2 
   3     MoinMoin - Processor for turning long lists of data into simple
   4     tables for a more compact display.  Loosly based on CSV processor.
   5 
   6     (C) 2002 Robert Kleemann <robertk@oz.net>
   7 
   8     Data is considered to be line separated cells that contain wiki
   9     markup.  Each line will become a cell in a wiki table.  Lines may
  10     contain table markup (e.g. <:> for centered)
  11 
  12     Arguments are the following in any order
  13 
  14     integer      : number of columns for the table
  15     top-bottom   : fill rows from top to bottom
  16     bottom-top   : fill rows from bottom to top
  17     right-left   : fill rows from right to left
  18     left-right   : fill rows from left to right
  19     row-major    : fill rows first, columns second
  20     column-major : fill columns first, rows second
  21     sort         : alphanumerically sort the data before
  22                    filling the table
  23 
  24     mutually exclusive items are top-bottom/bottom-top,
  25     right-left/left-right and row-major/column-major.
  26 
  27     Default is: 2 top-bottom left-right row-major
  28 
  29     examples
  30 
  31     {{{#!FlowTable
  32     A
  33     B
  34     C
  35     D
  36     E
  37     }}}
  38 
  39     A B
  40     C D
  41     E
  42 
  43     {{{#!FlowTable 3 column-major
  44     ...
  45 
  46     A C E
  47     B D
  48 
  49     {{{#!FlowTable right-left bottom-top
  50     ...
  51 
  52       E
  53     D C
  54     B A
  55 
  56 """
  57 
  58 import string, sys
  59 
  60 from MoinMoin.parser import text_moin_wiki as wiki
  61 
  62 # c'mon Guido, give us C's ternary operator
  63 IF = lambda a,b,c:(a and [b] or [c])[0]
  64 
  65 Dependencies = []
  66 
  67 class Parser:
  68     extensions = '*'
  69     Dependencies = []
  70         
  71 
  72     def __init__(self, raw, request, **kw):
  73         self.raw = raw
  74         self.request = request
  75         self.form = request.form
  76         self.args = kw.get('format_args', '')
  77         self._ = request.getText
  78 
  79     def format(self, formatter):
  80         """ Send the text. """
  81         lines = self.raw.split('\n')
  82 
  83         # parse firstline arguments
  84         yo = "top-bottom"
  85         xo = "left-right"
  86         first = "row-major"
  87         sort = 0
  88         cols = 2
  89         for arg in string.split(self.args):
  90             if arg=="top-bottom" or arg=="bottom-top":
  91                 yo=arg
  92             elif arg=="right-left" or arg=="left-right":
  93                 xo=arg
  94             elif arg=="row-major" or arg=="column-major":
  95                 first=arg
  96             elif arg=="sort":
  97                 sort=1
  98             else:
  99                 try:
 100                     cols = int(arg)
 101                 except ValueError:
 102                     pass
 103 
 104         if sort:
 105             lines.sort()
 106 
 107         # make the new matrix in the correct order
 108         rows = (len(lines)-1) / cols + 1
 109         size = rows*cols
 110         matrix = []
 111         for i in range(size):
 112             matrix.append(" ")
 113         if yo=="top-bottom":
 114             if xo=="left-right":
 115                 i=0
 116                 if first=="row-major":
 117                     inc=lambda i,rows,cols,size: i+1
 118                 else:
 119                     inc=lambda i,rows,cols,size: IF(i/cols==rows-1,i+1-(size-cols),i+cols)
 120             else:
 121                 i=cols-1
 122                 if first=="row-major":
 123                     inc=lambda i,rows,cols,size: IF(i%cols==0,i+cols*2-1,i-1)
 124                 else:
 125                     inc=lambda i,rows,cols,size: IF(i/cols==rows-1,i-1-(size-cols),i+cols)
 126         else:
 127             if xo=="left-right":
 128                 i=size-rows
 129                 if first=="row-major":
 130                     inc=lambda i,rows,cols,size: IF(i%cols==cols-1,i-cols*2+1,i+1)
 131                 else:
 132                     inc=lambda i,rows,cols,size: IF(i/cols==0,i+1+(size-cols),i-cols)
 133             else:
 134                 i=size-1
 135                 if first=="row-major":
 136                     inc=lambda i,rows,cols,size: i-1
 137                 else:
 138                     inc=lambda i,rows,cols,size: IF(i/cols==0,i-1+(size-cols),i-cols)
 139         i2=0
 140         while i2<len(lines):
 141             matrix[i]=lines[i2]
 142             i = inc(i,rows,cols,size)
 143             i2=i2+1
 144 
 145         # create output list
 146         output = []
 147         for r in range(rows):
 148             row_of_cells = matrix[r*cols:(r+1)*cols]
 149             output.append("||"+string.join(row_of_cells,"||")+"||")
 150 
 151         wikiizer = wiki.Parser(string.join(output,'\n'),self.request)
 152         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] (2010-09-19 23:39:09, 4.1 KB) [[attachment:FlowTable.py]]
 All files | Selected Files: delete move to page copy to page

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