-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_loader.py
45 lines (41 loc) · 1.55 KB
/
config_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
import configparser
import sys
def load(config_file):
"""
Source from my repo: https://github.com/jforjohn/procapi/blob/master/dbod/config.py
Reads configuration file
"""
requiredConfig = {
'cn2': ['dataset',
'output_dir'
#'bins_no',
#'beam_width',
#'min_significance',
#'negate',
#'disjunctive',
#'train_percentage'
],
}
try:
# Load configuration from file
config = configparser.ConfigParser()
number_read_files = config.read(config_file)
# check the config file exist and can be read
if len(number_read_files) != 1:
print("Configuration file '{0}' cannot be read or does not exist. Stopping.".format(args.config))
sys.exit(1)
# Force load of all required fields to avoid errors in runtime
for section, options in requiredConfig.items():
for option in options:
try:
config.get(section, option)
except configparser.NoSectionError:
print("Section '{0}' not present in config file. Stopping.".format(section))
sys.exit(2)
except configparser.NoOptionError:
print("Option '{0}' not present in section {1} in config file. Stopping.".format(option, section))
sys.exit(2)
return config
except IOError as e:
traceback.print_exc(file=sys.stdout)
sys.exit(e.code)