Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Add c2json #8817

Merged
merged 16 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/cli_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ Creates a keymap.c from a QMK Configurator export.
qmk json2c [-o OUTPUT] filename
```

## `qmk c2json`

Creates a keymap.json from a keymap.c.
**Note:** Parsing C source files is not easy, therefore this subcommand may not work your keymap. In some cases not using the C pre-processor helps.

**Usage**:

```
qmk c2json [--no-cpp] [-o OUTPUT] filename
```

## `qmk list-keyboards`

This command lists all the keyboards currently defined in `qmk_firmware`
Expand Down
23 changes: 23 additions & 0 deletions keyboards/handwired/onekey/keymaps/pytest_nocpp/keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include QMK_KEYBOARD_H
#include "audio.h"

/* THIS FILE WAS GENERATED AND IS EXPERIMENTAL!
*
* This file was generated by qmk-compile-json. You may or may not want to
* edit it directly.
*/

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT(KC_ENTER)
};

void encoder_update_user(uint8_t index, bool clockwise) {
if (index == 0) {
if (clockwise) {
tap_code(KC_UP);
} else {
tap_code(KC_DOWN);
}
}

};
3 changes: 3 additions & 0 deletions keyboards/handwired/onekey/pytest/templates/keymap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"documentation": "This file is a keymap.json file for handwired/onekey/pytest"
}
1 change: 1 addition & 0 deletions lib/python/qmk/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from milc import cli

from . import c2json
from . import cformat
from . import compile
from . import config
Expand Down
62 changes: 62 additions & 0 deletions lib/python/qmk/cli/c2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Generate a keymap.json from a keymap.c file.
"""
import json
import sys

from milc import cli

import qmk.keymap
import qmk.path


@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
@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('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name')
@cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name')
@cli.argument('filename', arg_only=True, help='keymap.c file')
@cli.subcommand('Creates a keymap.json from a keymap.c file.')
def c2json(cli):
"""Generate a keymap.json from a keymap.c file.

This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. 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('C file does not exist!')
cli.print_usage()
exit(1)

if str(cli.args.filename) == '-':
# TODO(skullydazed/anyone): Read file contents from STDIN
cli.log.error('Reading from STDIN is not (yet) supported.')
cli.print_usage()
exit(1)

# Environment processing
if cli.args.output == ('-'):
cli.args.output = None

# Parse the keymap.c
keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)

# Generate the keymap.json
try:
keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap'])
except KeyError:
cli.log.error('Something went wrong. Try to use --no-cpp.')
sys.exit(1)

if cli.args.output:
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
if cli.args.output.exists():
cli.args.output.replace(cli.args.output.name + '.bak')
cli.args.output.write_text(json.dumps(keymap_json))

if not cli.args.quiet:
cli.log.info('Wrote keymap to %s.', cli.args.output)

else:
print(json.dumps(keymap_json))
2 changes: 1 addition & 1 deletion lib/python/qmk/cli/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def parse_gcc_version(version):
return {
'major': int(m.group(1)),
'minor': int(m.group(2)) if m.group(2) else 0,
'patch': int(m.group(3)) if m.group(3) else 0
'patch': int(m.group(3)) if m.group(3) else 0,
}


Expand Down
3 changes: 0 additions & 3 deletions lib/python/qmk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import shlex
import shutil

from milc import cli
import qmk.keymap


Expand Down Expand Up @@ -84,6 +83,4 @@ def run(command, *args, **kwargs):
safecmd = ' '.join(safecmd)
command = [os.environ['SHELL'], '-c', safecmd]

cli.log.debug('Running command: %s', command)

return subprocess.run(command, *args, **kwargs)
Loading