1 """
2 Wrinting data in CSV.
3 """
4
5
6
7
8
9
10
11 import csv
12 import re
13
14 from gavo import base
15 from gavo import rsc
16 from gavo.formats import common
17
18
20 """return row with everything that's a unicode object turned into a
21 utf-8 encoded string.
22
23 This will also normalise whitespace to blanks.
24 """
25 res = []
26 for val in row:
27 if isinstance(val, str):
28 res.append(re.sub("\s+", " ", val))
29 elif isinstance(val, unicode):
30 res.append(re.sub("\s+", " ", val.encode("utf-8")))
31 elif isinstance(val, (tuple, list)):
32
33 res.append("[%s]"%" ".join(["%s"%v for v in val]))
34 else:
35 res.append(val)
36 return res
37
38
39 -def writeDataAsCSV(table, target, acquireSamples=True,
40 dialect=base.getConfig("async", "csvDialect"), headered=False):
41 """writes table to the file target in CSV.
42
43 The CSV format chosen is controlled through the async/csvDialect
44 config item.
45
46 If headered is True, we also include table params (if present)
47 in comments.
48 """
49 if isinstance(table, rsc.Data):
50 table = table.getPrimaryTable()
51 sm = base.SerManager(table, acquireSamples=acquireSamples)
52 writer = csv.writer(target, dialect)
53
54 if headered:
55 for param in table.iterParams():
56 if param.value is not None:
57 target.write(("# %s = %s // %s\r\n"%(
58 param.name,
59 param.getStringValue(),
60 param.description)).encode("utf-8"))
61
62 writer.writerow([c["name"] for c in sm])
63
64 for row in sm.getMappedTuples():
65 try:
66 writer.writerow(_encodeRow(row))
67 except UnicodeEncodeError:
68 writer.writerow(row)
69
70
74
75
76 common.registerDataWriter("csv", writeDataAsCSV, "text/csv",
77 "CSV without column labels", ".csv")
78 common.registerDataWriter("csv_header",
79 lambda table, target, **kwargs:
80 writeDataAsCSV(table, target, headered=True, **kwargs),
81 "text/csv;header=present",
82 "CSV with column labels", ".csv")
83