-
Notifications
You must be signed in to change notification settings - Fork 0
/
formats.py
70 lines (61 loc) · 2.39 KB
/
formats.py
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
"""Defines abstract Format interface and basic format-specific parsers
Format objects are responsible for parsing a dictionary or set of dictionaries
to and from plaintext. Because this does not include specific CRUD operations
against a specific data resource, only inbound/outbound operations are performed.
The appropriate Format parser is determined by the first registered entry with a
matching .pattern property, and specific instances of Format parsers are registered
by an instance of the Serial class, which handles a quasi-global context.
For now, "dicts" inputs and outputs are assumed to have a uniform schema. Eventually,
logic will need to be added to merge all fields when parsing/generating sets of dictionaries
(though it may be more appropriate to enforce this in Serial).
"""
import csv
import sys
from seres import parsers
if sys.version_info.major == 2:
from io import BytesIO as StringBuffer
else:
from io import StringIO as StringBuffer
class Format(parsers.Parser):
@classmethod
def ptext2dicts(cls, ptext):
# Should return a dictionary or collection of dictionaries as parsed
# from the given plaintext. Dictionary entries should have a uniform
# schema (though this is less important than for the reverse).
raise NotImplementedError("This format parser has an unimplemented ptext2dicts interface")
@classmethod
def dicts2ptext(cls, dicts):
# Should return a format-specific representation of the object or
# collection of objects in plaintext given a list of dictionaries with a
# uniform schema
raise NotImplementedError("This format parser has an unimplemented dicts2ptext interface")
class Csv(Format):
@classmethod
def get_filters(cls):
f = super(Csv, cls).get_filters()
f['ext'] = "^csv$"
return f
@classmethod
def ptext2dicts(cls, ptext):
header_row = None
dicts = []
for row in csv.reader(ptext.splitlines()):
if header_row is None:
header_row = row
else:
entry = {}
for ndx, field in enumerate(header_row):
entry[field] = Format.ptext2value(row[ndx])
dicts.append(entry)
return dicts
@classmethod
def dicts2ptext(cls, dicts):
field_names = list(dicts[0].keys())
output = StringBuffer()
writer = csv.writer(output)
writer.writerow(field_names)
for d in dicts:
writer.writerow(list(d.values()))
ptext = output.getvalue()
output.close()
return ptext