-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_configurator.py
47 lines (37 loc) · 1.38 KB
/
script_configurator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import tomlkit
import os
from pathlib import Path
from typing import Tuple
from warnings import warn
basepath = Path(os.path.dirname(__file__)).resolve(True)
JSON_T = "json"
TOML_T = "toml"
config_json = basepath / Path("config.json")
config_toml = basepath / Path("config.toml")
def find_config() -> Tuple[Path, str]:
"""Tries to find the config file and returns the path to either the TOML or JSON config.
JSON will always take precedence if both config files exist.
Returns:
Tuple[Path, str]: Path to the found config file and the type of the found file.
"""
if config_toml.exists() and config_json.exists():
warn(
"Both TOML and JSON configs exist. JSON will take precedence and TOML will be ignored.",
category=RuntimeWarning,
)
if config_json.exists():
return config_json, JSON_T
if config_toml.exists():
return config_toml, TOML_T
raise FileNotFoundError(
f"Configuration files not found. Expected to find {config_json} or {config_toml}"
)
config_path, config_type = find_config()
with open(config_path, "r", encoding="utf-8") as f:
if config_type == JSON_T:
SIGMOID_CONFIG = json.load(f)
elif config_type == TOML_T:
SIGMOID_CONFIG = tomlkit.load(f)
else:
raise ValueError(f"Unrecognized {config_type}. Expected {JSON_T} or {TOML_T}")