-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatconfig.py
187 lines (163 loc) · 5.65 KB
/
catconfig.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from enum import Enum
from typing import Any
from importlib import import_module
class ValidationError(Exception):
"""
Error class for validation failed
"""
def __init__(self, payload: dict):
"""
:param message: error message
"""
self.payload = payload
def generate_err_msg(self, payload: dict, indent: int = 0) -> str:
"""
Generate human-friendly error message
example output:
key1: Error message
key2:
inner_key: error message
inner_key2:
key3: error message
"""
make_indent = ''.join([' ' for i in range(0, indent)])
previous_text = ''
for (key, errors) in payload.items():
for err in errors:
if isinstance(err, dict):
previous_text += '{}{}:\n'.format(make_indent, key)
previous_text += self.generate_err_msg(err, indent+1)
pass
else:
previous_text += '{}{}: {}\n'.format(make_indent, key, err)
pass
return previous_text
@property
def message(self):
return self.generate_err_msg(self.payload)
class CatConfig:
def __init__(self, format: str = 'json', validator_schema: dict = None, data: dict = None):
"""
:param format: Format of data used for read (json/toml/yaml)
:param validator_schema: Schema for validator (see https://docs.python-cerberus.org/en/stable/usage.html)
:param data: Config data
"""
self._parser = None
self._data = {}
if not data == None:
self._data = data
self._validator_schema = validator_schema
if format:
self._import_parser(format)
self._config = {}
def _import_parser(self, parser_name: str):
if parser_name == 'json':
self._parser = import_module('json')
elif parser_name == 'toml':
try:
self._parser = import_module('toml')
except ImportError:
raise Exception(
"CatConfig needs toml parser to work, "
"please add `toml` module to your project")
elif parser_name == 'yaml':
try:
self._parser = import_module('yaml')
# it works! I love Python!
self._parser.loads = self._parser.load
except ImportError:
raise Exception(
"CatConfig needs yaml parser to work, "
"please add `pyyaml` module to your project\n")
else:
raise Exception('Unsupported parser type')
def load_from_file(self, file_path: str, format: 'str' = None) -> None:
"""
Update config from file
:param file_path: config file path
:param format: format of config file (default: json)
"""
with open(file_path, 'r') as f:
self.load_from_string(f.read(), format)
def load_from_string(self, data: str, format: 'str' = None) -> None:
"""
Update config from string and validate
:param data: target data
:param format: format of config file (default: json)
"""
if format:
self._import_parser(format)
return self.load(self._parser.loads(data))
def load(self, data: dict) -> None:
"""
Update config from param `data`
:param data: data
"""
if self._validator_schema:
self.validate(data)
self._data.update(data)
def validate(self, data: str) -> None:
"""
Validate data
:param data: config data
"""
try:
cerberus = import_module('cerberus')
except ImportError:
raise Exception('CatConfig need `cerberus` module to make validation work normally, '
'please add `cerberus` module to your project.')
v = cerberus.Validator(self._validator_schema)
v.validate(data)
if v != True:
raise ValidationError(v.errors)
def update(self, data: dict) -> None:
"""
Update config item
:param data: data to be updated
"""
self._data.update(data)
def set(self, key: str, value: str) -> None:
"""
Set config value
:param key: key of config item
:param value: value of config item
"""
return self.update({key: value})
def get(self, key: str=None) -> Any:
"""
Get item by key
It will return self contained object if param `key` == None
:param key: key
"""
if key == None:
return self._data
if key in self._data:
data = self._data.get(key)
if isinstance(data, dict):
return CatConfig(data=data)
elif isinstance(data, list):
return [CatConfig(data=x) for x in data]
else:
return data
return CatConfig()
def __getitem__(self, key: str) -> Any:
return self.get(key)
def __bool__(self):
"""
Return False if `self._data` has no item
"""
return len(self._data) != 0
def __getattr__(self, name: str) -> Any:
return self.__getitem__(name)
def __eq__(self, b):
"""
Make sure CatConfig object without any data equal False
"""
if b == None:
if len(self._data.keys()) == 0:
return True
return self._data == b
def __str__(self):
if self._data == {}:
return 'None'
return str(self._data)