annotate MoinMoin/parser/text_csv.py @ 3311:fbcd580085de

make new csv parser compatible with empty header line as in old
author Johannes Berg <johannes AT sipsolutions DOT net>
date Tue, 18 Mar 2008 22:13:29 +0100
parents 4c14c613e275
children 25c223e1b6e4
rev   line source
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
1 # -*- coding: utf-8 -*-
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
2 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
3 MoinMoin - Parser for CSV data
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
4
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
5 This parser uses the databrowser widget to display the data.
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
6
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
7 It supports the following parser arguments:
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
8
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
9 * delimiter/separator: the delimiter to use instead of ;
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
10 * quotechar: quoting character, default off, must be ascii!
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
11 * show: comma-separated list of columns to show only
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
12 * hide: comma-separated list of columns to hide
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
13 * autofilter: comma-separated list of columns to equip with
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
14 auto-filter drop down
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
15 * name: name of the dataset
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
16 * link: comma separated list of columns that take links, separate
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
17 the link and the description with a space
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
18 * static_cols: comma-separated list of columns that are static
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
19 and present in each row
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
20 * static_vals: comma-separated list of values for those static
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
21 columns
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
22
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
23 The static column feature is only really useful if the dataset
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
24 postprocessed by some other plugin collecting data from multiple
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
25 wiki pages.
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
26
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
27 @copyright: 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
28 @license: GNU GPL, see COPYING for details.
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
29 """
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
30
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
31 from csv import reader
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
32
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
33 from MoinMoin.util.dataset import TupleDataset, Column
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
34 from MoinMoin.widget.browser import DataBrowserWidget
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
35 from MoinMoin.wikiutil import escape
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
36
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
37
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
38 Dependencies = ['time']
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
39
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
40 class Parser:
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
41 extensions = ['.csvdata']
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
42 Dependencies = []
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
43
3311
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
44 def _read_rows(self, r):
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
45 if self._first_row is not None:
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
46 yield self._first_row
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
47 for row in r:
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
48 yield row
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
49
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
50 def __init__(self, raw, request, **kw):
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
51 self.request = request
3311
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
52 self._first_row = None
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
53 formatter = request.formatter
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
54
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
55 # workaround csv.reader deficiency by encoding to utf-8
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
56 data = raw.encode('utf-8').split('\n')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
57
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
58 visible = None
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
59 hiddenindexes = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
60 hiddencols = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
61 autofiltercols = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
62 staticcols = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
63 staticvals = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
64 linkcols = []
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
65 delimiter = ';'
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
66 quotechar = '\x00' # can't be entered
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
67 name = None
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
68 hdr = reader([kw.get('format_args', '').strip().encode('utf-8')], delimiter=" ")
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
69 args = hdr.next()
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
70
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
71 for arg in args:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
72 arg = arg.decode('utf-8')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
73 try:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
74 key, val = arg.split('=', 1)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
75 except:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
76 # handle compatibility with original 'csv' parser
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
77 if arg.startswith('-'):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
78 try:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
79 hiddenindexes.append(int(arg[1:]) - 1)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
80 except ValueError:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
81 pass
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
82 else:
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
83 delimiter = arg.encode('utf-8')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
84 continue
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
85 if key == 'separator' or key == 'delimiter':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
86 delimiter = val.encode('utf-8')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
87 if key == 'quotechar':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
88 if val == val.encode('utf-8'):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
89 quotechar = val.encode('utf-8')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
90 elif key == 'show':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
91 visible = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
92 elif key == 'hide':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
93 hiddencols = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
94 elif key == 'autofilter':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
95 autofiltercols = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
96 elif key == 'name':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
97 name = val
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
98 elif key == 'static_cols':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
99 staticcols = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
100 elif key == 'static_vals':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
101 staticvals = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
102 elif key == 'link':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
103 linkcols = val.split(',')
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
104
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
105 if len(staticcols) > len(staticvals):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
106 staticvals.extend([''] * (len(staticcols)-len(staticvals)))
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
107 elif len(staticcols) < len(staticvals):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
108 staticvals = staticvals[:len(staticcols)]
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
109
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
110 r = reader(data, delimiter=delimiter, quotechar=quotechar)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
111 cols = map(lambda x: x.decode('utf-8'), r.next()) + staticcols
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
112
3311
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
113 self._show_header = True
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
114
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
115 if cols == staticcols:
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
116 try:
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
117 self._first_row = map(lambda x: x.decode('utf-8'), r.next())
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
118 cols = [None] * len(self._first_row) + staticcols
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
119 self._show_header = False
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
120 except StopIteration:
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
121 pass
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
122
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
123 num_entry_cols = len(cols) - len(staticcols)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
124
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
125 if not visible is None:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
126 for col in cols:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
127 if not col in visible:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
128 hiddencols.append(col)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
129
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
130 linkparse = [False] * len(cols)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
131
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
132 data = TupleDataset(name)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
133 for colidx in range(len(cols)):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
134 col = cols[colidx]
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
135 autofilter = col in autofiltercols
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
136 hidden = col in hiddencols or colidx in hiddenindexes
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
137 data.columns.append(Column(col, autofilter=autofilter, hidden=hidden))
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
138
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
139 linkparse[colidx] = col in linkcols
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
140
3311
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
141 for row in self._read_rows(r):
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
142 row = map(lambda x: x.decode('utf-8'), row)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
143 if len(row) > num_entry_cols:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
144 row = row[:num_entry_cols]
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
145 elif len(row) < num_entry_cols:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
146 row.extend([''] * (num_entry_cols-len(row)))
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
147 row += staticvals
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
148 for colidx in range(len(row)):
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
149 item = row[colidx]
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
150 if linkparse[colidx]:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
151 try:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
152 url, item = item.split(' ', 1)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
153 if url == '':
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
154 display = escape(item)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
155 else:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
156 display = ''.join([
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
157 formatter.url(1, url=url),
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
158 formatter.text(item),
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
159 formatter.url(0)])
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
160 except ValueError:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
161 display = escape(item)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
162 else:
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
163 display = escape(item)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
164 row[colidx] = (display, item)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
165 data.addRow(tuple(row))
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
166 self.data = data
0
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
167
77665d8e2254 tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0
Thomas Waldmann <tw-public@gmx.de>
parents:
diff changeset
168 def format(self, formatter):
3311
fbcd580085de make new csv parser compatible with empty header line as in old
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 3309
diff changeset
169 browser = DataBrowserWidget(self.request, show_header=self._show_header)
3309
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
170 browser.setData(self.data)
4c14c613e275 new csv parser, uses data browser widget and supports much more, is backward compatible
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2286
diff changeset
171 self.request.write(browser.format())