Last active
February 17, 2021 16:24
-
-
Save niccolomineo/6387b488ef3d5ed45a2eb964e64f9d1d to your computer and use it in GitHub Desktop.
Settings env init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Initialise settings. | |
This implementation requires class-based definition of each environment | |
in an individual file (e.g. `production.py` / `class Production`). | |
This very init code should go in an `__init__.py` file in the same folder as the environment files. | |
""" | |
from importlib import import_module | |
from os import environ | |
from pathlib import Path | |
from sys import modules | |
def get_env(): | |
"""Return appropriate environment.""" | |
ENV = environ.get("ENV", None) | |
is_production = ENV == "production" | |
env = None | |
if is_production: | |
env = ENV | |
else: | |
from dotenv import load_dotenv | |
env = "test" if ENV == "test" else "development" | |
load_dotenv(BASE_DIR / f".env.{env}") | |
return env | |
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent | |
ENV = get_env() | |
env_module = import_module(f"niccolomineocom.settings.{ENV}") | |
env_class = getattr(env_module, ENV.capitalize()) | |
for attr in dir(env_class): | |
if attr.isupper(): | |
setattr(modules[__name__], attr, getattr(env_class, attr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment