Skip to content

Commit a63c2fd

Browse files
jad-odoobosd
authored andcommitted
[IMP] write_file: improve path compatibility under Windows: no default value, enclose name with blanks between double quotes
1 parent 2def615 commit a63c2fd

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

odoo_csv_tools/lib/internal/io.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def write_csv(filename, header, data, encoding="utf-8"):
4545

4646
def write_file(filename=None, header=None, data=None, fail=False, model="auto",
4747
launchfile="import_auto.sh", worker=1, batch_size=10, init=False, encoding="utf-8",
48-
conf_file=False, groupby='', sep=";", python_exe='', path='', context=None, ignore=""):
48+
conf_file=False, groupby='', sep=";", python_exe='python', path='', context=None, ignore=""):
4949
def get_model():
5050
if model == "auto":
5151
return filename.split(os.sep)[-1][:-4]
@@ -58,13 +58,65 @@ def get_model():
5858
if not launchfile:
5959
return
6060

61+
if not path.endswith(os.sep):
62+
path = os.path.join(path, "")
63+
64+
py_script = 'odoo_import_thread.py'
65+
os_cmd = os.path.join(path, py_script)
66+
if ' ' in os_cmd:
67+
os_cmd =''.join(('"', os_cmd, '"'))
68+
6169
mode = init and 'w' or 'a'
6270
with open(launchfile, mode) as myfile:
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))
71+
myfile.write("%s %s -c %s --file=%s --model=%s --encoding=%s --worker=%s --size=%s --groupby=%s --ignore=%s --sep=\"%s\" %s\n" %
72+
(python_exe, os_cmd, conf_file, filename, get_model(), encoding, worker, batch_size, groupby, ignore, sep, context))
6573
if fail:
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))
74+
myfile.write("%s %s -c %s --fail --file=%s --model=%s --encoding=%s --ignore=%s --sep=\"%s\" %s\n" %
75+
(python_exe, os_cmd, conf_file, filename, get_model(), encoding, ignore, sep, context))
76+
77+
78+
################################################
79+
# Method to merge file together based on a key #
80+
################################################
81+
82+
def write_file_dict(filename, header, data):
83+
data_rows = []
84+
for _, val in data.iteritems():
85+
r = [val.get(h, '') for h in header]
86+
data_rows.append(r)
87+
write_csv(filename, header, data_rows)
88+
89+
90+
91+
def read_file_dict(file_name, id_name):
92+
file_ref = open(file_name, 'r')
93+
reader = UnicodeReader(file_ref, delimiter=';')
94+
95+
head = reader.next()
96+
res = {}
97+
for line in reader:
98+
if any(line):
99+
line_dict = dict(zip(head, line))
100+
res[line_dict[id_name]] = line_dict
101+
return res, head
102+
103+
def merge_file(master, child, field):
104+
res = {}
105+
for key, val in master.iteritems():
106+
data = dict(child.get(val[field], {}))
107+
new_dict = dict(val)
108+
new_dict.update(data)
109+
res[key] = new_dict
110+
return res
111+
112+
113+
def merge_header(*args):
114+
old_header = [item for sublist in args for item in sublist]
115+
header = []
116+
for h in old_header:
117+
if h and h not in header:
118+
header.append(h)
119+
return header
68120

69121
class ListWriter(object):
70122
def __init__(self):

odoo_csv_tools/lib/transform.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from . internal.exceptions import SkippingException
1515
from . import mapper
1616

17-
1817
class Processor(object):
1918
def __init__(self, filename=None, delimiter=";", encoding='utf-8', header=None, data=None, preprocess=lambda header, data: (header, data), conf_file=False):
2019
self.file_to_write = OrderedDict()
@@ -74,7 +73,7 @@ def process(self, mapping, filename_out, import_args, t='list', null_values=['NU
7473
self._add_data(head, data, filename_out, import_args)
7574
return head, data
7675

77-
def write_to_file(self, script_filename, fail=True, append=False, python_exe='', path='', encoding='utf-8'):
76+
def write_to_file(self, script_filename, fail=True, append=False, python_exe='python', path='', encoding='utf-8'):
7877
init = not append
7978
for _, info in self.file_to_write.items():
8079
info_copy = dict(info)

0 commit comments

Comments
 (0)