vigiboard / vigiboard / lib / export_csv.py @ 8c198622
History | View | Annotate | Download (1.7 KB)
1 |
# vim: set fileencoding=utf-8 sw=4 ts=4 et :
|
---|---|
2 |
# Copyright (C) 2007-2019 CS-SI
|
3 |
# License: GNU GPL v2 <http://www.gnu.org/licenses/gpl-2.0.html>
|
4 |
|
5 |
"""Fonction d'export des alertes au format CSV."""
|
6 |
|
7 |
import csv |
8 |
from cStringIO import StringIO |
9 |
|
10 |
from tg import config |
11 |
|
12 |
def export(page, plugins_data): |
13 |
buf = StringIO() |
14 |
quoting = config.get('csv_quoting', 'ALL').upper() |
15 |
if quoting not in ('ALL', 'MINIMAL', 'NONNUMERIC', 'NONE'): |
16 |
quoting = 'ALL'
|
17 |
csv_writer = csv.DictWriter(buf, |
18 |
config['csv_columns'],
|
19 |
extrasaction='ignore',
|
20 |
delimiter=config.get("csv_delimiter_char", ';'), |
21 |
escapechar=config.get("csv_escape_char", '\\'), |
22 |
quotechar=config.get("csv_quote_char", '"'), |
23 |
quoting=getattr(csv, 'QUOTE_%s' % quoting)) |
24 |
csv_writer.writerow(dict(zip(config['csv_columns'], config['csv_columns']))) |
25 |
|
26 |
for item in page.items: |
27 |
values = {} |
28 |
for plugin_name, plugin_instance in config['columns_plugins']: |
29 |
if plugins_data[plugin_name]:
|
30 |
values[plugin_name] = repr(plugins_data[plugin_name])
|
31 |
else:
|
32 |
for data_key, data_value in \ |
33 |
plugin_instance.get_data(item).iteritems(): |
34 |
# Pour les valeurs en unicode, on convertit en UTF-8.
|
35 |
if isinstance(data_value, unicode): |
36 |
values[data_key] = data_value.encode('utf-8')
|
37 |
# Pour le reste, on suppose qu'on peut en obtenir une
|
38 |
# représentation adéquate dont l'encodage ne posera pas
|
39 |
# de problème.
|
40 |
else:
|
41 |
values[data_key] = data_value |
42 |
csv_writer.writerow(values) |
43 |
return buf.getvalue()
|