raise the propper exception if a required config setting is missing
This commit is contained in:
parent
c742fd90b8
commit
54c654f7ce
1 changed files with 27 additions and 5 deletions
|
@ -1,4 +1,7 @@
|
|||
from configparser import _UNSET, RawConfigParser
|
||||
from configparser import _UNSET, NoOptionError, NoSectionError, RawConfigParser
|
||||
from contextlib import contextmanager
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from c3nav.utils.environ import Env
|
||||
|
||||
|
@ -25,25 +28,44 @@ class C3navConfigParser(RawConfigParser):
|
|||
key = 'C3NAV_' + key
|
||||
return key
|
||||
|
||||
@classmethod
|
||||
@contextmanager
|
||||
def _error_wrapper(cls, section: str, option: str, env: bool | str):
|
||||
try:
|
||||
yield
|
||||
except (NoOptionError, NoSectionError) as e:
|
||||
error_msg = f'Missing required setting {section} -> {option}'
|
||||
if env:
|
||||
error_msg += f' ({cls.get_env_key(section, option, env)} environment variable)'
|
||||
raise ImproperlyConfigured(error_msg) from e
|
||||
|
||||
# for our error wrapper to work we need to make sure getint, getfloat, and getboolean call the original get method
|
||||
def _get(self, section, conv, option, **kwargs):
|
||||
return conv(super().get(section, option, **kwargs))
|
||||
|
||||
def get(self, section: str, option: str, *, raw=False, vars=None, fallback=_UNSET, env: bool | str = True) -> str:
|
||||
if env and (value := self.env.str(self.get_env_key(section, option, env), default=None)) is not None:
|
||||
return value
|
||||
with self._error_wrapper(section, option, env):
|
||||
return super().get(section, option, raw=raw, vars=vars, fallback=fallback)
|
||||
|
||||
def getint(self, section: str, option: str, *, raw=False, vars=None, fallback=_UNSET,
|
||||
env: bool | str = True, **kwargs) -> int:
|
||||
if env and (value := self.env.int(self.get_env_key(section, option, env), default=None)) is not None:
|
||||
return value
|
||||
with self._error_wrapper(section, option, env):
|
||||
return super().getint(section, option, raw=raw, vars=vars, fallback=fallback)
|
||||
|
||||
def getfloat(self, section: str, option: str, *, raw=False, vars=None, fallback=_UNSET,
|
||||
env: bool | str = True, **kwargs) -> float:
|
||||
if env and (value := self.env.float(self.get_env_key(section, option, env), default=None)) is not None:
|
||||
return value
|
||||
with self._error_wrapper(section, option, env):
|
||||
return super().getfloat(section, option, raw=raw, vars=vars, fallback=fallback)
|
||||
|
||||
def getboolean(self, section: str, option: str, *, raw=False, vars=None, fallback=_UNSET,
|
||||
env: bool | str = True, **kwargs) -> bool:
|
||||
if env and (value := self.env.bool(self.get_env_key(section, option, env), default=None)) is not None:
|
||||
return value
|
||||
with self._error_wrapper(section, option, env):
|
||||
return super().getboolean(section, option, raw=raw, vars=vars, fallback=fallback)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue