Relax NG 在Odoo中的应用

想必有些同学一定会奇怪,Odoo是如何将模块中的XML中的诸如record、menuitem是如何被组织和定义的,以及各种field的各种属性究竟有哪些,今天,我们就来一探究竟。

Relax NG:可扩展标记语言的下一代正规语言”是一种基于语法的可扩展标记语言模式语言,可用于描述、定义和限制 可扩展标记语言标准通用标记语言的子集)词汇表。简单地说 Relax NG是解释XML如何被定义的一套XML。Odoo就是通过定义了一套rng文件定义了自己一套xml框架结构,在模块被安装或者升级的时候将其解析成与之相对应的内置对象,存储在数据库中。关于Relax NG的语法规则,可以参考Relax NG的官网。

解析XML文件的代码在convert.py的convert_xml_import方法中:

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(), mode=mode)
    return True

而在xml_import方法中,处理了我们常见的各种节点:menuitem,record,template,url等等。

通过对rng文件的解读,我们可以总结Odoo xml的架构如下:

顶级节点:openerp

二级节点:元素(可多个):data

     属性 (可选): noupdate、context

三级节点:元素(可多个):menuitem、record、template、delete、act_window、url、assert、report、workflow、function、ir_set

常见节点属性:

menuitem:id(必填),name,parent,action,sequence,groups,icon,web_icon,web_icon_hover,string

record:id(可选),forcecreate(可选),model,context(可选),

            子节点:field(可多个)

template:id,t-name,name,forecreate,context,priority,inherit_id,primary,groups,active,customzie_show,page

delete:model,id,search

act_window:id,name,res_model,domain,src_model,context,view_id,view_type,view_mode,multi,target,key2,groups,limit,usage,auto_refresh

url:id,name,url,target

assert:model,search,count,string,id,context,severity,test

report:id,string,model,name,report_type,multi,menu,keyword,rml,file,sxw,xml,xsl,parser,auto,header,webkit_header,attachment,attachment_use,groups,usage

workflow:model,action,uid,context,ref,value

function:model,name,id,context,eval

ir_set:子节点:field

同样地,在View中也同样用到了Relax NG组织定义了View的架构,具体参见我的另一篇文章:

http://www.cnblogs.com/kfx2007/p/5478375.html

原文地址:https://www.cnblogs.com/kfx2007/p/5478338.html