Created
December 10, 2013 22:25
-
-
Save mmerickel/7901444 to your computer and use it in GitHub Desktop.
How to load a more useful ini file for use with pyramid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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