]> git.armaanb.net Git - lightcards.git/commitdiff
Add preliminary pickle support
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Feb 2021 03:08:06 +0000 (22:08 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Feb 2021 03:08:06 +0000 (22:08 -0500)
lightcards/__main__.py [deleted file]
lightcards/display.py
lightcards/lightcards.py
lightcards/progress.py [new file with mode: 0644]
man/lightcards.1
man/lightcards.1.md

diff --git a/lightcards/__main__.py b/lightcards/__main__.py
deleted file mode 100644 (file)
index 283493d..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-import sys
-import lightcards.lightcards
-
-lightcards.lightcards.main(sys.argv)
index 940d15efadae8e254c3e14a722ed0dd85a075ecd..1035d76eb337fb81a6af3957c0c586c4090575b3 100644 (file)
@@ -6,7 +6,7 @@ from random import shuffle
 import sys
 import textwrap
 
-from . import lightcards
+from . import lightcards, progress
 
 
 class Display():
@@ -23,6 +23,10 @@ class Display():
         curses.init_pair(3, curses.COLOR_YELLOW, 0)
         self.get_key()
 
+    def leave(self):
+        progress.dump(self.obj, "status", self.stack)
+        sys.exit(0)
+
     def disp_bar(self):
         """
         Display the statusbar at the bottom of the screen with progress, star
@@ -73,7 +77,7 @@ class Display():
             key = self.win.getkey()
             if key == "q":
                 if len(self.stack) == self.obj.getIdx():
-                    sys.exit(0)
+                    self.leave()
                 elif len(self.stack) < self.obj.getIdx():
                     self.obj.setIdx(0)
                 self.get_key()
@@ -226,7 +230,7 @@ class Display():
         while True:
             key = self.win.getkey()
             if key == "q":
-                sys.exit(0)
+                self.leave()
             elif key in ["l", "KEY_RIGHT"]:
                 self.obj.forward(self.stack)
                 self.obj.setSide(0)
index a3aa5cff0a1529cecfc7b8ccbc1a654b32dec29e..d170b354a048f43fc9bd0fd24afe169172d27d6d 100644 (file)
@@ -7,7 +7,7 @@ import os
 from random import shuffle
 import sys
 
-from . import parse
+from . import parse, progress
 from .display import Display
 from .deck import Status
 
@@ -21,6 +21,12 @@ def parse_args():
     parser.add_argument("-f", "--flip",
                         action='store_true',
                         help="show second column first")
+    parser.add_argument("-p", "--purge",
+                        action='store_true',
+                        help="don't check cached info before starting")
+    parser.add_argument("-P", "--purge-all",
+                        action='store_true',
+                        help="don't check cached info before starting")
     parser.add_argument("-r", "--reverse",
                         action='store_true',
                         help="reverse card order")
@@ -38,7 +44,17 @@ def show(args, stack, headers):
     Manipulate deck according to passed arguments, and send it to the display
     functions
     """
-    idx = Status()
+    if args.purge:
+        progress.purge(stack)
+    if args.purge_all:
+        progress.purge_all()
+
+    ida = progress.dive("status", stack)
+    if ida and not args.purge:
+        idx = ida
+    else:
+        idx = Status()
+
     if args.flip:
         for x in stack:
             x[0], x[1] = x[1], x[0]
diff --git a/lightcards/progress.py b/lightcards/progress.py
new file mode 100644 (file)
index 0000000..f096da4
--- /dev/null
@@ -0,0 +1,52 @@
+# Save and resume progress in lightcards
+# Armaan Bhojwani 2021
+
+import hashlib
+import os
+import pickle
+import shutil
+
+
+def gen_hash(inp):
+    hasher = hashlib.md5()
+    hasher.update(inp)
+
+    return(hasher.hexdigest())
+
+
+def name_gen(stra):
+    return gen_hash(str(stra).encode("utf-8"))
+
+
+def dump(obj, typer, stra):
+    dired = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/"
+    if os.path.exists(dired):
+        shutil.rmtree(dired)
+    os.makedirs(dired)
+
+    pickle.dump(obj, open(f"{dired}/{typer}.p", "wb"))
+
+
+def dive(typer, stra):
+    file = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/" + \
+        f"{typer}.p"
+    if os.path.exists(file):
+        return pickle.load(open(file, "rb"))
+    else:
+        return False
+
+
+def purge(stra):
+    dired = f"{os.path.expanduser('~')}/.cache/lightcards/{name_gen(stra)}/"
+    shutil.rmtree(dired)
+
+def purge_all():
+    dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
+    shutil.rmtree(dired)
+
+def main():
+    pass
+
+
+if __name__ == "__main__":
+    main()
index 1bd1ab9392f672c20ac766dfcbfd26d98c699917..a83884c7a6926b48b5196f83f386c0b6f051c2e4 100644 (file)
@@ -25,6 +25,12 @@ Show a help message and exit
 \f[B]-f\f[R], \f[B]\[en]flip\f[R]
 Show second column first
 .TP
+\f[B]-p\f[R], \f[B]\[en]purge\f[R]
+Purge cache for chosen set
+.TP
+\f[B]-P\f[R], \f[B]\[en]purge-all\f[R]
+Purge all caches
+.TP
 \f[B]-r\f[R], \f[B]\[en]reverse\f[R]
 Reverse card order.
 Has no effect when shuffling as well
index bb95875b7e04c595315df11e2410801ffa3cda8a..b4db85f7aa8f7dc123a463e62ee6ddf3c1c3d82a 100644 (file)
@@ -22,6 +22,12 @@ lightcards [[options]] [input file]
 **-f**, **--flip**
 : Show second column first
 
+**-p**, **--purge**
+: Purge cache for chosen set
+
+**-P**, **--purge-all**
+: Purge all caches
+
 **-r**, **--reverse**
 : Reverse card order. Has no effect when shuffling as well