Merge lp:~jri-openerp/enapps-csv-import-tool/python_csv_reader into lp:~enapps-london-uk/enapps-csv-import-tool/old_trunk

Proposed by Juan Rial (OpenERP) on 2012-05-18
Status: Needs review
Proposed branch: lp:~jri-openerp/enapps-csv-import-tool/python_csv_reader
Merge into: lp:~enapps-london-uk/enapps-csv-import-tool/old_trunk
Diff against target: 77 lines (+11/-19)
2 files modified
ea_import/ea_import_chain.py (+4/-16)
ea_import/ea_import_template.py (+7/-3)
To merge this branch: bzr merge lp:~jri-openerp/enapps-csv-import-tool/python_csv_reader
Reviewer Review Type Date Requested Status
Enapps Ltd OpenERP Partner London UK 2012-05-18 Pending
Review via email: mp+106373@code.launchpad.net

Description of the change

Fixes 2 issues:
1) LP Bug #999180: multiline text fields weren't properly handled since the file was read and parsed line by line
2) When the separator occurs within a text field (enclosed by string delimiters), the field it split in two, resulting in an incorrect line being parsed

To post a comment you must log in.

Unmerged revisions

4. By Juan Rial (OpenERP) on 2012-05-18

[FIX] LP Bug #999180: inline trivial file_to_list() functionality

3. By Juan Rial (OpenERP) on 2012-05-18

[FIX] LP Bug #999180: first commit, switching to python's own csv reader.

2. By Enapps Ltd OpenERP Partner London UK on 2012-04-10

fix invalid model reference

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ea_import/ea_import_chain.py'
2--- ea_import/ea_import_chain.py 2012-04-10 11:00:44 +0000
3+++ ea_import/ea_import_chain.py 2012-05-18 13:19:22 +0000
4@@ -24,6 +24,7 @@
5 import datetime
6 from cStringIO import StringIO
7 import time
8+import csv
9
10 class ea_import_chain(osv.osv):
11 _name='ea_import.chain'
12@@ -58,13 +59,13 @@
13 context['result_ids'] = []
14 result_pool = self.pool.get('ea_import.chain.result')
15 for chain in self.browse(cr, uid, ids, context=context):
16- csv_list = chain.file_to_list()
17+ csv_reader = csv.reader(StringIO(base64.b64decode(chain.input_file)), delimiter=str(chain.separator), quotechar=str(chain.delimiter))
18 if chain.header:
19- csv_list.pop(0)
20+ csv_reader.next()
21 result_ids = {}
22 for chain_link in chain.link_ids:
23 result_ids.update({chain_link.template_id.target_model_id.model: set([])})
24- for record_list in csv_list:
25+ for record_list in csv_reader:
26 for chain_link in sorted(chain.link_ids, key=lambda k: k.sequence):
27 result_id = chain_link.template_id.generate_record(record_list, context=context)
28 result_ids[chain_link.template_id.target_model_id.model] = result_ids[chain_link.template_id.target_model_id.model] | set(result_id)
29@@ -79,19 +80,6 @@
30 context['result_ids'].append(result_ids)
31 return True
32
33- def file_to_list(self, cr, uid, ids, context={}):
34- result = []
35- for chain in self.browse(cr, uid, ids, context={}):
36- input_file = StringIO(base64.b64decode(chain.input_file))
37- for string_record in input_file:
38- line = []
39- for field_string in string_record.split(chain.separator):
40- delimiter = chain.delimiter and chain.delimiter + "\n\r" or "\n\r"
41- line.append(field_string.strip(delimiter))
42- result.append(line)
43- return result
44-
45-
46 ea_import_chain()
47
48 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
49
50=== modified file 'ea_import/ea_import_template.py'
51--- ea_import/ea_import_template.py 2012-04-10 11:00:44 +0000
52+++ ea_import/ea_import_template.py 2012-05-18 13:19:22 +0000
53@@ -24,6 +24,7 @@
54 import base64
55 from cStringIO import StringIO
56 from types import *
57+import csv
58
59 class ea_import_template(osv.osv):
60 _name='ea_import.template'
61@@ -46,10 +47,13 @@
62 base_model = template.target_model_id.model
63 input_file = StringIO(base64.b64decode(template.test_input_file))
64 result = []
65+ # Let's hope Python's csv reader is good at guessing delimiter and quotechar since they're stored on chain level.
66+ # Then again, this function is never called for the moment. Can be fixed later, when this becomes live code.
67+ template_reader = csv.reader(template.test_input_file)
68 if template.header:
69- header_string = input_file.readline()
70- for record_string in input_file:
71- record = template.generate_record(record_string)
72+ header_row = template_reader.next()
73+ for row in template_reader:
74+ record = template.generate_record(row)
75 if record and record[0] not in result:
76 result.append(record[0])
77 return {

Subscribers

People subscribed via source and target branches