Isolate configuration related code better

- create the global `config` object inside the
  `urban_meal_delivery.configuration` module
- streamline documentation and comments
This commit is contained in:
Alexander Hess 2020-12-14 15:15:08 +01:00
parent 9ee9c04a69
commit c1064673aa
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
4 changed files with 12 additions and 16 deletions

View file

@ -272,4 +272,4 @@ console_output_style = count
env =
TESTING=true
markers =
e2e: integration tests, inlc., for example, tests touching a database
e2e: integration tests, incl., for example, tests touching the database

View file

@ -5,11 +5,12 @@ Example:
>>> umd.__version__ != '0.0.0'
True
"""
# The config object must come before all other project-internal imports.
from urban_meal_delivery.configuration import config # noqa:F401 isort:skip
import os as _os
from importlib import metadata as _metadata
from urban_meal_delivery import configuration as _configuration
from urban_meal_delivery import db # noqa:F401
try:
@ -24,14 +25,3 @@ else:
__author__ = _pkg_info['author']
__pkg_name__ = _pkg_info['name']
__version__ = _pkg_info['version']
# Global `config` object to be used in the package.
config: _configuration.Config = _configuration.make_config(
'testing' if _os.getenv('TESTING') else 'production',
)
# Import `db` down here as it depends on `config`.
# pylint:disable=wrong-import-position
from urban_meal_delivery import db # noqa:E402,F401 isort:skip

View file

@ -73,7 +73,7 @@ def make_config(env: str = 'production') -> Config:
"""Create a new `Config` object.
Args:
env: either 'production' or 'testing'; defaults to the first
env: either 'production' or 'testing'
Returns:
config: a namespace with all configurations
@ -81,7 +81,8 @@ def make_config(env: str = 'production') -> Config:
Raises:
ValueError: if `env` is not as specified
""" # noqa:DAR203
config: Config
config: Config # otherwise mypy is confused
if env.strip().lower() == 'production':
config = ProductionConfig()
elif env.strip().lower() == 'testing':
@ -94,3 +95,6 @@ def make_config(env: str = 'production') -> Config:
warnings.warn('Bad configurartion: no DATABASE_URI set in the environment')
return config
config = make_config('testing' if os.getenv('TESTING') else 'production')

View file

@ -5,6 +5,8 @@ import os
from urban_meal_delivery import config
# The TESTING environment variable is set
# in setup.cfg in pytest's config section.
if not os.getenv('TESTING'):
raise RuntimeError('Tests must be executed with TESTING set in the environment')