-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
64 lines (48 loc) · 1.79 KB
/
config.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Opens the config.yaml file and parses it into a dict."""
import logging
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
logger = logging.getLogger(__name__)
CONFIG_FILE_PATH = "config.yaml"
def open_config() -> dict:
"""Open the config file and parse the yaml contents."""
try:
with open(CONFIG_FILE_PATH, encoding="utf-8") as config_file:
try:
return yaml.load(config_file, Loader=Loader)
except Exception:
logger.error(f"Error: Failed to parse config file {CONFIG_FILE_PATH}")
logger.exception()
return {}
except Exception:
logger.info(f"Didn't find config file {CONFIG_FILE_PATH}, using default values for run.")
return {}
_route_config: dict[str, bool] = {}
def open_route_config(path: str) -> bool:
"""
Open a route config file and parse the yaml contents.
Assign the global `_route_config` and return true if everything worked.
"""
global _route_config
try:
with open(path, encoding="utf-8") as route_config:
try:
_route_config = yaml.load(route_config, Loader=Loader)
return True
except Exception:
logger.error(f"Error: Failed to parse route config file {path}")
logger.exception()
return False
except Exception:
logger.warning(f"Didn't find route config file {path}")
return False
def get_route_config() -> dict[str, bool]:
"""Return a handle to the global `_route_config`."""
return _route_config
def set_route_config(route_config: dict[str, bool]) -> None:
"""Assign the global `_route_config`."""
global _route_config
_route_config = route_config