Attachment 'ShowCSV-1.6.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ShowCSV
   4 
   5     This macro is used to show csv data as wiki table
   6 
   7     default extension is .csv
   8     you can change that with the args parameter
   9 
  10     First element of each column in csv table is used as column description
  11 
  12     (I have used the wiki parsers syntax, because then its quite easy to change that
  13      routine to add the csv content to the page as wiki content
  14      but its not clear to me if this is wanted)
  15 
  16     @copyright: 2007 MoinMoin:ReimarBauer
  17     @license: GNU GPL, see COPYING for details.
  18 """
  19 
  20 Dependencies = ['time'] # do not cache
  21 
  22 import os, codecs, csv
  23 from MoinMoin import config, wikiutil
  24 from MoinMoin.action import AttachFile
  25 from MoinMoin.parser.text_moin_wiki import Parser
  26 
  27 def utf_8_encoder(unicode_csv_data):
  28     for line in unicode_csv_data:
  29         yield line.encode('utf-8')
  30 
  31 def execute(macro, args):
  32     request = macro.request
  33     formatter = macro.formatter
  34 
  35     if args is None:
  36         extension = '.csv'
  37     else:
  38         extension = args
  39 
  40     pagename = formatter.page.page_name
  41     files = AttachFile._get_files(request, pagename)
  42     attach_dir = AttachFile.getAttachDir(request, pagename)
  43 
  44     for file in files:
  45         if file.lower().endswith(extension.lower()):
  46             file_id = codecs.open(os.path.join(attach_dir, file), 'rb', config.charset)
  47             reader = csv.reader(utf_8_encoder(file_id))
  48             index = 0
  49             result = ""
  50             for row in reader:
  51                 if index == 0:
  52                     result += "|| '''"
  53                     result += "''' || '''".join(row)
  54                     result += "''' ||\n"
  55                 else:
  56                     result += '|| '
  57                     result += '|| '.join(row)
  58                     result += ' ||\n'
  59                 index += 1
  60 
  61             result += ' . \n'
  62             result = wikiutil.url_unquote(result)
  63             result += '[[attachment:%s]]' % file
  64 
  65             result = wikiutil.escape(result)
  66             p = Parser(result, request)
  67             p.format(request.formatter)
  68 
  69     return ""

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] (2007-10-27 12:02:02, 38.3 KB) [[attachment:CSVinMoinWithShowCSVMacro.jpg]]
  • [get | view] (2007-10-27 12:01:41, 41.3 KB) [[attachment:CSVinSpreadsheet.jpg]]
  • [get | view] (2008-01-10 13:11:03, 2.0 KB) [[attachment:ShowCSV-1.6.py]]
  • [get | view] (2007-10-27 15:05:58, 2.1 KB) [[attachment:ShowCSV.py]]
  • [get | view] (2008-07-21 20:03:20, 3.1 KB) [[attachment:ShowCSV2.py]]
  • [get | view] (2007-10-27 12:01:11, 0.5 KB) [[attachment:dummy.csv]]
  • [get | view] (2007-10-27 18:44:59, 43.0 KB) [[attachment:example.png]]
  • [get | view] (2007-09-13 09:27:38, 6.4 KB) [[attachment:showcsv.png]]
 All files | Selected Files: delete move to page copy to page

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