[CLI] Add stdin support for json2c command (#11289)
* Implement stdin for json2c command * Refactor * Handle json decode error * Add stdin support for c2json cli command * Refactor to prevent code duplication * Change exit(1) to return False in c2json command * Remove unused import
This commit is contained in:
parent
3300164065
commit
221d8fd866
@ -1,7 +1,6 @@
|
|||||||
"""Generate a keymap.json from a keymap.c file.
|
"""Generate a keymap.json from a keymap.c file.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
import sys
|
|
||||||
|
|
||||||
from milc import cli
|
from milc import cli
|
||||||
|
|
||||||
@ -21,19 +20,14 @@ def c2json(cli):
|
|||||||
|
|
||||||
This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided.
|
This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided.
|
||||||
"""
|
"""
|
||||||
cli.args.filename = qmk.path.normpath(cli.args.filename)
|
if cli.args.filename != '-':
|
||||||
|
cli.args.filename = qmk.path.normpath(cli.args.filename)
|
||||||
|
|
||||||
# Error checking
|
# Error checking
|
||||||
if not cli.args.filename.exists():
|
if not cli.args.filename.exists():
|
||||||
cli.log.error('C file does not exist!')
|
cli.log.error('C file does not exist!')
|
||||||
cli.print_usage()
|
cli.print_usage()
|
||||||
exit(1)
|
return False
|
||||||
|
|
||||||
if str(cli.args.filename) == '-':
|
|
||||||
# TODO(skullydazed/anyone): Read file contents from STDIN
|
|
||||||
cli.log.error('Reading from STDIN is not (yet) supported.')
|
|
||||||
cli.print_usage()
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Environment processing
|
# Environment processing
|
||||||
if cli.args.output == ('-'):
|
if cli.args.output == ('-'):
|
||||||
@ -47,7 +41,7 @@ def c2json(cli):
|
|||||||
keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
|
keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
cli.log.error('Something went wrong. Try to use --no-cpp.')
|
cli.log.error('Something went wrong. Try to use --no-cpp.')
|
||||||
sys.exit(1)
|
return False
|
||||||
|
|
||||||
if cli.args.output:
|
if cli.args.output:
|
||||||
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Generate a keymap.c from a configurator export.
|
"""Generate a keymap.c from a configurator export.
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
from milc import cli
|
from milc import cli
|
||||||
|
|
||||||
@ -17,26 +18,31 @@ def json2c(cli):
|
|||||||
|
|
||||||
This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
|
This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
|
||||||
"""
|
"""
|
||||||
# Error checking
|
|
||||||
if cli.args.filename and cli.args.filename.name == '-':
|
|
||||||
# TODO(skullydazed/anyone): Read file contents from STDIN
|
|
||||||
cli.log.error('Reading from STDIN is not (yet) supported.')
|
|
||||||
cli.print_usage()
|
|
||||||
return False
|
|
||||||
|
|
||||||
if not cli.args.filename.exists():
|
try:
|
||||||
cli.log.error('JSON file does not exist!')
|
# Parse the configurator from stdin
|
||||||
cli.print_usage()
|
if cli.args.filename and cli.args.filename.name == '-':
|
||||||
|
user_keymap = json.load(sys.stdin)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Error checking
|
||||||
|
if not cli.args.filename.exists():
|
||||||
|
cli.log.error('JSON file does not exist!')
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Parse the configurator json file
|
||||||
|
else:
|
||||||
|
user_keymap = json.loads(cli.args.filename.read_text())
|
||||||
|
|
||||||
|
except json.decoder.JSONDecodeError as ex:
|
||||||
|
cli.log.error('The JSON input does not appear to be valid.')
|
||||||
|
cli.log.error(ex)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Environment processing
|
# Environment processing
|
||||||
if cli.args.output and cli.args.output.name == '-':
|
if cli.args.output and cli.args.output.name == '-':
|
||||||
cli.args.output = None
|
cli.args.output = None
|
||||||
|
|
||||||
# Parse the configurator json
|
|
||||||
with cli.args.filename.open('r') as fd:
|
|
||||||
user_keymap = json.load(fd)
|
|
||||||
|
|
||||||
# Generate the keymap
|
# Generate the keymap
|
||||||
keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
|
keymap_c = qmk.keymap.generate_c(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import json
|
import json
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
from pygments.lexers.c_cpp import CLexer
|
from pygments.lexers.c_cpp import CLexer
|
||||||
from pygments.token import Token
|
from pygments.token import Token
|
||||||
@ -312,16 +313,17 @@ def list_keymaps(keyboard, c=True, json=True, additional_files=None, fullpath=Fa
|
|||||||
return sorted(names)
|
return sorted(names)
|
||||||
|
|
||||||
|
|
||||||
def _c_preprocess(path):
|
def _c_preprocess(path, stdin=None):
|
||||||
""" Run a file through the C pre-processor
|
""" Run a file through the C pre-processor
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
path: path of the keymap.c file
|
path: path of the keymap.c file (set None to use stdin)
|
||||||
|
stdin: stdin pipe (e.g. sys.stdin)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
the stdout of the pre-processor
|
the stdout of the pre-processor
|
||||||
"""
|
"""
|
||||||
pre_processed_keymap = qmk.commands.run(['cpp', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
pre_processed_keymap = qmk.commands.run(['cpp', path] if path else ['cpp'], stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
return pre_processed_keymap.stdout
|
return pre_processed_keymap.stdout
|
||||||
|
|
||||||
|
|
||||||
@ -451,17 +453,23 @@ def parse_keymap_c(keymap_file, use_cpp=True):
|
|||||||
Currently only cares about the keymaps array.
|
Currently only cares about the keymaps array.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
keymap_file: path of the keymap.c file
|
keymap_file: path of the keymap.c file (or '-' to use stdin)
|
||||||
|
|
||||||
use_cpp: if True, pre-process the file with the C pre-processor
|
use_cpp: if True, pre-process the file with the C pre-processor
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
a dictionary containing the parsed keymap
|
a dictionary containing the parsed keymap
|
||||||
"""
|
"""
|
||||||
if use_cpp:
|
if keymap_file == '-':
|
||||||
keymap_file = _c_preprocess(keymap_file)
|
if use_cpp:
|
||||||
|
keymap_file = _c_preprocess(None, sys.stdin)
|
||||||
|
else:
|
||||||
|
keymap_file = sys.stdin.read()
|
||||||
else:
|
else:
|
||||||
keymap_file = keymap_file.read_text()
|
if use_cpp:
|
||||||
|
keymap_file = _c_preprocess(keymap_file)
|
||||||
|
else:
|
||||||
|
keymap_file = keymap_file.read_text()
|
||||||
|
|
||||||
keymap = dict()
|
keymap = dict()
|
||||||
keymap['layers'] = _get_layers(keymap_file)
|
keymap['layers'] = _get_layers(keymap_file)
|
||||||
|
@ -8,11 +8,20 @@ is_windows = 'windows' in platform.platform().lower()
|
|||||||
|
|
||||||
|
|
||||||
def check_subcommand(command, *args):
|
def check_subcommand(command, *args):
|
||||||
cmd = ['bin/qmk', command] + list(args)
|
cmd = ['bin/qmk', command, *args]
|
||||||
result = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
|
result = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def check_subcommand_stdin(file_to_read, command, *args):
|
||||||
|
"""Pipe content of a file to a command and return output.
|
||||||
|
"""
|
||||||
|
with open(file_to_read) as my_file:
|
||||||
|
cmd = ['bin/qmk', command, *args]
|
||||||
|
result = run(cmd, stdin=my_file, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def check_returncode(result, expected=[0]):
|
def check_returncode(result, expected=[0]):
|
||||||
"""Print stdout if `result.returncode` does not match `expected`.
|
"""Print stdout if `result.returncode` does not match `expected`.
|
||||||
"""
|
"""
|
||||||
@ -129,6 +138,12 @@ def test_json2c():
|
|||||||
assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
|
assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_json2c_stdin():
|
||||||
|
result = check_subcommand_stdin('keyboards/handwired/onekey/keymaps/default_json/keymap.json', 'json2c', '-')
|
||||||
|
check_returncode(result)
|
||||||
|
assert result.stdout == '#include QMK_KEYBOARD_H\nconst uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {\t[0] = LAYOUT_ortho_1x1(KC_A)};\n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_info():
|
def test_info():
|
||||||
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
|
result = check_subcommand('info', '-kb', 'handwired/onekey/pytest')
|
||||||
check_returncode(result)
|
check_returncode(result)
|
||||||
@ -186,6 +201,18 @@ def test_c2json_nocpp():
|
|||||||
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
|
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
|
||||||
|
|
||||||
|
|
||||||
|
def test_c2json_stdin():
|
||||||
|
result = check_subcommand_stdin("keyboards/handwired/onekey/keymaps/default/keymap.c", "c2json", "-kb", "handwired/onekey/pytest", "-km", "default", "-")
|
||||||
|
check_returncode(result)
|
||||||
|
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT_ortho_1x1", "layers": [["KC_A"]]}'
|
||||||
|
|
||||||
|
|
||||||
|
def test_c2json_nocpp_stdin():
|
||||||
|
result = check_subcommand_stdin("keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c", "c2json", "--no-cpp", "-kb", "handwired/onekey/pytest", "-km", "default", "-")
|
||||||
|
check_returncode(result)
|
||||||
|
assert result.stdout.strip() == '{"keyboard": "handwired/onekey/pytest", "documentation": "This file is a keymap.json file for handwired/onekey/pytest", "keymap": "default", "layout": "LAYOUT", "layers": [["KC_ENTER"]]}'
|
||||||
|
|
||||||
|
|
||||||
def test_clean():
|
def test_clean():
|
||||||
result = check_subcommand('clean', '-a')
|
result = check_subcommand('clean', '-a')
|
||||||
check_returncode(result)
|
check_returncode(result)
|
||||||
|
Loading…
Reference in New Issue
Block a user