diff options
| author | Nick Brassel <nick@tzarc.org> | 2024-06-15 19:37:47 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-15 10:37:47 +0100 |
| commit | 0262161914133e6abfc306e675dbac3ba816a6ee (patch) | |
| tree | 042db91495759ceb4bc54895017a4420427d581d /lib/python/qmk/cli | |
| parent | d4654ab8934f795bbfc294f5b128a94aaa645a78 (diff) | |
[CLI] Don't `exit()` when certain exceptions occur. (#23442)
Diffstat (limited to 'lib/python/qmk/cli')
| -rw-r--r-- | lib/python/qmk/cli/find.py | 3 | ||||
| -rw-r--r-- | lib/python/qmk/cli/generate/autocorrect_data.py | 12 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/mass_compile.py | 3 | ||||
| -rw-r--r-- | lib/python/qmk/cli/userspace/compile.py | 3 | ||||
| -rw-r--r-- | lib/python/qmk/cli/userspace/list.py | 3 | ||||
| -rwxr-xr-x | lib/python/qmk/cli/via2json.py | 10 |
6 files changed, 26 insertions, 8 deletions
diff --git a/lib/python/qmk/cli/find.py b/lib/python/qmk/cli/find.py index 8f3a29c90c..bfed91e22c 100644 --- a/lib/python/qmk/cli/find.py +++ b/lib/python/qmk/cli/find.py | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | """ | 2 | """ |
| 3 | from milc import cli | 3 | from milc import cli |
| 4 | from qmk.search import filter_help, search_keymap_targets | 4 | from qmk.search import filter_help, search_keymap_targets |
| 5 | from qmk.util import maybe_exit_config | ||
| 5 | 6 | ||
| 6 | 7 | ||
| 7 | @cli.argument( | 8 | @cli.argument( |
| @@ -19,6 +20,8 @@ from qmk.search import filter_help, search_keymap_targets | |||
| 19 | def find(cli): | 20 | def find(cli): |
| 20 | """Search through all keyboards and keymaps for a given search criteria. | 21 | """Search through all keyboards and keymaps for a given search criteria. |
| 21 | """ | 22 | """ |
| 23 | maybe_exit_config(should_exit=False, should_reraise=True) | ||
| 24 | |||
| 22 | targets = search_keymap_targets([('all', cli.config.find.keymap)], cli.args.filter) | 25 | targets = search_keymap_targets([('all', cli.config.find.keymap)], cli.args.filter) |
| 23 | for target in sorted(targets, key=lambda t: (t.keyboard, t.keymap)): | 26 | for target in sorted(targets, key=lambda t: (t.keyboard, t.keymap)): |
| 24 | print(f'{target}') | 27 | print(f'{target}') |
diff --git a/lib/python/qmk/cli/generate/autocorrect_data.py b/lib/python/qmk/cli/generate/autocorrect_data.py index b11c66d95d..01a29b46fe 100644 --- a/lib/python/qmk/cli/generate/autocorrect_data.py +++ b/lib/python/qmk/cli/generate/autocorrect_data.py | |||
| @@ -27,7 +27,6 @@ Example: | |||
| 27 | For full documentation, see QMK Docs | 27 | For full documentation, see QMK Docs |
| 28 | """ | 28 | """ |
| 29 | 29 | ||
| 30 | import sys | ||
| 31 | import textwrap | 30 | import textwrap |
| 32 | from typing import Any, Dict, Iterator, List, Tuple | 31 | from typing import Any, Dict, Iterator, List, Tuple |
| 33 | 32 | ||
| @@ -38,6 +37,7 @@ from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE | |||
| 38 | from qmk.keyboard import keyboard_completer, keyboard_folder | 37 | from qmk.keyboard import keyboard_completer, keyboard_folder |
| 39 | from qmk.keymap import keymap_completer, locate_keymap | 38 | from qmk.keymap import keymap_completer, locate_keymap |
| 40 | from qmk.path import normpath | 39 | from qmk.path import normpath |
| 40 | from qmk.util import maybe_exit | ||
| 41 | 41 | ||
| 42 | KC_A = 4 | 42 | KC_A = 4 |
| 43 | KC_SPC = 0x2c | 43 | KC_SPC = 0x2c |
| @@ -88,16 +88,16 @@ def parse_file(file_name: str) -> List[Tuple[str, str]]: | |||
| 88 | # Check that `typo` is valid. | 88 | # Check that `typo` is valid. |
| 89 | if not (all([c in TYPO_CHARS for c in typo])): | 89 | if not (all([c in TYPO_CHARS for c in typo])): |
| 90 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" has characters other than a-z, \' and :.', line_number, typo) | 90 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typo "{fg_cyan}%s{fg_reset}" has characters other than a-z, \' and :.', line_number, typo) |
| 91 | sys.exit(1) | 91 | maybe_exit(1) |
| 92 | for other_typo in typos: | 92 | for other_typo in typos: |
| 93 | if typo in other_typo or other_typo in typo: | 93 | if typo in other_typo or other_typo in typo: |
| 94 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typos may not be substrings of one another, otherwise the longer typo would never trigger: "{fg_cyan}%s{fg_reset}" vs. "{fg_cyan}%s{fg_reset}".', line_number, typo, other_typo) | 94 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typos may not be substrings of one another, otherwise the longer typo would never trigger: "{fg_cyan}%s{fg_reset}" vs. "{fg_cyan}%s{fg_reset}".', line_number, typo, other_typo) |
| 95 | sys.exit(1) | 95 | maybe_exit(1) |
| 96 | if len(typo) < 5: | 96 | if len(typo) < 5: |
| 97 | cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} It is suggested that typos are at least 5 characters long to avoid false triggers: "{fg_cyan}%s{fg_reset}"', line_number, typo) | 97 | cli.log.warning('{fg_yellow}Warning:%d:{fg_reset} It is suggested that typos are at least 5 characters long to avoid false triggers: "{fg_cyan}%s{fg_reset}"', line_number, typo) |
| 98 | if len(typo) > 127: | 98 | if len(typo) > 127: |
| 99 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typo exceeds 127 chars: "{fg_cyan}%s{fg_reset}"', line_number, typo) | 99 | cli.log.error('{fg_red}Error:%d:{fg_reset} Typo exceeds 127 chars: "{fg_cyan}%s{fg_reset}"', line_number, typo) |
| 100 | sys.exit(1) | 100 | maybe_exit(1) |
| 101 | 101 | ||
| 102 | check_typo_against_dictionary(typo, line_number, correct_words) | 102 | check_typo_against_dictionary(typo, line_number, correct_words) |
| 103 | 103 | ||
| @@ -136,7 +136,7 @@ def parse_file_lines(file_name: str) -> Iterator[Tuple[int, str, str]]: | |||
| 136 | tokens = [token.strip() for token in line.split('->', 1)] | 136 | tokens = [token.strip() for token in line.split('->', 1)] |
| 137 | if len(tokens) != 2 or not tokens[0]: | 137 | if len(tokens) != 2 or not tokens[0]: |
| 138 | print(f'Error:{line_number}: Invalid syntax: "{line}"') | 138 | print(f'Error:{line_number}: Invalid syntax: "{line}"') |
| 139 | sys.exit(1) | 139 | maybe_exit(1) |
| 140 | 140 | ||
| 141 | typo, correction = tokens | 141 | typo, correction = tokens |
| 142 | typo = typo.lower() # Force typos to lowercase. | 142 | typo = typo.lower() # Force typos to lowercase. |
| @@ -237,7 +237,7 @@ def encode_link(link: Dict[str, Any]) -> List[int]: | |||
| 237 | byte_offset = link['byte_offset'] | 237 | byte_offset = link['byte_offset'] |
| 238 | if not (0 <= byte_offset <= 0xffff): | 238 | if not (0 <= byte_offset <= 0xffff): |
| 239 | cli.log.error('{fg_red}Error:{fg_reset} The autocorrection table is too large, a node link exceeds 64KB limit. Try reducing the autocorrection dict to fewer entries.') | 239 | cli.log.error('{fg_red}Error:{fg_reset} The autocorrection table is too large, a node link exceeds 64KB limit. Try reducing the autocorrection dict to fewer entries.') |
| 240 | sys.exit(1) | 240 | maybe_exit(1) |
| 241 | return [byte_offset & 255, byte_offset >> 8] | 241 | return [byte_offset & 255, byte_offset >> 8] |
| 242 | 242 | ||
| 243 | 243 | ||
diff --git a/lib/python/qmk/cli/mass_compile.py b/lib/python/qmk/cli/mass_compile.py index 7db704d6c2..d13afc6143 100755 --- a/lib/python/qmk/cli/mass_compile.py +++ b/lib/python/qmk/cli/mass_compile.py | |||
| @@ -12,6 +12,7 @@ from qmk.constants import QMK_FIRMWARE | |||
| 12 | from qmk.commands import find_make, get_make_parallel_args, build_environment | 12 | from qmk.commands import find_make, get_make_parallel_args, build_environment |
| 13 | from qmk.search import search_keymap_targets, search_make_targets | 13 | from qmk.search import search_keymap_targets, search_make_targets |
| 14 | from qmk.build_targets import BuildTarget, JsonKeymapBuildTarget | 14 | from qmk.build_targets import BuildTarget, JsonKeymapBuildTarget |
| 15 | from qmk.util import maybe_exit_config | ||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | def mass_compile_targets(targets: List[BuildTarget], clean: bool, dry_run: bool, no_temp: bool, parallel: int, **env): | 18 | def mass_compile_targets(targets: List[BuildTarget], clean: bool, dry_run: bool, no_temp: bool, parallel: int, **env): |
| @@ -100,6 +101,8 @@ all: {keyboard_safe}_{keymap_name}_binary | |||
| 100 | def mass_compile(cli): | 101 | def mass_compile(cli): |
| 101 | """Compile QMK Firmware against all keyboards. | 102 | """Compile QMK Firmware against all keyboards. |
| 102 | """ | 103 | """ |
| 104 | maybe_exit_config(should_exit=False, should_reraise=True) | ||
| 105 | |||
| 103 | if len(cli.args.builds) > 0: | 106 | if len(cli.args.builds) > 0: |
| 104 | json_like_targets = list([Path(p) for p in filter(lambda e: Path(e).exists() and Path(e).suffix == '.json', cli.args.builds)]) | 107 | json_like_targets = list([Path(p) for p in filter(lambda e: Path(e).exists() and Path(e).suffix == '.json', cli.args.builds)]) |
| 105 | make_like_targets = list(filter(lambda e: Path(e) not in json_like_targets, cli.args.builds)) | 108 | make_like_targets = list(filter(lambda e: Path(e) not in json_like_targets, cli.args.builds)) |
diff --git a/lib/python/qmk/cli/userspace/compile.py b/lib/python/qmk/cli/userspace/compile.py index fb320a16f0..e8cdf6cd97 100644 --- a/lib/python/qmk/cli/userspace/compile.py +++ b/lib/python/qmk/cli/userspace/compile.py | |||
| @@ -9,6 +9,7 @@ from qmk.userspace import UserspaceDefs | |||
| 9 | from qmk.build_targets import JsonKeymapBuildTarget | 9 | from qmk.build_targets import JsonKeymapBuildTarget |
| 10 | from qmk.search import search_keymap_targets | 10 | from qmk.search import search_keymap_targets |
| 11 | from qmk.cli.mass_compile import mass_compile_targets | 11 | from qmk.cli.mass_compile import mass_compile_targets |
| 12 | from qmk.util import maybe_exit_config | ||
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | @cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.") | 15 | @cli.argument('-t', '--no-temp', arg_only=True, action='store_true', help="Remove temporary files during build.") |
| @@ -22,6 +23,8 @@ def userspace_compile(cli): | |||
| 22 | cli.log.error('Could not determine QMK userspace location. Please run `qmk doctor` or `qmk userspace-doctor` to diagnose.') | 23 | cli.log.error('Could not determine QMK userspace location. Please run `qmk doctor` or `qmk userspace-doctor` to diagnose.') |
| 23 | return False | 24 | return False |
| 24 | 25 | ||
| 26 | maybe_exit_config(should_exit=False, should_reraise=True) | ||
| 27 | |||
| 25 | userspace = UserspaceDefs(QMK_USERSPACE / 'qmk.json') | 28 | userspace = UserspaceDefs(QMK_USERSPACE / 'qmk.json') |
| 26 | 29 | ||
| 27 | build_targets = [] | 30 | build_targets = [] |
diff --git a/lib/python/qmk/cli/userspace/list.py b/lib/python/qmk/cli/userspace/list.py index a63f669dd7..8689c80a76 100644 --- a/lib/python/qmk/cli/userspace/list.py +++ b/lib/python/qmk/cli/userspace/list.py | |||
| @@ -10,6 +10,7 @@ from qmk.build_targets import BuildTarget | |||
| 10 | from qmk.keyboard import is_all_keyboards, keyboard_folder | 10 | from qmk.keyboard import is_all_keyboards, keyboard_folder |
| 11 | from qmk.keymap import is_keymap_target | 11 | from qmk.keymap import is_keymap_target |
| 12 | from qmk.search import search_keymap_targets | 12 | from qmk.search import search_keymap_targets |
| 13 | from qmk.util import maybe_exit_config | ||
| 13 | 14 | ||
| 14 | 15 | ||
| 15 | @cli.argument('-e', '--expand', arg_only=True, action='store_true', help="Expands any use of `all` for either keyboard or keymap.") | 16 | @cli.argument('-e', '--expand', arg_only=True, action='store_true', help="Expands any use of `all` for either keyboard or keymap.") |
| @@ -19,6 +20,8 @@ def userspace_list(cli): | |||
| 19 | cli.log.error('Could not determine QMK userspace location. Please run `qmk doctor` or `qmk userspace-doctor` to diagnose.') | 20 | cli.log.error('Could not determine QMK userspace location. Please run `qmk doctor` or `qmk userspace-doctor` to diagnose.') |
| 20 | return False | 21 | return False |
| 21 | 22 | ||
| 23 | maybe_exit_config(should_exit=False, should_reraise=True) | ||
| 24 | |||
| 22 | userspace = UserspaceDefs(QMK_USERSPACE / 'qmk.json') | 25 | userspace = UserspaceDefs(QMK_USERSPACE / 'qmk.json') |
| 23 | 26 | ||
| 24 | if cli.args.expand: | 27 | if cli.args.expand: |
diff --git a/lib/python/qmk/cli/via2json.py b/lib/python/qmk/cli/via2json.py index 77823b5d9d..73c9a61b3d 100755 --- a/lib/python/qmk/cli/via2json.py +++ b/lib/python/qmk/cli/via2json.py | |||
| @@ -69,7 +69,7 @@ def _via_to_keymap(via_backup, keyboard_data, keymap_layout): | |||
| 69 | layout_data = keyboard_data['layouts'].get(keymap_layout) | 69 | layout_data = keyboard_data['layouts'].get(keymap_layout) |
| 70 | if not layout_data: | 70 | if not layout_data: |
| 71 | cli.log.error(f'LAYOUT macro {keymap_layout} is not a valid one for keyboard {cli.args.keyboard}!') | 71 | cli.log.error(f'LAYOUT macro {keymap_layout} is not a valid one for keyboard {cli.args.keyboard}!') |
| 72 | exit(1) | 72 | return None |
| 73 | 73 | ||
| 74 | layout_data = layout_data['layout'] | 74 | layout_data = layout_data['layout'] |
| 75 | sorting_hat = list() | 75 | sorting_hat = list() |
| @@ -118,7 +118,7 @@ def via2json(cli): | |||
| 118 | keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(cli.args.keyboard) | 118 | keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(cli.args.keyboard) |
| 119 | if not keymap_layout: | 119 | if not keymap_layout: |
| 120 | cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.") | 120 | cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.") |
| 121 | exit(1) | 121 | return False |
| 122 | 122 | ||
| 123 | # Load the VIA backup json | 123 | # Load the VIA backup json |
| 124 | with cli.args.filename.open('r') as fd: | 124 | with cli.args.filename.open('r') as fd: |
| @@ -126,9 +126,15 @@ def via2json(cli): | |||
| 126 | 126 | ||
| 127 | # Generate keyboard metadata | 127 | # Generate keyboard metadata |
| 128 | keyboard_data = info_json(cli.args.keyboard) | 128 | keyboard_data = info_json(cli.args.keyboard) |
| 129 | if not keyboard_data: | ||
| 130 | cli.log.error(f'LAYOUT macro {keymap_layout} is not a valid one for keyboard {cli.args.keyboard}!') | ||
| 131 | return False | ||
| 129 | 132 | ||
| 130 | # Get keycode array | 133 | # Get keycode array |
| 131 | keymap_data = _via_to_keymap(via_backup, keyboard_data, keymap_layout) | 134 | keymap_data = _via_to_keymap(via_backup, keyboard_data, keymap_layout) |
| 135 | if not keymap_data: | ||
| 136 | cli.log.error(f'Could not extract valid keycode data from VIA backup matching keyboard {cli.args.keyboard}!') | ||
| 137 | return False | ||
| 132 | 138 | ||
| 133 | # Convert macros | 139 | # Convert macros |
| 134 | macro_data = list() | 140 | macro_data = list() |
