Skip to content

Commit bd22f64

Browse files
committed
[FIX] Encoding is now consistent utf-8 by default, is a parameter each time is needed and get propagate properly
1 parent cccfbdd commit bd22f64

9 files changed

+30
-27
lines changed

odoo_csv_tools/export_threaded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ def write_file(self, file_writer):
7070

7171

7272
def export_data(config_file, model, domain, header, context=None, output=None, max_connection=1, batch_size=100,
73-
separator=';', encoding='utf-8-sig'):
73+
separator=';', encoding='utf-8'):
7474
object_registry = conf_lib.get_server_connection(config_file).get_model(model)
7575

7676
if output:
77-
file_result = open_write(output)
77+
file_result = open_write(output, encoding=encoding)
7878
writer = UnicodeWriter(file_result, delimiter=separator, encoding=encoding, quoting=csv.QUOTE_ALL)
7979
else:
8080
writer = ListWriter()

odoo_csv_tools/import_threaded.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def filter_header_ignore(ignore, header):
112112
return new_header
113113

114114

115-
def read_file(file_to_read, delimiter=';', encoding='utf-8-sig', skip=0):
115+
def read_file(file_to_read, delimiter=';', encoding='utf-8', skip=0):
116116
def get_real_header(header):
117117
""" Get real header cut at the first empty column """
118118
new_header = []
@@ -136,8 +136,8 @@ def skip_line(reader):
136136
reader.next()
137137

138138
log('open %s' % file_to_read)
139-
file_ref = open_read(file_to_read, encoding='utf-8-sig')
140-
reader = UnicodeReader(file_ref, delimiter=delimiter, encoding='utf-8-sig')
139+
file_ref = open_read(file_to_read, encoding=encoding)
140+
reader = UnicodeReader(file_ref, delimiter=delimiter, encoding=encoding)
141141
header = next(reader)
142142
header = get_real_header(header)
143143
check_id_column(header)
@@ -180,7 +180,7 @@ def do_not_split(split, previous_split_value, split_index, line, o2m=False, id_i
180180

181181

182182
def import_data(config_file, model, header=None, data=None, file_csv=None, context=None, fail_file=False,
183-
encoding='utf-8-sig', separator=";", ignore=False, split=False, check=True, max_connection=1,
183+
encoding='utf-8', separator=";", ignore=False, split=False, check=True, max_connection=1,
184184
batch_size=10, skip=0, o2m=False):
185185
"""
186186
header and data mandatory in file_csv is not provided

odoo_csv_tools/lib/internal/io.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ def is_string(f):
1919
else:
2020
return isinstance(f, basestring)
2121

22-
def open_read(f, encoding='utf-8-sig'):
22+
def open_read(f, encoding='utf-8'):
2323
if not is_string(f):
2424
return f
2525
if sys.version_info >= (3, 0, 0):
2626
return open(f, 'r', newline='', encoding=encoding)
2727
else:
2828
return open(f, 'r')
2929

30-
def open_write(f, encoding='utf-8-sig'):
30+
def open_write(f, encoding='utf-8'):
3131
if not is_string(f):
3232
return f
3333
if sys.version_info >= (3, 0, 0):
3434
return open(f, "w", newline='', encoding=encoding)
3535
else:
3636
return open(f, "w")
3737

38-
def write_csv(filename, header, data):
39-
file_result = open_write(filename)
40-
c = UnicodeWriter(file_result, delimiter=';', quoting=csv.QUOTE_ALL)
38+
def write_csv(filename, header, data, encoding="utf-8"):
39+
file_result = open_write(filename, encoding=encoding)
40+
c = UnicodeWriter(file_result, delimiter=';', quoting=csv.QUOTE_ALL, encoding=encoding)
4141
c.writerow(header)
4242
for d in data:
4343
c.writerow(d)
4444
file_result.close()
4545

4646
def write_file(filename=None, header=None, data=None, fail=False, model="auto",
47-
launchfile="import_auto.sh", worker=1, batch_size=10, init=False,
47+
launchfile="import_auto.sh", worker=1, batch_size=10, init=False, encoding="utf-8",
4848
conf_file=False, groupby='', sep=";", python_exe='', path='', context=None, ignore=""):
4949
def get_model():
5050
if model == "auto":
@@ -54,17 +54,17 @@ def get_model():
5454

5555
context = '--context="%s"' % str(context) if context else ''
5656
conf_file = conf_file or "%s%s%s" % ('conf', os.sep, 'connection.conf')
57-
write_csv(filename, header, data)
57+
write_csv(filename, header, data, encoding=encoding)
5858
if not launchfile:
5959
return
6060

6161
mode = init and 'w' or 'a'
6262
with open(launchfile, mode) as myfile:
63-
myfile.write("%s %sodoo_import_thread.py -c %s --file=%s --model=%s --worker=%s --size=%s --groupby=%s --ignore=%s --sep=\"%s\" %s\n" %
64-
(python_exe, path, conf_file, filename, get_model(), worker, batch_size, groupby, ignore, sep, context))
63+
myfile.write("%s %sodoo_import_thread.py -c %s --file=%s --model=%s --encoding=%s --worker=%s --size=%s --groupby=%s --ignore=%s --sep=\"%s\" %s\n" %
64+
(python_exe, path, conf_file, filename, get_model(), encoding, worker, batch_size, groupby, ignore, sep, context))
6565
if fail:
66-
myfile.write("%s %sodoo_import_thread.py -c %s --fail --file=%s --model=%s --ignore=%s --sep=\"%s\" %s\n" %
67-
(python_exe, path, conf_file, filename, get_model(), ignore, sep, context))
66+
myfile.write("%s %sodoo_import_thread.py -c %s --fail --file=%s --model=%s --encoding=%s --ignore=%s --sep=\"%s\" %s\n" %
67+
(python_exe, path, conf_file, filename, get_model(), encoding, ignore, sep, context))
6868

6969
class ListWriter(object):
7070
def __init__(self):

odoo_csv_tools/lib/mapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def binary_val(line):
138138
return ''
139139

140140
with open(path, "rb") as image_file:
141-
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
141+
encoded_string = base64.b64encode(image_file.read()).decode(encoding)
142142
image_file.close()
143143
return encoded_string
144144
return binary_val
@@ -148,7 +148,7 @@ def binary(field, path_prefix, skip=False, encoding="utf-8"):
148148

149149

150150

151-
def binary_url_map(mapper, skip=False, verbose=False):
151+
def binary_url_map(mapper, skip=False, verbose=False, encoding="utf-8"):
152152
def binary_url_fun(line):
153153
url = mapper(line)
154154
if verbose:
@@ -159,7 +159,7 @@ def binary_url_fun(line):
159159
raise SkippingException("Cannot fetch file at url %s" % url)
160160
return ''
161161

162-
return base64.b64encode(res.content).decode("utf-8")
162+
return base64.b64encode(res.content).decode(encoding)
163163
return binary_url_fun
164164

165165
def binary_url(field, skip=False, verbose=False):

odoo_csv_tools/lib/transform.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
class Processor(object):
19-
def __init__(self, filename=None, delimiter=";", encoding='utf-8-sig', header=None, data=None, preprocess=lambda header, data: (header, data), conf_file=False):
19+
def __init__(self, filename=None, delimiter=";", encoding='utf-8', header=None, data=None, preprocess=lambda header, data: (header, data), conf_file=False):
2020
self.file_to_write = OrderedDict()
2121
if header and data:
2222
self.header = header
@@ -74,7 +74,7 @@ def process(self, mapping, filename_out, import_args, t='list', null_values=['NU
7474
self._add_data(head, data, filename_out, import_args)
7575
return head, data
7676

77-
def write_to_file(self, script_filename, fail=True, append=False, python_exe='', path=''):
77+
def write_to_file(self, script_filename, fail=True, append=False, python_exe='', path='', encoding='utf-8'):
7878
init = not append
7979
for _, info in self.file_to_write.items():
8080
info_copy = dict(info)
@@ -86,6 +86,7 @@ def write_to_file(self, script_filename, fail=True, append=False, python_exe='',
8686
'python_exe' : python_exe,
8787
'path' : path,
8888
'conf_file' : self.conf_file,
89+
'encoding': encoding,
8990
})
9091

9192
write_file(**info_copy)
@@ -94,7 +95,7 @@ def write_to_file(self, script_filename, fail=True, append=False, python_exe='',
9495
def get_processed_data(self, filename_out):
9596
return self.file_to_write[filename_out]
9697

97-
def join_file(self, filename, master_key, child_key, header_prefix="child", delimiter=";", encoding='utf-8-sig'):
98+
def join_file(self, filename, master_key, child_key, header_prefix="child", delimiter=";", encoding='utf-8'):
9899
"""
99100
Join another file with the main file defined in the constructor.
100101
Need a key (column name) on the master file and on the file to join

odoo_export_thread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
parser.add_argument('--context', dest='context',
3434
help='context that will be passed to the load function, need to be a valid python dict',
3535
default="{'tracking_disable' : True}")
36+
parser.add_argument('--encoding', dest='encoding', default="utf-8", help='Encoding of the data file')
3637
# TODO args : encoding
3738
# {'update_many2many': True,'tracking_disable' : True, 'create_product_variant' : True, 'check_move_validity' : False}
3839
args = parser.parse_args()
@@ -43,7 +44,7 @@
4344
model = args.model
4445
max_connection = int(args.worker)
4546
separator = args.separator
46-
encoding = 'utf-8-sig'
47+
encoding = args.encoding
4748
context = eval(args.context)
4849
domain = eval(args.domain)
4950
header = args.fields.split(',')

odoo_import_thread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
parser.add_argument('--check', dest='check', action='store_true', help='Check if record are imported after each batch.')
3535
parser.add_argument('--context', dest='context', help='context that will be passed to the load function, need to be a valid python dict', default="{'tracking_disable' : True}")
3636
parser.add_argument('--o2m', action='store_true', dest="o2m", help="When you want to import o2m field, don't cut the batch until we find a new id")
37+
parser.add_argument('--encoding', dest='encoding', default="utf-8", help='Encoding of the data file')
3738
#TODO args : encoding
3839
#{'update_many2many': True,'tracking_disable' : True, 'create_product_variant' : True, 'check_move_validity' : False}
3940
args = parser.parse_args()
@@ -43,7 +44,7 @@
4344
fail_file = file_csv + ".fail"
4445
max_connection = int(args.worker)
4546
split = False
46-
encoding='utf-8-sig'
47+
encoding= args.encoding
4748
context= eval(args.context)
4849
ignore = False
4950
if args.ignore:

tests/5_partner_export.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
$1 ../odoo_export_thread.py -c conf/connection.conf --file=data/res.partner.exported.csv --model=res.partner --worker=4 --size=200 --domain="[]" --field="id,name,phone,website,street,city,country_id/id" --sep=";"
2+
$1 ../odoo_export_thread.py -c conf/connection.conf --file=data/res.partner.exported.csv --model=res.partner --worker=4 --size=200 --domain="[]" --field="id,name,phone,website,street,city,country_id/id" --sep=";" --encoding=utf-8-sig

tests/test_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@
6161
'batch_size': 100,
6262
'model': 'res.partner',
6363
})
64-
processor.write_to_file(script, python_exe=EXEC, path='../')
64+
processor.write_to_file(script, python_exe=EXEC, path='../', encoding="utf-8-sig")

0 commit comments

Comments
 (0)