]> git.armaanb.net Git - lightcards.git/blob - lightcards/progress.py
Cache the stack and headers
[lightcards.git] / lightcards / progress.py
1 # Save and resume progress in lightcards
2 # Armaan Bhojwani 2021
3
4 import hashlib
5 import os
6 import pickle
7 import shutil
8
9
10 def gen_hash(inp):
11     hasher = hashlib.md5()
12     hasher.update(inp)
13
14     return(hasher.hexdigest())
15
16
17 def name_gen(stra):
18     return gen_hash(str(stra).encode("utf-8"))
19
20
21 def dump(obj, typer, stra):
22     dired = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/"
23     if not os.path.exists(dired):
24         os.makedirs(dired)
25
26     pickle.dump(obj, open(f"{dired}/{typer}.p", "wb"))
27
28
29 def dive(typer, stra):
30     file = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/" + \
31         f"{typer}.p"
32     if os.path.exists(file):
33         return pickle.load(open(file, "rb"))
34     else:
35         return False
36
37
38 def purge(stra):
39     dired = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/"
40     shutil.rmtree(dired)
41
42 def purge_all():
43     dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
44     shutil.rmtree(dired)
45
46 def main():
47     pass
48
49
50 if __name__ == "__main__":
51     main()