Fixes parsing of lists in config files.

Sadly everett parses lists wrongly in configObj, this patch disables it
and also aborts after the first found config file (like the docs
suggest).

Change-Id: Ib1d94a7c523c42087e20f959e6133f4d9e220f4d
This commit is contained in:
Florian Apolloner 2018-11-05 18:11:00 +01:00
parent 35d762c62b
commit ddf4d9f526
3 changed files with 40 additions and 5 deletions

View File

@ -2,4 +2,4 @@
debug = true
log_level = DEBUG
secret_key = integration
allowed_hosts = testserver
allowed_hosts = localhost

View File

@ -11,7 +11,7 @@ SECRET_KEY = env("SECRET_KEY")
DEBUG = env.bool("DEBUG", default=False)
ALLOWED_HOSTS = env("ALLOWED_HOSTS", cast=list, default=[])
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])
ADMINS = ()

View File

@ -1,12 +1,47 @@
import os
import environ
from configobj import ConfigObj
from everett import ConfigurationError
from everett.manager import ConfigEnvFileEnv, ConfigIniEnv, ConfigManager, ConfigOSEnv
from everett.manager import ConfigEnvFileEnv, ConfigIniEnv, ConfigManager, ConfigOSEnv, listify
__all__ = ["EverettEnviron"]
class DumbConfigIniEnv(ConfigIniEnv):
"""Simple ConfigIniEnv with disabled list parsing that actually aborts after the first file."""
# TODO: Remove once upstream is fixed (https://github.com/willkg/everett/pull/71)
def __init__(self, possible_paths):
self.cfg = {}
possible_paths = listify(possible_paths)
for path in possible_paths:
if not path:
continue
path = os.path.abspath(os.path.expanduser(path.strip()))
if path and os.path.isfile(path):
self.cfg = self.parse_ini_file(path)
break
def parse_ini_file(cls, path):
cfgobj = ConfigObj(path, list_values=False)
def extract_section(namespace, d):
cfg = {}
for key, val in d.items():
if isinstance(d[key], dict):
cfg.update(extract_section(namespace + [key], d[key]))
else:
cfg["_".join(namespace + [key]).upper()] = val
return cfg
return extract_section([], cfgobj.dict())
class EnvironProxy:
def __init__(self, cfg):
self.cfg = cfg
@ -20,7 +55,7 @@ class EnvironProxy:
def __getitem__(self, key):
try:
return self.cfg(key)
return self.cfg(key, raw_value=True)
except ConfigurationError as err:
raise KeyError("Missing key %r" % key) from err
@ -34,7 +69,7 @@ class EverettEnviron(environ.Env):
[
ConfigOSEnv(),
ConfigEnvFileEnv(".env"),
ConfigIniEnv([os.environ.get("ARA_CFG"), "~/.config/ara/server.cfg", "/etc/ara/server.cfg"]),
DumbConfigIniEnv([os.environ.get("ARA_CFG"), "~/.config/ara/server.cfg", "/etc/ara/server.cfg"]),
]
).with_namespace("ara")
)