vigiboard / vigiboard / lib / export_csv.py @ a2fa6a5b
History | View | Annotate | Download (2.41 KB)
1 |
# vim: set fileencoding=utf-8 sw=4 ts=4 et :
|
---|---|
2 |
################################################################################
|
3 |
#
|
4 |
# Copyright (C) 2007-2012 CS-SI
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License version 2 as
|
8 |
# published by the Free Software Foundation.
|
9 |
#
|
10 |
# This program is distributed in the hope that it will be useful,
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
# GNU General Public License for more details.
|
14 |
#
|
15 |
# You should have received a copy of the GNU General Public License
|
16 |
# along with this program; if not, write to the Free Software
|
17 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
18 |
################################################################################
|
19 |
|
20 |
"""Fonction d'export des alertes au format CSV."""
|
21 |
|
22 |
import csv |
23 |
from cStringIO import StringIO |
24 |
|
25 |
from tg import config |
26 |
|
27 |
def export(page, plugins_data): |
28 |
buf = StringIO() |
29 |
quoting = config.get('csv_quoting', 'ALL').upper() |
30 |
if quoting not in ('ALL', 'MINIMAL', 'NONNUMERIC', 'NONE'): |
31 |
quoting = 'ALL'
|
32 |
csv_writer = csv.DictWriter(buf, |
33 |
config['csv_columns'],
|
34 |
extrasaction='ignore',
|
35 |
delimiter=config.get("csv_delimiter_char", ';'), |
36 |
escapechar=config.get("csv_escape_char", '\\'), |
37 |
quotechar=config.get("csv_quote_char", '"'), |
38 |
quoting=getattr(csv, 'QUOTE_%s' % quoting)) |
39 |
csv_writer.writerow(dict(zip(config['csv_columns'], config['csv_columns']))) |
40 |
|
41 |
for item in page.items: |
42 |
values = {} |
43 |
for plugin_name, plugin_instance in config['columns_plugins']: |
44 |
if plugins_data[plugin_name]:
|
45 |
values[plugin_name] = repr(plugins_data[plugin_name])
|
46 |
else:
|
47 |
for data_key, data_value in \ |
48 |
plugin_instance.get_data(item).iteritems(): |
49 |
# Pour les valeurs en unicode, on convertit en UTF-8.
|
50 |
if isinstance(data_value, unicode): |
51 |
values[data_key] = data_value.encode('utf-8')
|
52 |
# Pour le reste, on suppose qu'on peut en obtenir une
|
53 |
# représentation adéquate dont l'encodage ne posera pas
|
54 |
# de problème.
|
55 |
else:
|
56 |
values[data_key] = data_value |
57 |
csv_writer.writerow(values) |
58 |
return buf.getvalue()
|