-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable_loader.py
77 lines (57 loc) · 2.17 KB
/
variable_loader.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
"""
This class is used to load a set of variables to apply to a template
"""
# Built in imports
from copy import deepcopy
# 3rd party imports
import yaml
import yamale
from rich.traceback import install
install(show_locals=False)
class VariableLoader(object):
def __init__(self, var_file, schema=None):
self._data = {}
self.state = "invalid"
self.var_file = var_file
self.schema = schema
def load(self):
data = self.load_from_file(self.var_file)
self._data = yaml.safe_load(data)
self.updated = True
self.validate()
def load_from_file(self, var_file):
return open(var_file, "r").read()
def validate(self):
if not hasattr(self, "schema") or self.schema is None:
self.state = "valid"
return True
compiled_schema = yamale.make_schema(self.schema)
compiled_data = yamale.make_data(content=yaml.dump(self._data))
# TODO: Figure out how we want to handle a failure. Likely a custom
# exception of some sort?
try:
yamale.validate(compiled_schema, compiled_data)
except ValueError:
self.state = "invalid"
return False
else:
self.state = "valid"
return True
def get_data(self):
self.updated = False
# Return a deepcopy of the data so that we keep it clean for other requestors
return deepcopy(dict(self._data))
def __setattr__(self, name, value):
# state, updated, and _ vars get set normally
if name in ('state', 'updated') or name.startswith('_'):
return super(VariableLoader, self).__setattr__(name, value)
# If defining the schema, we'll set the var and run the validation
elif name == 'schema':
ret = super(VariableLoader, self).__setattr__(name, value)
if not hasattr(self, 'var_file') or self.var_file is None:
return ret
return self.validate() and ret
# When setting the var file, we'll automatically load
elif name == 'var_file':
super(VariableLoader, self).__setattr__("var_file", value)
self.load()