aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-02-07 21:02:51 +0000
committerGitHub <noreply@github.com>2021-02-07 21:02:51 +0000
commitccc9c43161282bd6f37813cc85c13da1eb51b88d (patch)
tree92fc97bb2489ba92e2ab95597e4e88c7fcd8bbb9 /lib/python/qmk
parentfc29c7a589837c2d1e4173d59d2849f89d3cb72b (diff)
downloadqmk_firmware-ccc9c43161282bd6f37813cc85c13da1eb51b88d.tar.gz
qmk_firmware-ccc9c43161282bd6f37813cc85c13da1eb51b88d.zip
Allow flash/compile to accept relative json paths (#11767)
* Allow flash/compile to accept relative paths * Review suggestions * Review comments * Put back exists check otherwise stdin fails * fix lint
Diffstat (limited to 'lib/python/qmk')
-rwxr-xr-xlib/python/qmk/cli/compile.py5
-rw-r--r--lib/python/qmk/cli/flash.py3
-rwxr-xr-xlib/python/qmk/cli/json2c.py18
-rw-r--r--lib/python/qmk/path.py10
4 files changed, 16 insertions, 20 deletions
diff --git a/lib/python/qmk/cli/compile.py b/lib/python/qmk/cli/compile.py
index 322ce6a25..db195f78a 100755
--- a/lib/python/qmk/cli/compile.py
+++ b/lib/python/qmk/cli/compile.py
@@ -2,15 +2,14 @@
2 2
3You can compile a keymap already in the repo or using a QMK Configurator export. 3You can compile a keymap already in the repo or using a QMK Configurator export.
4""" 4"""
5from argparse import FileType
6
7from milc import cli 5from milc import cli
8 6
7import qmk.path
9from qmk.decorators import automagic_keyboard, automagic_keymap 8from qmk.decorators import automagic_keyboard, automagic_keymap
10from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json 9from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
11 10
12 11
13@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') 12@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), help='The configurator export to compile')
14@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') 13@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
15@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') 14@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
16@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") 15@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
diff --git a/lib/python/qmk/cli/flash.py b/lib/python/qmk/cli/flash.py
index a87629003..173dee3df 100644
--- a/lib/python/qmk/cli/flash.py
+++ b/lib/python/qmk/cli/flash.py
@@ -3,7 +3,6 @@
3You can compile a keymap already in the repo or using a QMK Configurator export. 3You can compile a keymap already in the repo or using a QMK Configurator export.
4A bootloader must be specified. 4A bootloader must be specified.
5""" 5"""
6from argparse import FileType
7 6
8from milc import cli 7from milc import cli
9 8
@@ -30,7 +29,7 @@ def print_bootloader_help():
30 cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') 29 cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
31 30
32 31
33@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.') 32@cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), help='The configurator export JSON to compile.')
34@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') 33@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
35@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') 34@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
36@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.') 35@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
diff --git a/lib/python/qmk/cli/json2c.py b/lib/python/qmk/cli/json2c.py
index 97d8fb0c3..e7babc0e4 100755
--- a/lib/python/qmk/cli/json2c.py
+++ b/lib/python/qmk/cli/json2c.py
@@ -1,7 +1,6 @@
1"""Generate a keymap.c from a configurator export. 1"""Generate a keymap.c from a configurator export.
2""" 2"""
3import json 3import json
4import sys
5 4
6from milc import cli 5from milc import cli
7 6
@@ -11,7 +10,7 @@ import qmk.path
11 10
12@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') 11@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
13@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") 12@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
14@cli.argument('filename', type=qmk.path.normpath, arg_only=True, help='Configurator JSON file') 13@cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, help='Configurator JSON file')
15@cli.subcommand('Creates a keymap.c from a QMK Configurator export.') 14@cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
16def json2c(cli): 15def json2c(cli):
17 """Generate a keymap.c from a configurator export. 16 """Generate a keymap.c from a configurator export.
@@ -20,19 +19,8 @@ def json2c(cli):
20 """ 19 """
21 20
22 try: 21 try:
23 # Parse the configurator from stdin 22 # Parse the configurator from json file (or stdin)
24 if cli.args.filename and cli.args.filename.name == '-': 23 user_keymap = json.load(cli.args.filename)
25 user_keymap = json.load(sys.stdin)
26
27 else:
28 # Error checking
29 if not cli.args.filename.exists():
30 cli.log.error('JSON file does not exist!')
31 return False
32
33 # Parse the configurator json file
34 else:
35 user_keymap = json.loads(cli.args.filename.read_text())
36 24
37 except json.decoder.JSONDecodeError as ex: 25 except json.decoder.JSONDecodeError as ex:
38 cli.log.error('The JSON input does not appear to be valid.') 26 cli.log.error('The JSON input does not appear to be valid.')
diff --git a/lib/python/qmk/path.py b/lib/python/qmk/path.py
index 54def1d5d..2aa1916f5 100644
--- a/lib/python/qmk/path.py
+++ b/lib/python/qmk/path.py
@@ -2,6 +2,7 @@
2""" 2"""
3import logging 3import logging
4import os 4import os
5import argparse
5from pathlib import Path 6from pathlib import Path
6 7
7from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE 8from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE
@@ -65,3 +66,12 @@ def normpath(path):
65 return path 66 return path
66 67
67 return Path(os.environ['ORIG_CWD']) / path 68 return Path(os.environ['ORIG_CWD']) / path
69
70
71class FileType(argparse.FileType):
72 def __call__(self, string):
73 """normalize and check exists
74 otherwise magic strings like '-' for stdin resolve to bad paths
75 """
76 norm = normpath(string)
77 return super().__call__(norm if norm.exists() else string)