[FIX] removed use of magic methods/attributes, plus some useless code duplication
[odoo/odoo.git] / bin / tools / yaml_import.py
1 # -*- encoding: utf-8 -*-
2 import types
3 import time # used to eval time.strftime expressions
4 import logging
5
6 import pooler
7 import netsvc
8 import misc
9 from config import config
10 import yaml_tag
11
12 import yaml
13
14 logger_channel = 'tests'
15
16 class YamlImportException(Exception):
17     pass
18
19 class YamlImportAbortion(Exception):
20     pass
21
22 def _is_yaml_mapping(node, tag_constructor):
23     value = isinstance(node, types.DictionaryType) \
24         and len(node.keys()) == 1 \
25         and isinstance(node.keys()[0], tag_constructor)
26     return value
27
28 def is_comment(node):
29     return isinstance(node, types.StringTypes)
30
31 def is_assert(node):
32     return _is_yaml_mapping(node, yaml_tag.Assert)
33
34 def is_record(node):
35     return _is_yaml_mapping(node, yaml_tag.Record)
36
37 def is_python(node):
38     return _is_yaml_mapping(node, yaml_tag.Python)
39
40 def is_menuitem(node):
41     return isinstance(node, yaml_tag.Menuitem) \
42         or _is_yaml_mapping(node, yaml_tag.Menuitem)
43
44 def is_function(node):
45     return isinstance(node, yaml_tag.Function) \
46         or _is_yaml_mapping(node, yaml_tag.Function)
47
48 def is_report(node):
49     return isinstance(node, yaml_tag.Report)
50
51 def is_workflow(node):
52     return isinstance(node, yaml_tag.Workflow)
53
54 def is_act_window(node):
55     return isinstance(node, yaml_tag.ActWindow)
56
57 def is_delete(node):
58     return isinstance(node, yaml_tag.Delete)
59
60 def is_context(node):
61     return isinstance(node, yaml_tag.Context)
62
63 def is_url(node):
64     return isinstance(node, yaml_tag.Url)
65
66 def is_eval(node):
67     return isinstance(node, yaml_tag.Eval)
68     
69 def is_ref(node):
70     return isinstance(node, yaml_tag.Ref) \
71         or _is_yaml_mapping(node, yaml_tag.Ref)
72     
73 def is_ir_set(node):
74     return _is_yaml_mapping(node, yaml_tag.IrSet)
75
76
77 class TestReport(object):
78     def __init__(self):
79         self._report = {}
80
81     def record(self, success, severity):
82         """
83         Records the result of an assertion for the failed/success count.
84         Returns success.
85         """
86         if severity in self._report:
87             self._report[severity][success] += 1
88         else:
89             self._report[severity] = {success: 1, not success: 0}
90         return success
91
92     def __str__(self):
93         res = []
94         res.append('\nAssertions report:\nLevel\tsuccess\tfailure')
95         success = failure = 0
96         for severity in self._report:
97             res.append("%s\t%s\t%s" % (severity, self._report[severity][True], self._report[severity][False]))
98             success += self._report[severity][True]
99             failure += self._report[severity][False]
100         res.append("total\t%s\t%s" % (success, failure))
101         res.append("end of report (%s assertion(s) checked)" % success + failure)
102         return "\n".join(res)
103
104 class RecordDictWrapper(dict):
105     """
106     Used to pass a record as locals in eval:
107     records do not strictly behave like dict, so we force them to.
108     """
109     def __init__(self, record):
110         self.record = record 
111     def __getitem__(self, key):
112         if key in self.record:
113             return self.record[key]
114         return dict.__getitem__(self, key)
115  
116 class YamlInterpreter(object):
117     def __init__(self, cr, module, id_map, mode, filename, noupdate=False):
118         self.cr = cr
119         self.module = module
120         self.id_map = id_map
121         self.mode = mode
122         self.filename = filename
123         self.assert_report = TestReport()
124         self.noupdate = noupdate
125         self.logger = logging.getLogger("%s.%s" % (logger_channel, self.module))
126         self.pool = pooler.get_pool(cr.dbname)
127         self.uid = 1
128         self.context = {} # opererp context
129         self.eval_context = {'ref': self._ref(), '_ref': self._ref(), 'time': time} # added '_ref' so that record['ref'] is possible
130
131     def _ref(self):
132         return lambda xml_id: self.get_id(xml_id)
133
134     def get_model(self, model_name):
135         model = self.pool.get(model_name)
136         assert model, "The model %s does not exist." % (model_name,)
137         return model
138     
139     def validate_xml_id(self, xml_id):
140         id = xml_id
141         if '.' in xml_id:
142             module, id = xml_id.split('.', 1)
143             assert '.' not in id, "The ID reference '%s' must contains maximum one dot.\n" \
144                                   "It is used to refer to other modules ID, in the form: module.record_id" \
145                                   % (xml_id,)
146             if module != self.module:
147                 module_count = self.pool.get('ir.module.module').search_count(self.cr, self.uid, \
148                         ['&', ('name', '=', module), ('state', 'in', ['installed'])])
149                 assert module_count == 1, 'The ID "%s" refers to an uninstalled module.' % (xml_id,)
150         if len(id) > 64: # TODO where does 64 come from (DB is 128)? should be a constant or loaded form DB
151             self.logger.log(logging.ERROR, 'id: %s is to long (max: 64)', id)
152
153     def get_id(self, xml_id):
154         if not xml_id:
155             raise YamlImportException("The xml_id should be a non empty string.")
156         if isinstance(xml_id, types.IntType):
157             id = xml_id
158         elif xml_id in self.id_map:
159             id = self.id_map[xml_id]
160         else:
161             if '.' in xml_id:
162                 module, checked_xml_id = xml_id.split('.', 1)
163             else:
164                 module = self.module
165                 checked_xml_id = xml_id
166             ir_id = self.pool.get('ir.model.data')._get_id(self.cr, self.uid, module, checked_xml_id)
167             obj = self.pool.get('ir.model.data').read(self.cr, self.uid, ir_id, ['res_id'])
168             id = int(obj['res_id'])
169             self.id_map[xml_id] = id
170         return id
171     
172     def get_context(self, node, eval_dict):
173         context = self.context.copy()
174         if node.context:
175             context.update(eval(node.context, eval_dict))
176         return context
177
178     def isnoupdate(self, node):
179         return self.noupdate or node.noupdate or False
180     
181     def _get_first_result(self, results, default=False):
182         if len(results):
183             value = results[0]
184             if isinstance(value, types.TupleType):
185                 value = value[0]
186         else:
187             value = default
188         return value
189     
190     def process_comment(self, node):
191         return node
192
193     def _log_assert_failure(self, severity, msg, *args):
194         if isinstance(severity, types.StringTypes):
195             levelname = severity.strip().upper()
196             level = logging.getLevelName(levelname)
197         else:
198             level = severity
199             levelname = logging.getLevelName(level)
200         self.assert_report.record(False, levelname)
201         self.logger.log(level, msg, *args)
202         if level >= config['assert_exit_level']:
203             raise YamlImportAbortion('Severe assertion failure (%s), aborting.' % levelname)
204         return
205
206     def _get_assertion_id(self, assertion):
207         if assertion.id:
208             ids = [self.get_id(assertion.id)]
209         elif assertion.search:
210             q = eval(assertion.search, self.eval_context)
211             ids = self.pool.get(assertion.model).search(self.cr, self.uid, q, context=assertion.context)
212         if not ids:
213             raise YamlImportException('Nothing to assert: you must give either an id or a search criteria.')
214         return ids
215
216     def process_assert(self, node):
217         assertion, expressions = node.items()[0]
218
219         if self.isnoupdate(assertion) and self.mode != 'init':
220             self.logger.warn('This assertion was not evaluated ("%s").' % assertion.string)
221             return
222         model = self.get_model(assertion.model)
223         ids = self._get_assertion_id(assertion)
224         if assertion.count and len(ids) != assertion.count:
225             msg = 'assertion "%s" failed!\n'   \
226                   ' Incorrect search count:\n' \
227                   ' expected count: %d\n'      \
228                   ' obtained count: %d\n'
229             args = (assertion.string, assertion.count, len(ids))
230             self._log_assert_failure(assertion.severity, msg, *args)
231         else:
232             context = self.get_context(assertion, self.eval_context)
233             for id in ids:
234                 record = model.browse(self.cr, self.uid, id, context)
235                 for test in expressions:
236                     try:
237                         success = eval(test, self.eval_context, RecordDictWrapper(record))
238                     except Exception, e:
239                         raise YamlImportAbortion(e)
240                     if not success:
241                         msg = 'Assertion "%s" FAILED\ntest: %s\n'
242                         args = (assertion.string, test)
243                         self._log_assert_failure(assertion.severity, msg, *args)
244                         return
245             else: # all tests were successful for this assertion tag (no break)
246                 self.assert_report.record(True, assertion.severity)
247
248     def _coerce_bool(self, value, default=False):
249         if isinstance(value, types.BooleanType):
250             b = value
251         if isinstance(value, types.StringTypes):
252             b = value.strip().lower() not in ('0', 'false', 'off', 'no')
253         elif isinstance(value, types.IntType):
254             b = bool(value)
255         else:
256             b = default
257         return b
258
259     def create_osv_memory_record(self, record, fields):
260         model = self.get_model(record.model)
261         record_dict = self._create_record(model, fields)
262         id_new=model.create(self.cr, self.uid, record_dict, context=self.context)
263         self.id_map[record.id] = int(id_new)
264         return record_dict
265
266     def process_record(self, node):
267         import osv
268         record, fields = node.items()[0]
269         model = self.get_model(record.model)
270         if isinstance(model, osv.osv.osv_memory):
271             record_dict=self.create_osv_memory_record(record, fields)
272         else:
273             self.validate_xml_id(record.id)
274             if self.isnoupdate(record) and self.mode != 'init':
275                 id = self.pool.get('ir.model.data')._update_dummy(self.cr, self.uid, record.model, self.module, record.id)
276                 # check if the resource already existed at the last update
277                 if id:
278                     self.id_map[record] = int(id)
279                     return None
280                 else:
281                     if not self._coerce_bool(record.forcecreate):
282                         return None
283
284             record_dict = self._create_record(model, fields)
285             self.logger.debug("RECORD_DICT %s" % record_dict)
286             id = self.pool.get('ir.model.data')._update(self.cr, self.uid, record.model, \
287                     self.module, record_dict, record.id, noupdate=self.isnoupdate(record), mode=self.mode)
288             self.id_map[record.id] = int(id)
289             if config.get('import_partial'):
290                 self.cr.commit()
291
292     def _create_record(self, model, fields):
293         record_dict = {}
294         for field_name, expression in fields.items():
295             field_value = self._eval_field(model, field_name, expression)
296             record_dict[field_name] = field_value
297         return record_dict
298
299     def process_ref(self, node, column=None):
300         if node.search:
301             if node.model:
302                 model_name = node.model
303             elif column:
304                 model_name = column._obj
305             else:
306                 raise YamlImportException('You need to give a model for the search, or a column to infer it.')
307             model = self.get_model(model_name)
308             q = eval(node.search, self.eval_context)
309             ids = model.search(self.cr, self.uid, q)
310             if node.use:
311                 instances = model.browse(self.cr, self.uid, ids)
312                 value = [inst[node.use] for inst in instances]
313             else:
314                 value = ids
315         elif node.id:
316             value = self.get_id(node.id)
317         else:
318             value = None
319         return value
320
321     def process_eval(self, node):
322         return eval(node.expression, self.eval_context)
323
324     def _eval_field(self, model, field_name, expression):
325         # TODO this should be refactored as something like model.get_field() in bin/osv
326         if field_name in model._columns:
327             column = model._columns[field_name]
328         elif field_name in model._inherit_fields:
329             column = model._inherit_fields[field_name][2]
330         else:
331             raise KeyError("Object '%s' does not contain field '%s'" % (model, field_name))
332         if is_ref(expression):
333             elements = self.process_ref(expression)
334             if column._type in ("many2many", "one2many"):
335                 value = [(6, 0, elements)]
336             else: # many2one
337                 value = self._get_first_result(elements)
338         elif column._type == "many2one":
339             value = self.get_id(expression)
340         elif column._type == "one2many":
341             other_model = self.get_model(column._obj)
342             value = [(0, 0, self._create_record(other_model, fields)) for fields in expression]
343         elif column._type == "many2many":
344             ids = [self.get_id(xml_id) for xml_id in expression]
345             value = [(6, 0, ids)]
346         else: # scalar field
347             if is_eval(expression):
348                 value = self.process_eval(expression)
349             else:
350                 value = expression
351             # raise YamlImportException('Unsupported column "%s" or value %s:%s' % (field_name, type(expression), expression))
352         return value
353     
354     def process_context(self, node):
355         self.context = node.__dict__
356         if node.uid:
357             self.uid = self.get_id(node.uid)
358         if node.noupdate:
359             self.noupdate = node.noupdate
360     
361     def process_python(self, node):
362         def log(msg, *args):
363             self.logger.log(logging.TEST, msg, *args)
364         python, statements = node.items()[0]
365         model = self.get_model(python.model)
366         statements = statements.replace("\r\n", "\n")
367         code_context = {'model': model, 'cr': self.cr, 'uid': self.uid, 'log': log, 'context': self.context}
368         code_context.update({'self': model}) # remove me when no !python block test uses 'self' anymore
369         try:
370             code = compile(statements, self.filename, 'exec')
371             eval(code, {'ref': self.get_id}, code_context)
372         except AssertionError, e:
373             self._log_assert_failure(python.severity, 'AssertionError in Python code %s: %s', python.name, e)
374             return
375         except Exception, e:
376             raise YamlImportAbortion(e)
377         else:
378             self.assert_report.record(True, python.severity)
379     
380     def process_workflow(self, node):
381         workflow, values = node.items()[0]
382         if self.isnoupdate(workflow) and self.mode != 'init':
383             return
384         if workflow.ref:
385             id = self.get_id(workflow.ref)
386         else:
387             if not values:
388                 raise YamlImportException('You must define a child node if you do not give a ref.')
389             if not len(values) == 1:
390                 raise YamlImportException('Only one child node is accepted (%d given).' % len(values))
391             value = values[0]
392             if not 'model' in value and (not 'eval' in value or not 'search' in value):
393                 raise YamlImportException('You must provide a "model" and an "eval" or "search" to evaluate.')
394             value_model = self.get_model(value['model'])
395             local_context = {'obj': lambda x: value_model.browse(self.cr, self.uid, x, context=self.context)}
396             local_context.update(self.id_map)
397             id = eval(value['eval'], self.eval_context, local_context)
398         
399         if workflow.uid is not None:
400             uid = workflow.uid
401         else:
402             uid = self.uid
403         self.cr.execute('select distinct signal from wkf_transition')
404         signals=[x['signal'] for x in self.cr.dictfetchall()]
405         if workflow.action not in signals:
406             raise YamlImportException('Incorrect action %s. No such action defined' % workflow.action)
407         wf_service = netsvc.LocalService("workflow")
408         wf_service.trg_validate(uid, workflow.model, id, workflow.action, self.cr)
409     
410     def _eval_params(self, model, params):
411         args = []
412         for i, param in enumerate(params):
413             if isinstance(param, types.ListType):
414                 value = self._eval_params(model, param)
415             elif is_ref(param):
416                 value = self.process_ref(param)
417             elif is_eval(param):
418                 value = self.process_eval(param)
419             elif isinstance(param, types.DictionaryType): # supports XML syntax
420                 param_model = self.get_model(param.get('model', model))
421                 if 'search' in param:
422                     q = eval(param['search'], self.eval_context)
423                     ids = param_model.search(self.cr, self.uid, q)
424                     value = self._get_first_result(ids)
425                 elif 'eval' in param:
426                     local_context = {'obj': lambda x: param_model.browse(self.cr, self.uid, x, self.context)}
427                     local_context.update(self.id_map)
428                     value = eval(param['eval'], self.eval_context, local_context)
429                 else:
430                     raise YamlImportException('You must provide either a !ref or at least a "eval" or a "search" to function parameter #%d.' % i)
431             else:
432                 value = param # scalar value
433             args.append(value)
434         return args
435         
436     def process_function(self, node):
437         function, params = node.items()[0]
438         if self.isnoupdate(function) and self.mode != 'init':
439             return
440         model = self.get_model(function.model)
441         context = self.get_context(function, self.eval_context)
442         if function.eval:
443             args = self.process_eval(function.eval)
444         else:
445             args = self._eval_params(function.model, params)
446         method = function.name
447         getattr(model, method)(self.cr, self.uid, *args)
448     
449     def _set_group_values(self, node, values):
450         if node.groups:
451             group_names = node.groups.split(',')
452             groups_value = []
453             for group in group_names:
454                 if group.startswith('-'):
455                     group_id = self.get_id(group[1:])
456                     groups_value.append((3, group_id))
457                 else:
458                     group_id = self.get_id(group)
459                     groups_value.append((4, group_id))
460             values['groups_id'] = groups_value
461
462     def process_menuitem(self, node):
463         self.validate_xml_id(node.id)
464
465         if not node.parent:
466             parent_id = False
467             self.cr.execute('select id from ir_ui_menu where parent_id is null and name=%s', (node.name,))
468             res = self.cr.fetchone()
469             values = {'parent_id': parent_id, 'name': node.name}
470         else:
471             parent_id = self.get_id(node.parent)
472             values = {'parent_id': parent_id}
473             if node.name:
474                 values['name'] = node.name
475             try:
476                 res = [ self.get_id(node.id) ]
477             except: # which exception ?
478                 res = None
479
480         if node.action:
481             action_type = node.type or 'act_window'
482             icons = {
483                 "act_window": 'STOCK_NEW',
484                 "report.xml": 'STOCK_PASTE',
485                 "wizard": 'STOCK_EXECUTE',
486                 "url": 'STOCK_JUMP_TO',
487             }
488             values['icon'] = icons.get(action_type, 'STOCK_NEW')
489             if action_type == 'act_window':
490                 action_id = self.get_id(node.action)
491                 self.cr.execute('select view_type,view_mode,name,view_id,target from ir_act_window where id=%s', (action_id,))
492                 ir_act_window_result = self.cr.fetchone()
493                 assert ir_act_window_result, "No window action defined for this id %s !\n" \
494                         "Verify that this is a window action or add a type argument." % (node.action,)
495                 action_type, action_mode, action_name, view_id, target = ir_act_window_result
496                 if view_id:
497                     self.cr.execute('SELECT type FROM ir_ui_view WHERE id=%s', (view_id,))
498                     # TODO guess why action_mode is ir_act_window.view_mode above and ir_ui_view.type here
499                     action_mode = self.cr.fetchone()
500                 self.cr.execute('SELECT view_mode FROM ir_act_window_view WHERE act_window_id=%s ORDER BY sequence LIMIT 1', (action_id,))
501                 if self.cr.rowcount:
502                     action_mode = self.cr.fetchone()
503                 if action_type == 'tree':
504                     values['icon'] = 'STOCK_INDENT'
505                 elif action_mode and action_mode.startswith('tree'):
506                     values['icon'] = 'STOCK_JUSTIFY_FILL'
507                 elif action_mode and action_mode.startswith('graph'):
508                     values['icon'] = 'terp-graph'
509                 elif action_mode and action_mode.startswith('calendar'):
510                     values['icon'] = 'terp-calendar'
511                 if target == 'new':
512                     values['icon'] = 'STOCK_EXECUTE'
513                 if not values.get('name', False):
514                     values['name'] = action_name
515             elif action_type == 'wizard':
516                 action_id = self.get_id(node.action)
517                 self.cr.execute('select name from ir_act_wizard where id=%s', (action_id,))
518                 ir_act_wizard_result = self.cr.fetchone()
519                 if (not values.get('name', False)) and ir_act_wizard_result:
520                     values['name'] = ir_act_wizard_result[0]
521             else:
522                 raise YamlImportException("Unsupported type '%s' in menuitem tag." % action_type)
523         if node.sequence:
524             values['sequence'] = node.sequence
525         if node.icon:
526             values['icon'] = node.icon
527
528         self._set_group_values(node, values)
529         
530         pid = self.pool.get('ir.model.data')._update(self.cr, self.uid, \
531                 'ir.ui.menu', self.module, values, node.id, mode=self.mode, \
532                 noupdate=self.isnoupdate(node), res_id=res and res[0] or False)
533
534         if node.id and parent_id:
535             self.id_map[node.id] = int(parent_id)
536
537         if node.action and pid:
538             action_type = node.type or 'act_window'
539             action_id = self.get_id(node.action)
540             action = "ir.actions.%s,%d" % (action_type, action_id)
541             self.pool.get('ir.model.data').ir_set(self.cr, self.uid, 'action', \
542                     'tree_but_open', 'Menuitem', [('ir.ui.menu', int(parent_id))], action, True, True, xml_id=node.id)
543
544     def process_act_window(self, node):
545         assert getattr(node, 'id'), "Attribute %s of act_window is empty !" % ('id',)
546         assert getattr(node, 'name'), "Attribute %s of act_window is empty !" % ('name',)
547         assert getattr(node, 'res_model'), "Attribute %s of act_window is empty !" % ('res_model',)
548         self.validate_xml_id(node.id)
549         view_id = False
550         if node.view:
551             view_id = self.get_id(node.view)
552         if not node.context:
553             node.context={}
554         context = eval(str(node.context), self.eval_context)
555         values = {
556             'name': node.name,
557             'type': node.type or 'ir.actions.act_window',
558             'view_id': view_id,
559             'domain': node.domain,
560             'context': context,
561             'res_model': node.res_model,
562             'src_model': node.src_model,
563             'view_type': node.view_type or 'form',
564             'view_mode': node.view_mode or 'tree,form',
565             'usage': node.usage,
566             'limit': node.limit,
567             'auto_refresh': node.auto_refresh,
568         }
569
570         self._set_group_values(node, values)
571
572         if node.target:
573             values['target'] = node.target
574         id = self.pool.get('ir.model.data')._update(self.cr, self.uid, \
575                 'ir.actions.act_window', self.module, values, node.id, mode=self.mode)
576         self.id_map[node.id] = int(id)
577
578         if node.src_model:
579             keyword = 'client_action_relate'
580             value = 'ir.actions.act_window,%s' % id
581             replace = node.replace or True
582             self.pool.get('ir.model.data').ir_set(self.cr, self.uid, 'action', keyword, \
583                     node.id, [node.src_model], value, replace=replace, noupdate=self.isnoupdate(node), isobject=True, xml_id=node.id)
584         # TODO add remove ir.model.data
585
586     def process_delete(self, node):
587         assert getattr(node, 'model'), "Attribute %s of delete tag is empty !" % ('model',)
588         if self.pool.get(node.model):
589             if len(node.search):
590                 ids = self.pool.get(node.model).search(self.cr, self.uid, eval(node.search, self.eval_context))
591             else:
592                 ids = [self.get_id(node.id)]
593             if len(ids):
594                 self.pool.get(node.model).unlink(self.cr, self.uid, ids)
595                 self.pool.get('ir.model.data')._unlink(self.cr, self.uid, node.model, ids)
596         else:
597             self.logger.log(logging.TEST, "Record not deleted.")
598     
599     def process_url(self, node):
600         self.validate_xml_id(node.id)
601
602         res = {'name': node.name, 'url': node.url, 'target': node.target}
603
604         id = self.pool.get('ir.model.data')._update(self.cr, self.uid, \
605                 "ir.actions.url", self.module, res, node.id, mode=self.mode)
606         self.id_map[node.id] = int(id)
607         # ir_set
608         if (not node.menu or eval(node.menu)) and id:
609             keyword = node.keyword or 'client_action_multi'
610             value = 'ir.actions.url,%s' % id
611             replace = node.replace or True
612             self.pool.get('ir.model.data').ir_set(self.cr, self.uid, 'action', \
613                     keyword, node.url, ["ir.actions.url"], value, replace=replace, \
614                     noupdate=self.isnoupdate(node), isobject=True, xml_id=node.id)
615     
616     def process_ir_set(self, node):
617         if not self.mode == 'init':
618             return False
619         _, fields = node.items()[0]
620         res = {}
621         for fieldname, expression in fields.items():
622             if is_eval(expression):
623                 value = eval(expression.expression, self.eval_context)
624             else:
625                 value = expression
626             res[fieldname] = value
627         self.pool.get('ir.model.data').ir_set(self.cr, self.uid, res['key'], res['key2'], \
628                 res['name'], res['models'], res['value'], replace=res.get('replace',True), \
629                 isobject=res.get('isobject', False), meta=res.get('meta',None))
630
631     def process_report(self, node):
632         values = {}
633         for dest, f in (('name','string'), ('model','model'), ('report_name','name')):
634             values[dest] = getattr(node, f)
635             assert values[dest], "Attribute %s of report is empty !" % (f,)
636         for field,dest in (('rml','report_rml'),('xml','report_xml'),('xsl','report_xsl'),('attachment','attachment'),('attachment_use','attachment_use')):
637             if getattr(node, field):
638                 values[dest] = getattr(node, field)
639         if node.auto:
640             values['auto'] = eval(node.auto)
641         if node.sxw:
642             sxw_content = misc.file_open(node.sxw).read()
643             values['report_sxw_content'] = sxw_content
644         if node.header:
645             values['header'] = eval(node.header)
646         values['multi'] = node.multi and eval(node.multi)
647         xml_id = node.id
648         self.validate_xml_id(xml_id)
649
650         self._set_group_values(node, values)
651
652         id = self.pool.get('ir.model.data')._update(self.cr, self.uid, "ir.actions.report.xml", \
653                 self.module, values, xml_id, noupdate=self.isnoupdate(node), mode=self.mode)
654         self.id_map[xml_id] = int(id)
655
656         if not node.menu or eval(node.menu):
657             keyword = node.keyword or 'client_print_multi'
658             value = 'ir.actions.report.xml,%s' % id
659             replace = node.replace or True
660             self.pool.get('ir.model.data').ir_set(self.cr, self.uid, 'action', \
661                     keyword, values['name'], [values['model']], value, replace=replace, isobject=True, xml_id=xml_id)
662             
663     def process_none(self):
664         """
665         Empty node or commented node should not pass silently.
666         """
667         self._log_assert_failure(logging.WARNING, "You have an empty block in your tests.")
668         
669
670     def process(self, yaml_string):
671         """
672         Processes a Yaml string. Custom tags are interpreted by 'process_' instance methods.
673         """
674         yaml_tag.add_constructors()
675
676         is_preceded_by_comment = False
677         for node in yaml.load(yaml_string):
678             is_preceded_by_comment = self._log(node, is_preceded_by_comment)
679             try:
680                 self._process_node(node)
681             except YamlImportException, e:
682                 self.logger.exception(e)
683             except Exception, e:
684                 self.logger.exception(e)
685                 raise e
686     
687     def _process_node(self, node):
688         if is_comment(node):
689             self.process_comment(node)
690         elif is_assert(node):
691             self.process_assert(node)
692         elif is_record(node):
693             self.process_record(node)
694         elif is_python(node):
695             self.process_python(node)
696         elif is_menuitem(node):
697             self.process_menuitem(node)
698         elif is_delete(node):
699             self.process_delete(node)
700         elif is_url(node):
701             self.process_url(node)
702         elif is_context(node):
703             self.process_context(node)
704         elif is_ir_set(node):
705             self.process_ir_set(node)
706         elif is_act_window(node):
707             self.process_act_window(node)
708         elif is_report(node):
709             self.process_report(node)
710         elif is_workflow(node):
711             if isinstance(node, types.DictionaryType):
712                 self.process_workflow(node)
713             else:
714                 self.process_workflow({node: []})
715         elif is_function(node):
716             if isinstance(node, types.DictionaryType):
717                 self.process_function(node)
718             else:
719                 self.process_function({node: []})
720         elif node is None:
721             self.process_none()
722         else:
723             raise YamlImportException("Can not process YAML block: %s" % node)
724     
725     def _log(self, node, is_preceded_by_comment):
726         if is_comment(node):
727             is_preceded_by_comment = True
728             self.logger.log(logging.TEST, node)
729         elif not is_preceded_by_comment:
730             if isinstance(node, types.DictionaryType):
731                 msg = "Creating %s\n with %s"
732                 args = node.items()[0]
733                 self.logger.log(logging.TEST, msg, *args)
734             else:
735                 self.logger.log(logging.TEST, node)
736         else:
737             is_preceded_by_comment = False
738         return is_preceded_by_comment
739
740 def yaml_import(cr, module, yamlfile, idref=None, mode='init', noupdate=False, report=None):
741     if idref is None:
742         idref = {}
743     yaml_string = yamlfile.read()
744     yaml_interpreter = YamlInterpreter(cr, module, idref, mode, filename=yamlfile.name, noupdate=noupdate)
745     yaml_interpreter.process(yaml_string)
746
747 # keeps convention of convert.py
748 convert_yaml_import = yaml_import
749
750 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: