Merge lp:~syleam/openobject-library/exportcsv into lp:openobject-library/1.0

Proposed by Sylvain Garancher
Status: Merged
Merged at revision: 102
Proposed branch: lp:~syleam/openobject-library/exportcsv
Merge into: lp:openobject-library/1.0
Diff against target: 125 lines (+121/-0)
1 file modified
example/exportcsv.py (+121/-0)
To merge this branch: bzr merge lp:~syleam/openobject-library/exportcsv
Reviewer Review Type Date Requested Status
Christophe CHAUVET Approve
Review via email: mp+114360@code.launchpad.net

Description of the change

Added exportcsv script, with following arguments :
--model (required) : name of the model to export from
--ids (required) : comma separated ids list
--fields (required) : comma separated fields names
--filemane : name of the file to write, set to "model.csv" if empty
--language : specify the language for the export, default en_US

To post a comment you must log in.
Revision history for this message
Christophe CHAUVET (christophe-chauvet) wrote :

Ok works for me

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'example/exportcsv.py'
2--- example/exportcsv.py 1970-01-01 00:00:00 +0000
3+++ example/exportcsv.py 2012-07-11 08:55:24 +0000
4@@ -0,0 +1,121 @@
5+#!/usr/bin/env python
6+# --*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# OpenObject Library
10+# Copyright (C) 2009-2011 Syleam (<http://syleam.fr>). Christophe Chauvet
11+# All Rights Reserved
12+#
13+# This program is free software: you can redistribute it and/or modify
14+# it under the terms of the GNU General Public License as published by
15+# the Free Software Foundation, either version 3 of the License, or
16+# (at your option) any later version.
17+#
18+# This program is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU General Public License for more details.
22+#
23+# You should have received a copy of the GNU General Public License
24+# along with this program. If not, see <http://www.gnu.org/licenses/>.
25+#
26+##############################################################################
27+
28+
29+"""
30+This script check all model in the module
31+and launch a search, read, field_view_get, etc...
32+"""
33+
34+import sys
35+sys.path.append('../')
36+
37+from oobjlib.connection import Connection
38+from oobjlib.component import Object
39+from oobjlib.common import GetParser
40+from optparse import OptionGroup
41+import os
42+import logging
43+import csv
44+
45+parser = GetParser('Export CSV compatible with OpenERP', '0.2')
46+group = OptionGroup(parser, "Object arguments", "Application Options")
47+group.add_option('-f', '--file', dest='filename', default=False, help='Enter the name of the file to export')
48+group.add_option('-m', '--model', dest='model', default=False, help='Enter the name of the model to export')
49+group.add_option('', '--ids', dest='ids', default=False, help='Enter the ids to export')
50+group.add_option('', '--fields', dest='fields', default=False, help='Enter the name of the fields to export')
51+group.add_option('', '--separator', dest='separator', default=',', help='Enter the comma separator, default ","')
52+group.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='Add verbose mode')
53+group.add_option('-l', '--log-file', dest='logfile', default=False, help='Enter the name of the log file')
54+group.add_option('', '--language', dest='lang', default='en_US', help='Specify the language to search on translate field, default en_US')
55+group.add_option('', '--with-inactive', dest='inactive', action='store_true', default=False, help='Extract inactive records')
56+parser.add_option_group(group)
57+
58+opts, args = parser.parse_args()
59+
60+if opts.logfile:
61+ ch = logging.FileHandler(opts.logfile)
62+else:
63+ ch = logging.StreamHandler()
64+
65+logger = logging.getLogger("exportcsv")
66+if opts.verbose:
67+ logger.setLevel(logging.DEBUG)
68+ ch.setLevel(logging.DEBUG)
69+else:
70+ logger.setLevel(logging.INFO)
71+ ch.setLevel(logging.INFO)
72+
73+formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
74+ch.setFormatter(formatter)
75+logger.addHandler(ch)
76+
77+if opts.filename:
78+ opts.filename = os.path.expanduser(opts.filename)
79+
80+logger.info('Language to export data: %s' % opts.lang)
81+
82+try:
83+ logger.info('Open connection to "%s:%s" on "%s" with user "%s" ' % (opts.server, opts.port, opts.dbname, opts.user))
84+ cnx = Connection(
85+ server=opts.server,
86+ dbname=opts.dbname,
87+ login=opts.user,
88+ password=opts.passwd,
89+ port=opts.port,
90+ )
91+except Exception, e:
92+ logger.error('Fail to connect to the server')
93+ logger.error('%s' % str(e))
94+ sys.exit(1)
95+
96+
97+class StopError(Exception):
98+ pass
99+
100+
101+filename = opts.filename
102+if not opts.filename:
103+ filename = '%s.csv' % opts.model
104+
105+# recherche du mon de l'objet dans le nom du fichier sans l'extension
106+obj = Object(cnx, opts.model)
107+
108+ctx = {'lang': opts.lang}
109+if opts.inactive:
110+ ctx['active_test'] = False
111+ids = [int(x.strip()) for x in opts.ids.split(',')]
112+fields = opts.fields.split(',')
113+
114+logger.info('Start execute export on the selected file')
115+datas = obj.export_data(ids, fields, ctx)['datas']
116+
117+csvWriter = csv.writer(open(filename, 'w'), delimiter=opts.separator, quoting=csv.QUOTE_NONNUMERIC)
118+csvWriter.writerow(fields)
119+for data in datas:
120+ csvWriter.writerow(data)
121+
122+
123+logger.info('Export done')
124+
125+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

Subscribers

People subscribed via source and target branches