-
Notifications
You must be signed in to change notification settings - Fork 13
/
local.py
31 lines (22 loc) · 982 Bytes
/
local.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import logging
import os
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
def load_local_env_vars(env_file: str = "local.env") -> None:
"""
Load environment variables from the local.env so
that they can be fetched with `os.getenv()` or with
other utils that pull env vars.
https://pypi.org/project/python-dotenv/
NOTE: any existing env vars will not be overriden by this
"""
environment = os.getenv("ENVIRONMENT", None)
# If the environment is explicitly local or undefined
# we'll use the dotenv file, otherwise we'll skip
# Should never run if not local development
if environment is None or environment == "local":
load_dotenv(env_file)
def error_if_not_local() -> None:
if (env := os.getenv("ENVIRONMENT")) != "local":
logger.error("Environment %s is not local - cannot run operation", env)
raise Exception("Local-only process called when environment was set to non-local")