#----------------------------------------------------------
-# Convert
+# Convert
#----------------------------------------------------------
import re
import StringIO,xml.dom.minidom
import csv
import os.path
import misc
+import netsvc
from config import config
-
-
-# Number of imported lines between two commit (see convert_csv_import()):
-COMMIT_STEP = 500
+import logging
class ConvertError(Exception):
def __init__(self, doc, orig_excpt):
self.d = doc
self.orig = orig_excpt
-
+
def __str__(self):
return 'Exception:\n\t%s\nUsing file:\n%s' % (self.orig, self.d)
-def _eval_xml(self,node, pool, cr, uid, idref):
+def _ref(self, cr):
+ return lambda x: self.id_get(cr, False, x)
+
+def _obj(pool, cr, uid, model_str, context=None):
+ model = pool.get(model_str)
+ return lambda x: model.browse(cr, uid, x, context=context)
+
+def _eval_xml(self,node, pool, cr, uid, idref, context=None):
+ if context is None:
+ context = {}
if node.nodeType == node.TEXT_NODE:
return node.data.encode("utf8")
elif node.nodeType == node.ELEMENT_NODE:
if node.nodeName in ('field','value'):
t = node.getAttribute('type') or 'char'
+ f_model = node.getAttribute("model").encode('ascii')
if len(node.getAttribute('search')):
f_search = node.getAttribute("search").encode('utf-8')
- f_model = node.getAttribute("model").encode('ascii')
f_use = node.getAttribute("use").encode('ascii')
f_name = node.getAttribute("name").encode('utf-8')
if len(f_use)==0:
if len(a_eval):
import time
idref['time'] = time
+ import release
+ idref['version'] = release.version.rsplit('.', 1)[0]
idref['ref'] = lambda x: self.id_get(cr, False, x)
+ if len(f_model):
+ idref['obj'] = _obj(self.pool, cr, uid, f_model, context=context)
try:
import pytz
except:
- import netsvc
logger = netsvc.Logger()
logger.notifyChannel("init", netsvc.LOG_INFO, 'could not find pytz library')
class pytzclass(object):
idref['ref'] = lambda x: self.id_get(cr, False, x)
args = eval(a_eval, idref)
for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]:
- args.append(_eval_xml(self,n, pool, cr, uid, idref))
+ args.append(_eval_xml(self,n, pool, cr, uid, idref, context))
model = pool.get(node.getAttribute('model'))
method = node.getAttribute('name')
res = getattr(model, method)(cr, uid, *args)
return res
+ elif node.nodeName=="test":
+ d = ""
+ for n in [i for i in node.childNodes]:
+ d+=str(_eval_xml(self,n,pool,cr,uid,idref, context=context))
+ return d
+
escape_re = re.compile(r'(?<!\\)/')
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
+ retrurns 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):
+
+ def isnoupdate(self, data_node = None):
+ return self.noupdate or (data_node and data_node.getAttribute('noupdate'))
+
+ def get_context(self, data_node, node, eval_dict):
+ data_node_context = (data_node and data_node.getAttribute('context').encode('utf8'))
+ if data_node_context:
+ context = eval(data_node_context, eval_dict)
+ else:
+ context = {}
+
+ node_context = node.getAttribute("context").encode('utf8')
+ if len(node_context):
+ context.update(eval(node_context, eval_dict))
+
+ return context
+
+ def get_uid(self, cr, uid, data_node, node):
+ node_uid = node.getAttribute('uid') or (data_node and data_node.getAttribute('uid'))
+ if len(node_uid):
+ return self.id_get(cr, None, node_uid)
+ return uid
+
+ def _test_xml_id(self, xml_id):
+ id = xml_id
+ if '.' in xml_id:
+ base, id = xml_id.split('.')
+ if len(id) > 64:
+ self.logger.notifyChannel('init', netsvc.LOG_ERROR, 'id: %s is to long (max: 64)'%xml_id)
def _tag_delete(self, cr, rec, data_node=None):
d_model = rec.getAttribute("model")
d_search = rec.getAttribute("search")
- ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
+ d_id = rec.getAttribute("id")
+ ids = []
+ if len(d_search):
+ ids = self.pool.get(d_model).search(cr,self.uid,eval(d_search))
+ if len(d_id):
+ ids.append(self.id_get(cr, d_model, d_id))
if len(ids):
self.pool.get(d_model).unlink(cr, self.uid, ids)
#self.pool.get('ir.model.data')._unlink(cr, self.uid, d_model, ids, direct=True)
res['report_sxw_content'] = sxw_content
if rec.hasAttribute('header'):
res['header'] = eval(rec.getAttribute('header'))
+ res['multi'] = rec.hasAttribute('multi') and eval(rec.getAttribute('multi'))
xml_id = rec.getAttribute('id').encode('utf8')
+ self._test_xml_id(xml_id)
id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.report.xml", self.module, res, xml_id, mode=self.mode)
- self.idref[xml_id] = id
+ self.idref[xml_id] = int(id)
if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
keyword = str(rec.getAttribute('keyword') or 'client_print_multi')
keys = [('action',keyword),('res_model',res['model'])]
return False
def _tag_function(self, cr, rec, data_node=None):
- _eval_xml(self,rec, self.pool, cr, self.uid, self.idref)
+ if self.isnoupdate(data_node) and self.mode != 'init':
+ return
+ context = self.get_context(data_node, rec, {'ref': _ref(self, cr)})
+ uid = self.get_uid(cr, self.uid, data_node, rec)
+ _eval_xml(self,rec, self.pool, cr, uid, self.idref, context=context)
return False
def _tag_wizard(self, cr, rec, data_node=None):
model = rec.getAttribute("model").encode('utf8')
name = rec.getAttribute("name").encode('utf8')
xml_id = rec.getAttribute('id').encode('utf8')
+ self._test_xml_id(xml_id)
multi = rec.hasAttribute('multi') and eval(rec.getAttribute('multi'))
res = {'name': string, 'wiz_name': name, 'multi':multi}
id = self.pool.get('ir.model.data')._update(cr, self.uid, "ir.actions.wizard", self.module, res, xml_id, mode=self.mode)
- self.idref[xml_id] = id
+ self.idref[xml_id] = int(id)
# ir_set
- if not rec.hasAttribute('menu') or eval(rec.getAttribute('menu')):
+ if (not rec.hasAttribute('menu') or eval(rec.getAttribute('menu'))) and id:
keyword = str(rec.getAttribute('keyword') or 'client_action_multi')
keys = [('action',keyword),('res_model',model)]
value = 'ir.actions.wizard,'+str(id)
def _tag_act_window(self, cr, rec, data_node=None):
name = rec.hasAttribute('name') and rec.getAttribute('name').encode('utf-8')
xml_id = rec.getAttribute('id').encode('utf8')
+ self._test_xml_id(xml_id)
type = rec.hasAttribute('type') and rec.getAttribute('type').encode('utf-8') or 'ir.actions.act_window'
view_id = False
if rec.hasAttribute('view'):
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 }
id = self.pool.get('ir.model.data')._update(cr, self.uid, 'ir.actions.act_window', self.module, res, xml_id, mode=self.mode)
- self.idref[xml_id] = id
+ self.idref[xml_id] = int(id)
if src_model:
keyword = 'client_action_relate'
value = 'ir.actions.act_window,'+str(id)
replace = rec.hasAttribute('replace') and rec.getAttribute('replace')
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)
+ # TODO add remove ir.model.data
return False
def _tag_ir_set(self, cr, rec, data_node=None):
return False
def _tag_workflow(self, cr, rec, data_node=None):
- import netsvc
+ if self.isnoupdate(data_node) and self.mode != 'init':
+ return
model = str(rec.getAttribute('model'))
+ w_ref = rec.getAttribute('ref')
+ if len(w_ref):
+ id = self.id_get(cr, model, w_ref)
+ else:
+ assert rec.childNodes, 'You must define a child node if you dont give a ref'
+ element_childs = [i for i in rec.childNodes if i.nodeType == i.ELEMENT_NODE]
+ assert len(element_childs) == 1, 'Only one child node is accepted (%d given)' % len(rec.childNodes)
+ id = _eval_xml(self, element_childs[0], self.pool, cr, self.uid, self.idref)
+
+ uid = self.get_uid(cr, self.uid, data_node, rec)
wf_service = netsvc.LocalService("workflow")
- wf_service.trg_validate(self.uid, model,
- self.id_get(cr, model, rec.getAttribute('ref')),
+ wf_service.trg_validate(uid, model,
+ id,
str(rec.getAttribute('action')), cr)
return False
def _tag_menuitem(self, cr, rec, data_node=None):
rec_id = rec.getAttribute("id").encode('ascii')
+ self._test_xml_id(rec_id)
m_l = map(escape, escape_re.split(rec.getAttribute("name").encode('utf8')))
pid = False
for idx, menu_elem in enumerate(m_l):
elif action_mode and action_mode.startswith('tree'):
values['icon'] = 'STOCK_JUSTIFY_FILL'
elif action_mode and action_mode.startswith('graph'):
- values['icon'] = 'terp-account'
+ values['icon'] = 'terp-graph'
+ elif action_mode and action_mode.startswith('calendar'):
+ values['icon'] = 'terp-calendar'
if not values['name']:
values['name'] = action_name
if rec.hasAttribute('sequence'):
values['icon'] = str(rec.getAttribute('icon'))
if rec.hasAttribute('groups'):
g_names = rec.getAttribute('groups').split(',')
- g_ids = []
+ groups_value = []
+ groups_obj = self.pool.get('res.groups')
for group in g_names:
- g_ids.extend(self.pool.get('res.groups').search(cr, self.uid, [('name', '=', group)]))
- values['groups_id'] = [(6, 0, g_ids)]
+ if group.startswith('-'):
+ id = groups_obj.search(cr, self.uid,
+ [('name', '=', group[1:])])[0]
+ groups_value.append((3, id))
+ else:
+ id = groups_obj.search(cr, self.uid,
+ [('name', '=', group)])[0]
+ groups_value.append((4, id))
+ values['groups_id'] = groups_value
xml_id = rec.getAttribute('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, idx==len(m_l)-1, mode=self.mode, res_id=res and res[0] or False)
elif res:
# the menuitem already exists
# the menuitem does't exist but we are in branch (not a leaf)
pid = self.pool.get('ir.ui.menu').create(cr, self.uid, {'parent_id' : pid, 'name' : menu_elem})
if rec_id and pid:
- self.idref[rec_id] = pid
+ self.idref[rec_id] = int(pid)
if rec.hasAttribute('action') and pid:
a_action = rec.getAttribute('action').encode('utf8')
a_type = rec.getAttribute('type').encode('utf8') or 'act_window'
a_id = self.id_get(cr, 'ir.actions.%s' % a_type, 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)
+ 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)
+ def _assert_equals(self, f1, f2, prec = 4):
+ return not round(f1 - f2, prec)
+
+ def _tag_assert(self, cr, rec, data_node=None):
+ if self.isnoupdate(data_node) and self.mode != 'init':
+ return
+
+ rec_model = rec.getAttribute("model").encode('ascii')
+ model = self.pool.get(rec_model)
+ assert model, "The model %s does not exist !" % (rec_model,)
+ rec_id = rec.getAttribute("id").encode('ascii')
+ self._test_xml_id(rec_id)
+ rec_src = rec.getAttribute("search").encode('utf8')
+ rec_src_count = rec.getAttribute("count")
+
+ severity = rec.getAttribute("severity").encode('ascii') or 'info'
+
+ rec_string = rec.getAttribute("string").encode('utf8') or 'unknown'
+
+ ids = None
+ eval_dict = {'ref': _ref(self, cr)}
+ context = self.get_context(data_node, rec, eval_dict)
+ uid = self.get_uid(cr, self.uid, data_node, rec)
+ if len(rec_id):
+ ids = [self.id_get(cr, rec_model, rec_id)]
+ elif len(rec_src):
+ q = eval(rec_src, eval_dict)
+ ids = self.pool.get(rec_model).search(cr, uid, q, context=context)
+ if len(rec_src_count):
+ count = int(rec_src_count)
+ if len(ids) != count:
+ self.assert_report.record_assertion(False, severity)
+ self.logger.notifyChannel('init', severity, 'assertion "' + rec_string + '" failed ! (search count is incorrect: ' + str(len(ids)) + ')' )
+ sevval = getattr(logging, severity.upper())
+ if sevval > config['assert_exit_level']:
+ # TODO: define a dedicated exception
+ raise Exception('Severe assertion failure')
+ return
+
+ assert ids != None, 'You must give either an id or a search criteria'
+
+ ref = _ref(self, cr)
+ for id in ids:
+ brrec = model.browse(cr, uid, id, context)
+ class d(dict):
+ def __getitem__(self2, key):
+ if key in brrec:
+ return brrec[key]
+ return dict.__getitem__(self2, key)
+ globals = d()
+ globals['floatEqual'] = self._assert_equals
+ globals['ref'] = ref
+ globals['_ref'] = ref
+ for test in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="test")]:
+ f_expr = test.getAttribute("expr").encode('utf-8')
+ f_val = _eval_xml(self, test, self.pool, cr, uid, self.idref, context=context) or True
+ if eval(f_expr, globals) != f_val: # assertion failed
+ self.assert_report.record_assertion(False, severity)
+ self.logger.notifyChannel('init', severity, 'assertion "' + rec_string + '" failed ! (tag ' + test.toxml() + ')' )
+ sevval = getattr(logging, severity.upper())
+ if sevval > config['assert_exit_level']:
+ # TODO: define a dedicated exception
+ raise Exception('Severe assertion failure')
+ return
+ else: # all tests were successful for this assertion tag (no break)
+ self.assert_report.record_assertion(True, severity)
+
def _tag_record(self, cr, rec, data_node=None):
rec_model = rec.getAttribute("model").encode('ascii')
model = self.pool.get(rec_model)
assert model, "The model %s does not exist !" % (rec_model,)
rec_id = rec.getAttribute("id").encode('ascii')
+ self._test_xml_id(rec_id)
-# if not rec_id and not data_node.getAttribute('noupdate'):
+# if not rec_id and not self.isnoupdate(data_node):
# print "Warning", rec_model
- if data_node.getAttribute('noupdate') and not self.mode == 'init':
+ if self.isnoupdate(data_node) and not self.mode == 'init':
# check if the xml record has an id string
if rec_id:
id = self.pool.get('ir.model.data')._update_dummy(cr, self.uid, rec_model, self.module, rec_id)
# 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
+ # if it existed, we don't update the data, but we need to
# know the id of the existing record anyway
- self.idref[rec_id] = id
+ self.idref[rec_id] = int(id)
return None
else:
# if the resource didn't exist
else:
# otherwise it is skipped
return None
-
+
res = {}
for field in [i for i in rec.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=="field")]:
#TODO: most of this code is duplicated above (in _eval_xml)...
if isinstance(model._columns[f_name], osv.fields.integer):
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 data_node.getAttribute('noupdate'), noupdate=data_node.getAttribute('noupdate'), mode=self.mode )
+ 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 )
if rec_id:
- self.idref[rec_id] = id
+ self.idref[rec_id] = int(id)
return rec_model, id
def id_get(self, cr, model, id_str):
if '.' in id_str:
mod,id_str = id_str.split('.')
result = self.pool.get('ir.model.data')._get_id(cr, self.uid, mod, id_str)
- return self.pool.get('ir.model.data').read(cr, self.uid, [result], ['res_id'])[0]['res_id']
+ return int(self.pool.get('ir.model.data').read(cr, self.uid, [result], ['res_id'])[0]['res_id'])
def parse(self, xmlstr):
d = xml.dom.minidom.parseString(xmlstr)
try:
self._tags[rec.nodeName](self.cr, rec, n)
except:
- import netsvc
- logger = netsvc.Logger()
- logger.notifyChannel("init", netsvc.LOG_INFO, '\n'+rec.toxml())
+ self.logger.notifyChannel("init", netsvc.LOG_INFO, '\n'+rec.toxml())
self.cr.rollback()
raise
- self.cr.commit()
return True
- def __init__(self, cr, module, idref, mode):
+ def __init__(self, cr, module, idref, mode, report=assertion_report(), noupdate = False):
+ self.logger = netsvc.Logger()
self.mode = mode
self.module = module
self.cr = cr
self.pool = pooler.get_pool(cr.dbname)
# self.pool = osv.osv.FakePool(module)
self.uid = 1
+ self.assert_report = report
+ self.noupdate = noupdate
self._tags = {
'menuitem': self._tag_menuitem,
'record': self._tag_record,
+ 'assert': self._tag_assert,
'report': self._tag_report,
'wizard': self._tag_wizard,
'delete': self._tag_delete,
'act_window': self._tag_act_window,
}
-#
-# Import a CSV file:
-# quote: "
-# delimiter: ,
-# encoding: UTF8
-#
-def convert_csv_import(cr, module, fname, csvcontent, idref={}, mode='init'):
- if mode != 'init':
- return
- model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
- #model = fname.split('.')[0].replace('_', '.')
+CSV_STEP = 500
+def convert_csv_import(cr, module, fname, csvcontent, idref=None, mode='init', noupdate=False):
+ '''Import csv file :
+ quote: "
+ delimiter: ,
+ encoding: utf-8'''
+ if not idref:
+ idref={}
+ model = ('.'.join(fname.split('.')[:-1]).split('-'))[0]
#remove folder path from model
head, model = os.path.split(model)
pool = pooler.get_pool(cr.dbname)
-# pool = osv.osv.FakePool(module)
input=StringIO.StringIO(csvcontent)
reader = csv.reader(input, quotechar='"', delimiter=',')
fields = reader.next()
+ if not (mode == 'init' or 'id' in fields):
+ return
+
uid = 1
datas = []
for line in reader:
datas.append( map(lambda x:x.decode('utf8').encode('utf8'), line))
- if len(datas) > COMMIT_STEP:
- pool.get(model).import_data(cr, uid, fields, datas)
- cr.commit()
+ if len(datas) > CSV_STEP:
+ pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
datas=[]
-
if datas:
- pool.get(model).import_data(cr, uid, fields, datas)
- cr.commit()
+ pool.get(model).import_data(cr, uid, fields, datas,mode, module,noupdate)
#
# xml import/export
#
-def convert_xml_import(cr, module, xmlstr, idref={}, mode='init'):
- obj = xml_import(cr, module, idref, mode)
+def convert_xml_import(cr, module, xmlstr, idref=None, mode='init', noupdate = False, report=None):
+ if not idref:
+ idref={}
+ if report is None:
+ report=assertion_report()
+ obj = xml_import(cr, module, idref, mode, report=report, noupdate = noupdate)
obj.parse(xmlstr)
del obj
return True