-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmatrixlabs_edit_settings.py
executable file
·56 lines (45 loc) · 1.62 KB
/
matrixlabs_edit_settings.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
#!/usr/bin/python2
' Modify Raspberry Pi settins '
import sys
# BUGS: We don't uncomment lines and instead add a new line with the
# required value.
def read_lines(file_name):
' Parse config file '
ret = []
with open(file_name) as input_file:
line = input_file.readline()
while len(line):
line = line.strip()
if line.startswith('#') or line == '':
ret.append(['REMARK', line])
else:
ret.append(['SETTING', line])
line = input_file.readline()
return ret
def key_and_value(line):
' Get the key and the value of a setting '
assert line[0] == 'SETTING'
splitted = line[1].split('=')
return '='.join(splitted[:-1]), splitted[-1]
ORIGINAL_LINES = read_lines(sys.argv[1])
KEYS_TO_CHANGE = {}
for new_line in read_lines(sys.argv[2]):
# We throw away comments in the modifications. It's not wasy to place them!
if new_line[0] == 'SETTING':
key, value = key_and_value(new_line)
KEYS_TO_CHANGE[key] = value
for original in ORIGINAL_LINES:
if original[0] == 'REMARK':
print original[1]
else:
key, value = key_and_value(original)
if key in KEYS_TO_CHANGE and KEYS_TO_CHANGE[key] == value:
print '{}={}'.format(key, KEYS_TO_CHANGE[key])
del KEYS_TO_CHANGE[key]
else:
print original[1]
if len(KEYS_TO_CHANGE):
print '# Lines added by matrixlabs_edit_settings.py.'
print '# Commented definitions of the settings might be above.'
for key in sorted(KEYS_TO_CHANGE):
print '{}={}'.format(key, KEYS_TO_CHANGE[key])