Created
June 4, 2023 21:40
-
-
Save xescuder/79b4b8a2fcd0a26503353a84a23518f6 to your computer and use it in GitHub Desktop.
Configuration
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
import os | |
from os import environ as env | |
from typing import get_type_hints, Union | |
from dotenv import load_dotenv | |
env = os.getenv('ENVIRONMENT') | |
dotenv_path = f'.env.{env}' if env is not None else '.env' | |
load_dotenv(dotenv_path=dotenv_path) | |
class AppConfigError(Exception): | |
pass | |
def _parse_bool(val: Union[str, bool]) -> bool: # pylint: disable=E1136 | |
return val if type(val) == bool else val.lower() in ['true', 'yes', '1'] | |
# AppConfig class with required fields, default values, type checking, and typecasting for int and bool values | |
# Based on: https://gist.github.com/ryan-blunden/fc9fbaf2da65dd2200fb997bfb0aa365#file-config-py | |
class AppConfig: | |
DEBUG: bool = False | |
""" | |
Map environment variables to class fields according to these rules: | |
- Field won't be parsed unless it has a type annotation | |
- Field will be skipped if not in all caps | |
- Class field and environment variable name are the same | |
""" | |
def __init__(self, env): | |
for field in self.__annotations__: | |
if not field.isupper(): | |
continue | |
# Raise AppConfigError if required field not supplied | |
default_value = getattr(self, field, None) | |
if default_value is None and env.get(field) is None: | |
raise AppConfigError('The {} field is required'.format(field)) | |
# Cast env var value to expected type and raise AppConfigError on failure | |
try: | |
var_type = get_type_hints(AppConfig)[field] | |
if var_type == bool: | |
value = _parse_bool(env.get(field, default_value)) | |
else: | |
value = var_type(env.get(field, default_value)) | |
self.__setattr__(field, value) | |
except ValueError: | |
raise AppConfigError('Unable to cast value of "{}" to type "{}" for "{}" field'.format( | |
env[field], | |
var_type, | |
field | |
) | |
) | |
def __repr__(self): | |
return str(self.__dict__) | |
def get_postgres_uri(self): | |
return 'postgresql://{user}:{password}@{host}:{port}/{name}'.format( | |
user=env['DB_USER'], | |
password=env['DB_PASSWORD'], | |
host=env['DB_HOST'], | |
port=env.get('DB_PORT', '5432'), | |
name=env['DB_NAME'] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment