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:

  1. Command-line arguments.

  2. Configuration files specified as command-line arguments.

  3. Environment variables.

  4. Configuration files supplied to ResConfig initializer.

  5. 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.

  1. $ DB_HOST=env.org python myapp.py --conf=myapp.conf \
                                   --db-host=cmdline.org
    
  2. $ DB_HOST=env.org python myapp.py --conf=myapp.conf \
    
  3. $ DB_HOST=env.org python myapp.py
    
  4. $ python myapp.py
    
  5. $ python myapp.py  # with ~/.myapp.conf not found
    

Answers: (a) cmdline.org, (b) local.org, (c) env.org, (d) home.org, (e) localhost.