Is there a way to share common configuration using

2019-05-28 13:58发布

问题:

This is a pretty simple idea conceptually. In terms of specifics, I'm working with a pretty generic Kotti installation, where I'm customizing some pages / templates.

Much of my configuration is shared between a production and development server. Currently, these settings are included in two separate ini files. It would be nice to DRY this configuration, with common settings in one place.

I'm quite open to this happening in python or an an ini file / section (or perhaps a third, yet-to-be-considered place). I think it's equivalent to use a [DEFAULT] section, or pass dictionaries to loadapp via global_conf, but that seems to be processed in a squirrelly way. For example, Kotti thinks I've properly set sqlalchemy.url, but sqlalchemy iteself fails on url = opts.pop('url'). Moreover, since Kotti defines some default settings, Paste doesn't end up searching for them in the global_conf (e.g., kotti_configurators).

I don't like the idea of passing in a large dict for %(var)s substitution, as it requires effectively renaming all of the shared variables.

In my initial experiments with Paste Deploy, it demands a "main" section in each ini file. So, I don't think I can just use a use = config:shared.ini line. But that's conceptually close to what I'd like to do.

Is there a way to explicitly (and properly) include settings from DEFAULT or global_conf? Or perhaps do this programmatically with python on the results of loadapp?

回答1:

For example, Kotti thinks I've properly set sqlalchemy.url, but sqlalchemy iteself fails on url = opts.pop('url').

If you think something is odd and you're asking on SO it'd be wise to show a stacktrace and an example of what you tried to do.

Kotti gets its settings the same as any pyramid app. Your entry point (def main(global_conf, **settings) usually) is passed the global_conf and settings dictionary. You're then responsible for fixing that up and shuffling it off into the configurator.

For various reasons PasteDeploy keeps the global_conf separate from the settings, thus you're responsible for merging them if you wish.

[DEFAULT]
foo = bar

[app:main]
use = egg:myapp#main
baz = xyz

def main(global_conf, **app_settings):
    settings = global_conf
    settings.update(app_settings)
    config = Configurator(settings=settings)
    config.include('kotti')
    return config.make_wsgi_app()