-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCONST_abated.py
More file actions
32 lines (25 loc) · 1.06 KB
/
CONST_abated.py
File metadata and controls
32 lines (25 loc) · 1.06 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
import logging
# Whoops. Too lax(ative) with the syntax.
class CONSTAbatedException(Exception):
UNEXPECTED_OBJECT_TYPE_GOT_S = 'Unexpected object type. Need dictionary, got %s.'
def __populate_object(const_object, values):
if isinstance(values, dict):
for var, val in values.items():
logging.debug('setting variable %s' % var)
setattr(const_object, var, val)
else:
raise CONSTAbatedException(CONSTAbatedException.UNEXPECTED_OBJECT_TYPE_GOT_S % type(values))
def load_from_yaml(yaml_configuration_file, const_object):
import yaml
try:
fin = open(yaml_configuration_file, 'r')
raw_yaml = fin.read()
fin.close()
__populate_object(const_object, yaml.load(raw_yaml, Loader=yaml.SafeLoader))
except IOError as e:
logging.error('Could not open config file %s. Bailing out.' % yaml_configuration_file)
raise e
except yaml.scanner.ScannerError as e:
logging.error('Syntax error in %s. Bailing out.' % yaml_configuration_file)
raise e
# TODO: load_from_json