File size: 1,295 Bytes
f16bb9f
 
 
 
 
 
 
1688157
f16bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1688157
 
f16bb9f
 
 
 
 
 
 
1688157
 
f16bb9f
 
 
 
 
 
 
 
 
 
 
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
"""
File: config.py
Author: Dmitry Ryumin, Maxim Markitantov, Elena Ryumina, Anastasia Dvoynikova, and Alexey Karpov
Description: Configuration module for handling settings.
License: MIT License
"""

import toml
from pathlib import Path
from collections.abc import Callable
from types import SimpleNamespace

CONFIG_NAME = "config.toml"


def flatten_dict(prefix: str, d: dict) -> dict:
    result = {}

    for k, v in d.items():
        result.update(
            flatten_dict(f"{prefix}{k}_", v)
            if isinstance(v, dict)
            else {f"{prefix}{k}": v}
        )

    return result


def load_tab_creators(
    file_path: str, available_functions: dict[str, Callable]
) -> dict[str, Callable]:
    with open(file_path, "r") as f:
        config = toml.load(f)

    tab_creators_data = config.get("TabCreators", {})

    return {key: available_functions[value] for key, value in tab_creators_data.items()}


def load_config(file_path: str) -> SimpleNamespace:
    with open(file_path, "r") as f:
        config = toml.load(f)

    config_data = flatten_dict("", config)

    config_namespace = SimpleNamespace(**config_data)

    setattr(config_namespace, "Path_APP", Path(__file__).parent.parent.resolve())

    return config_namespace


config_data = load_config(CONFIG_NAME)