gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:10:06 27 4
gpt4 key购买 nike

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

关于python - 从模块中导入没有名称的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com