summaryrefslogtreecommitdiff
path: root/demo/fastcsv.py
blob: 7f04107242c521f05af54c9da84fc6364cedfa05 (plain)
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
import csv
import cffi

# IN-PROGRESS.  See the demo at the end of the file


dialect2ffi = {}

def _make_ffi_from_dialect(dialect):

    ffi = cffi.FFI()

    ffi.cdef("""
        long parse_line(char *rawline, long inputlength);
    """)

    d = {'quotechar': ord(dialect.quotechar),
         'quoting': int(dialect.quoting),
         'skipinitialspace': int(dialect.skipinitialspace),
         'delimiter': ord(dialect.delimiter),
         'doublequote': int(dialect.doublequote),
         'strict': int(dialect.strict),
         }
    if dialect.escapechar is not None:
        d['is_escape_char'] = '== %d' % ord(dialect.escapechar)
    else:
        d['is_escape_char'] = '&& 0'

    lib = ffi.verify(r'''

    typedef enum {
        START_RECORD, START_FIELD, ESCAPED_CHAR, IN_FIELD,
        IN_QUOTED_FIELD, ESCAPE_IN_QUOTED_FIELD, QUOTE_IN_QUOTED_FIELD,
        EAT_CRNL
    } ParserState;

    typedef enum {
        QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
    } QuoteStyle;

    typedef struct {
        ParserState state;          /* current CSV parse state */
        char *field;                /* build current field in here */
        int field_size;             /* size of allocated buffer */
        int field_len;              /* length of current field */
        int numeric_field;          /* treat field as numeric */
    } ReaderObj;

    static void
    parse_add_char(ReaderObj *self, char c)
    {
        *self->field++ = c;
    }

    static void
    parse_save_field(ReaderObj *self)
    {
        *self->field++ = 0;
    }

    static int
    parse_process_char(ReaderObj *self, char c)
    {
        switch (self->state) {
        case START_RECORD:
            /* start of record */
            if (c == '\0')
                /* empty line - return [] */
                break;
            else if (c == '\n' || c == '\r') {
                self->state = EAT_CRNL;
                break;
            }
            /* normal character - handle as START_FIELD */
            self->state = START_FIELD;
            /* fallthru */
        case START_FIELD:
            /* expecting field */
            if (c == '\n' || c == '\r' || c == '\0') {
                /* save empty field - return [fields] */
                parse_save_field(self);
                self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
            }
            else if (c == %(quotechar)d &&
                     %(quoting)d != QUOTE_NONE) {
                /* start quoted field */
                self->state = IN_QUOTED_FIELD;
            }
            else if (c %(is_escape_char)s) {
                /* possible escaped character */
                self->state = ESCAPED_CHAR;
            }
            else if (c == ' ' && %(skipinitialspace)d)
                /* ignore space at start of field */
                ;
            else if (c == %(delimiter)d) {
                /* save empty field */
                parse_save_field(self);
            }
            else {
                /* begin new unquoted field */
                if (%(quoting)d == QUOTE_NONNUMERIC)
                    self->numeric_field = 1;
                parse_add_char(self, c);
                self->state = IN_FIELD;
            }
            break;

        case ESCAPED_CHAR:
            if (c == '\0')
                c = '\n';
            parse_add_char(self, c);
            self->state = IN_FIELD;
            break;

        case IN_FIELD:
            /* in unquoted field */
            if (c == '\n' || c == '\r' || c == '\0') {
                /* end of line - return [fields] */
                parse_save_field(self);
                self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
            }
            else if (c %(is_escape_char)s) {
                /* possible escaped character */
                self->state = ESCAPED_CHAR;
            }
            else if (c == %(delimiter)d) {
                /* save field - wait for new field */
                parse_save_field(self);
                self->state = START_FIELD;
            }
            else {
                /* normal character - save in field */
                parse_add_char(self, c);
            }
            break;

        case IN_QUOTED_FIELD:
            /* in quoted field */
            if (c == '\0')
                ;
            else if (c %(is_escape_char)s) {
                /* Possible escape character */
                self->state = ESCAPE_IN_QUOTED_FIELD;
            }
            else if (c == %(quotechar)d &&
                     %(quoting)d != QUOTE_NONE) {
                if (%(doublequote)d) {
                    /* doublequote; " represented by "" */
                    self->state = QUOTE_IN_QUOTED_FIELD;
                }
                else {
                    /* end of quote part of field */
                    self->state = IN_FIELD;
                }
            }
            else {
                /* normal character - save in field */
                parse_add_char(self, c);
            }
            break;

        case ESCAPE_IN_QUOTED_FIELD:
            if (c == '\0')
                c = '\n';
            parse_add_char(self, c);
            self->state = IN_QUOTED_FIELD;
            break;

        case QUOTE_IN_QUOTED_FIELD:
            /* doublequote - seen a quote in an quoted field */
            if (%(quoting)d != QUOTE_NONE &&
                c == %(quotechar)d) {
                /* save "" as " */
                parse_add_char(self, c);
                self->state = IN_QUOTED_FIELD;
            }
            else if (c == %(delimiter)d) {
                /* save field - wait for new field */
                parse_save_field(self);
                self->state = START_FIELD;
            }
            else if (c == '\n' || c == '\r' || c == '\0') {
                /* end of line - return [fields] */
                parse_save_field(self);
                self->state = (c == '\0' ? START_RECORD : EAT_CRNL);
            }
            else if (!%(strict)d) {
                parse_add_char(self, c);
                self->state = IN_FIELD;
            }
            else {
                /* illegal */
                /*PyErr_Format(error_obj, "'%%c' expected after '%%c'",
                                dialect->delimiter,
                                dialect->quotechar);*/
                return -1;
            }
            break;

        case EAT_CRNL:
            if (c == '\n' || c == '\r')
                ;
            else if (c == '\0')
                self->state = START_RECORD;
            else {
                /*PyErr_Format(error_obj, "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?");*/
                return -1;
            }
            break;

        }
        return 0;
    }

    static void
    parse_reset(ReaderObj *self, char *rawline)
    {
        self->field = rawline;
        self->state = START_RECORD;
        self->numeric_field = 0;
    }

    long parse_line(char *rawline, long inputlength)
    {
        char *p;
        ReaderObj reader;
        parse_reset(&reader, rawline);

        for (p=rawline; inputlength > 0; inputlength--, p++) {
            if (parse_process_char(&reader, *p) < 0)
                return -1;
        }
        if (parse_process_char(&reader, 0) < 0)
            return -1;
        return reader.field - rawline - 1;
    }
    ''' % d)

    return ffi, lib


def fastcsv_reader(f, dialect):
    dialect = csv.get_dialect(dialect)
    try:
        ffi, lib = dialect2ffi[dialect]
    except KeyError:
        ffi, lib = dialect2ffi[dialect] = _make_ffi_from_dialect(dialect)
    #
    linelen = -1
    for line in f:
        if linelen <= len(line):
            linelen = 2 * len(line)
            rawline = ffi.new("char[]", linelen)
        ffi.buffer(rawline, len(line))[:] = line
        n = lib.parse_line(rawline, len(line))
        assert n >= 0
        yield ffi.buffer(rawline, n)[:].split('\x00')


if __name__ == '__main__':
    csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
    with open('/etc/passwd', 'rb') as f:
        reader = fastcsv_reader(f, 'unixpwd')
        for row in reader:
            print row