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:
parent
9ee9c04a69
commit
c1064673aa
4 changed files with 12 additions and 16 deletions
|
|
@ -272,4 +272,4 @@ console_output_style = count
|
||||||
env =
|
env =
|
||||||
TESTING=true
|
TESTING=true
|
||||||
markers =
|
markers =
|
||||||
e2e: integration tests, inlc., for example, tests touching a database
|
e2e: integration tests, incl., for example, tests touching the database
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,12 @@ Example:
|
||||||
>>> umd.__version__ != '0.0.0'
|
>>> umd.__version__ != '0.0.0'
|
||||||
True
|
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 importlib import metadata as _metadata
|
||||||
|
|
||||||
from urban_meal_delivery import configuration as _configuration
|
from urban_meal_delivery import db # noqa:F401
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -24,14 +25,3 @@ else:
|
||||||
__author__ = _pkg_info['author']
|
__author__ = _pkg_info['author']
|
||||||
__pkg_name__ = _pkg_info['name']
|
__pkg_name__ = _pkg_info['name']
|
||||||
__version__ = _pkg_info['version']
|
__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
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ def make_config(env: str = 'production') -> Config:
|
||||||
"""Create a new `Config` object.
|
"""Create a new `Config` object.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
env: either 'production' or 'testing'; defaults to the first
|
env: either 'production' or 'testing'
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
config: a namespace with all configurations
|
config: a namespace with all configurations
|
||||||
|
|
@ -81,7 +81,8 @@ def make_config(env: str = 'production') -> Config:
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: if `env` is not as specified
|
ValueError: if `env` is not as specified
|
||||||
""" # noqa:DAR203
|
""" # noqa:DAR203
|
||||||
config: Config
|
config: Config # otherwise mypy is confused
|
||||||
|
|
||||||
if env.strip().lower() == 'production':
|
if env.strip().lower() == 'production':
|
||||||
config = ProductionConfig()
|
config = ProductionConfig()
|
||||||
elif env.strip().lower() == 'testing':
|
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')
|
warnings.warn('Bad configurartion: no DATABASE_URI set in the environment')
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
config = make_config('testing' if os.getenv('TESTING') else 'production')
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import os
|
||||||
from urban_meal_delivery import config
|
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'):
|
if not os.getenv('TESTING'):
|
||||||
raise RuntimeError('Tests must be executed with TESTING set in the environment')
|
raise RuntimeError('Tests must be executed with TESTING set in the environment')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue