Attachment 'csvstripes.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Parser for CSV data
   4 
   5     This parsers works like CSV parser, but with the possibility 
   6     to define colors for a zebra striped tables.
   7     
   8     It it a simple modification from CSV parser 
   9     written by Oliver Graf and Alexander Schremmer
  10 
  11 
  12     @copyright: 2010 Ulysses Almeida <ulysses.almeida@gmail.com>
  13     @license: GNU GPL, see COPYING for details.
  14 """
  15 
  16 Dependencies = []
  17 
  18 
  19 class Parser:
  20     """ Format CSV data as table
  21     """
  22 
  23     parsername = "CSVStripes"
  24     extensions = ['.csv']
  25     Dependencies = []
  26 
  27     def __init__(self, raw, request, **kw):
  28         """ Store the source text.
  29         """
  30         self.raw = raw
  31         self.request = request
  32         self.form = request.form
  33         self._ = request.getText
  34         self.color_odd=None
  35         self.color_even=None
  36 
  37         # parse extra arguments for excludes
  38         self.exclude = []
  39         self.separator = ';'
  40         for arg in kw.get('format_args', '').split():
  41             if arg[0] == '-':
  42                 try:
  43                     idx = int(arg[1:])
  44                 except ValueError:
  45                     pass
  46                 else:
  47                     self.exclude.append(idx-1)
  48             elif arg[0] == "#":
  49                 if not self.color_odd:
  50                     self.color_odd = arg
  51                 elif not self.color_even:
  52                     self.color_even = arg
  53             else:
  54                 self.separator = arg
  55         
  56         if not self.color_odd:
  57             self.color_odd="#FFFFFF"
  58         if not self.color_even:
  59             self.color_even="#DDDDDD"
  60 
  61     def format(self, formatter):
  62         """ Parse and send the table.
  63         """
  64         lines = self.raw.split('\n')
  65         if lines[0]:
  66             # expect column headers in first line
  67             first = 1
  68         else:
  69             # empty first line, no bold headers
  70             first = 0
  71             del lines[0]
  72 
  73         self.request.write(formatter.table(1))
  74         i=0
  75         for line in lines:
  76             if first:
  77                 self.request.write(formatter.table_row(1))
  78             elif i%2:
  79                 self.request.write(formatter.table_row(1,style='background-color: %s' % (self.color_odd)))
  80             else:
  81                 self.request.write(formatter.table_row(1,style='background-color: %s' % (self.color_even)))
  82             cells = line.split(self.separator)
  83             for idx in range(len(cells)):
  84                 if idx in self.exclude:
  85                     continue
  86                 self.request.write(formatter.table_cell(1))
  87                 if first:
  88                     self.request.write(formatter.strong(1))
  89                 self.request.write(formatter.text(cells[idx]))
  90                 if first:
  91                     self.request.write(formatter.strong(0))
  92                 self.request.write(formatter.table_cell(0))
  93             self.request.write(formatter.table_row(0))
  94             first = 0
  95             i+=1
  96         self.request.write(formatter.table(0))

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-01-29 19:31:35, 2.9 KB) [[attachment:csvstripes.py]]
 All files | Selected Files: delete move to page copy to page

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