-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_klipper_config.py
57 lines (47 loc) · 1.45 KB
/
diff_klipper_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
CONFIG_LEFT = ('.', 'printer.cfg')
CONFIG_RIGHT = ('.', 'printer.cfg.old')
import os
import webbrowser
import difflib as dl
def get_config_raw(path, filename):
f = open(os.path.join(path, filename)).readlines()
f = [x.rstrip() for x in f]
f = [x for x in f if not x.startswith('#')]
f = [x.rstrip() for x in f]
f = [x for x in f if x]
return f
def get_config(path, filename, included_from='', result=None):
if result is None:
result = {}
f = get_config_raw(path, filename)
key = None
for x in f:
if x.startswith('[include '):
result = get_config(path, x[9:-1], x[9:-1], result)
elif x.startswith('['):
key = x
if included_from:
key = f'{key} -> {included_from}'
while key in result:
key += '_'
result[key] = []
else:
result[key].append(x)
return result
def config_as_str(config):
result = ""
for k in sorted(config):
result += k + '\n'
result += '\n'.join(config[k]) + '\n'
result += '\n'
return result
def open_in_browser(diff):
html_file = '/tmp/klipper_config_diff.html'
f = open(html_file,'w')
f.write(diff)
f.close()
webbrowser.open(f'file://{html_file}')
c1 = config_as_str(get_config(*CONFIG_LEFT))
c2 = config_as_str(get_config(*CONFIG_RIGHT))
diff = dl.HtmlDiff().make_file(c1.split('\n'), c2.split('\n'))
open_in_browser(diff)