1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # Bikecoop Init for OpenERP, Tools for begin a Bikecoop OpenERP project
5 # Copyright (C) 2017 L'Heureux Cyclage (<http://www.heureux-cyclage.org>)
8 # This file is a part of Bikecoop Init
10 # Bikecoop Init is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # Bikecoop Init is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ##############################################################################
27 from lxml
import etree
28 from description
import ds
, xml_prefix
, files
31 # from dateutil.relativedelta import relativedelta
43 print('Membership Import documentation')
44 print('===============================')
45 print(' --help: read this doc.')
46 print(' --debug: debug mode. Displays debug messages and doesn\'t generate xml files.')
47 print(' --noupdate: add noupdate feature in xml files.')
48 print(' --nodisplay: no display result in standard output and write it in previous files. This is not compatible with debug mode.')
52 elif arg
== 'noupdate':
54 elif arg
== 'nodisplay':
58 def create_xml_tree():
59 openerp
= etree
.Element('openerp')
61 data
= etree
.SubElement(openerp
, 'data', noupdate
="1")
63 data
= etree
.SubElement(openerp
, 'data')
64 return (openerp
, data
)
67 def pass_head(f
, nb_ignore_lines
):
68 """Ignore a defined number of lines"""
70 while repeat
<= nb_ignore_lines
- 1:
77 def __init__(self
, row
, descr
):
80 def uppercase(self
, value
):
83 def capitalize(self
, value
):
84 return value
.capitalize()
86 def test(self
, value
):
87 return "test_%s" % value
90 def pre(self
, pre
, values
):
92 if type(pre
[col
]) == str:
93 f
= getattr(self
, pre
[col
])
94 values
[col
] = f(values
[col
])
98 values
[col
] = f(values
[col
])
102 """Analyse descr and return a value calculated from descr and row"""
104 if descr
.has_key('col'):
105 if type(descr
['col']) == int:
106 cols
= (descr
['col'],)
110 values
[col
] = row
[col
]
112 if descr
.has_key('pre'):
113 values
= self
.pre(descr
['pre'], values
)
117 value
= value
+ (values
[col
],)
118 value
= " ".join(value
)
123 fname
= '../data_to_import/%s.csv' % f
['name']
124 xml_tree
, xml_data
= create_xml_tree()
126 with
open(fname
, 'rb') as csvfile
:
127 rows
= csv
.reader(csvfile
, delimiter
=',', quotechar
='"')
128 pass_head(rows
, f
['nb_ignore_lines'])
129 line
= f
['nb_ignore_lines']
133 record
= etree
.SubElement(xml_data
, 'record', id="%s_%s_%d" % (xml_prefix
, xml_id
, line
), model
=ds
[f
['name']]['model'])
134 fields
= ds
[f
['name']]['descr']
136 descr
= fields
[field
]
137 thisfield
= Field(row
, descr
)
138 value
= thisfield
.get_value()
139 etree
.SubElement(record
, 'field', name
=field
).text
= unicode(str(value
), 'utf-8')
140 content
= '<?xml version="1.0" encoding="UTF-8"?>\n%s' % etree
.tostring(xml_tree
, pretty_print
=True)
142 dest_dir
= '%s/%s' % (base_dir
, f
['dest_dir'])
143 dest_file
= '%s/%s.xml' % (dest_dir
, f
['name'])
145 os
.makedirs(dest_dir
)
148 with
open(dest_file
, 'wb') as cur_file
:
151 elif not nodisplay
and not debug
: