-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.py
executable file
·54 lines (43 loc) · 1.61 KB
/
cache.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
import json
import os
from scanners.scanner import Apps, App
class Cache:
def __init__(self, cache_directory):
self.cache_directory = cache_directory
self.cache_parts = 10
pass
def load_all(self):
cache = []
for i in range(1, self.cache_parts + 1):
cache.extend(self.load_cache_part(i))
return cache
def save_current_run(self, apps):
# Delete oldest cache part
if os.path.exists(self.path(self.cache_parts)):
os.remove(self.path(self.cache_parts))
# Move each part one to the right
for i in range(self.cache_parts - 1, 0, -1):
print("Moving cache part " + str(i) + " to " + str(i + 1))
if os.path.exists(self.path(i)):
os.replace(self.path(i), self.path(i + 1))
# Add current run
self.save_cache(1, apps)
def path(self, part_id):
return self.cache_directory + '/cache' + str(part_id) + '.json'
def load_cache_part(self, part_id):
apps = []
try:
with open(self.path(part_id)) as f:
for o in Apps(**json.loads(f.read())).apps:
apps.append(App(**o))
print("Loaded cache part " + str(part_id))
return apps
except FileNotFoundError:
return []
except Exception as e:
print("Cache not usable")
print(e)
return apps
def save_cache(self, part_id, apps):
with open(self.path(part_id), mode='w') as f:
f.write(json.dumps(Apps(list(apps)), default=lambda o: o.__dict__, indent=4))