1
0
Fork 0

[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:
LongerHV 2020-12-29 20:34:48 +01:00 committed by GitHub
parent 3300164065
commit 221d8fd866
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 35 deletions

View file

@ -3,6 +3,7 @@
from pathlib import Path
import json
import subprocess
import sys
from pygments.lexers.c_cpp import CLexer
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)
def _c_preprocess(path):
def _c_preprocess(path, stdin=None):
""" Run a file through the C pre-processor
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:
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
@ -451,17 +453,23 @@ def parse_keymap_c(keymap_file, use_cpp=True):
Currently only cares about the keymaps array.
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
Returns:
a dictionary containing the parsed keymap
"""
if use_cpp:
keymap_file = _c_preprocess(keymap_file)
if keymap_file == '-':
if use_cpp:
keymap_file = _c_preprocess(None, sys.stdin)
else:
keymap_file = sys.stdin.read()
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['layers'] = _get_layers(keymap_file)