1 # -*- coding: utf-8 -*-
7 from description
import ds
, xml_prefix
, files
10 # from dateutil.relativedelta import relativedelta
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.')
31 elif arg
== 'noupdate':
33 elif arg
== 'nodisplay':
37 def create_xml_tree():
38 openerp
= etree
.Element('openerp')
40 data
= etree
.SubElement(openerp
, 'data', noupdate
="1")
42 data
= etree
.SubElement(openerp
, 'data')
43 return (openerp
, data
)
46 def pass_head(f
, nb_ignore_lines
):
47 """Ignore a defined number of lines"""
49 while repeat
<= nb_ignore_lines
- 1:
56 def __init__(self
, row
, descr
):
59 def uppercase(self
, value
):
62 def capitalize(self
, value
):
63 return value
.capitalize()
65 def test(self
, value
):
66 return "test_%s" % value
69 def pre(self
, pre
, values
):
71 if type(pre
[col
]) == str:
72 f
= getattr(self
, pre
[col
])
73 values
[col
] = f(values
[col
])
77 values
[col
] = f(values
[col
])
81 """Analyse descr and return a value calculated from descr and row"""
83 if descr
.has_key('col'):
84 if type(descr
['col']) == int:
85 cols
= (descr
['col'],)
89 values
[col
] = row
[col
]
91 if descr
.has_key('pre'):
92 values
= self
.pre(descr
['pre'], values
)
96 value
= value
+ (values
[col
],)
97 value
= " ".join(value
)
102 fname
= '../data_to_import/%s.csv' % f
['name']
103 xml_tree
, xml_data
= create_xml_tree()
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']
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']
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)
121 dest_dir
= '%s/%s' % (base_dir
, f
['dest_dir'])
122 dest_file
= '%s/%s.xml' % (dest_dir
, f
['name'])
124 os
.makedirs(dest_dir
)
127 with
open(dest_file
, 'wb') as cur_file
:
130 elif not nodisplay
and not debug
: