aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli
diff options
context:
space:
mode:
authorCody Bender <50554676+cfbender@users.noreply.github.com>2019-11-12 21:55:41 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-11-12 20:55:41 -0800
commit7329c2d02d38f40a23d38f789de34057fd2acd42 (patch)
treebb4e0640164b71d60714b964a72025517c2ade61 /lib/python/qmk/cli
parent00fb1bd1f0550645997b61870d7d092494265a60 (diff)
downloadqmk_firmware-7329c2d02d38f40a23d38f789de34057fd2acd42.tar.gz
qmk_firmware-7329c2d02d38f40a23d38f789de34057fd2acd42.zip
Add cli convert subcommand, from raw KLE to JSON (#6898)
* Add initial pass at KLE convert * Add cli log on convert * Move kle2xy, add absolute filepath arg support * Add overwrite flag, and context sensitive conversion * Update docs/cli.md * Fix converter.py typo * Add convert unit test * Rename to kle2qmk * Rename subcommand * Rename subcommand to kle2json * Change tests to cover rename * Rename in __init__.py * Update CLI docs with new subcommand name * Fix from suggestions in PR #6898 * Help with cases of case sensitivity * Update cli.md * Use angle brackets to indicate required option * Make the output text more accurate
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/kle2json.py79
2 files changed, 80 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index e41cc3dcb..1b83e78c7 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -10,6 +10,7 @@ from . import doctor
10from . import hello 10from . import hello
11from . import json 11from . import json
12from . import list 12from . import list
13from . import kle2json
13from . import new 14from . import new
14from . import pyformat 15from . import pyformat
15from . import pytest 16from . import pytest
diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py
new file mode 100755
index 000000000..22eb515df
--- /dev/null
+++ b/lib/python/qmk/cli/kle2json.py
@@ -0,0 +1,79 @@
1"""Convert raw KLE to JSON
2
3"""
4import json
5import os
6from pathlib import Path
7from argparse import FileType
8from decimal import Decimal
9from collections import OrderedDict
10
11from milc import cli
12from kle2xy import KLE2xy
13
14from qmk.converter import kle2qmk
15
16
17class CustomJSONEncoder(json.JSONEncoder):
18 def default(self, obj):
19 try:
20 if isinstance(obj, Decimal):
21 if obj % 2 in (Decimal(0), Decimal(1)):
22 return int(obj)
23 return float(obj)
24 except TypeError:
25 pass
26 return JSONEncoder.default(self, obj)
27
28
29@cli.argument('filename', help='The KLE raw txt to convert')
30@cli.argument('-f', '--force', action='store_true', help='Flag to overwrite current info.json')
31@cli.subcommand('Convert a KLE layout to a Configurator JSON')
32def kle2json(cli):
33 """Convert a KLE layout to QMK's layout format.
34 """ # If filename is a path
35 if cli.args.filename.startswith("/") or cli.args.filename.startswith("./"):
36 file_path = Path(cli.args.filename)
37 # Otherwise assume it is a file name
38 else:
39 file_path = Path(os.environ['ORIG_CWD'], cli.args.filename)
40 # Check for valid file_path for more graceful failure
41 if not file_path.exists():
42 return cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', str(file_path))
43 out_path = file_path.parent
44 raw_code = file_path.open().read()
45 # Check if info.json exists, allow overwrite with force
46 if Path(out_path, "info.json").exists() and not cli.args.force:
47 cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', str(out_path))
48 return False;
49 try:
50 # Convert KLE raw to x/y coordinates (using kle2xy package from skullydazed)
51 kle = KLE2xy(raw_code)
52 except Exception as e:
53 cli.log.error('Could not parse KLE raw data: %s', raw_code)
54 cli.log.exception(e)
55 # FIXME: This should be better
56 return cli.log.error('Could not parse KLE raw data.')
57 keyboard = OrderedDict(
58 keyboard_name=kle.name,
59 url='',
60 maintainer='qmk',
61 width=kle.columns,
62 height=kle.rows,
63 layouts={'LAYOUT': {
64 'layout': 'LAYOUT_JSON_HERE'
65 }},
66 )
67 # Initialize keyboard with json encoded from ordered dict
68 keyboard = json.dumps(keyboard, indent=4, separators=(
69 ', ', ': '), sort_keys=False, cls=CustomJSONEncoder)
70 # Initialize layout with kle2qmk from converter module
71 layout = json.dumps(kle2qmk(kle), separators=(
72 ', ', ':'), cls=CustomJSONEncoder)
73 # Replace layout in keyboard json
74 keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout)
75 # Write our info.json
76 file = open(str(out_path) + "/info.json", "w")
77 file.write(keyboard)
78 file.close()
79 cli.log.info('Wrote out {fg_cyan}%s/info.json', str(out_path))