#---------------------------------------------------------- # Convert #---------------------------------------------------------- import re import StringIO,xml.dom.minidom import osv,ir,pooler import csv import os.path from config import config # Number of imported lines between two commit (see convert_csv_import()): COMMIT_STEP = 500 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): 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' 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: f_use = "id" q = eval(f_search, idref) ids = pool.get(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 if (f_name in _cols) and _cols[f_name]._type=='many2many': return ids f_val = False if len(ids): f_val = ids[0] if isinstance(f_val, tuple): f_val = f_val[0] return f_val a_eval = node.getAttribute('eval') if len(a_eval): import time idref['time'] = time idref['ref'] = lambda x: self.id_get(cr, False, x) try: import pytz except: import netsvc logger = netsvc.Logger() logger.notifyChannel("init", netsvc.LOG_INFO, 'could not find pytz library') class pytzclass(object): all_timezones=[] pytz=pytzclass() idref['pytz'] = pytz return eval(a_eval, idref) 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, False, id) return s % idref txt = '\n'+_process("".join([i.toxml().encode("utf8") for i in node.childNodes]), idref) # txt = '\n'+"".join([i.toxml().encode("utf8") for i in node.childNodes]) % idref return txt if t in ('char', 'int', 'float'): d = "" for n in [i for i in node.childNodes]: d+=str(_eval_xml(self,n,pool,cr,uid,idref)) if t == 'int': d = d.strip() if d=='None': return None else: d=int(d.strip()) elif t=='float': d=float(d.strip()) return d elif t in ('list','tuple'): res=[] for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE and i.nodeName=='value')]: res.append(_eval_xml(self,n,pool,cr,uid,idref)) if t=='tuple': return tuple(res) return res elif node.nodeName=="getitem": for n in [i for i in node.childNodes if (i.nodeType == i.ELEMENT_NODE)]: res=_eval_xml(self,n,pool,cr,uid,idref) if not res: raise LookupError elif node.getAttribute('type') in ("int", "list"): return res[int(node.getAttribute('index'))] else: return res[node.getAttribute('index').encode("utf8")] elif node.nodeName=="function": args = [] a_eval = node.getAttribute('eval') if len(a_eval): 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)) model = pool.get(node.getAttribute('model')) method = node.getAttribute('name') res = getattr(model, method)(cr, uid, *args) return res escape_re = re.compile(r'(? COMMIT_STEP: pool.get(model).import_data(cr, uid, fields, datas) cr.commit() datas=[] if datas: pool.get(model).import_data(cr, uid, fields, datas) cr.commit() # # xml import/export # def convert_xml_import(cr, module, xmlstr, idref={}, mode='init'): obj = xml_import(cr, module, idref, mode) obj.parse(xmlstr) del obj return True def convert_xml_export(res): uid=1 pool=pooler.get_pool(cr.dbname) cr=pooler.db.cursor() idref = {} d = xml.dom.minidom.getDOMImplementation().createDocument(None, "terp", None) de = d.documentElement data=d.createElement("data") de.appendChild(data) de.appendChild(d.createTextNode('Some textual content.')) cr.commit() cr.close()