38 lines
861 B
Python
38 lines
861 B
Python
|
"""
|
||
|
mimi.core: core functionality for mimipath
|
||
|
"""
|
||
|
|
||
|
from pathlib import Path
|
||
|
import pydantic
|
||
|
import tomlkit
|
||
|
|
||
|
from tomlantic import ModelBoundTOML
|
||
|
|
||
|
|
||
|
CONFIG_PATH = Path(__file__).parent.joinpath("config.toml")
|
||
|
|
||
|
|
||
|
class ConfigSecretsSection(pydantic.BaseModel):
|
||
|
model_config = pydantic.ConfigDict(validate_assignment=True)
|
||
|
key: str
|
||
|
|
||
|
|
||
|
class ConfigFile(pydantic.BaseModel):
|
||
|
model_config = pydantic.ConfigDict(validate_assignment=True)
|
||
|
secrets: ConfigSecretsSection
|
||
|
|
||
|
|
||
|
def read_config() -> ModelBoundTOML[ConfigFile]:
|
||
|
if not CONFIG_PATH.exists():
|
||
|
raise FileNotFoundError(f"config file not found at '{CONFIG_PATH}'")
|
||
|
return ModelBoundTOML(ConfigFile, tomlkit.parse(CONFIG_PATH.read_text()), handle_errors=True)
|
||
|
|
||
|
|
||
|
def _test() -> None:
|
||
|
config = read_config()
|
||
|
print(config.model.secrets.key)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
_test()
|