Created
September 11, 2024 08:33
-
-
Save mnogom/2b03ff8380812f9c0478ff3c2e98ca64 to your computer and use it in GitHub Desktop.
Config class from env
This file contains hidden or 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
from pathlib import Path | |
import os | |
from dotenv import load_dotenv | |
class BaseConfig: | |
def __init__(self, config_file: Path = Path("cxsdn-env")): | |
load_dotenv(config_file) | |
def __getattribute__(self, name: str): | |
if name.startswith("__") and name.endswith("__"): | |
return super().__getattribute__(name) | |
klass = self.__annotations__.get(name) | |
return klass(os.getenv(name)) | |
def __dict__(self) -> str: | |
dict_ = {} | |
for name, klass in self.__annotations__.items(): | |
dict_[name] = klass(os.getenv(name)) | |
return dict_ | |
def __repr__(self) -> dict: | |
return self.__dict__() | |
def __str__(self) -> str: | |
return str(self.__dict__()) | |
class Config(BaseConfig): | |
ACTIVE_DIR: Path | |
HOST: str | |
PORT: int | |
DEBUG: bool | |
def get_config() -> Config: | |
return Config() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment