Split the pyramid framework ini file

I found out how to split the ini file to write environment-dependent settings, so make a note.

Since python does not have a function to define constants by default, I am currently writing constants in the ini file. Since an ini file is created for each environment, the same content is described multiple times, which complicates management, so I decided to create a common ini file and load it into each environment ini file.

Prepare common.ini in addition to development.ini and production.ini.

#common.ini
[common]

name = Taro Yamada tel = 03-xxxx-xxxx

Define the file path of common.ini in the environment file of development.ini and production.ini as follows. (When in the same hierarchy)

common.path = %(here)s/common.ini

Create a function to load common.ini in \ ini.py where the application startup settings are written.

def common_ini(settings):
    import os
    from ConfigParser import SafeConfigParser
    ini = settings.get("common.path")
    if ini is None:
        return
    if not os.path.exists(ini) and os.path.isfile(ini):
        raise ConfigurationError("ini is not found")

    conf = SafeConfigParser()
    conf.read(ini)
    return dict(conf.items('common'))

Config.add_settings the above function with the main function.

def main(global_config, **settings):
    """ This function returns a WSGI application.
    from pyramid.config import Configurator
    config = Configurator(settings=settings)
    config.add_settings(common_ini(settings))

Now you can load the common ini file in addition to each environment ini file.

Recommended Posts

Split the pyramid framework ini file
The Common Clk Framework
[Python] Split the date
Download the file in Python
File access under the directory
Can you delete the file?
How to make a command to read the configuration file with pyramid
Trial of writing the configuration file in Python instead of .ini etc.