Skip to content

Commit

Permalink
Improvements based on @Erovia's feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Feb 3, 2020
1 parent 6e492a1 commit f31e110
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/python/qmk/cli/json/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
from milc import cli

import qmk.keymap
import qmk.path


@cli.argument('-o', '--output', arg_only=True, type=Path, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('filename', arg_only=True, type=Path, help='Configurator JSON file')
@cli.argument('filename', arg_only=True, help='Configurator JSON file')
@cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
def json_keymap(cli):
"""Generate a keymap.c from a configurator export.
This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
"""
cli.args.filename = qmk.path.normpath(cli.args.filename)

# Error checking
if not cli.args.filename.exists():
cli.log.error('JSON file does not exist!')
Expand All @@ -34,7 +37,7 @@ def json_keymap(cli):
cli.args.output = None

# Parse the configurator json
with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
with cli.args.filename.open('r') as fd:
user_keymap = json.load(fd)

# Generate the keymap
Expand Down
5 changes: 5 additions & 0 deletions lib/python/qmk/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Information that should be available to the python library.
"""

# This is the number of directories under `qmk_firmware/keyboards` that will be traversed. This is currently a limitation of our make system.
MAX_KEYBOARD_SUBFOLDERS = 5
5 changes: 4 additions & 1 deletion lib/python/qmk/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from pathlib import Path

from qmk.constants import MAX_KEYBOARD_SUBFOLDERS
from qmk.errors import NoSuchKeyboardError


Expand All @@ -16,7 +17,7 @@ def keymap(keyboard):
"""
keyboard_folder = Path('keyboards') / keyboard

for i in range(5):
for i in range(MAX_KEYBOARD_SUBFOLDERS):
if (keyboard_folder / 'keymaps').exists():
return (keyboard_folder / 'keymaps').resolve()

Expand All @@ -31,6 +32,8 @@ def normpath(path):
This will use the path to a file as seen from the directory the script was called from. You should use this to normalize filenames supplied from the command line.
"""
path = Path(path)

if path.is_absolute():
return Path(path)

Expand Down
2 changes: 1 addition & 1 deletion lib/python/qmk/tests/test_qmk_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def test_keymap_onekey_pytest():

def test_normpath():
path = qmk.path.normpath('lib/python')
assert str(path) == str(Path(os.environ['ORIG_CWD']) / 'lib/python')
assert path.samefile(Path(os.environ['ORIG_CWD']) / 'lib/python')

0 comments on commit f31e110

Please sign in to comment.