Commit b6b51f61 authored by Jérome Perrin's avatar Jérome Perrin

accounting: update Base_importGapFile to use python's csv module

This was reimplemented a broken version of csv.DictReader
parent fff85e8d
import csv
def recursiveDocumentList(obj):
ret = []
ret.append(obj.getPath())
......@@ -5,64 +7,12 @@ def recursiveDocumentList(obj):
ret.extend(recursiveDocumentList(i))
return ret
def splitCsvLine(str_line):
unclean_list = []
pieces_of_line = str_line.split(',')
p_stack = ''
for p in pieces_of_line:
p_stack += p
if p_stack.count('"')%2 == 0:
unclean_list.append(p_stack)
p_stack = ''
clean_list = []
for item in unclean_list:
clean_item = item
if clean_item.find('"') != -1:
if len(clean_item) <= 2:
clean_item = ''
else:
clean_item = clean_item[1:]
clean_item = clean_item[:-1]
clean_item = clean_item.replace('""', '"')
else:
if len(clean_item) > 0:
if clean_item.find('.') != -1:
clean_item = float(clean_item)
else:
clean_item = int(clean_item)
else:
clean_item = None
clean_list.append(clean_item)
return clean_list
def getSubCategory(parent, category_id):
try:
return parent[category_id]
except KeyError:
return parent.newContent(id=category_id)
csv_file_line_list = import_file.readlines()
csv_line_list = []
for csv_line in csv_file_line_list:
csv_line_list.append( string.replace(csv_line, '\n', '').decode(encoding).encode('utf-8') )
object_list = []
csv_property_list = splitCsvLine(csv_line_list[0])
for csv_line in csv_line_list[2:]:
property_dict = {}
csv_data_list = splitCsvLine(csv_line)
data_n = 0
for property_ in csv_property_list:
property_dict[property_] = csv_data_list[data_n]
data_n += 1
object_list.append(property_dict)
root = context.getPortalObject().portal_categories
for path in gap_root_path.split('/'):
......@@ -71,7 +21,7 @@ for path in gap_root_path.split('/'):
existing_path_list = recursiveDocumentList(root)
existing_path_list.remove(root.getPath())
for property_dict in object_list:
for property_dict in list(csv.DictReader(import_file)):
description = property_dict.get('Description', None) or ''
gap = property_dict.get('Gap', None) or ''
title = property_dict.get('Title', None) or ''
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment