【问题标题】:Pyramid and .ini configurationPyramid 和 .ini 配置
【发布时间】:2011-11-26 02:55:51
【问题描述】:

每个 Pyramid 应用程序都有一个关联的 .ini 文件,其中包含其设置。例如,默认值可能如下所示:

[app:main]
use = egg:MyProject
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
...

我想知道是否可以在其中添加您自己的配置值,并在运行时读取它们(主要来自可调用视图)。例如,我可能想要拥有

[app:main]
blog.title = "Custom blog name"
blog.comments_enabled = true
...

或者最好有一个单独的 .ini 文件并在启动时对其进行解析?

【问题讨论】:

    标签: python configuration-files pyramid ini


    【解决方案1】:

    当然可以。

    在您的入口点函数(大多数情况下,__init__.py 中的main(global_config, **settings))中,您的配置可以在settings 变量中访问。

    例如,在您的.ini:

    [app:main]
    blog.title = "Custom blog name"
    blog.comments_enabled = true
    

    在你的__init__.py

    def main(global_config, **settings):
        config = Configurator(settings=settings)
        blog_title = settings['blog.title']
        # you can also access you settings via config
        comments_enabled = config.registry.settings['blog.comments_enabled']
        return config.make_wsgi_app()
    

    根据latest Pyramid docs,您可以通过request.registry.settings访问视图功能中的设置。另外,据我所知,它将通过event.request.registry.settings 发送给事件订阅者。

    关于您关于使用另一个文件的问题,我很确定将所有配置放入常规初始化文件中是一种很好的做法,就像您一样使用点分表示法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 2012-08-15
      • 2010-10-03
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2017-12-26
      相关资源
      最近更新 更多