From d2ff903deb8df5492385de187bb5e409cc71a058 Mon Sep 17 00:00:00 2001 From: Pokey Rule Date: Fri, 10 Dec 2021 21:01:28 +0000 Subject: [PATCH] Settings file robustness (#128) * Fall back to full hat enablement on error * Refactor * Black --- src/marks/mark.py | 34 +++++++++++++++++++++++++++++++--- src/marks/vscode_settings.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/marks/mark.py b/src/marks/mark.py index 2963440d..f26a380f 100644 --- a/src/marks/mark.py +++ b/src/marks/mark.py @@ -1,9 +1,10 @@ from dataclasses import dataclass from pathlib import Path -from ..conventions import get_cursorless_list_name +from typing import Any from talon import Module, actions, app, Context, fs, cron from ..csv_overrides import init_csv_and_watch_changes from .lines_number import DEFAULT_DIRECTIONS +from .vscode_settings import vscode_get_setting_with_fallback mod = Module() ctx = Context() @@ -121,6 +122,23 @@ def cursorless_mark(m) -> str: "crosshairs": False, } +# Fall back to full enablement in case of error reading settings file +# NB: This won't actually enable all the shapes and colors extension-side. +# It'll just make it so that the user can say them whether or not they are enabled +FALLBACK_SHAPE_ENABLEMENT = { + "ex": True, + "fox": True, + "wing": True, + "hole": True, + "frame": True, + "curve": True, + "eye": True, + "play": True, + "bolt": True, + "crosshairs": True, +} +FALLBACK_COLOR_ENABLEMENT = DEFAULT_COLOR_ENABLEMENT + unsubscribe_hat_styles = None @@ -129,11 +147,21 @@ def setup_hat_styles_csv(): color_enablement = { **DEFAULT_COLOR_ENABLEMENT, - **actions.user.vscode_get_setting("cursorless.hatEnablement.colors", {}), + **vscode_get_setting_with_fallback( + "cursorless.hatEnablement.colors", + default_value={}, + fallback_value=FALLBACK_COLOR_ENABLEMENT, + fallback_message="Error finding color enablement; falling back to full enablement", + ), } shape_enablement = { **DEFAULT_SHAPE_ENABLEMENT, - **actions.user.vscode_get_setting("cursorless.hatEnablement.shapes", {}), + **vscode_get_setting_with_fallback( + "cursorless.hatEnablement.shapes", + default_value={}, + fallback_value=FALLBACK_SHAPE_ENABLEMENT, + fallback_message="Error finding shape enablement; falling back to full enablement", + ), } active_hat_colors = { diff --git a/src/marks/vscode_settings.py b/src/marks/vscode_settings.py index 8524e50b..76b1db53 100644 --- a/src/marks/vscode_settings.py +++ b/src/marks/vscode_settings.py @@ -1,7 +1,9 @@ import os +from typing import Any from talon import Context, Module, actions from pathlib import Path from ..vendor.jstyleson import loads +import traceback mod = Module() @@ -26,7 +28,7 @@ def vscode_settings_path() -> Path: """Get path of vscode settings json file""" pass - def vscode_get_setting(key: str, default_value: any = None): + def vscode_get_setting(key: str, default_value: Any = None): """Get the value of vscode setting at the given key""" path: Path = actions.user.vscode_settings_path() settings: dict = loads(path.read_text()) @@ -77,3 +79,28 @@ def vscode_settings_path() -> Path: Path(f"{os.environ['APPDATA']}/VSCodium/User/settings.json"), ] ) + + +def vscode_get_setting_with_fallback( + key: str, + default_value: Any, + fallback_value: Any, + fallback_message: str, +): + """Returns a vscode setting with a fallback in case there's an error + + Args: + key (str): The key of the setting to look up + default_value (Any): The default value to return if the setting is not defined + fallback_value (Any): The value to return if there is an error looking up the setting + fallback_message (str): The message to show to the user if we end up having to use the fallback + + Returns: + Any: The value of the setting or the default or fall back + """ + try: + return actions.user.vscode_get_setting(key, default_value) + except Exception as e: + print(fallback_message) + traceback.print_exc() + return fallback_value