-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconversor.py
214 lines (160 loc) · 6.26 KB
/
conversor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
# This file is part of the account_coop_ar module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
import csv
import xml.etree.ElementTree as ET
from unicodedata import normalize
def normalizar_string(unicode_string):
"""Retorna unicode_string normalizado para efectuar una búsqueda.
>>> normalizar_string(u'Mónica Viñao')
'monica vinao'
"""
return normalize('NFKD', unicode_string).encode('ASCII', 'ignore').lower()
def sanitize(unicode_string):
"""cleanup an string, to use as id"""
return normalizar_string(unicode_string).replace(' ', '_').replace('.', '')
def indent(elem, level=0):
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
class XMLElement(object):
"""Abstract Represents a xml element"""
def __init__(self, tag, attrs={}, value=None):
self.element = ET.Element(tag, attrs)
if value:
self.element.text = value
class Document(object):
"""Represents a data document, importable by tryton"""
def __init__(self):
self.tree = ET.ElementTree()
self.root = ET.Element('tryton')
self.data = ET.SubElement(self.root, 'data')
self.tree._setroot(self.root)
def add_record(self, record):
"""Adds a record on current tree"""
self.data.append(record.element)
def get_xml(self):
return self.tree
class Field(XMLElement):
"""Represents a Field of tryton"""
def __init__(self, name, attrs={}, value=None):
attrs['name'] = name
super().__init__('field', attrs, value)
class Record(XMLElement):
"""Represents a record of a tryton model"""
def __init__(self, model, id):
attrs = {'model': model, 'id': id}
super().__init__('record', attrs)
def add_field(self, field):
"""Adds a new field inside the record"""
self.element.append(field.element)
class WierdXMLGenerator(object):
"""Takes a csv and creates a lot of xml tags
as java programmers like"""
types = {
'vista': 'view',
'otro': 'other',
'a pagar': 'payable',
'a cobrar': 'receivable',
'ingresos': 'revenue',
'existencias': 'stock',
'gastos': 'expense',
}
account_types = {}
def __init__(self, types_file, account_file):
self.account_file = account_file
self.types_file = types_file
self.document = Document()
self.account_parents = {}
self.record_ids = set()
def inflate(self):
"""Make data grow up, because java
programmers dont worry about storage"""
with open(self.types_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
self.process_type_row(row)
root = Record('account.account.template', 'root')
root.add_field(Field('name',
value='Plan Contable Argentino para Cooperativas'))
root.add_field(Field('kind', value='view'))
root.add_field(Field('type', {'ref': 'ar'}))
self.document.add_record(root)
with open(self.account_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
self.process_account_row(row)
return self.document.tree
def process_type_row(self, row):
"""Manages account type rows"""
name = row['name'].decode('utf8')
id = row['id']
if not id:
id = 'account_type_%s' % sanitize(name)
self.account_types[name.lower()] = id
record = Record('account.account.type.template', id)
record.add_field(Field('name', value=name))
record.add_field(Field('sequence', {'eval': row['sequence']}))
if row['parent']:
record.add_field(Field('parent', {'ref': row['parent']}))
self.document.add_record(record)
def process_account_row(self, row):
#FIXME unicode
group = row['GRUPO']
code = row['NUMERO']
kind = self.types[row['clase']]
description = row['DESCRIPCION'].decode('utf8')
id = sanitize(description)
if id in self.record_ids:
old_id = id
id = '_'.join([id, code])
print("Wups, looks like you have duplicated names "
"using '%s' instead '%s'" % (id, old_id))
if kind == 'view':
code += '*'
self.record_ids.add(id)
parent = self.get_parent_for(group, id)
record = Record('account.account.template', id)
record.add_field(Field('name', value=description))
record.add_field(Field('code', value=code))
if row['aplazar']:
record.add_field(Field('deferral', {'eval': 'True'}))
if row['conciliar']:
record.add_field(Field('reconcile', {'eval': 'True'}))
record.add_field(Field('kind', value=kind))
if kind != 'view':
type = self.account_types[row['tipo']]
record.add_field(Field('type', {'ref': type}))
record.add_field(Field('parent', {'ref': parent}))
self.document.add_record(record)
def get_parent_for(self, group, id):
"""Return parent of a given group"""
self.account_parents[group] = id
if '.' in group:
key = '.'.join(group.split('.')[:-1])
return self.account_parents[key]
else:
return 'root'
if __name__ == '__main__':
import xml.dom.minidom
def imprimir_lindo(xml_string):
tree = xml.dom.minidom.parseString(xml_string)
return tree.toprettyxml(encoding='utf8')
tipos = 'account_types.csv'
cuentas = 'cuentas.csv'
g = WierdXMLGenerator(tipos, cuentas)
with open('accounts_coop_ar.xml', 'w') as fh:
indent(g.inflate()._root)
g.document.tree.write(fh, 'utf-8')
#fh.write(indent(g.inflate()._root).tostring())