import os.path
import pickle
import re
+import sys
# for eval context:
import time
-import openerp.release as release
+
+import openerp
+import openerp.release
+import openerp.workflow
+from yaml_import import convert_yaml_import
+
+import assertion_report
+
+_logger = logging.getLogger(__name__)
+
try:
import pytz
except:
- logging.getLogger("init").warning('could not find pytz library, please install it')
+ _logger.warning('could not find pytz library, please install it')
class pytzclass(object):
all_timezones=[]
pytz=pytzclass()
from datetime import datetime, timedelta
-from lxml import etree
+from dateutil.relativedelta import relativedelta
+from lxml import etree, builder
import misc
-import openerp.loglevels as loglevels
-import openerp.pooler as pooler
from config import config
from translate import _
-from yaml_import import convert_yaml_import
# List of etree._Element subclasses that we choose to ignore when parsing XML.
from misc import SKIPPED_ELEMENT_TYPES
unsafe_eval = eval
from safe_eval import safe_eval as eval
-class ConvertError(Exception):
- def __init__(self, doc, orig_excpt):
- self.d = doc
- self.orig = orig_excpt
+class ParseError(Exception):
+ def __init__(self, msg, text, filename, lineno):
+ self.msg = msg
+ self.text = text
+ self.filename = filename
+ self.lineno = lineno
def __str__(self):
- return 'Exception:\n\t%s\nUsing file:\n%s' % (self.orig, self.d)
+ return '"%s" while parsing %s:%s, near\n%s' \
+ % (self.msg, self.filename, self.lineno, self.text)
def _ref(self, cr):
return lambda x: self.id_get(cr, x)
def _obj(pool, cr, uid, model_str, context=None):
- model = pool.get(model_str)
+ model = pool[model_str]
return lambda x: model.browse(cr, uid, x, context=context)
def _get_idref(self, cr, uid, model_str, context, idref):
idref2 = dict(idref,
time=time,
DateTime=datetime,
+ datetime=datetime,
timedelta=timedelta,
- version=release.major_version,
+ relativedelta=relativedelta,
+ version=openerp.release.major_version,
ref=_ref(self, cr),
pytz=pytz)
if len(model_str):
if f_search:
idref2 = _get_idref(self, cr, uid, f_model, context, idref)
q = unsafe_eval(f_search, idref2)
- ids = pool.get(f_model).search(cr, uid, q)
+ ids = pool[f_model].search(cr, uid, q)
if f_use != 'id':
- ids = map(lambda x: x[f_use], pool.get(f_model).read(cr, uid, ids, [f_use]))
- _cols = pool.get(f_model)._columns
+ ids = map(lambda x: x[f_use], pool[f_model].read(cr, uid, ids, [f_use]))
+ _cols = pool[f_model]._columns
if (f_name in _cols) and _cols[f_name]._type=='many2many':
return ids
f_val = False
f_val = f_val[0]
return f_val
a_eval = node.get('eval','')
- idref2 = {}
if a_eval:
idref2 = _get_idref(self, cr, uid, f_model, context, idref)
try:
return unsafe_eval(a_eval, idref2)
except Exception:
- logger = logging.getLogger('init')
- logger.warning('could not eval(%s) for %s in %s' % (a_eval, node.get('name'), context), exc_info=True)
- return ""
+ logging.getLogger('openerp.tools.convert.init').error(
+ 'Could not eval(%s) for %s in %s', a_eval, node.get('name'), context)
+ raise
+ def _process(s, idref):
+ matches = re.finditer('[^%]%\((.*?)\)[ds]', s)
+ done = []
+ for m in matches:
+ found = m.group()[1:]
+ if found in done:
+ continue
+ done.append(found)
+ id = m.groups()[0]
+ if not id in idref:
+ idref[id] = self.id_get(cr, id)
+ s = s.replace(found, str(idref[id]))
+
+ s = s.replace('%%', '%') # Quite wierd but it's for (somewhat) backward compatibility sake
+
+ return s
+
if t == 'xml':
- def _process(s, idref):
- m = re.findall('[^%]%\((.*?)\)[ds]', s)
- for id in m:
- if not id in idref:
- idref[id]=self.id_get(cr, id)
- return s % idref
_fix_multiple_roots(node)
return '<?xml version="1.0"?>\n'\
+_process("".join([etree.tostring(n, encoding='utf-8')
- for n in node]),
- idref)
- if t in ('char', 'int', 'float'):
- d = node.text
- if t == 'int':
- d = d.strip()
- if d == 'None':
- return None
- else:
- return int(d.strip())
- elif t == 'float':
- return float(d.strip())
- return d
- elif t in ('list','tuple'):
+ for n in node]), idref)
+ if t == 'html':
+ return _process("".join([etree.tostring(n, encoding='utf-8')
+ for n in node]), idref)
+
+ data = node.text
+ if node.get('file'):
+ with openerp.tools.file_open(node.get('file'), 'rb') as f:
+ data = f.read()
+
+ if t == 'file':
+ from ..modules import module
+ path = data.strip()
+ if not module.get_module_resource(self.module, path):
+ raise IOError("No such file or directory: '%s' in %s" % (
+ path, self.module))
+ return '%s,%s' % (self.module, path)
+
+ if t == 'char':
+ return data
+
+ if t == 'base64':
+ return data.encode('base64')
+
+ if t == 'int':
+ d = data.strip()
+ if d == 'None':
+ return None
+ return int(d)
+
+ if t == 'float':
+ return float(data.strip())
+
+ if t in ('list','tuple'):
res=[]
- for n in node.findall('./value'):
+ for n in node.iterchildren(tag='value'):
res.append(_eval_xml(self,n,pool,cr,uid,idref))
if t=='tuple':
return tuple(res)
return_val = _eval_xml(self,n, pool, cr, uid, idref, context)
if return_val is not None:
args.append(return_val)
- model = pool.get(node.get('model',''))
+ model = pool[node.get('model','')]
method = node.get('name','')
res = getattr(model, method)(cr, uid, *args)
return res
def escape(x):
return x.replace('\\/', '/')
-class assertion_report(object):
- def __init__(self):
- self._report = {}
-
- def record_assertion(self, success, severity):
- """
- Records the result of an assertion for the failed/success count
- returns success
- """
- if severity in self._report:
- self._report[severity][success] += 1
- else:
- self._report[severity] = {success:1, not success: 0}
- return success
-
- def get_report(self):
- return self._report
-
- def __str__(self):
- res = '\nAssertions report:\nLevel\tsuccess\tfailed\n'
- success = failed = 0
- for sev in self._report:
- res += sev + '\t' + str(self._report[sev][True]) + '\t' + str(self._report[sev][False]) + '\n'
- success += self._report[sev][True]
- failed += self._report[sev][False]
- res += 'total\t' + str(success) + '\t' + str(failed) + '\n'
- res += 'end of report (' + str(success + failed) + ' assertion(s) checked)'
- return res
-
class xml_import(object):
- __logger = logging.getLogger('tools.convert.xml_import')
@staticmethod
def nodeattr2bool(node, attr, default=False):
if not node.get(attr):
# client-side, so in that case we keep the original context string
# as it is. We also log it, just in case.
context = ctx
- logging.getLogger("init").debug('Context value (%s) for element with id "%s" or its data node does not parse '\
+ _logger.debug('Context value (%s) for element with id "%s" or its data node does not parse '\
'at server-side, keeping original string, in case it\'s meant for client side only',
ctx, node.get('id','n/a'), exc_info=True)
return context
maximum one dot. They are used to refer to other modules ID, in the
form: module.record_id""" % (xml_id,)
if module != self.module:
- modcnt = self.pool.get('ir.module.module').search_count(self.cr, self.uid, ['&', ('name', '=', module), ('state', 'in', ['installed'])])
+ modcnt = self.pool['ir.module.module'].search_count(self.cr, self.uid, ['&', ('name', '=', module), ('state', 'in', ['installed'])])
assert modcnt == 1, """The ID "%s" refers to an uninstalled module""" % (xml_id,)
if len(id) > 64:
- self.logger.error('id: %s is to long (max: 64)', id)
+ _logger.error('id: %s is to long (max: 64)', id)
def _tag_delete(self, cr, rec, data_node=None):
d_model = rec.get("model",'')
if d_search:
idref = _get_idref(self, cr, self.uid, d_model, context={}, idref={})
- ids = self.pool.get(d_model).search(cr, self.uid, unsafe_eval(d_search, idref))
+ ids = self.pool[d_model].search(cr, self.uid, unsafe_eval(d_search, idref))
if d_id:
try:
ids.append(self.id_get(cr, d_id))
# d_id cannot be found. doesn't matter in this case
pass
if ids:
- self.pool.get(d_model).unlink(cr, self.uid, ids)
+ self.pool[d_model].unlink(cr, self.uid, ids)
def _remove_ir_values(self, cr, name, value, model):
- ir_values_obj = self.pool.get('ir.values')
+ ir_values_obj = self.pool['ir.values']
ir_value_ids = ir_values_obj.search(cr, self.uid, [('name','=',name),('value','=',value),('model','=',model)])
if ir_value_ids:
ir_values_obj.unlink(cr, self.uid, ir_value_ids)
for dest,f in (('name','string'),('model','model'),('report_name','name')):
res[dest] = rec.get(f,'').encode('utf8')
assert res[dest], "Attribute %s of report is empty !" % (f,)
- for field,dest in (('rml','report_rml'),('file','report_rml'),('xml','report_xml'),('xsl','report_xsl'),('attachment','attachment'),('attachment_use','attachment_use')):
+ for field,dest in (('rml','report_rml'),('file','report_rml'),('xml','report_xml'),('xsl','report_xsl'),
+ ('attachment','attachment'),('attachment_use','attachment_use'), ('usage','usage'),
+ ('report_type', 'report_type'), ('parser', 'parser')):
if rec.get(field):
res[dest] = rec.get(field).encode('utf8')
if rec.get('auto'):
res['report_sxw_content'] = sxw_content
if rec.get('header'):
res['header'] = eval(rec.get('header','False'))
- if rec.get('report_type'):
- res['report_type'] = rec.get('report_type')
res['multi'] = rec.get('multi') and eval(rec.get('multi','False'))
groups_value.append((4, group_id))
res['groups_id'] = groups_value
- id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
+ id = self.pool['ir.model.data']._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
self.idref[xml_id] = int(id)
if not rec.get('menu') or eval(rec.get('menu','False')):
keyword = str(rec.get('keyword', 'client_print_multi'))
value = 'ir.actions.report.xml,'+str(id)
replace = rec.get('replace', True)
- 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)
+ self.pool['ir.model.data'].ir_set(cr, self.uid, 'action', keyword, res['name'], [res['model']], value, replace=replace, isobject=True, xml_id=xml_id)
elif self.mode=='update' and eval(rec.get('menu','False'))==False:
# Special check for report having attribute menu=False on update
value = 'ir.actions.report.xml,'+str(id)
self._remove_ir_values(cr, res['name'], value, res['model'])
- return False
+ return id
def _tag_function(self, cr, rec, data_node=None):
if self.isnoupdate(data_node) and self.mode != 'init':
groups_value.append((4, group_id))
res['groups_id'] = groups_value
- id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
+ id = self.pool['ir.model.data']._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
self.idref[xml_id] = int(id)
# ir_set
if (not rec.get('menu') or eval(rec.get('menu','False'))) and id:
keyword = str(rec.get('keyword','') or 'client_action_multi')
value = 'ir.actions.wizard,'+str(id)
replace = rec.get("replace",'') or True
- self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, string, [model], value, replace=replace, isobject=True, xml_id=xml_id)
+ self.pool['ir.model.data'].ir_set(cr, self.uid, 'action', keyword, string, [model], value, replace=replace, isobject=True, xml_id=xml_id)
elif self.mode=='update' and (rec.get('menu') and eval(rec.get('menu','False'))==False):
# Special check for wizard having attribute menu=False on update
value = 'ir.actions.wizard,'+str(id)
res = {'name': name, 'url': url, 'target':target}
- id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.url", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
+ id = self.pool['ir.model.data']._update(cr, self.uid, "ir.actions.act_url", self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
self.idref[xml_id] = int(id)
- # ir_set
- if (not rec.get('menu') or eval(rec.get('menu','False'))) and id:
- keyword = str(rec.get('keyword','') or 'client_action_multi')
- value = 'ir.actions.url,'+str(id)
- replace = rec.get("replace",'') or True
- self.pool.get('ir.model.data').ir_set(cr, self.uid, 'action', keyword, url, ["ir.actions.url"], value, replace=replace, isobject=True, xml_id=xml_id)
- elif self.mode=='update' and (rec.get('menu') and eval(rec.get('menu','False'))==False):
- # Special check for URL having attribute menu=False on update
- value = 'ir.actions.url,'+str(id)
- self._remove_ir_values(cr, url, value, "ir.actions.url")
def _tag_act_window(self, cr, rec, data_node=None):
name = rec.get('name','').encode('utf-8')
# Some domains contain references that are only valid at runtime at
# client-side, so in that case we keep the original domain string
# as it is. We also log it, just in case.
- logging.getLogger("init").debug('Domain value (%s) for element with id "%s" does not parse '\
- 'at server-side, keeping original string, in case it\'s meant for client side only',
- domain, xml_id or 'n/a', exc_info=True)
+ _logger.debug('Domain value (%s) for element with id "%s" does not parse '\
+ 'at server-side, keeping original string, in case it\'s meant for client side only',
+ domain, xml_id or 'n/a', exc_info=True)
res = {
'name': name,
'type': type,
if rec.get('target'):
res['target'] = rec.get('target','')
if rec.get('multi'):
- res['multi'] = rec.get('multi', False)
- id = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
+ res['multi'] = eval(rec.get('multi', 'False'))
+ id = self.pool['ir.model.data']._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode)
self.idref[xml_id] = int(id)
if src_model:
keyword = rec.get('key2','').encode('utf-8') or 'client_action_relate'
value = 'ir.actions.act_window,'+str(id)
replace = rec.get('replace','') or True
- 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)
+ self.pool['ir.model.data'].ir_set(cr, self.uid, 'action', keyword, xml_id, [src_model], value, replace=replace, isobject=True, xml_id=xml_id)
# TODO add remove ir.model.data
def _tag_ir_set(self, cr, rec, data_node=None):
f_name = field.get("name",'').encode('utf-8')
f_val = _eval_xml(self,field,self.pool, cr, self.uid, self.idref)
res[f_name] = f_val
- 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))
+ self.pool['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))
def _tag_workflow(self, cr, rec, data_node=None):
if self.isnoupdate(data_node) and self.mode != 'init':
id = _eval_xml(self, rec[0], self.pool, cr, self.uid, self.idref)
uid = self.get_uid(cr, self.uid, data_node, rec)
- import openerp.netsvc as netsvc
- wf_service = netsvc.LocalService("workflow")
- wf_service.trg_validate(uid, model,
+ openerp.workflow.trg_validate(uid, model,
id,
str(rec.get('action','')), cr)
pid = res[0]
else:
# the menuitem does't exist but we are in branch (not a leaf)
- self.logger.warning('Warning no ID for submenu %s of menu %s !', menu_elem, str(m_l))
- pid = self.pool.get('ir.ui.menu').create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
+ _logger.warning('Warning no ID for submenu %s of menu %s !', menu_elem, str(m_l))
+ pid = self.pool['ir.ui.menu'].create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
values['parent_id'] = pid
else:
# The parent attribute was specified, if non-empty determine its ID, otherwise
if rec.get('action'):
a_action = rec.get('action','').encode('utf8')
- a_type = rec.get('type','').encode('utf8') or 'act_window'
+
+ # determine the type of action
+ a_type, a_id = self.model_id_get(cr, a_action)
+ a_type = a_type.split('.')[-1] # keep only type part
+
icons = {
"act_window": 'STOCK_NEW',
"report.xml": 'STOCK_PASTE',
"wizard": 'STOCK_EXECUTE',
- "url": 'STOCK_JUMP_TO'
+ "url": 'STOCK_JUMP_TO',
+ "client": 'STOCK_EXECUTE',
+ "server": 'STOCK_EXECUTE',
}
values['icon'] = icons.get(a_type,'STOCK_NEW')
+
if a_type=='act_window':
- a_id = self.id_get(cr, a_action)
cr.execute('select view_type,view_mode,name,view_id,target from ir_act_window where id=%s', (int(a_id),))
rrres = cr.fetchone()
assert rrres, "No window action defined for this id %s !\n" \
"Verify that this is a window action or add a type argument." % (a_action,)
action_type,action_mode,action_name,view_id,target = rrres
if view_id:
- cr.execute('SELECT type FROM ir_ui_view WHERE id=%s', (int(view_id),))
- action_mode, = cr.fetchone()
+ view_arch = self.pool['ir.ui.view'].read(cr, 1, [view_id], ['arch'])
+ action_mode = etree.fromstring(view_arch[0]['arch'].encode('utf8')).tag
cr.execute('SELECT view_mode FROM ir_act_window_view WHERE act_window_id=%s ORDER BY sequence LIMIT 1', (int(a_id),))
if cr.rowcount:
action_mode, = cr.fetchone()
if action_type=='tree':
values['icon'] = 'STOCK_INDENT'
- elif action_mode and action_mode.startswith('tree'):
+ elif action_mode and action_mode.startswith(('tree','kanban','gantt')):
values['icon'] = 'STOCK_JUSTIFY_FILL'
elif action_mode and action_mode.startswith('graph'):
values['icon'] = 'terp-graph'
values['icon'] = 'STOCK_EXECUTE'
if not values.get('name', False):
values['name'] = action_name
- elif a_type=='wizard':
- a_id = self.id_get(cr, a_action)
- cr.execute('select name from ir_act_wizard where id=%s', (int(a_id),))
- resw = cr.fetchone()
- if (not values.get('name', False)) and resw:
- values['name'] = resw[0]
- elif a_type=='url':
- a_id = self.id_get(cr, a_action)
- cr.execute('select name from ir_act_url where id=%s', (int(a_id),))
+
+ elif a_type in ['wizard', 'url', 'client', 'server'] and not values.get('name'):
+ a_table = 'ir_act_%s' % a_type
+ cr.execute('select name from %s where id=%%s' % a_table, (int(a_id),))
resw = cr.fetchone()
- if (not values.get('name')) and resw:
+ if resw:
values['name'] = resw[0]
+
+ if not values.get('name'):
+ # ensure menu has a name
+ values['name'] = rec_id or '?'
+
if rec.get('sequence'):
values['sequence'] = int(rec.get('sequence'))
if rec.get('icon'):
groups_value.append((4, group_id))
values['groups_id'] = groups_value
- xml_id = rec.get('id','').encode('utf8')
- self._test_xml_id(xml_id)
- pid = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.ui.menu', self.module, values, xml_id, noupdate=self.isnoupdate(data_node), mode=self.mode, res_id=res and res[0] or False)
+ pid = self.pool['ir.model.data']._update(cr, self.uid, 'ir.ui.menu', self.module, values, rec_id, noupdate=self.isnoupdate(data_node), mode=self.mode, res_id=res and res[0] or False)
if rec_id and pid:
self.idref[rec_id] = int(pid)
if rec.get('action') and pid:
- a_action = rec.get('action').encode('utf8')
- a_type = rec.get('type','').encode('utf8') or 'act_window'
- a_id = self.id_get(cr, a_action)
action = "ir.actions.%s,%d" % (a_type, a_id)
- 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)
- return ('ir.ui.menu', pid)
+ self.pool['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)
+ return 'ir.ui.menu', pid
def _assert_equals(self, f1, f2, prec=4):
return not round(f1 - f2, prec)
return
rec_model = rec.get("model",'').encode('ascii')
- model = self.pool.get(rec_model)
- assert model, "The model %s does not exist !" % (rec_model,)
+ model = self.pool[rec_model]
rec_id = rec.get("id",'').encode('ascii')
self._test_xml_id(rec_id)
rec_src = rec.get("search",'').encode('utf8')
rec_src_count = rec.get("count")
- severity = rec.get("severity",'').encode('ascii') or loglevels.LOG_ERROR
rec_string = rec.get("string",'').encode('utf8') or 'unknown'
ids = None
ids = [self.id_get(cr, rec_id)]
elif rec_src:
q = unsafe_eval(rec_src, eval_dict)
- ids = self.pool.get(rec_model).search(cr, uid, q, context=context)
+ ids = self.pool[rec_model].search(cr, uid, q, context=context)
if rec_src_count:
count = int(rec_src_count)
if len(ids) != count:
- self.assert_report.record_assertion(False, severity)
+ self.assertion_report.record_failure()
msg = 'assertion "%s" failed!\n' \
' Incorrect search count:\n' \
' expected count: %d\n' \
' obtained count: %d\n' \
% (rec_string, count, len(ids))
- sevval = getattr(logging, severity.upper())
- self.logger.log(sevval, msg)
- if sevval >= config['assert_exit_level']:
- # TODO: define a dedicated exception
- raise Exception('Severe assertion failure')
+ _logger.error(msg)
return
assert ids is not None,\
expected_value = _eval_xml(self, test, self.pool, cr, uid, self.idref, context=context) or True
expression_value = unsafe_eval(f_expr, globals_dict)
if expression_value != expected_value: # assertion failed
- self.assert_report.record_assertion(False, severity)
+ self.assertion_report.record_failure()
msg = 'assertion "%s" failed!\n' \
' xmltag: %s\n' \
' expected value: %r\n' \
' obtained value: %r\n' \
% (rec_string, etree.tostring(test), expected_value, expression_value)
- sevval = getattr(logging, severity.upper())
- self.logger.log(sevval, msg)
- if sevval >= config['assert_exit_level']:
- # TODO: define a dedicated exception
- raise Exception('Severe assertion failure')
+ _logger.error(msg)
return
else: # all tests were successful for this assertion tag (no break)
- self.assert_report.record_assertion(True, severity)
+ self.assertion_report.record_success()
def _tag_record(self, cr, rec, data_node=None):
rec_model = rec.get("model").encode('ascii')
- model = self.pool.get(rec_model)
- assert model, "The model %s does not exist !" % (rec_model,)
+ model = self.pool[rec_model]
rec_id = rec.get("id",'').encode('ascii')
rec_context = rec.get("context", None)
if rec_context:
else:
module = self.module
rec_id2 = rec_id
- id = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, rec_model, module, rec_id2)
+ id = self.pool['ir.model.data']._update_dummy(cr, self.uid, rec_model, module, rec_id2)
# check if the resource already existed at the last update
if id:
# if it existed, we don't update the data, but we need to
q = unsafe_eval(f_search, self.idref)
field = []
assert f_model, 'Define an attribute model="..." in your .XML file !'
- f_obj = self.pool.get(f_model)
+ f_obj = self.pool[f_model]
# browse the objects searched
s = f_obj.browse(cr, self.uid, f_obj.search(cr, self.uid, q))
# column definitions of the "local" object
- _cols = self.pool.get(rec_model)._columns
+ _cols = self.pool[rec_model]._columns
# if the current field is many2many
if (f_name in _cols) and _cols[f_name]._type=='many2many':
f_val = [(6, 0, map(lambda x: x[f_use], s))]
f_val = int(f_val)
res[f_name] = f_val
- 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, context=rec_context )
+ id = self.pool['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, context=rec_context )
if rec_id:
self.idref[rec_id] = int(id)
if config.get('import_partial', False):
cr.commit()
return rec_model, id
+ def _tag_template(self, cr, el, data_node=None):
+ # This helper transforms a <template> element into a <record> and forwards it
+ tpl_id = el.get('id', el.get('t-name', '')).encode('ascii')
+ module = self.module
+ if '.' in tpl_id:
+ module, tpl_id = tpl_id.split('.', 1)
+ # set the full template name for qweb <module>.<id>
+ if not (el.get('inherit_id') or el.get('inherit_option_id')):
+ el.set('t-name', '%s.%s' % (module, tpl_id))
+ el.tag = 't'
+ else:
+ el.tag = 'data'
+ el.attrib.pop('id', None)
+
+ record_attrs = {
+ 'id': tpl_id,
+ 'model': 'ir.ui.view',
+ }
+ for att in ['forcecreate', 'context', 'priority']:
+ if att in el.keys():
+ record_attrs[att] = el.attrib.pop(att)
+
+ Field = builder.E.field
+ name = el.get('name', tpl_id)
+
+ record = etree.Element('record', attrib=record_attrs)
+ record.append(Field(name, name='name'))
+ record.append(Field("qweb", name='type'))
+ record.append(Field(el, name="arch", type="xml"))
+ for field_name in ('inherit_id','inherit_option_id'):
+ value = el.attrib.pop(field_name, None)
+ if value: record.append(Field(name=field_name, ref=value))
+ if el.attrib.pop('page', None) == 'True':
+ record.append(Field(name="page", eval="True"))
+
+ return self._tag_record(cr, record, data_node)
+
def id_get(self, cr, id_str):
if id_str in self.idref:
return self.idref[id_str]
return res
def model_id_get(self, cr, id_str):
- model_data_obj = self.pool.get('ir.model.data')
+ model_data_obj = self.pool['ir.model.data']
mod = self.module
if '.' in id_str:
mod,id_str = id_str.split('.')
return model_data_obj.get_object_reference(cr, self.uid, mod, id_str)
def parse(self, de):
- if not de.tag in ['terp', 'openerp']:
- self.logger.error("Mismatch xml format")
- raise Exception( "Mismatch xml format: only terp or openerp as root tag" )
-
- if de.tag == 'terp':
- self.logger.warning("The tag <terp/> is deprecated, use <openerp/>")
+ if de.tag != 'openerp':
+ raise Exception("Mismatch xml format: root tag must be `openerp`.")
for n in de.findall('./data'):
for rec in n:
- if rec.tag in self._tags:
- try:
- self._tags[rec.tag](self.cr, rec, n)
- except:
- self.__logger.error('Parse error in %s:%d: \n%s',
- rec.getroottree().docinfo.URL,
- rec.sourceline,
- etree.tostring(rec).strip(), exc_info=True)
- self.cr.rollback()
- raise
+ if rec.tag in self._tags:
+ try:
+ self._tags[rec.tag](self.cr, rec, n)
+ except Exception, e:
+ self.cr.rollback()
+ exc_info = sys.exc_info()
+ raise ParseError, (misc.ustr(e), etree.tostring(rec).rstrip(), rec.getroottree().docinfo.URL, rec.sourceline), exc_info[2]
return True
def __init__(self, cr, module, idref, mode, report=None, noupdate=False):
- self.logger = logging.getLogger('init')
self.mode = mode
self.module = module
self.cr = cr
self.idref = idref
- self.pool = pooler.get_pool(cr.dbname)
+ self.pool = openerp.registry(cr.dbname)
self.uid = 1
if report is None:
- report = assertion_report()
- self.assert_report = report
+ report = assertion_report.assertion_report()
+ self.assertion_report = report
self.noupdate = noupdate
self._tags = {
'menuitem': self._tag_menuitem,
'record': self._tag_record,
+ 'template': self._tag_template,
'assert': self._tag_assert,
'report': self._tag_report,
'wizard': self._tag_wizard,
'url': self._tag_url
}
+def convert_file(cr, module, filename, idref, mode='update', noupdate=False, kind=None, report=None):
+ pathname = os.path.join(module, filename)
+ fp = misc.file_open(pathname)
+ ext = os.path.splitext(filename)[1].lower()
+ try:
+ if ext == '.csv':
+ convert_csv_import(cr, module, pathname, fp.read(), idref, mode, noupdate)
+ elif ext == '.sql':
+ convert_sql_import(cr, fp)
+ elif ext == '.yml':
+ convert_yaml_import(cr, module, fp, kind, idref, mode, noupdate, report)
+ elif ext == '.xml':
+ convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
+ elif ext == '.js':
+ pass # .js files are valid but ignored here.
+ else:
+ _logger.warning("Can't load unknown file type %s.", filename)
+ finally:
+ fp.close()
+
+def convert_sql_import(cr, fp):
+ queries = fp.read().split(';')
+ for query in queries:
+ new_query = ' '.join(query.split())
+ if new_query:
+ cr.execute(new_query)
+
def convert_csv_import(cr, module, fname, csvcontent, idref=None, mode='init',
noupdate=False):
'''Import csv file :
encoding: utf-8'''
if not idref:
idref={}
- logger = logging.getLogger('init')
model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
#remove folder path from model
head, model = os.path.split(model)
- pool = pooler.get_pool(cr.dbname)
-
input = cStringIO.StringIO(csvcontent) #FIXME
reader = csv.reader(input, quotechar='"', delimiter=',')
fields = reader.next()
reader.next()
if not (mode == 'init' or 'id' in fields):
- logger.error("Import specification does not contain 'id' and we are in init mode, Cannot continue.")
+ _logger.error("Import specification does not contain 'id' and we are in init mode, Cannot continue.")
return
uid = 1
datas = []
for line in reader:
- if (not line) or not reduce(lambda x,y: x or y, line) :
+ if not (line and any(line)):
continue
try:
- datas.append(map(lambda x: misc.ustr(x), line))
+ datas.append(map(misc.ustr, line))
except:
- logger.error("Cannot import the line: %s", line)
- result, rows, warning_msg, dummy = pool.get(model).import_data(cr, uid, fields, datas,mode, module, noupdate, filename=fname_partial)
+ _logger.error("Cannot import the line: %s", line)
+
+ registry = openerp.registry(cr.dbname)
+ result, rows, warning_msg, dummy = registry[model].import_data(cr, uid, fields, datas,mode, module, noupdate, filename=fname_partial)
if result < 0:
# Report failed import and abort module install
raise Exception(_('Module loading failed: file %s/%s could not be processed:\n %s') % (module, fname, warning_msg))
try:
relaxng.assert_(doc)
except Exception:
- logger = loglevels.Logger()
- logger.notifyChannel('init', loglevels.LOG_ERROR, 'The XML file does not fit the required schema !')
- logger.notifyChannel('init', loglevels.LOG_ERROR, misc.ustr(relaxng.error_log.last_error))
+ _logger.error('The XML file does not fit the required schema !')
+ _logger.error(misc.ustr(relaxng.error_log.last_error))
raise
if idref is None:
obj.parse(doc.getroot())
return True
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: