Attachment 'ShowCSV2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ShowCSV2
   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     extension=<namen> (extension=csv)
  10    
  11     default delimiter is ; (semicolon)
  12     you can change that with the args paramter:
  13     delimiter = semicolon | comma | pipe (delimiter=pipe)
  14 
  15     First element of each column in csv table is used as column description
  16 
  17     History:
  18     07-21-2008: Added some arguments for the extension and the delimiter 
  19                 MoinMoin:MarcelHäfner
  20 
  21     @copyright: 2007 MoinMoin:ReimarBauer
  22     @license: GNU GPL, see COPYING for details.
  23 """
  24 
  25 Dependencies = ['time'] # do not cache
  26 
  27 import os, codecs, csv
  28 from MoinMoin import config, wikiutil
  29 from MoinMoin.action import AttachFile
  30 from MoinMoin.parser.text_moin_wiki import Parser
  31 
  32 def utf_8_encoder(unicode_csv_data):
  33     for line in unicode_csv_data:
  34         yield line.encode('utf-8')
  35 
  36 def execute(macro, args):
  37     request = macro.request
  38     formatter = macro.formatter
  39 
  40     if args is None:
  41         args = u''
  42     try:
  43         # Parse given arguments
  44         params, named_params, trailing = wikiutil.parse_quoted_separated(args)
  45         # Unpack parameters; but no default parameter are needed
  46     except (ValueError, TypeError), err:
  47         return macro.format_error(err)
  48 
  49     # get the params and add some default, also lower them
  50     extension = wikiutil.escape(named_params.get("extension", "csv")).lower()
  51     delimiter = wikiutil.escape(named_params.get("delimiter", "semikolon")).lower()
  52 
  53     class SKV(csv.excel):
  54         # class for the csv.reader 
  55         # default delimiter is the semicolon
  56         delimiter = ";"
  57 
  58     # parse the args for a delimiter, if no match the default in class SKV is used
  59     if delimiter == "semicolon":
  60         SKV.delimiter = ";"
  61     if delimiter == "comma":
  62         SKV.delimiter = ","
  63     if delimiter == "pipe":
  64        SKV.delimiter = "|"
  65     
  66     #register the SKV for the csv.reader later
  67     csv.register_dialect("SKV", SKV)
  68 
  69     pagename = formatter.page.page_name
  70     files = AttachFile._get_files(request, pagename)
  71     attach_dir = AttachFile.getAttachDir(request, pagename)
  72 
  73     for file in files:
  74         if file.lower().endswith(extension.lower()):
  75             file_id = codecs.open(os.path.join(attach_dir, file), 'rb', config.charset)
  76             reader = csv.reader(utf_8_encoder(file_id), "SKV" )
  77             index = 0
  78             result = ""
  79             for row in reader:
  80                 if index == 0:
  81                     result += "|| '''"
  82                     result += "''' || '''".join(row)
  83                     result += "''' ||\n"
  84                 else:
  85                     result += '|| '
  86                     result += '|| '.join(row)
  87                     result += ' ||\n'
  88                 index += 1
  89 
  90             result += ' . \n'
  91             result = wikiutil.url_unquote(result)
  92             result += '[[attachment:%s]]' % file
  93 
  94             result = wikiutil.escape(result)
  95             p = Parser(result, request)
  96             p.format(request.formatter)
  97 
  98     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.