Skip to content
Snippets Groups Projects
Commit 80b4ae39 authored by Raphaël Gomès's avatar Raphaël Gomès
Browse files

WIP config

parent a1631d29
No related branches found
No related tags found
No related merge requests found
# User config for poulpe and its components
import pathlib
import toml
import platformdirs
from poulpe import errors
APP_NAME = "poulpe"
AUTHOR = "poulpe-developers" # needed for Windows
def validate_config(config_data):
"""Make sure the user config is like we expect"""
hints = []
# For now this is quite manual. There are no facilities for validating
# declarative schemas in Python. We'll probably write this in Rust shortly,
# and add Python bindings.
folders = config_data.get("folders")
if folders is not None:
if isinstance(folders, dict):
expected_keys = {"bin-envs", "data-envs", "results", "repos"}
for key, path in folders:
if key not in expected_keys:
hints.append(f"Unexpected key '{key}' in [folders] section")
else:
wrong_path_msg = f"folders.{key} should be an absolute path"
try:
path = pathlib.Path(path)
except TypeError:
hints.append(wrong_path_msg)
else:
if not path.is_absolute():
hints.append(wrong_path_msg)
else:
hints.append("[folders] should be a mapping of name to path")
else:
hints.append("Missing top-level [folders] section")
return hints
def user_config():
"""Returns the validated user config"""
config_dir = platformdirs.user_config_path(APP_NAME, AUTHOR)
config_file = config_dir / "config.toml"
if not config_file.exists():
msg = f"Please define a valid `config.toml` in `{config_dir}`"
raise errors.ConfigError(msg)
config_data = toml.loads(config_file.read_text())
errors = validate_config(config_data)
if hints:
hints = "\n - ".join(hints)
raise errors.ConfigError(f"Invalid config file:\n{hints}")
return config_data
......@@ -20,3 +20,7 @@
class MissingDataEnvInputVars(KeyError):
pass
class ConfigError(RuntimeError):
pass
......@@ -5,6 +5,7 @@
[options]
install_requires =
click == 8.1
platformdirs
importlib-resources
toml
packages = find_namespace:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment