-
Notifications
You must be signed in to change notification settings - Fork 0
/
confchecks.py
79 lines (60 loc) · 1.99 KB
/
confchecks.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
"""
Various functions for checking configuration.
"""
import sys
try:
from secrets import secrets
except ImportError:
print(
"WiFi credentials and configuration are kept in secrets.py, please add them there!"
)
raise
def bail(message):
"""
Print message and exit with code 1.
"""
print(message)
sys.exit(1)
def check_string(name, mandatory=True):
"""
Check is string with given name is present in secrets.
"""
value = secrets.get(name)
if value is None and mandatory:
bail(f"{name} is missing")
if value and not isinstance(value, str):
bail(f"not a string value for {name}: {value}")
def check_int(name, mandatory=True, min_val=None, max_val=None):
"""
Check is integer with given name is present in secrets.
"""
value = secrets.get(name)
if value is None and mandatory:
bail(f"{name} is missing")
if value and not isinstance(value, int):
bail(f"not a integer value for {name}: {value}")
if value < min_val or value > max_val:
bail(f"{name} value not within {min_val},{max_val}: {value}")
def check_list(name, subtype, mandatory=True):
"""
Check whether list with given name is present in secrets.
"""
value = secrets.get(name)
if value is None and mandatory:
bail(f"{name} is missing")
if value and not isinstance(value, list):
bail(f"not a integer value for {name}: {value}")
for item in value:
if item and not isinstance(item, subtype):
bail(f"not a {subtype}: {item}")
def check_bytes(name, length, mandatory=True):
"""
Check is bytes with given name is present in secrets.
"""
value = secrets.get(name)
if value is None and mandatory:
bail(f"{name} is missing")
if value and not isinstance(value, bytes):
bail(f"not a byte value for {name}: {value}")
if value and len(value) != length:
bail(f"not correct length for {name}: {len(value)} should be {length}")