comparison 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
comparison
equal deleted inserted replaced
3310:d67113dc5a89 3311:fbcd580085de
39 39
40 class Parser: 40 class Parser:
41 extensions = ['.csvdata'] 41 extensions = ['.csvdata']
42 Dependencies = [] 42 Dependencies = []
43 43
44 def _read_rows(self, r):
45 if self._first_row is not None:
46 yield self._first_row
47 for row in r:
48 yield row
49
44 def __init__(self, raw, request, **kw): 50 def __init__(self, raw, request, **kw):
45 self.request = request 51 self.request = request
52 self._first_row = None
46 formatter = request.formatter 53 formatter = request.formatter
47 54
48 # workaround csv.reader deficiency by encoding to utf-8 55 # workaround csv.reader deficiency by encoding to utf-8
49 data = raw.encode('utf-8').split('\n') 56 data = raw.encode('utf-8').split('\n')
50 57
101 staticvals = staticvals[:len(staticcols)] 108 staticvals = staticvals[:len(staticcols)]
102 109
103 r = reader(data, delimiter=delimiter, quotechar=quotechar) 110 r = reader(data, delimiter=delimiter, quotechar=quotechar)
104 cols = map(lambda x: x.decode('utf-8'), r.next()) + staticcols 111 cols = map(lambda x: x.decode('utf-8'), r.next()) + staticcols
105 112
113 self._show_header = True
114
115 if cols == staticcols:
116 try:
117 self._first_row = map(lambda x: x.decode('utf-8'), r.next())
118 cols = [None] * len(self._first_row) + staticcols
119 self._show_header = False
120 except StopIteration:
121 pass
122
106 num_entry_cols = len(cols) - len(staticcols) 123 num_entry_cols = len(cols) - len(staticcols)
107 124
108 if not visible is None: 125 if not visible is None:
109 for col in cols: 126 for col in cols:
110 if not col in visible: 127 if not col in visible:
119 hidden = col in hiddencols or colidx in hiddenindexes 136 hidden = col in hiddencols or colidx in hiddenindexes
120 data.columns.append(Column(col, autofilter=autofilter, hidden=hidden)) 137 data.columns.append(Column(col, autofilter=autofilter, hidden=hidden))
121 138
122 linkparse[colidx] = col in linkcols 139 linkparse[colidx] = col in linkcols
123 140
124 for row in r: 141 for row in self._read_rows(r):
125 row = map(lambda x: x.decode('utf-8'), row) 142 row = map(lambda x: x.decode('utf-8'), row)
126 if len(row) > num_entry_cols: 143 if len(row) > num_entry_cols:
127 row = row[:num_entry_cols] 144 row = row[:num_entry_cols]
128 elif len(row) < num_entry_cols: 145 elif len(row) < num_entry_cols:
129 row.extend([''] * (num_entry_cols-len(row))) 146 row.extend([''] * (num_entry_cols-len(row)))
147 row[colidx] = (display, item) 164 row[colidx] = (display, item)
148 data.addRow(tuple(row)) 165 data.addRow(tuple(row))
149 self.data = data 166 self.data = data
150 167
151 def format(self, formatter): 168 def format(self, formatter):
152 browser = DataBrowserWidget(self.request) 169 browser = DataBrowserWidget(self.request, show_header=self._show_header)
153 browser.setData(self.data) 170 browser.setData(self.data)
154 self.request.write(browser.format()) 171 self.request.write(browser.format())