-
Notifications
You must be signed in to change notification settings - Fork 118
/
process.py
87 lines (65 loc) · 2.26 KB
/
process.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
import time
import schedule
import threading
from cache import Cache
from config import config
from onedrive import OneDrive
from utils import path_format
od = OneDrive()
class Process:
tasks = []
@staticmethod
def runner():
while True:
schedule.run_pending()
time.sleep(1)
@staticmethod
def refresh_token():
od.get_access()
od.get_resource()
od.get_access(od.resource_id)
@classmethod
def refresh_difference(cls):
cls.tasks.append({'full_path': config.start_directory})
@classmethod
def worker(cls):
while True:
if len(cls.tasks) < 1:
time.sleep(.1)
continue
c = cls.tasks.pop(0)
info = od.list_items_with_cache(c['full_path'], True)
for f in info.files:
p = f['full_path']
if not Cache.has(p):
continue
file = Cache.get(p).files[0]
if file['hash'] != f['hash']:
print('expired file: %s' % p)
Cache.rem(p)
for f in info.folders:
p = f['full_path']
if not Cache.has(p):
print('no cached: %s' % p)
new = od.list_items_with_cache(p, True)
cls.cache_all(new)
cls.tasks += new.folders[1:]
continue
folder = Cache.get(p).folders[0]
if folder['hash'] != f['hash']:
print('expired folder: %s' % p)
new = od.list_items_with_cache(p, True)
cls.cache_all(new)
cls.tasks += new.folders[1:]
@staticmethod
def cache_all(info):
for f in info.folders:
Cache.set(f['full_path'], od.list_items_with_cache(
f['full_path'], True), config.structure_cached_seconds)
Process.refresh_token()
Process.refresh_difference()
schedule.every(config.refresh_seconds).seconds.do(Process.refresh_token)
schedule.every(config.diff_seconds).seconds.do(Process.refresh_difference)
threading.Thread(target=Process.runner).start()
for _ in range(config.threads):
threading.Thread(target=Process.worker).start()