-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
123 lines (103 loc) · 2.96 KB
/
config.py
File metadata and controls
123 lines (103 loc) · 2.96 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
'''
config.py:
Config R/W Utility
format:
[device:type_num, ]
type:type_num
'NOT_SET':0,
'DISABLED':1,
'LECTURE(w/ AUDIO)':2,
'WHITEBOARD':3,
'BLACKBOARD':4,
'COMPUTER':5,
'''
import json
import os
import utils
CONFSRC = "conf.json"
def load_all_config():
'''
Load conf from @CONFSRC and return dict
return None if no conf file is found
return None if no conf entries is found
'''
os.chdir(os.path.dirname(__file__))
utils.log('INFO', 'Loading all Conf...')
j_file = None
try:
j_file = open(CONFSRC)
except IOError:
utils.log('WARN', 'No config file found.')
return None
j_str = j_file.read()
j_data = json.loads(j_str)
# Check if entries are empty
if len(j_data) < 1:
utils.log('WARN', 'Conf file contents no configs.')
return None
j_dict = j_data
return j_dict
def is_config_valid():
'''Check if not all NOT_SET or out-dated'''
os.chdir(os.path.dirname(__file__))
utils.log('INFO', 'Checking Config')
j_file = None
try:
j_file = open(CONFSRC)
except IOError:
utils.log('WARN', 'No config file found.')
return None
j_str = j_file.read()
j_data = json.loads(j_str)
# Check if entries are empty
if len(j_data) < 1:
utils.log('WARN', 'Conf file contents no configs.')
return None
j_dict = j_data
dev_list = os.listdir('/dev')
video_devices = [s for s in dev_list if "video" in s]
# Check if device count matches config file count
if len(video_devices) != len(j_dict.items()):
utils.log('INFO', 'Config validation is outdated.')
return False
ret = False
for entry in j_dict.values():
if entry is not '0':
ret = True
utils.log('INFO', 'Config validation returns ' + str(ret))
return ret
def load_config(key):
'''
Load conf from @CONFSRC and return corresponding val of @key
return None if no conf file is found
return None if no conf entries is found
'''
os.chdir(os.path.dirname(__file__))
utils.log('INFO', 'Loading Conf...')
j_file = None
try:
j_file = open(CONFSRC)
except IOError:
utils.log('WARN', 'No config file found.')
return None
j_str = j_file.read()
j_data = json.loads(j_str)
# Check if entries are empty
if len(j_data) < 1:
utils.log('WARN', 'Conf file contents no configs.')
return None
j_dict = j_data
return j_dict.get(key)
def add_config(jdict):
'''Add entries in @jdict to @CONFSRC'''
os.chdir(os.path.dirname(__file__))
with open(CONFSRC, 'w+') as f:
j_data = json.load(f)
j_data.update(jdict)
json.dump(j_data, f)
def write_config(jdict):
'''Write entries in @jdict to @CONFSRC'''
os.chdir(os.path.dirname(__file__))
with open(CONFSRC, 'w+') as f:
json.dump(jdict, f)