-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyWriter.java
More file actions
300 lines (252 loc) · 8.85 KB
/
PyWriter.java
File metadata and controls
300 lines (252 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Copyright (c)2017 Jython Developers */
package org.python.modules._csv;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFloat;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
import org.python.expose.ExposedType;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedGet;
/**
* CSV file writer.
*
* Analogous to CPython's _csv.c::WriterObj struct.
*/
@ExposedType(name = "_csv.writer", doc = PyWriter.writer_doc)
public class PyWriter extends PyObject implements Traverseproc {
public static final String writer_doc = "CSV writer\n\n"//
+ "Writer objects are responsible for generating tabular data\n"
+ "in CSV format from sequence input.\n";
public static final PyType TYPE = PyType.fromClass(PyWriter.class);
/** Parsing dialect. */
@ExposedGet
public PyDialect dialect;
/** Output lines writer callable. */
private PyObject writeline;
/** Buffer for parser.join. */
private StringBuffer rec;
/** Length of record. */
private int rec_len = 0;
/** Number of fields in record. */
private int num_fields = 0;
/** Whether field should be quoted during a join. */
private boolean quoted = false;
public PyWriter(PyObject writeline, PyDialect dialect) {
this.writeline = writeline;
this.dialect = dialect;
}
public static PyString __doc__writerows = Py.newString(//
"writerows(sequence of sequences)\n\n"
+ "Construct and write a series of sequences to a csv file. Non-string\n"
+ "elements will be converted to string.");
public void writerows(PyObject seqseq) {
writer_writerows(seqseq);
}
@ExposedMethod
final void writer_writerows(PyObject seqseq) {
PyObject row_iter;
PyObject row_obj;
boolean result;
row_iter = seqseq.__iter__();
if (row_iter == null) {
throw _csv.Error("writerows() argument must be iterable");
}
while ((row_obj = row_iter.__iternext__()) != null) {
result = writerow(row_obj);
if (!result) {
break;
}
}
}
public static PyString __doc__writerow = Py.newString(//
"writerow(sequence)\n\n"
+ "Construct and write a CSV record from a sequence of fields. Non-string\n"
+ "elements will be converted to string.");
public boolean writerow(PyObject seq) {
return writer_writerow(seq);
}
@ExposedMethod
final boolean writer_writerow(PyObject seq) {
int len;
int i;
if (!seq.isSequenceType()) {
throw _csv.Error("sequence expected");
}
len = seq.__len__();
if (len < 0) {
return false;
}
// Join all fields in internal buffer.
join_reset();
for (i = 0; i < len; i++) {
PyObject field;
boolean append_ok;
quoted = false;
field = seq.__getitem__(i);
if (field == null) {
return false;
}
switch (dialect.quoting) {
case QUOTE_NONNUMERIC:
try {
field.__float__();
} catch (PyException ex) {
quoted = true;
}
break;
case QUOTE_ALL:
quoted = true;
break;
default:
quoted = false;
}
if (field instanceof PyUnicode) {
// Unicode fields get the default encoding (must yield U16 bytes).
append_ok = join_append(((PyString) field).encode(), len == 1);
} else if (field instanceof PyString) {
// Not unicode, so must be U16 bytes.
append_ok = join_append(field.toString(), len == 1);
} else if (field == Py.None) {
append_ok = join_append("", len == 1);
} else {
PyObject str;
// XXX: in 3.x this check can go away and we can just always use __str__
if (field.getClass() == PyFloat.class) {
str = field.__repr__();
} else {
str = field.__str__();
}
if (str == null) {
return false;
}
append_ok = join_append(str.toString(), len == 1);
}
if (!append_ok) {
return false;
}
}
// Add line terminator.
if (!join_append_lineterminator()) {
return false;
}
writeline.__call__(new PyString(rec.toString()));
return true;
}
private void join_reset() {
rec_len = 0;
num_fields = 0;
quoted = false;
rec = new StringBuffer();
}
private boolean join_append_lineterminator() {
rec.append(dialect.lineterminator);
return true;
}
private boolean join_append(String field, boolean quote_empty) {
int rec_len;
rec_len = join_append_data(field, quote_empty, false);
if (rec_len < 0) {
return false;
}
this.rec_len = join_append_data(field, quote_empty, true);
num_fields++;
return true;
}
/**
* This method behaves differently depending on the value of copy_phase: if copy_phase is false,
* then the method determines the new record length. If copy_phase is true then the new field is
* appended to the record.
*/
private int join_append_data(String field, boolean quote_empty, boolean copy_phase) {
int i;
// If this is not the first field we need a field separator.
if (num_fields > 0) {
addChar(dialect.delimiter, copy_phase);
}
// Handle preceding quote
if (copy_phase && quoted) {
addChar(dialect.quotechar, copy_phase);
}
// parsing below is based on _csv.c which expects all strings to be terminated
// with a nul byte.
field += '\0';
// Copy/count field data.
for (i = 0;; i++) {
char c = field.charAt(i);
boolean want_escape = false;
if (c == '\0') {
break;
}
if (c == dialect.delimiter || c == dialect.escapechar || c == dialect.quotechar
|| dialect.lineterminator.indexOf(c) > -1) {
if (dialect.quoting == QuoteStyle.QUOTE_NONE) {
want_escape = true;
} else {
if (c == dialect.quotechar) {
if (dialect.doublequote) {
addChar(dialect.quotechar, copy_phase);
} else {
want_escape = true;
}
}
if (!want_escape) {
quoted = true;
}
}
if (want_escape) {
if (dialect.escapechar == '\0') {
throw _csv.Error("need to escape, but no escapechar set");
}
addChar(dialect.escapechar, copy_phase);
}
}
// Copy field character into record buffer.
addChar(c, copy_phase);
}
// If field is empty check if it needs to be quoted.
if (i == 0 && quote_empty) {
if (dialect.quoting == QuoteStyle.QUOTE_NONE) {
throw _csv.Error("single empty field record must be quoted");
} else {
quoted = true;
}
}
// Handle final quote character on field.
if (quoted) {
if (copy_phase) {
addChar(dialect.quotechar, copy_phase);
} else {
// Didn't know about leading quote until we found it necessary in field
// data - compensate for it now.
rec_len += 2;
}
}
return rec_len;
}
private void addChar(char c, boolean copy_phase) {
if (copy_phase) {
rec.append(c);
}
rec_len++;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
if (dialect != null) {
int retVal = visit.visit(dialect, arg);
if (retVal != 0) {
return retVal;
}
}
return writeline != null ? visit.visit(writeline, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == dialect || ob == writeline);
}
}