confuse icon indicating copy to clipboard operation
confuse copied to clipboard

configuration from environment variables

Open thejohnfreeman opened this issue 10 years ago • 5 comments

There should be a function to generate a configuration from environment variables. Overriding configuration in the environment is handy for

  • scripts that cannot create a configuration file (or cannot edit an existing one) and cannot change the command line, and
  • modules that want to read configuration but do not control sys.argv (e.g. logging and security modules).

I believe Confit already supports an optional environment variable naming a configuration path. That's good, but I'd extend it to be a colon-separated path (like PATH, CLASSPATH, or PYTHONPATH) of configurations to load in order.

thejohnfreeman avatar Mar 28 '15 20:03 thejohnfreeman

Sounds good!

sampsyo avatar Mar 29 '15 20:03 sampsyo

One important caveat is that most shells have a limited character set for environment variable names. Usually the OS will support all non null characters, but shells will only support letters, numbers, and underscores. I think that will translate to only allowing overrides of variables with "well behaved names".

thejohnfreeman avatar Mar 29 '15 21:03 thejohnfreeman

That should be easy to implement yourself due to good code design of confuse. You can combine layers of data gathered from different sources.

from confuse import RootView, ConfigSource
c = RootView([])
# Higher priority first
c.add(ConfigSource.of({
    'corn': 300,
    'potato': 200,
    'berries': ['blue', 'red'],
    'map': {
        'x': 3,
        'y': 4,
    }
}))
# Lower priority last
c.add(ConfigSource.of({
    'banana': 150,
    'apple': 240,
    'corn': 200,
    'berries': ['green', 'purple'],
    'map': {
        'y': 6,
        'z': 5,
        'm': 1,
    }
}))

neumond avatar Mar 13 '18 01:03 neumond

@sampsyo any plan to support this? This would be an incredible addition in docker environment since its preferred to add config entries in docker compose files. Dynaconf does this exact thing, but for someone who already invested in confuse having this capability might be really helpful.

mvadu avatar Feb 25 '21 23:02 mvadu

I would be happy to review a PR, but I do not have the bandwidth to implement this myself.

sampsyo avatar Feb 26 '21 00:02 sampsyo