Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export layer names into json when cli arg is set #24722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/python/qmk/cli/c2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
@cli.argument('--export-layer-names', arg_only=True, action='store_true', help='Export layer names when they are defined as enum')
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name')
Expand Down Expand Up @@ -51,7 +52,7 @@ def c2json(cli):
return False

try:
keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp)
keymap_json = c2json_impl(keyboard, keymap, filename, use_cpp=cli.args.no_cpp, export_layer_names=cli.args.export_layer_names)
except CppError as e:
if cli.config.general.verbose:
cli.log.debug('The C pre-processor ran into a fatal error: %s', e)
Expand All @@ -60,7 +61,7 @@ def c2json(cli):

# Generate the keymap.json
try:
keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
keymap_json = generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], keymap_json['layer_names'] if cli.args.export_layer_names else None)
except KeyError:
cli.log.error('Something went wrong. Try to use --no-cpp.')
return False
Expand Down
2 changes: 1 addition & 1 deletion lib/python/qmk/cli/via2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def via2json(cli):
keymap_data = _fix_macro_keys(keymap_data)

# Generate the keymap.json
keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, macro_data)
keymap_json = generate_json(cli.args.keymap, cli.args.keyboard, keymap_layout, keymap_data, None, macro_data)

keymap_lines = [json.dumps(keymap_json, cls=KeymapJSONEncoder, sort_keys=True)]
dump_lines(cli.args.output, keymap_lines, cli.args.quiet)
16 changes: 12 additions & 4 deletions lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def is_keymap_dir(keymap, c=True, json=True, additional_files=None):
return True


def generate_json(keymap, keyboard, layout, layers, macros=None):
def generate_json(keymap, keyboard, layout, layers, layer_names=None, macros=None):
"""Returns a `keymap.json` for the specified keyboard, layout, and layers.

Args:
Expand All @@ -242,13 +242,18 @@ def generate_json(keymap, keyboard, layout, layers, macros=None):
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.

layer_names
An array of strings describing the names of each layer.

macros
A sequence of strings containing macros to implement for this keyboard.
"""
new_keymap = {'keyboard': keyboard}
new_keymap['keymap'] = keymap
new_keymap['layout'] = layout
new_keymap['layers'] = layers
if layer_names:
new_keymap['layer_names'] = layer_names
if macros:
new_keymap['macros'] = macros

Expand Down Expand Up @@ -327,7 +332,7 @@ def write_json(keyboard, keymap, layout, layers, macros=None):
layers
An array of arrays describing the keymap. Each item in the inner array should be a string that is a valid QMK keycode.
"""
keymap_json = generate_json(keyboard, keymap, layout, layers, macros=None)
keymap_json = generate_json(keyboard, keymap, layout, layers, layer_names=None, macros=None)
keymap_content = json.dumps(keymap_json)
keymap_file = qmk.path.keymaps(keyboard)[0] / keymap / 'keymap.json'

Expand Down Expand Up @@ -653,7 +658,7 @@ def parse_keymap_c(keymap_file, use_cpp=True):
return keymap


def c2json(keyboard, keymap, keymap_file, use_cpp=True):
def c2json(keyboard, keymap, keymap_file, use_cpp=True, export_layer_names=False):
""" Convert keymap.c to keymap.json

Args:
Expand All @@ -673,14 +678,17 @@ def c2json(keyboard, keymap, keymap_file, use_cpp=True):
keymap_json = parse_keymap_c(keymap_file, use_cpp)

dirty_layers = keymap_json.pop('layers', None)
layer_names = list()
keymap_json['layers'] = list()
for layer in dirty_layers:
layer.pop('name')
layer_names.append(layer.pop('name'))
layout = layer.pop('layout')
if not keymap_json.get('layout', False):
keymap_json['layout'] = layout
keymap_json['layers'].append(layer.pop('keycodes'))

keymap_json['keyboard'] = keyboard
keymap_json['keymap'] = keymap
if export_layer_names:
keymap_json['layer_names'] = layer_names
return keymap_json
Loading