How can I access a custom section in a .ini Pyramid file?

I am currently writing a data collection service for several services. There are probably 5 different API endpoints with different hosts and port numbers. I wanted to create a settings file for this, but thought .ini should be the best place, or so I thought ...

My development.ini looks something like this:

[app:main]
use = egg:awesomeproject
auth.tkt = 'abc'
auth.secret = 'I love python'

mongodb.host = 'somehost'
mongodb.port= 6379

[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

[user:sg:qa]
host = 127.0.0.1
port = 1234

[customer:sg:qa]
host = 127.0.0.2
port = 4567

I tried to access user sections inside the pyramid event, for example:

def add_api_path(event):
    request = event.request
    settings = request.registry.settings
    _type = 'customer:sg:qa'
    base_config = settings[_type]

But that did not work, because settings are actually a type of attribute [app:main]. Can someone teach me how to access Pyramid sections? I read about it in a different way using ConfigParser, but I wanted to ask if there is another simpler way in Pyramid.

+4
1

, . , , .

def main(global_conf, **settings):
    parser = ConfigParser({'here': global_conf['__here__']})
    parser.read(global_conf['__file__'])
    for k, v in parser.items('user:sg:qa'):
        settings['user:sg:qa:' + k] = v

    config = Configurator(settings=settings)

:

request.registry.settings['user:sg:qa:host']

Pyramid 1.9 ini parsing , . :

import plaster

def main(global_conf, **settings):
    user_settings = plaster.get_settings(global_conf['here'], 'user:sg:qa')
    for k, v in user_settings.items():
        settings['user:sg:qa:' + k] = v

    config = Configurator(settings=settings)
+5

All Articles