Attachment 'table.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Parser for CSV data
   4 
   5     This is just a copy of the old CSV processor.
   6     I think it is intended as an example, cause it
   7     lacks to flexibility to read arbitary csv dialects.
   8 
   9     Perhaps this should be rewritten using another CSV lib
  10     because the standard module csv does not support unicode.
  11 
  12     @copyright: 2004 by Oliver Graf <ograf@bitart.de>, Alexander Schremmer
  13     @license: GNU GPL, see COPYING for details.
  14 """
  15 
  16 from MoinMoin import Page
  17 from MoinMoin.parser import wiki
  18 
  19 Dependencies = []
  20 
  21 class Parser:
  22     """ Format CSV data as table
  23     """
  24 
  25     extensions = ['.csv']
  26     Dependencies = []
  27 
  28     def __init__(self, raw, request, **kw):
  29         """ Store the source text.
  30         """
  31         self.raw = raw
  32         self.request = request
  33         self.form = request.form
  34         self._ = request.getText
  35 
  36         # parse extra arguments for excludes
  37         self.exclude = []
  38         self.separator = ';'
  39         for arg in kw.get('format_args','').split():
  40             if arg[0] == '-':
  41                 try:
  42                     idx = int(arg[1:])
  43                 except ValueError:
  44                     pass
  45                 else:
  46                     self.exclude.append(idx-1)
  47             else:
  48                 self.separator = arg
  49 
  50     def format(self, formatter):
  51         """ Parse and send the table.
  52         """
  53         lines = self.raw.split('\n')
  54         if lines[0]:
  55             # expect column headers in first line
  56             first = 1
  57         else:
  58             # empty first line, no bold headers
  59             first = 0
  60             del lines[0]
  61 
  62         self.request.write(formatter.table(1))
  63         for line in lines:
  64             self.request.write(formatter.table_row(1))
  65             cells = line.split(self.separator)
  66             for idx in range(len(cells)):
  67                 if idx in self.exclude:
  68                     continue
  69                 self.request.write(formatter.table_cell(1))
  70                 if first:
  71                     self.request.write(formatter.strong(1))
  72                 wikiizer = wiki.Parser(cells[idx], self.request)
  73                 wikiizer.format(formatter)
  74                 if first:
  75                     self.request.write(formatter.strong(0))
  76                 self.request.write(formatter.table_cell(0))
  77             self.request.write(formatter.table_row(0))
  78             first = 0
  79         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] (2006-02-09 23:51:37, 2.4 KB) [[attachment:table.py]]
 All files | Selected Files: delete move to page copy to page

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