diff options
Diffstat (limited to 'lib/python/qmk')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 1 | ||||
-rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 79 | ||||
-rw-r--r-- | lib/python/qmk/converter.py | 33 | ||||
-rw-r--r-- | lib/python/qmk/tests/kle.txt | 5 | ||||
-rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 2 |
5 files changed, 120 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 | |||
10 | from . import hello | 10 | from . import hello |
11 | from . import json | 11 | from . import json |
12 | from . import list | 12 | from . import list |
13 | from . import kle2json | ||
13 | from . import new | 14 | from . import new |
14 | from . import pyformat | 15 | from . import pyformat |
15 | from . import pytest | 16 | from . 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 | """ | ||
4 | import json | ||
5 | import os | ||
6 | from pathlib import Path | ||
7 | from argparse import FileType | ||
8 | from decimal import Decimal | ||
9 | from collections import OrderedDict | ||
10 | |||
11 | from milc import cli | ||
12 | from kle2xy import KLE2xy | ||
13 | |||
14 | from qmk.converter import kle2qmk | ||
15 | |||
16 | |||
17 | class 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') | ||
32 | def 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)) | ||
diff --git a/lib/python/qmk/converter.py b/lib/python/qmk/converter.py new file mode 100644 index 000000000..bbd353131 --- /dev/null +++ b/lib/python/qmk/converter.py | |||
@@ -0,0 +1,33 @@ | |||
1 | """Functions to convert to and from QMK formats | ||
2 | """ | ||
3 | from collections import OrderedDict | ||
4 | |||
5 | |||
6 | def kle2qmk(kle): | ||
7 | """Convert a KLE layout to QMK's layout format. | ||
8 | """ | ||
9 | layout = [] | ||
10 | |||
11 | for row in kle: | ||
12 | for key in row: | ||
13 | if key['decal']: | ||
14 | continue | ||
15 | |||
16 | qmk_key = OrderedDict( | ||
17 | label="", | ||
18 | x=key['column'], | ||
19 | y=key['row'], | ||
20 | ) | ||
21 | |||
22 | if key['width'] != 1: | ||
23 | qmk_key['w'] = key['width'] | ||
24 | if key['height'] != 1: | ||
25 | qmk_key['h'] = key['height'] | ||
26 | if 'name' in key and key['name']: | ||
27 | qmk_key['label'] = key['name'].split('\n', 1)[0] | ||
28 | else: | ||
29 | del (qmk_key['label']) | ||
30 | |||
31 | layout.append(qmk_key) | ||
32 | |||
33 | return layout | ||
diff --git a/lib/python/qmk/tests/kle.txt b/lib/python/qmk/tests/kle.txt new file mode 100644 index 000000000..862a899ab --- /dev/null +++ b/lib/python/qmk/tests/kle.txt | |||
@@ -0,0 +1,5 @@ | |||
1 | ["¬\n`","!\n1","\"\n2","£\n3","$\n4","%\n5","^\n6","&\n7","*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace"], | ||
2 | [{w:1.5},"Tab","Q","W","E","R","T","Y","U","I","O","P","{\n[","}\n]",{x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25},"Enter"], | ||
3 | [{w:1.75},"Caps Lock","A","S","D","F","G","H","J","K","L",":\n;","@\n'","~\n#"], | ||
4 | [{w:1.25},"Shift","|\n\\","Z","X","C","V","B","N","M","<\n,",">\n.","?\n/",{w:2.75},"Shift"], | ||
5 | [{w:1.25},"Ctrl",{w:1.25},"Win",{w:1.25},"Alt",{a:7,w:6.25},"",{a:4,w:1.25},"AltGr",{w:1.25},"Win",{w:1.25},"Menu",{w:1.25},"Ctrl"] | ||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 55b8d253f..d91af992a 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
@@ -19,6 +19,8 @@ def test_config(): | |||
19 | assert result.returncode == 0 | 19 | assert result.returncode == 0 |
20 | assert 'general.color' in result.stdout | 20 | assert 'general.color' in result.stdout |
21 | 21 | ||
22 | def test_kle2json(): | ||
23 | assert check_subcommand('kle2json', 'kle.txt', '-f').returncode == 0 | ||
22 | 24 | ||
23 | def test_doctor(): | 25 | def test_doctor(): |
24 | result = check_subcommand('doctor') | 26 | result = check_subcommand('doctor') |