-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.py
42 lines (32 loc) · 855 Bytes
/
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
from yamale import make_schema, make_data, validate, YamaleError
SCHEMA = f"""
exchange:
domain: str()
username: str()
password: str()
server: str()
port: int(required=False)
email: str()
ca_cert: str(required=False)
days_back: int(required=False)
rules: list(include('rule'))
---
rule:
pattern: str()
description: str()
response: enum('ACCEPT', 'MAYBE', 'DECLINE', 'DELETE', 'NOOP', required=False, none='NOOP')
message: str(required=False)
"""
def loads_config(filename: str) -> dict:
try:
schema = make_schema(content=SCHEMA)
data = make_data(filename)
except FileNotFoundError as e:
raise e
try:
validate(schema, data, strict=True, _raise_error=True)
except YamaleError as e:
raise ConfigError(e)
return data.pop()[0]
class ConfigError(Exception):
pass