Config Precedence¶
With several ways to set configuration, the precedence matters when
multiple methods are used simultaneously. ResConfig
resolves
conflicts based on the following order, with the earlier methods in
the list take precedence when defined:
Command-line arguments.
Configuration files specified as command-line arguments.
Environment variables.
Configuration files supplied to
ResConfig
initializer.Default configuration supplied to
ResConfig
initializer.
As an example, consider the following code, which we call myapp.py:
config = ResConfig({"db.host": "localhost", "db.port": 5432},
config_files=["~/.myapp.conf",
"/etc/myapp.conf"],
load_on_init=False)
parser = argparse.ArgumentParser()
parser.add_argument("--conf")
config.add_arguments_to_argparse(parser)
args = parser.parse_args()
config.prepare_from_argparse(args, config_file_arg="conf")
config.load()
and the following files:
myapp.conf:
[db]
host = local.org
~/.myapp.conf:
[db]
host = home.org
(Let us assume that /etc/myapp.conf does not exist.)
What would conf["db.host"]
return? The answer depends on how
myapp.py is started.
$ DB_HOST=env.org python myapp.py --conf=myapp.conf \ --db-host=cmdline.org
$ DB_HOST=env.org python myapp.py --conf=myapp.conf \
$ DB_HOST=env.org python myapp.py
$ python myapp.py
$ python myapp.py # with ~/.myapp.conf not found
Answers: (a) cmdline.org
, (b) local.org
, (c) env.org
, (d)
home.org
, (e) localhost
.