Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active February 17, 2021 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niccolomineo/6387b488ef3d5ed45a2eb964e64f9d1d to your computer and use it in GitHub Desktop.
Save niccolomineo/6387b488ef3d5ed45a2eb964e64f9d1d to your computer and use it in GitHub Desktop.
Settings env init
"""
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