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.You are not allowed to attach a file to this page.