-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtt_cache.py
42 lines (33 loc) · 1.35 KB
/
btt_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
import json
from utils import make_status, State, CACHE_TYPE, debug
from config import WID_PID_DICT, PATH_TO_CACHE_FILE
def write_cache(state: State):
"""Write the current state of each project to the cache file."""
debug("Making cache")
d: CACHE_TYPE = dict()
for wid, pids in WID_PID_DICT.items():
d[wid] = {}
for pid in pids.keys():
d[wid][pid] = make_status(state, False, wid, pid)
if state is not None:
debug("Adding active tags to cache")
d["tags"] = state.get("tags", list())
debug("Tags: %s", str(d['tags']))
debug("Writing to cache")
with open(PATH_TO_CACHE_FILE, "w") as f: json.dump(d, f)
debug("Done writing to cache")
return state
def read_cache(wid: str, pid: str):
"""Read the current style string of a project from the cache file."""
debug("Reading from cache (%s, %s)", wid, pid)
with open(PATH_TO_CACHE_FILE, "r") as f:
out_dict: CACHE_TYPE = json.load(f)
return out_dict[wid][pid]
def read_cache_tag(tag: str):
"""Checking if a tag is in the cache file."""
debug("Looking for %s in cache", tag)
with open(PATH_TO_CACHE_FILE, "r") as f:
out_dict: CACHE_TYPE = json.load(f)
match = tag in out_dict.get("tags", list())
debug("Found %s in cache" if match else "No %s in cache", tag)
return match