From 97c5bcec136eb549b57cdb74ebd9da7ca1338e90 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Sun, 15 Apr 2018 10:29:43 -0400 Subject: config: allow reading both the defaults file and the modified config In the process, rename config.proto to config.defaults (because that is what it is now). Also use dict.get('key', default_value) when querying os.environ, rather than an if block, as it is more pythonic/readable/concise, and reduces the number of dict lookups. This change allows aurweb configuration to be done via either: - copying config.defaults to config and modifying values - creating a new config only containing modified values, next to a config.defaults containing unmodified values The motivation for this change is to enable ansible configuration in our flagship deployment by storing only changed values, and deferring to config.defaults otherwise. A side benefit is, it is easier to see what has changed by inspecting only the site configuration file. If a config.defaults file does not exist next to $AUR_CONFIG or in $AUR_CONFIG_DEFAULTS, it is ignored and *all* values are expected to live in the modified config file. Signed-off-by: Eli Schwartz Signed-off-by: Lukas Fleischer --- aurweb/config.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'aurweb') diff --git a/aurweb/config.py b/aurweb/config.py index a52d9422..52ec461e 100644 --- a/aurweb/config.py +++ b/aurweb/config.py @@ -8,11 +8,13 @@ def _get_parser(): global _parser if not _parser: + path = os.environ.get('AUR_CONFIG', '/etc/aurweb/config') + defaults = os.environ.get('AUR_CONFIG_DEFAULTS', path + '.defaults') + _parser = configparser.RawConfigParser() - if 'AUR_CONFIG' in os.environ: - path = os.environ.get('AUR_CONFIG') - else: - path = "/etc/aurweb/config" + if os.path.isfile(defaults): + with open(defaults) as f: + _parser.read_file(f) _parser.read(path) return _parser -- cgit v1.2.3-24-g4f1b