从模块中导入没有名称的字典

Import dict without name from a module

Odoo 模块总是有两个文件 __init__.py__openerp__.py

dhl_module
          |-- controller
          |-- models
          |-- views
          |-- __init__.py
          |-- __openerp__.py

文件 __openerp__.py 包含一个字典,但没有为它指定名称。该字典存储有关模块的信息。它看起来像这样:

# -*- 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 变量未分配给名称。怎么导入?

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}

那就是exactly what OpenERP does:

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()