Attachment 'ShowCSV.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 from MoinMoin.parser.wiki import Parser
  27 
  28 def utf_8_encoder(unicode_csv_data):
  29     for line in unicode_csv_data:
  30         yield line.encode('utf-8')
  31 
  32 def execute(macro, args):
  33     request = macro.request
  34     formatter = macro.formatter
  35 
  36     if args is None:
  37         extension = '.csv'
  38     else:
  39         extension = args
  40 
  41     pagename = formatter.page.page_name
  42     files = AttachFile._get_files(request, pagename)
  43     attach_dir = AttachFile.getAttachDir(request, pagename)
  44 
  45     for file in files:
  46         if file.lower().endswith(extension.lower()):
  47             file_id = codecs.open(os.path.join(attach_dir, file), 'rb', config.charset)
  48             reader = csv.reader(utf_8_encoder(file_id))
  49             index = 0
  50             result = ""
  51             for row in reader:
  52                 if index == 0:
  53                     result += "|| '''"
  54                     result += "''' || '''".join(row)
  55                     result += "''' ||\n"
  56                 else:
  57                     result += '|| '
  58                     result += '|| '.join(row)
  59                     result += ' ||\n'
  60                 index += 1
  61 
  62             result += ' . \n'
  63             result = wikiutil.url_unquote(result)
  64             # result += '[[attachment:%s]]' % file
  65             result += 'attachment:%s' % file
  66 
  67             result = wikiutil.escape(result)
  68             p = Parser(result, request)
  69             p.format(request.formatter)
  70 
  71     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.