-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
163 additions
and
37 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,70 @@ | ||
from pathlib import Path | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
from typing import Type | ||
from typing import Union, List, get_type_hints | ||
from pydantic import Field | ||
|
||
ENV_FILE_ENCODING = "utf-8" | ||
|
||
|
||
class BaseENVSettings(BaseSettings): | ||
""" | ||
This class acts as the BaseClass which can be used to define custom | ||
ENV-Variables which can be used across the ComputeBlock & for entrypoints | ||
This definition, and pydantic, will then take care of validating the envs | ||
""" | ||
Allow kwargs to propagate to any fields whose default factory extends | ||
BaseSettings, | ||
This is mostly to allow _env_file to be passed through. | ||
""" | ||
model_config = SettingsConfigDict( | ||
env_file_encoding=ENV_FILE_ENCODING, | ||
case_sensitive=True, | ||
extra="ignore" | ||
) | ||
|
||
@classmethod | ||
def load_settings( | ||
cls: Type["BaseENVSettings"], | ||
env_file: str = ".env" | ||
) -> "BaseENVSettings": | ||
def from_env( | ||
cls, | ||
env_file: Union[str, Path, List[Union[str, Path]]] = None, | ||
*args, | ||
**kwargs | ||
): | ||
return cls(propagate_kwargs={"_env_file": env_file}, *args, **kwargs) | ||
|
||
@classmethod | ||
def _basesettings_fields(cls): | ||
""" | ||
load_settings loads the env file. The name of the env_file can be | ||
passed as an argument. | ||
Returns the parsed ENVs | ||
:return a dict of field_name: default_factory for any fields that | ||
extend BaseSettings | ||
""" | ||
return cls(_env_file=env_file, _env_file_encoding=ENV_FILE_ENCODING) | ||
type_hints = get_type_hints(cls) | ||
return { | ||
name: typ for name, typ in type_hints.items() | ||
if isinstance(typ, type) and issubclass(typ, BaseSettings) | ||
|
||
} | ||
|
||
@classmethod | ||
def _propagate_kwargs(cls, kwargs): | ||
""" | ||
Any settings that extend BaseSettings be passed the kwargs. | ||
""" | ||
sub_settings = cls._basesettings_fields() | ||
for name, field_type in sub_settings.items(): | ||
kwargs[name] = field_type(**kwargs) | ||
return kwargs | ||
|
||
def __init_subclass__(cls, **kwargs): | ||
""" | ||
Automatically set up nested settings fields with default_factory. | ||
""" | ||
super().__init_subclass__(**kwargs) | ||
type_hints = get_type_hints(cls) | ||
for field_name, field_type in type_hints.items(): | ||
if isinstance(field_type, type) and issubclass( | ||
field_type, BaseSettings): | ||
# Set a default factory for nested BaseSettings fields | ||
default_field = Field(default_factory=field_type) | ||
setattr(cls, field_name, default_field) | ||
|
||
def __init__(self, propagate_kwargs=None, *args, **kwargs): | ||
if propagate_kwargs: | ||
kwargs = self._propagate_kwargs(propagate_kwargs) | ||
super().__init__(*args, **kwargs) |
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