From 221d8fd8669ff528bfedd01f41486f5298d960e1 Mon Sep 17 00:00:00 2001 From: LongerHV <46924944+LongerHV@users.noreply.github.com> Date: Tue, 29 Dec 2020 20:34:48 +0100 Subject: [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 --- lib/python/qmk/keymap.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'lib/python/qmk/keymap.py') 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) -- cgit v1.2.3