From 79afb3261a260272d0c3975eac67872a1c0b25df Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 13 Feb 2021 12:26:06 -0500 Subject: [PATCH] Allow for config files to be incomplete --- config.py | 40 +++++++++++++++++++++++++++++++++++++--- lightcards/config.py | 32 ++++++++++++++++---------------- lightcards/display.py | 6 +++--- lightcards/runner.py | 15 +++++++++------ 4 files changed, 65 insertions(+), 28 deletions(-) diff --git a/config.py b/config.py index 64296bb..46d6bf0 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,32 @@ -############### -# KEYBINDINGS # -############### +############################################################################### +# LIGHTCARDS DEFAULT CONFIG FILE # +############################################################################### + +############################################################################### +# STARTUP OPTIONS + +alphabetize = False +shuffle = False +reverse = False +cache = True + +default_view = 1 + +############################################################################### +# APPEARANCE + +disp_progress = True +progress_char = "»" +disp_sidebar = True +disp_bar = True + +bar_sections = ["starred_status", "starred_count", "side", "view"] + +highlight_color = "green" +starred_color = "yellow" + +############################################################################### +# KEYBINDINGS card_prev = ["h", "KEY_LEFT"] card_next = ["l", "KEY_RIGHT"] @@ -16,3 +42,11 @@ menu_disp = "m" view_one = "1" view_two = "2" view_three = "3" + +############################################################################### +# OTHER + +confirm_quit = True +debug = False + +############################################################################### diff --git a/lightcards/config.py b/lightcards/config.py index 56f7261..3c99dc2 100644 --- a/lightcards/config.py +++ b/lightcards/config.py @@ -13,8 +13,10 @@ class ConfigException(BaseException): sys.exit(4) -def find_file(file): +def read_file(file): + config = {} file = str(file) + files = [] local_xdg = f"{os.path.expanduser('~')}/{os.environ.get('XDG_CACHE_HOME')}/lightcards/config.py" local = f"{os.path.expanduser('~')}/.config/lightcards/config.py" world = "/etc/lightcards/config.py" @@ -23,22 +25,20 @@ def find_file(file): f"{os.path.expanduser('~')}/.local/share/doc/lightcards/config.py" ) + if os.path.exists(world_default): + files.append(world_default) + if os.path.exists(local_default): + files.append(local_default) + if os.path.exists(world): + files.append(world) + if os.path.exists(local_xdg): + files.append(local_xdg) + if os.path.exists(local): + files.append(local) if os.path.exists(file): - return file - elif os.path.exists(local_xdg): - return local_xdg - elif os.path.exists(local): - return local - elif os.path.exists(world): - return world - elif os.path.exists(world_default): - return world_default - elif os.path.exists(local_default): - return local_default - + files.append(file) -def read_file(file): - config = {} - exec(Path(str(find_file(file))).read_text(), {}, config) + for f in files: + exec(Path(str(f)).read_text(), {}, config) return config diff --git a/lightcards/display.py b/lightcards/display.py index 8023ff3..cb52c8d 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -9,7 +9,7 @@ import sys import textwrap import time -from . import runner, progress, parse, config +from . import runner, progress, parse def panel_create(x, y): @@ -231,13 +231,13 @@ class Menu: class Display: - def __init__(self, stack, headers, obj, view, args): + def __init__(self, stack, headers, obj, view, args, conf): self.stack = stack self.headers = headers self.obj = obj self.view = view self.input_file = args.inp[0] - self.config = config.read_file(args.config) + self.config = conf def run(self, stdscr): """Set important options that require stdscr before starting""" diff --git a/lightcards/runner.py b/lightcards/runner.py index 6cb87d4..15a6229 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -3,12 +3,11 @@ import argparse import curses -import os import pkg_resources from random import shuffle import sys -from . import parse, progress +from . import parse, progress, config from .display import Display, CursesError from .deck import Status @@ -61,7 +60,7 @@ def parse_args(): return parser.parse_args() -def show(args, stack, headers, input_file): +def show(args, stack, headers, conf): """ Get objects from cache, manipulate deck according to passed arguments, and send it to the display functions @@ -85,7 +84,7 @@ def show(args, stack, headers, input_file): stack.reverse() # Send to display - win = Display(stack, headers, idx, args.view, input_file) + win = Display(stack, headers, idx, args.view, args, conf) try: curses.wrapper(win.run) except curses.error as e: @@ -98,11 +97,15 @@ def get_orig(): def main(args=sys.argv): - sys.tracebacklimit = 0 args = parse_args() global headers, stack (headers, stack) = parse.parse_html(parse.md2html(args.inp[0])) - show(args, stack, headers, args) + conf = config.read_file(args.config) + + if not conf["debug"]: + sys.tracebacklimit = 0 + + show(args, stack, headers, conf) if __name__ == "__main__": -- 2.39.2