Odoo模块始终包含两个文件__init__.py
和__openerp__.py
。
dhl_module
|-- controller
|-- models
|-- views
|-- __init__.py
|-- __openerp__.py
文件__openerp__.py
包含一个dict,但未将其指定给名称。该dict存储有关模块的信息。它看起来像这样:
# -*- coding: utf-8 -*-
{
'name': "DHL connector",
# used as subtitle
'summary': "Configuration for DHL connector ",
'description': """ DHL connector
""",
'author': "me",
'website': "mysite.com",
'category': 'Technical Settings',
# Change the version every release for apps.
'version': '0.0.1',
# any module necessary for this one to work correctly
'depends': [],
# always loaded
'data': ['views/dhl.xml', ],
# only loaded in demonstration mode
'demo': [],
# only loaded in test
'test': [],
'installable': True,
'application': True,
}
Odoo或我如何从__openerp__.py
模块访问此dict? dict变量未分配给名称。如何进口?
答案 0 :(得分:6)
OpenERP不必导入该模块来获取该字典。他们只需将文件作为文本阅读,然后使用eval()
评估内容:
>>> text = '''\
... # -*- coding: utf-8 -*-
... {
... 'name': "DHL connector",
... # used as subtitle
... 'summary': "Configuration for DHL connector ",
... 'description': """ DHL connector
... """,
... 'author': "me",
... 'website': "mysite.com",
... 'category': 'Technical Settings',
... # Change the version every release for apps.
... 'version': '0.0.1',
... # any module necessary for this one to work correctly
... 'depends': [],
... # always loaded
... 'data': ['views/dhl.xml', ],
... # only loaded in demonstration mode
... 'demo': [],
... # only loaded in test
... 'test': [],
... 'installable': True,
... 'application': True,
... }
... '''
>>> eval(text)
{'website': 'mysite.com', 'description': ' DHL connector\n ', 'demo': [], 'depends': [], 'data': ['views/dhl.xml'], 'category': 'Technical Settings', 'name': 'DHL connector', 'author': 'me', 'summary': 'Configuration for DHL connector ', 'application': True, 'version': '0.0.1', 'test': [], 'installable': True}
MANIFEST = '__openerp__.py' # ... terp_file = mod_path and opj(mod_path, MANIFEST) or False # ... f = tools.file_open(terp_file) try: info.update(eval(f.read())) finally: f.close()