]> git.armaanb.net Git - lightcards.git/blob - lightcards/runner.py
Allow for config files to be incomplete
[lightcards.git] / lightcards / runner.py
1 # Markdown flashcard utility
2 # Armaan Bhojwani 2021
3
4 import argparse
5 import curses
6 import pkg_resources
7 from random import shuffle
8 import sys
9
10 from . import parse, progress, config
11 from .display import Display, CursesError
12 from .deck import Status
13
14
15 def parse_args():
16     """Parse command line arguments"""
17     parser = argparse.ArgumentParser(
18         description="Terminal flashcards from Markdown"
19     )
20     parser.add_argument(
21         "-c",
22         "--config",
23         type=str,
24         help="specify custom config file",
25     )
26     parser.add_argument(
27         "-V",
28         "--view",
29         metavar="view",
30         type=int,
31         choices=range(1, 4),
32         default=1,
33         help="specify which view to start in (default = 1)",
34     )
35     parser.add_argument("inp", metavar="input file", type=str, nargs=1)
36     parser.add_argument(
37         "-a",
38         "--alphabetize",
39         action="store_true",
40         help="alphabetize card order",
41     )
42     parser.add_argument(
43         "-p",
44         "--purge",
45         action="store_true",
46         help="don't check cached info before starting",
47     )
48     parser.add_argument(
49         "-r", "--reverse", action="store_true", help="reverse card order"
50     )
51     parser.add_argument(
52         "-s", "--shuffle", action="store_true", help="shuffle card order"
53     )
54     parser.add_argument(
55         "-v",
56         "--version",
57         action="version",
58         version=f"lightcards {pkg_resources.require('lightcards')[0].version}",
59     )
60     return parser.parse_args()
61
62
63 def show(args, stack, headers, conf):
64     """
65     Get objects from cache, manipulate deck according to passed arguments, and
66     send it to the display functions
67     """
68     # Purge caches if asked
69     if args.purge:
70         progress.purge(get_orig()[1])
71
72     # Check for caches
73     idx = Status()
74     cache = progress.dive(get_orig()[1])
75     if cache:
76         (stack) = cache
77
78     # Manipulate deck
79     if args.shuffle:
80         shuffle(stack)
81     if args.alphabetize:
82         stack.sort(key=lambda x: x.front)
83     if args.reverse:
84         stack.reverse()
85
86     # Send to display
87     win = Display(stack, headers, idx, args.view, args, conf)
88     try:
89         curses.wrapper(win.run)
90     except curses.error as e:
91         raise CursesError() from e
92
93
94 def get_orig():
95     """Return original header and stack"""
96     return (headers, stack)
97
98
99 def main(args=sys.argv):
100     args = parse_args()
101     global headers, stack
102     (headers, stack) = parse.parse_html(parse.md2html(args.inp[0]))
103     conf = config.read_file(args.config)
104
105     if not conf["debug"]:
106         sys.tracebacklimit = 0
107
108     show(args, stack, headers, conf)
109
110
111 if __name__ == "__main__":
112     main()