-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.py
34 lines (26 loc) · 1.08 KB
/
watcher.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
import pickle, os, time
from datetime import datetime
fname = 'settings.pkl'
START = 0
old_dict = {}
while True:
lastmodified = os.stat(fname).st_mtime
if lastmodified-START > 1:
print(f'Change at {datetime.fromtimestamp(lastmodified)}')
with open(fname, 'rb') as inFile:
new_dict = pickle.load(inFile)
print(f'\n\n\n\n\n{"="*15}\nChanges Identifed:')
cleaner = lambda x : [] if x == set() else x
shared = cleaner( set(new_dict) and set(old_dict) )
additions = cleaner( set(new_dict) - set(old_dict) )
deletions = cleaner( set(old_dict) - set(new_dict) )
updates = {k: {'New': new_dict[k], 'Old': old_dict[k]} for k in shared if new_dict[k] != old_dict[k]}
adds = {k:new_dict[k] for k in additions}
subs = {k:new_dict[k] for k in deletions}
print(f'Updates Identified: {updates}\n\n')
print(f'Additions Identified: {adds}\n\n')
print(f'Deletions Identified: {subs}\n\n')
print('='*15)
old_dict = new_dict
time.sleep(5)
START = lastmodified