forked from jeremydhoon/tfutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadconfig.py
48 lines (40 loc) · 1.39 KB
/
loadconfig.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
#!/usr/bin/env python
"""
loadconfig.py -- manages assignment configuration options
"""
try:
import json
except ImportError:
import simplejson as json
import os
from os import path
REQUIRED_FIELDS = ("title", "testmodule", "workmodule", "taskmodule")
DEFAULT_FIELDS = {"subtitle":"", "assignment_number": None,
"course_name": None}
DEFAULT_CONFIG_NAME = "config.js"
def load_config_file(sDir,sConfigFile=DEFAULT_CONFIG_NAME):
if sConfigFile not in set(os.listdir(sDir)):
raise ValueError("%s not found in %s, could not load config file."
% (sConfigFile, sDir))
dictConfig = dict(DEFAULT_FIELDS)
try:
infile = open(path.join(sDir,sConfigFile))
dictConfig.update(json.load(infile))
finally:
infile.close()
for sRequired in REQUIRED_FIELDS:
if sRequired not in dictConfig:
raise ValueError("Required field '%s' not found in %s."
% (sRequired, sConfigFile))
dictConfig["config_filename"] = sConfigFile
return dictConfig
def get_config_dir(sConfigFile=DEFAULT_CONFIG_NAME):
sPath = path.dirname(path.abspath(__file__))
while sPath.strip('/') and sConfigFile not in os.listdir(sPath):
sPath = path.dirname(sPath)
if not sPath.strip('/'):
return None
return sPath
__all__ = [load_config_file]
if __name__ == "__main__":
pass