如何在金字塔rest框架中添加应用程序特定的设置?

2024-09-30 02:20:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉金字塔。在

问题是我不能弄清楚特定于应用程序的设置(键值对)是如何在金字塔中工作的。在

以下是我在各种谷歌搜索和其他stackoverflow答案后所做的:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    if '__file__' in global_config:
        settings.update(
            load_sensitive_settings(global_config['__file__'], global_config))
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    # config.add_static_view('static', 'static', cache_max_age=3600)
    # config.add_route('home', '/')
    config.add_route(
        'tags',
        '/tags', request_method='POST', accept='application/json', )
    config.scan()
    return config.make_wsgi_app()


def load_sensitive_settings(configurationPath, defaultByKey):
    'Load sensitive settings from hidden configuration file'
    # configFolder, configName = os.path.split(configurationPath)
    # sensitivePath = os.path.join(configFolder, '.' + configName)
    sensitivePath = configurationPath
    settings = {}
    configParser = ConfigParser.ConfigParser(defaultByKey)
    if not configParser.read(sensitivePath):
        log.warn('Could not open %s' % sensitivePath)
        return settings
    settings.update(configParser.items('custom'))
    return settings

我在一个文件中尝试获取如下设置:

^{pr2}$

但我总是把设置对象设为无。在

这就是我在中定义自定义设置的方式开发.ini在

[custom]
my_key = ''

这就是我在develpoment中启动服务器的方法

pserve development.ini

我读过了请求设置可以给我设置,但这种方法对我不可行,因为我的密钥包含一个文件的名称,它是1.5gb,它必须一直存在于内存中。在服务器中加载该文件大约需要5分钟,因此无法按需加载该文件。在

请指教。在

非常感谢你的帮助。在

更新:

多亏了所有的答案,我终于成功了。

我的主要功能如下:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    if '__file__' in global_config:
        init_config(global_config['__file__'])

我做了一个配置文件,我的配置文件是这样的:

import ConfigParser

settings = dict()

def init_config(filename):
    config = ConfigParser.ConfigParser()
    config.read(filename)
    settings_dict = config.items('custom')
    settings.update(settings_dict)

现在,无论我想要什么设置,我只需要:

from projectname.config import settings
settings.get('my_key')

我把我的应用程序特定的设置(开发/生产.py)像这样

[custom]
my_key = value

问候


Tags: 文件addconfigifsettingsapplicationdefcustom
3条回答

以下是我的解决方案:

配置.ini

[APP.CONFIG]
url = http://....


[SMTP.CONFIG]
smtp.server = ...
smtp.port = 25
smtp.login = ...
smtp.password = ...
smtp.from = ...


[DB.CONFIG] 
db.database=...
db.host=...
db.port=..
db.user=...
db.password=...

配置.py

^{pr2}$

以及如何在应用程序中使用它

from config import db
host = db['db.host']

如果像我一样,您使用的是PasteDeploy with Pyramid,the Pyramid docs here解释如何使用.ini配置文件中的[DEFAULT]部分来保存自定义参数。在

您还可以从阅读the documentation on ^{} files中受益,因为它提供了一些片段,使其更加清晰。在

最简单的方法是用点分隔的名称将您的设置放入应用程序main部分。示例:

[app:main]
websauna.site_name = Trees
websauna.site_tag_line = Enjoy
websauna.site_url = http://localhost:6543
websauna.site_author = Trees team

然后您可以:

^{pr2}$

WSGI管道不会为您带来来自其他部分的设置,如果您想访问其他部分(据我所知),您需要使用ConfigParser重新分析INI文件。在

如果需要在开发期间加载大量数据,只需在“设置”中存储一个文件名,并在需要访问数据时加载该文件,这样就不会减慢web服务器的启动速度。在

相关问题 更多 >

    热门问题