Skip to content

Commit

Permalink
Update via2json layout macro searching (#24640)
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr authored Jan 20, 2025
1 parent 58aea4b commit d30cd87
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions lib/python/qmk/cli/via2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
import qmk.path
from qmk.info import info_json
from qmk.json_encoders import KeymapJSONEncoder
from qmk.commands import parse_configurator_json, dump_lines
from qmk.keymap import generate_json, list_keymaps, locate_keymap, parse_keymap_c
from qmk.commands import dump_lines
from qmk.keymap import generate_json


def _find_via_layout_macro(keyboard):
keymap_layout = None
if 'via' in list_keymaps(keyboard):
keymap_path = locate_keymap(keyboard, 'via')
if keymap_path.suffix == '.json':
keymap_layout = parse_configurator_json(keymap_path)['layout']
else:
keymap_layout = parse_keymap_c(keymap_path)['layers'][0]['layout']
return keymap_layout
def _find_via_layout_macro(keyboard_data):
"""Assume layout macro when only 1 is available
"""
layouts = list(keyboard_data['layouts'].keys())
return layouts[0] if len(layouts) == 1 else None


def _convert_macros(via_macros):
Expand Down Expand Up @@ -130,20 +126,16 @@ def via2json(cli):
This command uses the `qmk.keymap` module to generate a keymap.json from a VIA backup json. The generated keymap is written to stdout, or to a file if -o is provided.
"""
# Find appropriate layout macro
keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(cli.args.keyboard)
if not keymap_layout:
cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.")
return False

# Load the VIA backup json
with cli.args.filename.open('r') as fd:
via_backup = json.load(fd)

# Generate keyboard metadata
keyboard_data = info_json(cli.args.keyboard)
if not keyboard_data:
cli.log.error(f'LAYOUT macro {keymap_layout} is not a valid one for keyboard {cli.args.keyboard}!')

# Find appropriate layout macro
keymap_layout = cli.args.layout if cli.args.layout else _find_via_layout_macro(keyboard_data)
if not keymap_layout:
cli.log.error(f"Couldn't find LAYOUT macro for keyboard {cli.args.keyboard}. Please specify it with the '-l' argument.")
return False

# Get keycode array
Expand Down

0 comments on commit d30cd87

Please sign in to comment.