Attachment 'CSV.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     @copyright: 2005 by Julian Romero <julian.romero@gmail.come> for the modifications
  14     @license: GNU GPL, see COPYING for details.
  15 """
  16 
  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         csv_table = ""
  63         for line in lines:
  64             row = line.split( self.separator )
  65             for idx in self.exclude:
  66                 del row[idx]
  67             if first:
  68                 csv_table += '|| \'\'\'' + ('\'\'\' || \'\'\''.join( row )) + '\'\'\' ||\n'
  69             else:
  70                 csv_table += '|| ' + (' || '.join( row )) + ' ||\n'
  71             first = 0
  72 
  73         wikifier = wiki.Parser( csv_table, self.request )
  74         wikifier.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] (2005-12-10 02:54:33, 2.1 KB) [[attachment:CSV.py]]
  • [get | view] (2006-10-10 10:42:36, 1.1 KB) [[attachment:box.py]]
  • [get | view] (2005-11-21 02:56:09, 0.5 KB) [[attachment:css-action.py]]
  • [get | view] (2005-11-21 02:56:33, 0.8 KB) [[attachment:css-macro.py]]
  • [get | view] (2005-11-13 12:21:38, 0.9 KB) [[attachment:patch-1.5b2-Form.py]]
  • [get | view] (2005-11-13 12:13:19, 0.8 KB) [[attachment:patch-1.5b2-wikiform.py]]
  • [get | view] (2006-05-12 12:59:21, 2.6 KB) [[attachment:todo.py]]
 All files | Selected Files: delete move to page copy to page

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