示例#1
0
def convert_xml_import(cr,
                       module,
                       xmlfile,
                       idref=None,
                       mode='init',
                       noupdate=False,
                       report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'], 'import_xml.rng')))
    try:
        relaxng.assert_(doc)
    except Exception:
        logger = netsvc.Logger()
        logger.notifyChannel(
            'init', netsvc.LOG_ERROR,
            'The XML file does not fit the required schema !')
        logger.notifyChannel('init', netsvc.LOG_ERROR,
                             misc.ustr(relaxng.error_log.last_error))
        raise

    if idref is None:
        idref = {}
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate)
    obj.parse(doc.getroot())
    return True
示例#2
0
def convert_xml_import(cr,
                       module,
                       xmlfile,
                       idref=None,
                       mode='init',
                       noupdate=False,
                       report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'], 'import_xml.rng')))
    try:
        relaxng.assert_(doc)
    except Exception:
        _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:
        idref = {}
    if isinstance(xmlfile, file):
        xml_filename = xmlfile.name
    else:
        xml_filename = xmlfile
    obj = xml_import(cr,
                     module,
                     idref,
                     mode,
                     report=report,
                     noupdate=noupdate,
                     xml_filename=xml_filename)
    obj.parse(doc.getroot(), mode=mode)
    return True
示例#3
0
def convert_xml_import(cr,
                       module,
                       xmlfile,
                       idref=None,
                       mode='init',
                       noupdate=False,
                       report=None,
                       context=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'], 'import_xml.rng')))
    try:
        relaxng.assert_(doc)
        # TODO: perhaps explicitly catch only RelaxNG exceptions
    except Exception:
        logger = logging.getLogger('init')
        logger.error('The XML file does not fit the required schema !\n%s', \
                misc.ustr(relaxng.error_log.last_error))
        raise

    if idref is None:
        idref = {}
    obj = xml_import(cr,
                     module,
                     idref,
                     mode,
                     report=report,
                     noupdate=noupdate,
                     context=context)
    obj.parse(doc.getroot())
    return True
示例#4
0
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)

    input = cStringIO.StringIO(csvcontent)  # FIXME
    reader = csv.reader(input, quotechar='"', delimiter=",")
    fields = reader.next()
    fname_partial = ""
    if config.get("import_partial"):
        fname_partial = module + "/" + fname
        if not os.path.isfile(config.get("import_partial")):
            pickle.dump({}, file(config.get("import_partial"), "w+"))
        else:
            data = pickle.load(file(config.get("import_partial")))
            if fname_partial in data:
                if not data[fname_partial]:
                    return
                else:
                    for i in range(data[fname_partial]):
                        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.")
        return

    uid = 1
    datas = []
    for line in reader:
        if (not line) or not reduce(lambda x, y: x or y, line):
            continue
        try:
            datas.append(map(lambda x: misc.ustr(x), 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
    )
    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)
        )
    if config.get("import_partial"):
        data = pickle.load(file(config.get("import_partial")))
        data[fname_partial] = 0
        pickle.dump(data, file(config.get("import_partial"), "wb"))
        cr.commit()
示例#5
0
    def process(self, yaml_string, fatal=False, **kwargs):
        """
        Processes a Yaml string. Custom tags are interpreted by `process_` instance methods.
        @param kwargs extra arguments, for future expansions. Ignored now.
        """
        yaml_tag.add_constructors()

        is_preceded_by_comment = False
        for node in yaml.load(yaml_string):
            is_preceded_by_comment = self._log(node, is_preceded_by_comment)
            try:
                self._process_node(node)
            except YamlImportException, e:
                self.logger.exception(misc.ustr(e))
                if fatal:
                    raise
            except Exception, e:
                self.logger.exception(misc.ustr(e))
                raise
示例#6
0
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)

    input = cStringIO.StringIO(csvcontent) #FIXME
    reader = csv.reader(input, quotechar='"', delimiter=',')
    fields = reader.next()
    fname_partial = ""
    if config.get('import_partial'):
        fname_partial = module + '/'+ fname
        if not os.path.isfile(config.get('import_partial')):
            pickle.dump({}, file(config.get('import_partial'),'w+'))
        else:
            data = pickle.load(file(config.get('import_partial')))
            if fname_partial in data:
                if not data[fname_partial]:
                    return
                else:
                    for i in range(data[fname_partial]):
                        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.")
        return

    uid = 1
    datas = []
    for line in reader:
        if (not line) or not reduce(lambda x,y: x or y, line) :
            continue
        try:
            datas.append(map(lambda x: misc.ustr(x), 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)
    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))
    if config.get('import_partial'):
        data = pickle.load(file(config.get('import_partial')))
        data[fname_partial] = 0
        pickle.dump(data, file(config.get('import_partial'),'wb'))
        cr.commit()
示例#7
0
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)

    input = cStringIO.StringIO(csvcontent)
    reader = csv.reader(input, quotechar='"', delimiter=',')
    fields = reader.next()
    fname_partial = ""
    if config.get('import_partial'):
        fname_partial = module + '/'+ fname
        if not os.path.isfile(config.get('import_partial')):
            pickle.dump({}, file(config.get('import_partial'),'w+'))
        else:
            data = pickle.load(file(config.get('import_partial')))
            if fname_partial in data:
                if not data[fname_partial]:
                    return
                else:
                    for i in range(data[fname_partial]):
                        reader.next()

    if not (mode == 'init' or 'id' in fields):
        logger = netsvc.Logger()
        logger.notifyChannel("init", netsvc.LOG_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) :
            continue
        try:
            datas.append(map(lambda x: misc.ustr(x), line))
        except:
            logger = netsvc.Logger()
            logger.notifyChannel("init", netsvc.LOG_ERROR, "Cannot import the line: %s" % line)
    pool.get(model).import_data(cr, uid, fields, datas,mode, module, noupdate, filename=fname_partial)
    if config.get('import_partial'):
        data = pickle.load(file(config.get('import_partial')))
        data[fname_partial] = 0
        pickle.dump(data, file(config.get('import_partial'),'wb'))
        cr.commit()
示例#8
0
    def parse(self, de):
        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 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]
示例#9
0
文件: convert.py 项目: Ben17000/odoo
 def parse(self, de, mode=None):
     roots = ['openerp','data','odoo']
     if de.tag not in roots:
         raise Exception("Root xml tag must be <openerp>, <odoo> or <data>.")
     for rec in de:
         if rec.tag in roots:
             self.parse(rec, mode)
         elif rec.tag in self._tags:
             try:
                 self._tags[rec.tag](self.cr, rec, de, mode=mode)
             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]
示例#10
0
文件: convert.py 项目: ecoreos/hz
 def parse(self, de, mode=None):
     roots = ['ecore','data','ecore']
     if de.tag not in roots:
         raise Exception("Root xml tag must be <ecore>, <ecore> or <data>.")
     for rec in de:
         if rec.tag in roots:
             self.parse(rec, mode)
         elif rec.tag in self._tags:
             try:
                 self._tags[rec.tag](self.cr, rec, de, mode=mode)
             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]
示例#11
0
def convert_xml_import(cr, module, xmlfile, idref=None, mode="init", noupdate=False, report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(etree.parse(os.path.join(config["root_path"], "import_xml.rng")))
    try:
        relaxng.assert_(doc)
    except Exception:
        _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:
        idref = {}
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate)
    obj.parse(doc.getroot())
    return True
示例#12
0
def convert_xml_import(cr, module, xmlfile, idref=None, mode='init', noupdate=False, report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'],'import_xml.rng' )))
    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))
        raise

    if idref is None:
        idref={}
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate)
    obj.parse(doc.getroot())
    return True
示例#13
0
文件: convert.py 项目: Ben17000/odoo
def convert_xml_import(cr, module, xmlfile, idref=None, mode='init', noupdate=False, report=None):
    doc = etree.parse(xmlfile)
    relaxng = etree.RelaxNG(
        etree.parse(os.path.join(config['root_path'],'import_xml.rng' )))
    try:
        relaxng.assert_(doc)
    except Exception:
        _logger.info('The XML file does not fit the required schema !', exc_info=True)
        _logger.info(misc.ustr(relaxng.error_log.last_error))
        raise

    if idref is None:
        idref={}
    if isinstance(xmlfile, file):
        xml_filename = xmlfile.name
    else:
        xml_filename = xmlfile
    obj = xml_import(cr, module, idref, mode, report=report, noupdate=noupdate, xml_filename=xml_filename)
    obj.parse(doc.getroot(), mode=mode)
    return True