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