-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathconfig.py
More file actions
85 lines (65 loc) · 2.82 KB
/
config.py
File metadata and controls
85 lines (65 loc) · 2.82 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import functools
import logging
import os
import tomllib
import typing
from pathlib import Path
from dotenv import load_dotenv
logger = logging.getLogger(__name__)
TomlTable = dict[str, typing.Any]
CONFIG_DIRECTORY_ENV = "OPENML_REST_API_CONFIG_DIRECTORY"
CONFIG_FILE_ENV = "OPENML_REST_API_CONFIG_FILE"
DOTENV_FILE_ENV = "OPENML_REST_API_DOTENV_FILE"
OPENML_DB_USERNAME_ENV = "OPENML_DATABASES_OPENML_USERNAME"
OPENML_DB_PASSWORD_ENV = "OPENML_DATABASES_OPENML_PASSWORD" # noqa: S105 # not a password
EXPDB_DB_USERNAME_ENV = "OPENML_DATABASES_EXPDB_USERNAME"
EXPDB_DB_PASSWORD_ENV = "OPENML_DATABASES_EXPDB_PASSWORD" # noqa: S105 # not a password
_config_directory = Path(os.getenv(CONFIG_DIRECTORY_ENV, Path(__file__).parent))
_config_directory = _config_directory.expanduser().absolute()
_config_file = Path(os.getenv(CONFIG_FILE_ENV, _config_directory / "config.toml"))
_config_file = _config_file.expanduser().absolute()
_dotenv_file = Path(os.getenv(DOTENV_FILE_ENV, _config_directory / ".env"))
_dotenv_file = _dotenv_file.expanduser().absolute()
logger.info("Configuration directory is '%s'", _config_directory)
logger.info("Loading configuration file from '%s'", _config_file)
logger.info("Loading environment variables from '%s'", _dotenv_file)
load_dotenv(dotenv_path=_dotenv_file)
def _apply_defaults_to_siblings(configuration: TomlTable) -> TomlTable:
defaults = configuration["defaults"]
return {
subtable: (defaults | overrides) if isinstance(overrides, dict) else overrides
for subtable, overrides in configuration.items()
if subtable != "defaults"
}
@functools.cache
def _load_configuration(file: Path) -> TomlTable:
return tomllib.loads(file.read_text())
def load_routing_configuration(file: Path = _config_file) -> TomlTable:
return typing.cast("TomlTable", _load_configuration(file)["routing"])
@functools.cache
def load_database_configuration(file: Path = _config_file) -> TomlTable:
configuration = _load_configuration(file)
database_configuration = _apply_defaults_to_siblings(
configuration["databases"],
)
database_configuration["openml"]["username"] = os.environ.get(
OPENML_DB_USERNAME_ENV,
"root",
)
database_configuration["openml"]["password"] = os.environ.get(
OPENML_DB_PASSWORD_ENV,
"ok",
)
database_configuration["expdb"]["username"] = os.environ.get(
EXPDB_DB_USERNAME_ENV,
"root",
)
database_configuration["expdb"]["password"] = os.environ.get(
EXPDB_DB_PASSWORD_ENV,
"ok",
)
return database_configuration
def load_configuration(file: Path = _config_file) -> TomlTable:
return tomllib.loads(file.read_text())
def load_minio_configuration(file: Path = _config_file) -> TomlTable:
return typing.cast("TomlTable", _load_configuration(file).get("minio", {}))