-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconfig.py
129 lines (106 loc) · 3.5 KB
/
config.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
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
124
125
126
127
128
129
import os
class Settings:
def __init__(self, path: str, settings_dict: dict, dtype_dict: dict):
self.path = path
self.settings_dict = settings_dict
self.dtype_dict = dtype_dict
if not os.path.exists(self.path):
self.write()
def read(self):
with open(self.path, 'r') as f:
lines = f.readlines()
temp_dict = {}
for line in lines:
line = line.strip()
# skip comments
if line[0] == '#':
continue
if ':' not in line:
continue
line_split = line.split(':', 1)
key = line_split[0]
value = line_split[1]
if key not in self.settings_dict.keys():
continue
# convert to the proper data type
dtype = self.dtype_dict[key]
value = dtype(value)
# add to the dict
temp_dict[key] = value
# check if the number of read settings equals the number of settings
# that should be in the dict
if len(temp_dict.keys()) == len(self.settings_dict.keys()):
ret = True
self.settings_dict = temp_dict
self.print_values()
else:
ret = False
return ret
def get_value(self, key):
return self.settings_dict[key]
def update_value(self, key, value):
"""
for updating a value during runtime
"""
self.settings_dict[key] = value
def write(self):
"""
writes self.settings_dict as a txt file
"""
str_to_write = '# Settings for the Flight Controller'
for key, value in self.settings_dict.items():
line_to_add = f'\n{key}:{value}'
str_to_write += line_to_add
with open(self.path, 'w') as f:
f.write(str_to_write)
def print_values(self):
"""
prints all values of self.settings_dict
"""
print('-----SETTINGS-----')
for key, value in self.settings_dict.items():
print(f'{key}: {value} {type(value)}')
print('------------------')
if __name__ == '__main__':
path = 'test_settings.txt'
settings_dict = {
'ail_trim': 0,
'elev_trim': 0,
'ail_kp': .001,
'elev_kp': .0025,
'source': '0',
'fps': 30,
'inf_res': '(100,100)',
'resolution': '(640,480)',
'acceptable_variance': 1.3
}
dtype_dict = {
'ail_trim': float,
'elev_trim': float,
'ail_kp': float,
'elev_kp': float,
'source': str,
'fps': int,
'inf_res': eval,
'resolution': eval,
'acceptable_variance': float
}
settings = Settings(path, settings_dict, dtype_dict)
ret = settings.read()
print(f'ret: {ret}')
if ret:
settings.print_values()
print('----------')
print('Getting a value...')
ail_trim = settings.get_value('ail_trim')
print(f'ail_trim: {ail_trim}')
print('----------')
print('Updating a value...')
settings.update_value('ail_trim', .09999)
print('----------')
print('Getting value again...')
ail_trim = settings.get_value('ail_trim')
print(f'ail_trim: {ail_trim}')
print('----------')
print('Writing values...')
settings.write()