Don't commit in the middle of loading modules
[odoo/odoo.git] / bin / tools / convert.py
1 #----------------------------------------------------------
2 # Convert
3 #----------------------------------------------------------
4 import re
5 import StringIO,xml.dom.minidom
6 import osv,ir,pooler
7
8 import csv
9 import os.path
10 import misc
11 import netsvc
12
13 from config import config
14 import logging
15
16
17 class ConvertError(Exception):
18         def __init__(self, doc, orig_excpt):
19                 self.d = doc
20                 self.orig = orig_excpt
21
22         def __str__(self):
23                 return 'Exception:\n\t%s\nUsing file:\n%s' % (self.orig, self.d)
24
25 def _ref(self, cr):
26         return lambda x: self.id_get(cr, False, x)
27
28 def _obj(pool, cr, uid, model_str, context=None):
29         model = pool.get(model_str)
30         return lambda x: model.browse(cr, uid, x, context=context)
31
32 def _eval_xml(self,node, pool, cr, uid, idref, context=None):
33         if context is None:
34                 context = {}
35         if node.nodeType == node.TEXT_NODE:
36                 return node.data.encode("utf8")
37         elif node.nodeType == node.ELEMENT_NODE:
38                 if node.nodeName in ('field','value'):
39                         t = node.getAttribute('type') or 'char'
40                         f_model = node.getAttribute("model").encode('ascii')
41                         if len(node.getAttribute('search')):
42                                 f_search = node.getAttribute("search").encode('utf-8')
43                                 f_use = node.getAttribute("use").encode('ascii')
44                                 f_name = node.getAttribute("name").encode('utf-8')
45                                 if len(f_use)==0:
46                                         f_use = "id"
47                                 q = eval(f_search, idref)
48                                 ids = pool.get(f_model).search(cr, uid, q)
49                                 if f_use<>'id':
50                                         ids = map(lambda x: x[f_use], pool.get(f_model).read(cr, uid, ids, [f_use]))
51                                 _cols = pool.get(f_model)._columns
52                                 if (f_name in _cols) and _cols[f_name]._type=='many2many':
53                                         return ids
54                                 f_val = False
55                                 if len(ids):
56                                         f_val = ids[0]
57                                         if isinstance(f_val, tuple):
58                                                 f_val = f_val[0]
59                                 return f_val
60                         a_eval = node.getAttribute('eval')
61                         if len(a_eval):
62                                 import time
63                                 idref['time'] = time
64                                 import release
65                                 idref['version'] = release.version.rsplit('.', 1)[0]
66                                 idref['ref'] = lambda x: self.id_get(cr, False, x)
67                                 if len(f_model):
68                                         idref['obj'] = _obj(self.pool, cr, uid, f_model, context=context)
69                                 try:
70                                         import pytz
71                                 except:
72                                         logger = netsvc.Logger()
73                                         logger.notifyChannel("init", netsvc.LOG_INFO, 'could not find pytz library')
74                                         class pytzclass(object):
75                                                 all_timezones=[]
76                                         pytz=pytzclass()
77                                 idref['pytz'] = pytz
78                                 return eval(a_eval, idref)
79                         if t == 'xml':
80                                 def _process(s, idref):
81                                         m = re.findall('[^%]%\((.*?)\)[ds]', s)
82                                         for id in m:
83                                                 if not id in idref:
84                                                         idref[id]=self.id_get(cr, False, id)
85                                         return s % idref
86                                 txt = '<?xml version="1.0"?>\n'+_process("".join([i.toxml().encode("utf8") for i in node.childNodes]), idref)
87 #                               txt = '<?xml version="1.0"?>\n'+"".join([i.toxml().encode("utf8") for i in node.childNodes]) % idref
88
89                                 return txt
90                         if t in ('char', 'int', 'float'):
91                                 d = ""
92                                 for n in [i for i in node.childNodes]:
93                                         d+=str(_eval_xml(self,n,pool,cr,uid,idref))
94                                 if t == 'int':
95                                         d = d.strip()
96                                         if d=='None':
97                                                 return None
98                                         else:
99                                                 d=int(d.strip())
100                                 elif t=='float':
101                                         d=float(d.strip())
102                                 return d
103                         elif t in ('list','tuple'):
104                                 res=[]
105                                 for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=='value')]:
106                                         res.append(_eval_xml(self,n,pool,cr,uid,idref))
107                                 if t=='tuple':
108                                         return tuple(res)
109                                 return res
110                 elif node.nodeName=="getitem":
111                         for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
112                                 res=_eval_xml(self,n,pool,cr,uid,idref)
113                         if not res:
114                                 raise LookupError
115                         elif node.getAttribute('type') in ("int", "list"):
116                                 return res[int(node.getAttribute('index'))]
117                         else:
118                                 return res[node.getAttribute('index').encode("utf8")]
119                 elif node.nodeName=="function":
120                         args = []
121                         a_eval = node.getAttribute('eval')
122                         if len(a_eval):
123                                 idref['ref'] = lambda x: self.id_get(cr, False, x)
124                                 args = eval(a_eval, idref)
125                         for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
126                                 args.append(_eval_xml(self,n, pool, cr, uid, idref, context))
127                         model = pool.get(node.getAttribute('model'))
128                         method = node.getAttribute('name')
129                         res = getattr(model, method)(cr, uid, *args)
130                         return res
131                 elif node.nodeName=="test":
132                         d = ""
133                         for n in [i for i in node.childNodes]:
134                                 d+=str(_eval_xml(self,n,pool,cr,uid,idref, context=context))
135                         return d
136
137
138 escape_re = re.compile(r'(?<!\\)/')
139 def escape(x):
140         return x.replace('\\/', '/')
141
142 class assertion_report(object):
143         def __init__(self):
144                 self._report = {}
145
146         def record_assertion(self, success, severity):
147                 """
148                         Records the result of an assertion for the failed/success count
149                         retrurns success
150                 """
151                 if severity in self._report:
152                         self._report[severity][success] += 1
153                 else:
154                         self._report[severity] = {success:1, not success: 0}
155                 return success
156
157         def get_report(self):
158                 return self._report
159
160         def __str__(self):
161                 res = '\nAssertions report:\nLevel\tsuccess\tfailed\n'
162                 success = failed = 0
163                 for sev in self._report:
164                         res += sev + '\t' + str(self._report[sev][True]) + '\t' + str(self._report[sev][False]) + '\n'
165                         success += self._report[sev][True]
166                         failed += self._report[sev][False]
167                 res += 'total\t' + str(success) + '\t' + str(failed) + '\n'
168                 res += 'end of report (' + str(success + failed) + ' assertion(s) checked)'
169                 return res
170
171 class xml_import(object):
172
173         def isnoupdate(self, data_node = None):
174                 return self.noupdate or (data_node and data_node.getAttribute('noupdate'))
175
176         def get_context(self, data_node, node, eval_dict):
177                 data_node_context = (data_node and data_node.getAttribute('context').encode('utf8'))
178                 if data_node_context:
179                         context = eval(data_node_context, eval_dict)
180                 else:
181                         context = {}
182
183                 node_context = node.getAttribute("context").encode('utf8')
184                 if len(node_context):
185                         context.update(eval(node_context, eval_dict))
186
187                 return context
188
189         def get_uid(self, cr, uid, data_node, node):
190                 node_uid = node.getAttribute('uid') or (data_node and data_node.getAttribute('uid'))
191                 if len(node_uid):
192                         return self.id_get(cr, None, node_uid)
193                 return uid
194
195         def _test_xml_id(self, xml_id):
196                 id = xml_id
197                 if '.' in xml_id:
198                         base, id = xml_id.split('.')
199                 if len(id) > 64:
200                         self.logger.notifyChannel('init', netsvc.LOG_ERROR, 'id: %s is to long (max: 64)'%xml_id)
201         def _tag_delete(self, cr, rec, data_node=None):
202                 d_model = rec.getAttribute("model")
203                 d_search = rec.getAttribute("search")
204                 d_id = rec.getAttribute("id")
205                 ids = []
206                 if len(d_search):
207                         ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
208                 if len(d_id):
209                         ids.append(self.id_get(cr, d_model, d_id))
210                 if len(ids):
211                         self.pool.get(d_model).unlink(cr, self.uid, ids)
212                         #self.pool.get('ir.model.data')._unlink(cr, self.uid, d_model, ids, direct=True)
213                 return False
214
215         def _tag_report(self, cr, rec, data_node=None):
216                 res = {}
217                 for dest,f in (('name','string'),('model','model'),('report_name','name')):
218                         res[dest] = rec.getAttribute(f).encode('utf8')
219                         assert res[dest], "Attribute %s of report is empty !" % (f,)
220                 for field,dest in (('rml','report_rml'),('xml','report_xml'),('xsl','report_xsl')):
221                         if rec.hasAttribute(field):
222                                 res[dest] = rec.getAttribute(field).encode('utf8')
223                 if rec.hasAttribute('auto'):
224                         res['auto'] = eval(rec.getAttribute('auto'))
225                 if rec.hasAttribute('sxw'):
226                         sxw_content = misc.file_open(rec.getAttribute('sxw')).read()
227                         res['report_sxw_content'] = sxw_content
228                 if rec.hasAttribute('header'):
229                         res['header'] = eval(rec.getAttribute('header'))
230                 res['multi'] = rec.hasAttribute('multi') and  eval(rec.getAttribute('multi'))
231                 xml_id = rec.getAttribute('id').encode('utf8')
232                 self._test_xml_id(xml_id)
233                 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, mode=self.mode)
234                 self.idref[xml_id] = int(id)
235                 if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
236                         keyword = str(rec.getAttribute('keyword') or 'client_print_multi')
237                         keys = [('action',keyword),('res_model',res['model'])]
238                         value = 'ir.actions.report.xml,'+str(id)
239                         replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
240                         self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, res['name'], [res['model']], value, replace=replace, isobject=True, xml_id=xml_id)
241                 return False
242
243         def _tag_function(self, cr, rec, data_node=None):
244                 if self.isnoupdate(data_node) and self.mode != 'init':
245                         return
246                 context = self.get_context(data_node, rec, {'ref': _ref(self, cr)})
247                 uid = self.get_uid(cr, self.uid, data_node, rec)
248                 _eval_xml(self,rec, self.pool, cr, uid, self.idref, context=context)
249                 return False
250
251         def _tag_wizard(self, cr, rec, data_node=None):
252                 string = rec.getAttribute("string").encode('utf8')
253                 model = rec.getAttribute("model").encode('utf8')
254                 name = rec.getAttribute("name").encode('utf8')
255                 xml_id = rec.getAttribute('id').encode('utf8')
256                 self._test_xml_id(xml_id)
257                 multi = rec.hasAttribute('multi') and  eval(rec.getAttribute('multi'))
258                 res = {'name': string, 'wiz_name': name, 'multi':multi}
259
260                 id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, mode=self.mode)
261                 self.idref[xml_id] = int(id)
262                 # ir_set
263                 if (not rec.hasAttribute('menu') or eval(rec.getAttribute('menu'))) and id:
264                         keyword = str(rec.getAttribute('keyword') or 'client_action_multi')
265                         keys = [('action',keyword),('res_model',model)]
266                         value = 'ir.actions.wizard,'+str(id)
267                         replace = rec.hasAttribute('replace') and rec.getAttribute("replace")
268                         self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, string, [model], value, replace=replace, isobject=True, xml_id=xml_id)
269                 return False
270
271         def _tag_act_window(self, cr, rec, data_node=None):
272                 name = rec.hasAttribute('name') and rec.getAttribute('name').encode('utf-8')
273                 xml_id = rec.getAttribute('id').encode('utf8')
274                 self._test_xml_id(xml_id)
275                 type = rec.hasAttribute('type') and rec.getAttribute('type').encode('utf-8') or 'ir.actions.act_window'
276                 view_id = False
277                 if rec.hasAttribute('view'):
278                         view_id = self.id_get(cr, 'ir.actions.act_window', rec.getAttribute('view').encode('utf-8'))
279                 domain = rec.hasAttribute('domain') and rec.getAttribute('domain').encode('utf-8')
280                 context = rec.hasAttribute('context') and rec.getAttribute('context').encode('utf-8') or '{}'
281                 res_model = rec.getAttribute('res_model').encode('utf-8')
282                 src_model = rec.hasAttribute('src_model') and rec.getAttribute('src_model').encode('utf-8')
283                 view_type = rec.hasAttribute('view_type') and rec.getAttribute('view_type').encode('utf-8') or 'form'
284                 view_mode = rec.hasAttribute('view_mode') and rec.getAttribute('view_mode').encode('utf-8') or 'tree,form'
285                 usage = rec.hasAttribute('usage') and rec.getAttribute('usage').encode('utf-8')
286
287                 res = {'name': name, 'type': type, 'view_id': view_id, 'domain': domain, 'context': context, 'res_model': res_model, 'src_model': src_model, 'view_type': view_type, 'view_mode': view_mode, 'usage': usage }
288
289                 id = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, mode=self.mode)
290                 self.idref[xml_id] = int(id)
291
292                 if src_model:
293                         keyword = 'client_action_relate'
294                         keys = [('action', keyword), ('res_model', res_model)]
295                         value = 'ir.actions.act_window,'+str(id)
296                         replace = rec.hasAttribute('replace') and rec.getAttribute('replace')
297                         self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, xml_id, [src_model], value, replace=replace, isobject=True, xml_id=xml_id)
298                 # TODO add remove ir.model.data
299                 return False
300
301         def _tag_ir_set(self, cr, rec, data_node=None):
302                 if not self.mode=='init':
303                         return False
304                 res = {}
305                 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
306                         f_name = field.getAttribute("name").encode('utf-8')
307                         f_val = _eval_xml(self,field,self.pool, cr, self.uid, self.idref)
308                         res[f_name] = f_val
309                 self.pool.get('ir.model.data').ir_set(cr, self.uid, res['key'], res['key2'], res['name'], res['models'], res['value'], replace=res.get('replace',True), isobject=res.get('isobject', False), meta=res.get('meta',None))
310                 return False
311
312         def _tag_workflow(self, cr, rec, data_node=None):
313                 if self.isnoupdate(data_node) and self.mode != 'init':
314                         return
315                 model = str(rec.getAttribute('model'))
316                 w_ref = rec.getAttribute('ref')
317                 if len(w_ref):
318                         id = self.id_get(cr, model, w_ref)
319                 else:
320                         assert rec.childNodes, 'You must define a child node if you dont give a ref'
321                         element_childs = [i for i in rec.childNodes if i.nodeType == i.ELEMENT_NODE]
322                         assert len(element_childs) == 1, 'Only one child node is accepted (%d given)' % len(rec.childNodes)
323                         id = _eval_xml(self, element_childs[0], self.pool, cr, self.uid, self.idref)
324
325                 uid = self.get_uid(cr, self.uid, data_node, rec)
326                 wf_service = netsvc.LocalService("workflow")
327                 wf_service.trg_validate(uid, model,
328                         id,
329                         str(rec.getAttribute('action')), cr)
330                 return False
331
332         def _tag_menuitem(self, cr, rec, data_node=None):
333                 rec_id = rec.getAttribute("id").encode('ascii')
334                 self._test_xml_id(rec_id)
335                 m_l = map(escape, escape_re.split(rec.getAttribute("name").encode('utf8')))
336                 pid = False
337                 for idx, menu_elem in enumerate(m_l):
338                         if pid:
339                                 cr.execute('select id from ir_ui_menu where parent_id=%d and name=%s', (pid, menu_elem))
340                         else:
341                                 cr.execute('select id from ir_ui_menu where parent_id is null and name=%s', (menu_elem,))
342                         res = cr.fetchone()
343                         if idx==len(m_l)-1:
344                                 # we are at the last menu element/level (it's a leaf)
345                                 values = {'parent_id': pid,'name':menu_elem}
346
347                                 if rec.hasAttribute('action'):
348                                         a_action = rec.getAttribute('action').encode('utf8')
349                                         a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
350                                         icons = {
351                                                 "act_window": 'STOCK_NEW',
352                                                 "report.xml": 'STOCK_PASTE',
353                                                 "wizard": 'STOCK_EXECUTE',
354                                         }
355                                         values['icon'] = icons.get(a_type,'STOCK_NEW')
356                                         if a_type=='act_window':
357                                                 a_id = self.id_get(cr, 'ir.actions.%s'% a_type, a_action)
358                                                 cr.execute('select view_type,view_mode,name,view_id from ir_act_window where id=%d', (int(a_id),))
359                                                 action_type,action_mode,action_name,view_id = cr.fetchone()
360                                                 if view_id:
361                                                         cr.execute('SELECT type FROM ir_ui_view WHERE id=%d', (int(view_id),))
362                                                         action_mode, = cr.fetchone()
363                                                 cr.execute('SELECT view_mode FROM ir_act_window_view WHERE act_window_id=%d ORDER BY sequence LIMIT 1', (int(a_id),))
364                                                 if cr.rowcount:
365                                                         action_mode, = cr.fetchone()
366                                                 if action_type=='tree':
367                                                         values['icon'] = 'STOCK_INDENT'
368                                                 elif action_mode and action_mode.startswith('tree'):
369                                                         values['icon'] = 'STOCK_JUSTIFY_FILL'
370                                                 elif action_mode and action_mode.startswith('graph'):
371                                                         values['icon'] = 'terp-graph'
372                                                 elif action_mode and action_mode.startswith('calendar'):
373                                                         values['icon'] = 'terp-calendar'
374                                                 if not values['name']:
375                                                         values['name'] = action_name
376                                 if rec.hasAttribute('sequence'):
377                                         values['sequence'] = int(rec.getAttribute('sequence'))
378                                 if rec.hasAttribute('icon'):
379                                         values['icon'] = str(rec.getAttribute('icon'))
380                                 if rec.hasAttribute('groups'):
381                                         g_names = rec.getAttribute('groups').split(',')
382                                         groups_value = []
383                                         groups_obj = self.pool.get('res.groups')
384                                         for group in g_names:
385                                                 if group.startswith('-'):
386                                                         id = groups_obj.search(cr, self.uid,
387                                                                         [('name', '=', group[1:])])[0]
388                                                         groups_value.append((3, id))
389                                                 else:
390                                                         id = groups_obj.search(cr, self.uid,
391                                                                 [('name', '=', group)])[0]
392                                                         groups_value.append((4, id))
393                                         values['groups_id'] = groups_value
394                                 xml_id = rec.getAttribute('id').encode('utf8')
395                                 self._test_xml_id(xml_id)
396                                 pid = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.ui.menu', self.module, values, xml_id, idx==len(m_l)-1, mode=self.mode, res_id=res and res[0] or False)
397                         elif res:
398                                 # the menuitem already exists
399                                 pid = res[0]
400                                 xml_id = idx==len(m_l)-1 and rec.getAttribute('id').encode('utf8')
401                                 try:
402                                         npid = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, 'ir.ui.menu', self.module, xml_id, idx==len(m_l)-1)
403                                 except:
404                                         print 'Menu Error', self.module, xml_id, idx==len(m_l)-1
405                         else:
406                                 # the menuitem does't exist but we are in branch (not a leaf)
407                                 pid = self.pool.get('ir.ui.menu').create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
408                 if rec_id and pid:
409                         self.idref[rec_id] = int(pid)
410
411                 if rec.hasAttribute('action') and pid:
412                         a_action = rec.getAttribute('action').encode('utf8')
413                         a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
414                         a_id = self.id_get(cr, 'ir.actions.%s' % a_type, a_action)
415                         action = "ir.actions.%s,%d" % (a_type, a_id)
416                         self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', 'tree_but_open', 'Menuitem', [('ir.ui.menu', int(pid))], action, True, True, xml_id=rec_id)
417                 return ('ir.ui.menu', pid)
418
419         def _assert_equals(self, f1, f2, prec = 4):
420                 return not round(f1 - f2, prec)
421
422         def _tag_assert(self, cr, rec, data_node=None):
423                 if self.isnoupdate(data_node) and self.mode != 'init':
424                         return
425
426                 rec_model = rec.getAttribute("model").encode('ascii')
427                 model = self.pool.get(rec_model)
428                 assert model, "The model %s does not exist !" % (rec_model,)
429                 rec_id = rec.getAttribute("id").encode('ascii')
430                 self._test_xml_id(rec_id)
431                 rec_src = rec.getAttribute("search").encode('utf8')
432                 rec_src_count = rec.getAttribute("count")
433
434                 severity = rec.getAttribute("severity").encode('ascii') or 'info'
435
436                 rec_string = rec.getAttribute("string").encode('utf8') or 'unknown'
437
438                 ids = None
439                 eval_dict = {'ref': _ref(self, cr)}
440                 context = self.get_context(data_node, rec, eval_dict)
441                 uid = self.get_uid(cr, self.uid, data_node, rec)
442                 if len(rec_id):
443                         ids = [self.id_get(cr, rec_model, rec_id)]
444                 elif len(rec_src):
445                         q = eval(rec_src, eval_dict)
446                         ids = self.pool.get(rec_model).search(cr, uid, q, context=context)
447                         if len(rec_src_count):
448                                 count = int(rec_src_count)
449                                 if len(ids) != count:
450                                         self.assert_report.record_assertion(False, severity)
451                                         self.logger.notifyChannel('init', severity, 'assertion "' + rec_string + '" failed ! (search count is incorrect: ' + str(len(ids)) + ')' )
452                                         sevval = getattr(logging, severity.upper())
453                                         if sevval > config['assert_exit_level']:
454                                                 # TODO: define a dedicated exception
455                                                 raise Exception('Severe assertion failure')
456                                         return
457
458                 assert ids != None, 'You must give either an id or a search criteria'
459
460                 ref = _ref(self, cr)
461                 for id in ids:
462                         brrec =  model.browse(cr, uid, id, context)
463                         class d(dict):
464                                 def __getitem__(self2, key):
465                                         if key in brrec:
466                                                 return brrec[key]
467                                         return dict.__getitem__(self2, key)
468                         globals = d()
469                         globals['floatEqual'] = self._assert_equals
470                         globals['ref'] = ref
471                         globals['_ref'] = ref
472                         for test in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="test")]:
473                                 f_expr = test.getAttribute("expr").encode('utf-8')
474                                 f_val = _eval_xml(self, test, self.pool, cr, uid, self.idref, context=context) or True
475                                 if eval(f_expr, globals) != f_val: # assertion failed
476                                         self.assert_report.record_assertion(False, severity)
477                                         self.logger.notifyChannel('init', severity, 'assertion "' + rec_string + '" failed ! (tag ' + test.toxml() + ')' )
478                                         sevval = getattr(logging, severity.upper())
479                                         if sevval > config['assert_exit_level']:
480                                                 # TODO: define a dedicated exception
481                                                 raise Exception('Severe assertion failure')
482                                         return
483                 else: # all tests were successful for this assertion tag (no break)
484                         self.assert_report.record_assertion(True, severity)
485
486         def _tag_record(self, cr, rec, data_node=None):
487                 rec_model = rec.getAttribute("model").encode('ascii')
488                 model = self.pool.get(rec_model)
489                 assert model, "The model %s does not exist !" % (rec_model,)
490                 rec_id = rec.getAttribute("id").encode('ascii')
491                 self._test_xml_id(rec_id)
492
493 #               if not rec_id and not self.isnoupdate(data_node):
494 #                       print "Warning", rec_model
495
496                 if self.isnoupdate(data_node) and not self.mode == 'init':
497                         # check if the xml record has an id string
498                         if rec_id:
499                                 id = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, rec_model, self.module, rec_id)
500                                 # check if the resource already existed at the last update
501                                 if id:
502                                         # if it existed, we don't update the data, but we need to
503                                         # know the id of the existing record anyway
504                                         self.idref[rec_id] = int(id)
505                                         return None
506                                 else:
507                                         # if the resource didn't exist
508                                         if rec.getAttribute("forcecreate"):
509                                                 # we want to create it, so we let the normal "update" behavior happen
510                                                 pass
511                                         else:
512                                                 # otherwise do nothing
513                                                 return None
514                         else:
515                                 # otherwise it is skipped
516                                 return None
517
518                 res = {}
519                 for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
520 #TODO: most of this code is duplicated above (in _eval_xml)...
521                         f_name = field.getAttribute("name").encode('utf-8')
522                         f_ref = field.getAttribute("ref").encode('ascii')
523                         f_search = field.getAttribute("search").encode('utf-8')
524                         f_model = field.getAttribute("model").encode('ascii')
525                         if not f_model and model._columns.get(f_name,False):
526                                 f_model = model._columns[f_name]._obj
527                         f_use = field.getAttribute("use").encode('ascii') or 'id'
528                         f_val = False
529
530                         if len(f_search):
531                                 q = eval(f_search, self.idref)
532                                 field = []
533                                 assert f_model, 'Define an attribute model="..." in your .XML file !'
534                                 f_obj = self.pool.get(f_model)
535                                 # browse the objects searched
536                                 s = f_obj.browse(cr, self.uid, f_obj.search(cr, self.uid, q))
537                                 # column definitions of the "local" object
538                                 _cols = self.pool.get(rec_model)._columns
539                                 # if the current field is many2many
540                                 if (f_name in _cols) and _cols[f_name]._type=='many2many':
541                                         f_val = [(6, 0, map(lambda x: x[f_use], s))]
542                                 elif len(s):
543                                         # otherwise (we are probably in a many2one field),
544                                         # take the first element of the search
545                                         f_val = s[0][f_use]
546                         elif len(f_ref):
547                                 if f_ref=="null":
548                                         f_val = False
549                                 else:
550                                         f_val = self.id_get(cr, f_model, f_ref)
551                         else:
552                                 f_val = _eval_xml(self,field, self.pool, cr, self.uid, self.idref)
553                                 if model._columns.has_key(f_name):
554                                         if isinstance(model._columns[f_name], osv.fields.integer):
555                                                 f_val = int(f_val)
556                         res[f_name] = f_val
557                 id = self.pool.get('ir.model.data')._update(cr, self.uid, rec_model, self.module, res, rec_id or False, not self.isnoupdate(data_node), noupdate=self.isnoupdate(data_node), mode=self.mode )
558                 if rec_id:
559                         self.idref[rec_id] = int(id)
560                 return rec_model, id
561
562         def id_get(self, cr, model, id_str):
563                 if id_str in self.idref:
564                         return self.idref[id_str]
565                 mod = self.module
566                 if '.' in id_str:
567                         mod,id_str = id_str.split('.')
568                 result = self.pool.get('ir.model.data')._get_id(cr, self.uid, mod, id_str)
569                 return int(self.pool.get('ir.model.data').read(cr, self.uid, [result], ['res_id'])[0]['res_id'])
570
571         def parse(self, xmlstr):
572                 d = xml.dom.minidom.parseString(xmlstr)
573                 de = d.documentElement
574                 for n in [i for i in de.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="data")]:
575                         for rec in n.childNodes:
576                                 if rec.nodeType == rec.ELEMENT_NODE:
577                                         if rec.nodeName in self._tags:
578                                                 try:
579                                                         self._tags[rec.nodeName](self.cr, rec, n)
580                                                 except:
581                                                         self.logger.notifyChannel("init", netsvc.LOG_INFO, '\n'+rec.toxml())
582                                                         self.cr.rollback()
583                                                         raise
584                 return True
585
586         def __init__(self, cr, module, idref, mode, report=assertion_report(), noupdate = False):
587                 self.logger = netsvc.Logger()
588                 self.mode = mode
589                 self.module = module
590                 self.cr = cr
591                 self.idref = idref
592                 self.pool = pooler.get_pool(cr.dbname)
593 #               self.pool = osv.osv.FakePool(module)
594                 self.uid = 1
595                 self.assert_report = report
596                 self.noupdate = noupdate
597                 self._tags = {
598                         'menuitem': self._tag_menuitem,
599                         'record': self._tag_record,
600                         'assert': self._tag_assert,
601                         'report': self._tag_report,
602                         'wizard': self._tag_wizard,
603                         'delete': self._tag_delete,
604                         'ir_set': self._tag_ir_set,
605                         'function': self._tag_function,
606                         'workflow': self._tag_workflow,
607                         'act_window': self._tag_act_window,
608                 }
609
610 CSV_STEP = 500
611
612 def convert_csv_import(cr, module, fname, csvcontent, idref=None, mode='init', noupdate=False):
613         '''Import csv file :
614                 quote: "
615                 delimiter: ,
616                 encoding: utf-8'''
617         if not idref:
618                 idref={}
619         model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
620         #remove folder path from model
621         head, model = os.path.split(model)
622
623         pool = pooler.get_pool(cr.dbname)
624
625         input=StringIO.StringIO(csvcontent)
626         reader = csv.reader(input, quotechar='"', delimiter=',')
627         fields = reader.next()
628
629         if not (mode == 'init' or 'id' in fields):
630                 return
631
632         uid = 1
633         datas = []
634         for line in reader:
635                 datas.append( map(lambda x:x.decode('utf8').encode('utf8'), line))
636                 if len(datas) > CSV_STEP:
637                         pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
638                         datas=[]
639         if datas:
640                 pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
641
642 #
643 # xml import/export
644 #
645 def convert_xml_import(cr, module, xmlstr, idref=None, mode='init', noupdate = False, report=None):
646         if not idref:
647                 idref={}
648         if report is None:
649                 report=assertion_report()
650         obj = xml_import(cr, module, idref, mode, report=report, noupdate = noupdate)
651         obj.parse(xmlstr)
652         del obj
653         return True
654
655 def convert_xml_export(res):
656         uid=1
657         pool=pooler.get_pool(cr.dbname)
658         cr=pooler.db.cursor()
659         idref = {}
660         d = xml.dom.minidom.getDOMImplementation().createDocument(None, "terp", None)
661         de = d.documentElement
662         data=d.createElement("data")
663         de.appendChild(data)
664         de.appendChild(d.createTextNode('Some textual content.'))
665         cr.commit()
666         cr.close()
667