]> git.armaanb.net Git - lightcards.git/blob - lightcards/progress.py
Add preliminary pickle support
[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 os.path.exists(dired):
24         shutil.rmtree(dired)
25     os.makedirs(dired)
26
27     pickle.dump(obj, open(f"{dired}/{typer}.p", "wb"))
28
29
30 def dive(typer, stra):
31     file = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/" + \
32         f"{typer}.p"
33     if os.path.exists(file):
34         return pickle.load(open(file, "rb"))
35     else:
36         return False
37
38
39 def purge(stra):
40     dired = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/"
41     shutil.rmtree(dired)
42
43 def purge_all():
44     dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
45     shutil.rmtree(dired)
46
47 def main():
48     pass
49
50
51 if __name__ == "__main__":
52     main()