6794ee43cd02cc737b51a758fa7fb922bceaca29
[burette/bikecoop_init.git] / tools / scripts / csv2xml.py
1 # -*- coding: utf-8 -*-
2 # v2.0
3
4 from sys import argv
5 import os
6 from lxml import etree
7 from description import ds, xml_prefix, files
8 import csv
9 # import datetime
10 # from dateutil.relativedelta import relativedelta
11
12 base_dir = './../..'
13 debug = 0
14 nodisplay = 0
15 limit = 0
16 noupdate = 0
17
18 for arg in argv:
19 if arg[:2] == '--':
20 arg = arg[2:]
21 if arg == 'help':
22 print('Membership Import documentation')
23 print('===============================')
24 print(' --help: read this doc.')
25 print(' --debug: debug mode. Displays debug messages and doesn\'t generate xml files.')
26 print(' --noupdate: add noupdate feature in xml files.')
27 print(' --nodisplay: no display result in standard output and write it in previous files. This is not compatible with debug mode.')
28 exit(0)
29 elif arg == 'debug':
30 debug = 1
31 elif arg == 'noupdate':
32 noupdate = 1
33 elif arg == 'nodisplay':
34 nodisplay = 1
35
36
37 def create_xml_tree():
38 openerp = etree.Element('openerp')
39 if noupdate:
40 data = etree.SubElement(openerp, 'data', noupdate="1")
41 else:
42 data = etree.SubElement(openerp, 'data')
43 return (openerp, data)
44
45
46 def pass_head(f, nb_ignore_lines):
47 """Ignore a defined number of lines"""
48 repeat = 0
49 while repeat <= nb_ignore_lines - 1:
50 f.next()
51 repeat += 1
52
53 class Field():
54 """Field object"""
55
56 def __init__(self, row, descr):
57 """ __init__"""
58
59 def uppercase(self, value):
60 return value.upper()
61
62 def capitalize(self, value):
63 return value.capitalize()
64
65 def test(self, value):
66 return "test_%s" % value
67
68
69 def pre(self, pre, values):
70 for col in pre:
71 if type(pre[col]) == str:
72 f = getattr(self, pre[col])
73 values[col] = f(values[col])
74 else:
75 for p in pre[col]:
76 f = getattr(self, p)
77 values[col] = f(values[col])
78 return values
79
80 def get_value(self):
81 """Analyse descr and return a value calculated from descr and row"""
82 values = {}
83 if descr.has_key('col'):
84 if type(descr['col']) == int:
85 cols = (descr['col'],)
86 else:
87 cols = descr['col']
88 for col in cols:
89 values[col] = row[col]
90
91 if descr.has_key('pre'):
92 values = self.pre(descr['pre'], values)
93
94 value = ()
95 for col in cols:
96 value = value + (values[col],)
97 value = " ".join(value)
98 return value
99
100
101 for f in files:
102 fname = '../data_to_import/%s.csv' % f['name']
103 xml_tree, xml_data = create_xml_tree()
104 repeat = 0
105 with open(fname, 'rb') as csvfile:
106 rows = csv.reader(csvfile, delimiter=',', quotechar='"')
107 pass_head(rows, f['nb_ignore_lines'])
108 line = f['nb_ignore_lines']
109 xml_id = f['xml_id']
110 for row in rows:
111 line += 1
112 record = etree.SubElement(xml_data, 'record', id="%s_%s_%d" % (xml_prefix, xml_id, line), model=ds[f['name']]['model'])
113 fields = ds[f['name']]['descr']
114 for field in fields:
115 descr = fields[field]
116 thisfield = Field(row, descr)
117 value = thisfield.get_value()
118 etree.SubElement(record, 'field', name=field).text = unicode(str(value), 'utf-8')
119 content = '<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree.tostring(xml_tree, pretty_print=True)
120 if nodisplay:
121 dest_dir = '%s/%s' % (base_dir, f['dest_dir'])
122 dest_file = '%s/%s.xml' % (dest_dir, f['name'])
123 try:
124 os.makedirs(dest_dir)
125 except OSError:
126 pass
127 with open(dest_file, 'wb') as cur_file:
128 for line in content:
129 cur_file.write(line)
130 elif not nodisplay and not debug:
131 print(content)