Skip to content

Instantly share code, notes, and snippets.

@mmerickel
Created December 10, 2013 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmerickel/7901444 to your computer and use it in GitHub Desktop.
Save mmerickel/7901444 to your computer and use it in GitHub Desktop.
How to load a more useful ini file for use with pyramid.
from ConfigParser import RawConfigParser
def load_settings(global_config, app_settings):
"""
Load an INI file with multiple sections into a single dictionary.
.. code-block:: ini
[sqlalchemy]
url = sqlite://%(here)s
[auth]
secret = foo
expires_in = 3600
[app:myapp]
use = egg:myapp
This file can be loaded and used via:
.. code-block:: python
def main(global_conf, **settings)
settings = load_settings(global_conf, settings)
config = Configurator(settings=settings)
engine = engine_from_config(settings, prefix='sqlalchemy.')
dbmaker = sessionmaker(bind=engine)
config.registry['dbmaker'] = dbmaker
return config.make_wsgi_app()
"""
config_file = global_config.get('__file__')
parser = RawConfigParser()
parser.read(config_file)
settings = {}
for section in parser.sections():
prefix = section.replace(':', '.')
for k, v in parser.items(section):
settings[prefix + '.' + k] = v
settings.update(app_settings)
return settings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment