]> git.armaanb.net Git - lightcards.git/blobdiff - lightcards/progress.py
Add menu keybindings to config
[lightcards.git] / lightcards / progress.py
index 665ae315485cba08b742073af68e188608f031d3..a4fdc43b267b687e984210b1e7670c4f2dfacf23 100644 (file)
@@ -4,29 +4,34 @@
 import hashlib
 import os
 import pickle
-import shutil
 
-global dired
-dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
+xdg = os.environ.get("XDG_CACHE_HOME")
+if xdg:
+    dired = f"{os.path.expanduser('~')}/{xdg}/lightcards/"
+else:
+    dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
+
 
 def name_gen(stra):
-    hasher = hashlib.md5()
-    hasher.update(str(stra).encode("utf-8"))
-    return(hasher.hexdigest())
+    """Generate hash of stack for name of pickle file"""
+    return hashlib.md5(str([str(x) for x in stra]).encode("utf-8")).hexdigest()
 
 
 def make_dirs(dired):
+    """mkdir -p equivalent"""
     if not os.path.exists(dired):
         os.makedirs(dired)
 
 
 def dump(obj, stra):
+    """Write pickle file"""
     make_dirs(dired)
 
     pickle.dump(obj, open(f"{dired}/{name_gen(stra)}.p", "wb"))
 
 
 def dive(stra):
+    """Get pickle file"""
     file = f"{dired}/{name_gen(stra)}.p"
     make_dirs(dired)
     if os.path.exists(file):
@@ -36,15 +41,7 @@ def dive(stra):
 
 
 def purge(stra):
-    file = f"{dired}/{name_gen(stra)}/"
+    """Delete pickle file"""
+    file = f"{dired}/{name_gen(stra)}.p"
     if os.path.exists(file):
-        shutil.rmtree(file)
-
-
-def purge_all():
-    if os.path.exists(dired):
-        shutil.rmtree(dired)
-
-
-if __name__ == "__main__":
-    main()
+        os.remove(file)