Skip to content

Commit

Permalink
Settings file robustness (#128)
Browse files Browse the repository at this point in the history
* Fall back to full hat enablement on error

* Refactor

* Black
  • Loading branch information
pokey authored Dec 10, 2021
1 parent 5a573ea commit d2ff903
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/marks/mark.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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


Expand All @@ -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 = {
Expand Down
29 changes: 28 additions & 1 deletion src/marks/vscode_settings.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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())
Expand Down Expand Up @@ -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

0 comments on commit d2ff903

Please sign in to comment.