Repository navigation
Expand file tree
/
Copy pathload.py
More file actions
125 lines (101 loc) · 3.47 KB
/
Copy pathload.py
File metadata and controls
125 lines (101 loc) · 3.47 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from __future__ import annotations
import json
import os
import typing
from . import exceptions
from . import overrides
from . import validation
if typing.TYPE_CHECKING:
from typing_extensions import Literal, Any
from .types import RpcConfig
def read_config_data() -> RpcConfig:
"""read MESC config according to MESC environment variables"""
# locate and load config using env vars
mode = os.environ.get('MESC_MODE')
if mode == 'DISABLED':
raise exceptions.MescDisabled(
'MESC_MODE=DISABLED, check with is_mesc_enabled()'
)
elif mode == 'PATH':
config = read_file_config(validate=False)
elif mode == 'ENV':
config = read_env_config(validate=False)
elif mode not in ['', None]:
raise Exception(
'invalid mode: ' + str(mode) + ', must be PATH, ENV, or DISABLED'
)
elif os.environ.get('MESC_PATH') not in ['', None]:
config = read_file_config(validate=False)
elif os.environ.get('MESC_ENV') not in ['', None]:
config = read_env_config(validate=False)
else:
raise exceptions.MescDisabled('To enable MESC, set MESC_PATH or MESC_ENV')
# apply overrides
config = overrides.apply_env_overrides(config)
# validate
validation.validate(config)
return config # type: ignore
@typing.overload
def read_env_config(*, validate: Literal[False]) -> Any:
...
@typing.overload
def read_env_config(*, validate: Literal[True]) -> RpcConfig:
...
def read_env_config(*, validate: bool = True) -> RpcConfig | Any:
"""read config from MESC_ENV environment variable"""
# obtain raw config data from env
value = os.environ.get('MESC_ENV')
if value is None or value == '':
raise exceptions.LoadError(
'Cannot load MESC config from MESC_ENV, value is not set'
)
# parse config data as JSON
try:
config = json.loads(value)
except json.JSONDecodeError:
raise exceptions.InvalidConfig('MESC_ENV is not formatted as valid JSON')
# validate config
if validate:
validation.validate(config)
return config
@typing.overload
def read_file_config(*, path: str | None = None, validate: Literal[False]) -> Any:
...
@typing.overload
def read_file_config(*, path: str | None = None, validate: Literal[True]) -> RpcConfig:
...
def read_file_config(
*, path: str | None = None, validate: bool = True
) -> RpcConfig | Any:
"""read config from MESC config file"""
# obtain config file path
if path is None:
path = os.environ.get('MESC_PATH')
if path is None or path == '':
raise exceptions.LoadError(
'Cannot load MESC config from MESC_ENV, value is not set'
)
path = os.path.expanduser(path)
# load file contents
try:
with open(path, 'r') as f:
content = f.read()
except FileNotFoundError:
raise exceptions.LoadError('File does not exist: ' + str(path))
except PermissionError:
raise exceptions.LoadError(
'Insufficient permissions to load file: ' + str(path)
)
except Exception:
raise exceptions.LoadError('Unable to load file: ' + str(path))
# parse raw data as json
try:
config = json.loads(content)
except json.JSONDecodeError:
raise exceptions.InvalidConfig(
'file at MESC_PATH is not formatted as valid JSON'
)
# validate config
if validate:
validation.validate(config)
return config