From e99950c3023a64f68cdc968e2759f45653d6b9af Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sun, 31 Jan 2021 22:08:06 -0500 Subject: [PATCH] Add preliminary pickle support --- lightcards/__main__.py | 4 ---- lightcards/display.py | 10 +++++--- lightcards/lightcards.py | 20 ++++++++++++++-- lightcards/progress.py | 52 ++++++++++++++++++++++++++++++++++++++++ man/lightcards.1 | 6 +++++ man/lightcards.1.md | 6 +++++ 6 files changed, 89 insertions(+), 9 deletions(-) delete mode 100644 lightcards/__main__.py create mode 100644 lightcards/progress.py diff --git a/lightcards/__main__.py b/lightcards/__main__.py deleted file mode 100644 index 283493d..0000000 --- a/lightcards/__main__.py +++ /dev/null @@ -1,4 +0,0 @@ -import sys -import lightcards.lightcards - -lightcards.lightcards.main(sys.argv) diff --git a/lightcards/display.py b/lightcards/display.py index 940d15e..1035d76 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -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) diff --git a/lightcards/lightcards.py b/lightcards/lightcards.py index a3aa5cf..d170b35 100644 --- a/lightcards/lightcards.py +++ b/lightcards/lightcards.py @@ -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 index 0000000..f096da4 --- /dev/null +++ b/lightcards/progress.py @@ -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() diff --git a/man/lightcards.1 b/man/lightcards.1 index 1bd1ab9..a83884c 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -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 diff --git a/man/lightcards.1.md b/man/lightcards.1.md index bb95875..b4db85f 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -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 -- 2.39.2