-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashhandler.py
75 lines (68 loc) · 2.98 KB
/
hashhandler.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
from slingshot import Slingshot
from hashlistener import HashListener
import time
class HashHandler():
@classmethod
def __init__(self):
self.state = {
'run_loop': True,
'local_active_state': HashListener.check_local_active_state(),
'local_save_state': HashListener.check_local_save_state(),
'remote_active_state': HashListener.check_remote_active_state(),
'remote_save_state': HashListener.check_remote_save_state(),
}
if self.is_uninitialized():
self.mirror_active_to_saved_state()
self.watch_loop()
@classmethod
def is_uninitialized(self):
if ( len(str(self.state['local_save_state'])) == 32 and
str(self.state['local_save_state']) != "0" and
len(str(self.state['remote_save_state'])) == 32 and
str(self.state['remote_save_state']) != "0"
):
return False
else:
return True
@classmethod
def mirror_active_to_saved_state(self):
self.state['local_save_state'] = self.state['local_active_state']
self.state['remote_save_state'] = self.state['remote_active_state']
@classmethod
def watch_loop(self):
try:
while True:
if not self.state['run_loop']:
break
else:
new_local_active_state = HashListener.check_local_active_state()
new_remote_active_state = HashListener.check_remote_active_state()
if ( new_local_active_state != self.state['local_active_state'] and
new_remote_active_state == self.state['remote_active_state']
):
print 'local file changed'
Slingshot.push_files()
self.update_state()
elif ( new_remote_active_state != self.state['remote_active_state'] and
new_local_active_state == self.state['local_active_state']
):
print 'remote file changed'
Slingshot.pull_files()
self.update_state()
time.sleep(1)
except KeyboardInterrupt:
self.update_state(True)
Slingshot.stop()
exit()
@classmethod
def update_state(self, quitting_app=False):
self.state['run_loop'] = False
self.state['local_active_state'] = HashListener.check_local_active_state()
self.state['remote_active_state'] = HashListener.check_remote_active_state()
self.mirror_active_to_saved_state()
Slingshot.runtime_settings['localHash'] = self.state['local_save_state']
Slingshot.runtime_settings['remoteHash'] = self.state['remote_save_state']
Slingshot.write_runtime_settings_to_JSON()
self.state['run_loop'] = True
if not quitting_app:
self.watch_loop()