From 90bba92fe1635634d0e475421e4ffff6cd46f156 Mon Sep 17 00:00:00 2001 From: Ludovic CHEVALIER Date: Thu, 30 Mar 2017 15:24:05 +0200 Subject: [PATCH] +csv2xml --- tools/scripts/csv2xml.py | 131 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tools/scripts/csv2xml.py diff --git a/tools/scripts/csv2xml.py b/tools/scripts/csv2xml.py new file mode 100644 index 0000000..6794ee4 --- /dev/null +++ b/tools/scripts/csv2xml.py @@ -0,0 +1,131 @@ +# -*- coding: utf-8 -*- +# v2.0 + +from sys import argv +import os +from lxml import etree +from description import ds, xml_prefix, files +import csv +# import datetime +# from dateutil.relativedelta import relativedelta + +base_dir = './../..' +debug = 0 +nodisplay = 0 +limit = 0 +noupdate = 0 + +for arg in argv: + if arg[:2] == '--': + arg = arg[2:] + if arg == 'help': + print('Membership Import documentation') + print('===============================') + print(' --help: read this doc.') + print(' --debug: debug mode. Displays debug messages and doesn\'t generate xml files.') + print(' --noupdate: add noupdate feature in xml files.') + print(' --nodisplay: no display result in standard output and write it in previous files. This is not compatible with debug mode.') + exit(0) + elif arg == 'debug': + debug = 1 + elif arg == 'noupdate': + noupdate = 1 + elif arg == 'nodisplay': + nodisplay = 1 + + +def create_xml_tree(): + openerp = etree.Element('openerp') + if noupdate: + data = etree.SubElement(openerp, 'data', noupdate="1") + else: + data = etree.SubElement(openerp, 'data') + return (openerp, data) + + +def pass_head(f, nb_ignore_lines): + """Ignore a defined number of lines""" + repeat = 0 + while repeat <= nb_ignore_lines - 1: + f.next() + repeat += 1 + +class Field(): + """Field object""" + + def __init__(self, row, descr): + """ __init__""" + + def uppercase(self, value): + return value.upper() + + def capitalize(self, value): + return value.capitalize() + + def test(self, value): + return "test_%s" % value + + + def pre(self, pre, values): + for col in pre: + if type(pre[col]) == str: + f = getattr(self, pre[col]) + values[col] = f(values[col]) + else: + for p in pre[col]: + f = getattr(self, p) + values[col] = f(values[col]) + return values + + def get_value(self): + """Analyse descr and return a value calculated from descr and row""" + values = {} + if descr.has_key('col'): + if type(descr['col']) == int: + cols = (descr['col'],) + else: + cols = descr['col'] + for col in cols: + values[col] = row[col] + + if descr.has_key('pre'): + values = self.pre(descr['pre'], values) + + value = () + for col in cols: + value = value + (values[col],) + value = " ".join(value) + return value + + +for f in files: + fname = '../data_to_import/%s.csv' % f['name'] + xml_tree, xml_data = create_xml_tree() + repeat = 0 + with open(fname, 'rb') as csvfile: + rows = csv.reader(csvfile, delimiter=',', quotechar='"') + pass_head(rows, f['nb_ignore_lines']) + line = f['nb_ignore_lines'] + xml_id = f['xml_id'] + for row in rows: + line += 1 + record = etree.SubElement(xml_data, 'record', id="%s_%s_%d" % (xml_prefix, xml_id, line), model=ds[f['name']]['model']) + fields = ds[f['name']]['descr'] + for field in fields: + descr = fields[field] + thisfield = Field(row, descr) + value = thisfield.get_value() + etree.SubElement(record, 'field', name=field).text = unicode(str(value), 'utf-8') + content = '\n%s' % etree.tostring(xml_tree, pretty_print=True) + if nodisplay: + dest_dir = '%s/%s' % (base_dir, f['dest_dir']) + dest_file = '%s/%s.xml' % (dest_dir, f['name']) + try: + os.makedirs(dest_dir) + except OSError: + pass + with open(dest_file, 'wb') as cur_file: + for line in content: + cur_file.write(line) + elif not nodisplay and not debug: + print(content) -- 2.20.1