1
0
Fork 0

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
This commit is contained in:
Joel Challis 2021-02-07 21:02:51 +00:00 committed by GitHub
parent fc29c7a589
commit ccc9c43161
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 20 deletions

View file

@ -2,15 +2,14 @@
You can compile a keymap already in the repo or using a QMK Configurator export. You can compile a keymap already in the repo or using a QMK Configurator export.
""" """
from argparse import FileType
from milc import cli from milc import cli
import qmk.path
from qmk.decorators import automagic_keyboard, automagic_keymap from qmk.decorators import automagic_keyboard, automagic_keymap
from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export to compile') @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), help='The configurator export to compile')
@cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-kb', '--keyboard', help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.') @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.") @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")

View file

@ -3,7 +3,6 @@
You can compile a keymap already in the repo or using a QMK Configurator export. You can compile a keymap already in the repo or using a QMK Configurator export.
A bootloader must be specified. A bootloader must be specified.
""" """
from argparse import FileType
from milc import cli from milc import cli
@ -30,7 +29,7 @@ def print_bootloader_help():
cli.echo('For more info, visit https://docs.qmk.fm/#/flashing') cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
@cli.argument('filename', nargs='?', arg_only=True, type=FileType('r'), help='The configurator export JSON to compile.') @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), help='The configurator export JSON to compile.')
@cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.') @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
@cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.') @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
@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.') @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.')

View file

@ -1,7 +1,6 @@
"""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
@ -11,7 +10,7 @@ import qmk.path
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('filename', type=qmk.path.normpath, arg_only=True, help='Configurator JSON file') @cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, help='Configurator JSON file')
@cli.subcommand('Creates a keymap.c from a QMK Configurator export.') @cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
def json2c(cli): def json2c(cli):
"""Generate a keymap.c from a configurator export. """Generate a keymap.c from a configurator export.
@ -20,19 +19,8 @@ def json2c(cli):
""" """
try: try:
# Parse the configurator from stdin # Parse the configurator from json file (or stdin)
if cli.args.filename and cli.args.filename.name == '-': user_keymap = json.load(cli.args.filename)
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: except json.decoder.JSONDecodeError as ex:
cli.log.error('The JSON input does not appear to be valid.') cli.log.error('The JSON input does not appear to be valid.')

View file

@ -2,6 +2,7 @@
""" """
import logging import logging
import os import os
import argparse
from pathlib import Path from pathlib import Path
from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE from qmk.constants import MAX_KEYBOARD_SUBFOLDERS, QMK_FIRMWARE
@ -65,3 +66,12 @@ def normpath(path):
return path return path
return Path(os.environ['ORIG_CWD']) / path return Path(os.environ['ORIG_CWD']) / path
class FileType(argparse.FileType):
def __call__(self, string):
"""normalize and check exists
otherwise magic strings like '-' for stdin resolve to bad paths
"""
norm = normpath(string)
return super().__call__(norm if norm.exists() else string)