opensteno_qmk/lib/python/qmk/commands.py
Erovia 058737f116
[CLI] Add c2json (#8817)
* Basic keymap parsing finally works

* Add 'keymap.json' creation to the qmk.keymap module

* Add tests and fix formatting

* Fix/exclude flake8 errors

* Convert keymap.c to valid keymap.json

* Fix some errors

* Add tests

* Finalize keymap.json creation, add json template

* Add docs

* Move pygments to the standard requirements

* Add support for nameless layers, fix tests

* Fix things after rebase

* Add missing 'keymap' value.

* Fix missing layer numbers from advanced keycodes

Buckwich noticed that if the advanced keycode / layer toggling key
contains a number, it goes missing.
Now we properly handle them.
Thx for noticing!

* Apply suggestions from code review

* fixup tests

Co-authored-by: Zach White <skullydazed@drpepper.org>
Co-authored-by: skullY <skullydazed@gmail.com>
2020-10-06 17:10:19 -07:00

86 lines
2.2 KiB
Python

"""Helper functions for commands.
"""
import json
import os
import platform
import subprocess
import shlex
import shutil
import qmk.keymap
def create_make_command(keyboard, keymap, target=None):
"""Create a make compile command
Args:
keyboard
The path of the keyboard, for example 'plank'
keymap
The name of the keymap, for example 'algernon'
target
Usually a bootloader.
Returns:
A command that can be run to make the specified keyboard and keymap
"""
make_args = [keyboard, keymap]
make_cmd = 'gmake' if shutil.which('gmake') else 'make'
if target:
make_args.append(target)
return [make_cmd, ':'.join(make_args)]
def compile_configurator_json(user_keymap, bootloader=None):
"""Convert a configurator export JSON file into a C file
Args:
configurator_filename
The configurator JSON export file
bootloader
A bootloader to flash
Returns:
A command to run to compile and flash the C file.
"""
# Write the keymap C file
qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
# Return a command that can be run to make the keymap and flash if given
if bootloader is None:
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'])
return create_make_command(user_keymap['keyboard'], user_keymap['keymap'], bootloader)
def parse_configurator_json(configurator_file):
"""Open and parse a configurator json export
"""
# FIXME(skullydazed/anyone): Add validation here
user_keymap = json.load(configurator_file)
return user_keymap
def run(command, *args, **kwargs):
"""Run a command with subprocess.run
"""
platform_id = platform.platform().lower()
if isinstance(command, str):
raise TypeError('`command` must be a non-text sequence such as list or tuple.')
if 'windows' in platform_id:
safecmd = map(shlex.quote, command)
safecmd = ' '.join(safecmd)
command = [os.environ['SHELL'], '-c', safecmd]
return subprocess.run(command, *args, **kwargs)