diff options
author | LongerHV <46924944+LongerHV@users.noreply.github.com> | 2020-12-29 20:34:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-29 11:34:48 -0800 |
commit | 221d8fd8669ff528bfedd01f41486f5298d960e1 (patch) | |
tree | c82fe3f0e032ce2057039b4baadbb1f91c0608ed /lib/python/qmk/keymap.py | |
parent | 3300164065949e6bc9423632ccbcd0022be8074d (diff) | |
download | qmk_firmware-221d8fd8669ff528bfedd01f41486f5298d960e1.tar.gz qmk_firmware-221d8fd8669ff528bfedd01f41486f5298d960e1.zip |
[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
Diffstat (limited to 'lib/python/qmk/keymap.py')
-rw-r--r-- | lib/python/qmk/keymap.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/lib/python/qmk/keymap.py b/lib/python/qmk/keymap.py index 31c61ae6a8..266532f503 100644 --- a/lib/python/qmk/keymap.py +++ b/lib/python/qmk/keymap.py @@ -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) |