summaryrefslogtreecommitdiff
path: root/lib/python/qmk
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk')
-rw-r--r--lib/python/qmk/cli/__init__.py1
-rwxr-xr-xlib/python/qmk/cli/kle2json.py79
-rw-r--r--lib/python/qmk/converter.py33
-rw-r--r--lib/python/qmk/tests/kle.txt5
-rw-r--r--lib/python/qmk/tests/test_cli_commands.py2
5 files changed, 120 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py
index e41cc3dcb2..1b83e78c70 100644
--- a/lib/python/qmk/cli/__init__.py
+++ b/lib/python/qmk/cli/__init__.py
@@ -10,6 +10,7 @@ from . import doctor
from . import hello
from . import json
from . import list
+from . import kle2json
from . import new
from . import pyformat
from . import pytest
diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py
new file mode 100755
index 0000000000..22eb515dff
--- /dev/null
+++ b/lib/python/qmk/cli/kle2json.py
@@ -0,0 +1,79 @@
+"""Convert raw KLE to JSON
+
+"""
+import json
+import os
+from pathlib import Path
+from argparse import FileType
+from decimal import Decimal
+from collections import OrderedDict
+
+from milc import cli
+from kle2xy import KLE2xy
+
+from qmk.converter import kle2qmk
+
+
+class CustomJSONEncoder(json.JSONEncoder):
+ def default(self, obj):
+ try:
+ if isinstance(obj, Decimal):
+ if obj % 2 in (Decimal(0), Decimal(1)):
+ return int(obj)
+ return float(obj)
+ except TypeError:
+ pass
+ return JSONEncoder.default(self, obj)
+
+
+@cli.argument('filename', help='The KLE raw txt to convert')
+@cli.argument('-f', '--force', action='store_true', help='Flag to overwrite current info.json')
+@cli.subcommand('Convert a KLE layout to a Configurator JSON')
+def kle2json(cli):
+ """Convert a KLE layout to QMK's layout format.
+ """ # If filename is a path
+ if cli.args.filename.startswith("/") or cli.args.filename.startswith("./"):
+ file_path = Path(cli.args.filename)
+ # Otherwise assume it is a file name
+ else:
+ file_path = Path(os.environ['ORIG_CWD'], cli.args.filename)
+ # Check for valid file_path for more graceful failure
+ if not file_path.exists():
+ return cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', str(file_path))
+ out_path = file_path.parent
+ raw_code = file_path.open().read()
+ # Check if info.json exists, allow overwrite with force
+ if Path(out_path, "info.json").exists() and not cli.args.force:
+ cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', str(out_path))
+ return False;
+ try:
+ # Convert KLE raw to x/y coordinates (using kle2xy package from skullydazed)
+ kle = KLE2xy(raw_code)
+ except Exception as e:
+ cli.log.error('Could not parse KLE raw data: %s', raw_code)
+ cli.log.exception(e)
+ # FIXME: This should be better
+ return cli.log.error('Could not parse KLE raw data.')
+ keyboard = OrderedDict(
+ keyboard_name=kle.name,
+ url='',
+ maintainer='qmk',
+ width=kle.columns,
+ height=kle.rows,
+ layouts={'LAYOUT': {
+ 'layout': 'LAYOUT_JSON_HERE'
+ }},
+ )
+ # Initialize keyboard with json encoded from ordered dict
+ keyboard = json.dumps(keyboard, indent=4, separators=(
+ ', ', ': '), sort_keys=False, cls=CustomJSONEncoder)
+ # Initialize layout with kle2qmk from converter module
+ layout = json.dumps(kle2qmk(kle), separators=(
+ ', ', ':'), cls=CustomJSONEncoder)
+ # Replace layout in keyboard json
+ keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout)
+ # Write our info.json
+ file = open(str(out_path) + "/info.json", "w")
+ file.write(keyboard)
+ file.close()
+ 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 0000000000..bbd3531317
--- /dev/null
+++ b/lib/python/qmk/converter.py
@@ -0,0 +1,33 @@
+"""Functions to convert to and from QMK formats
+"""
+from collections import OrderedDict
+
+
+def kle2qmk(kle):
+ """Convert a KLE layout to QMK's layout format.
+ """
+ layout = []
+
+ for row in kle:
+ for key in row:
+ if key['decal']:
+ continue
+
+ qmk_key = OrderedDict(
+ label="",
+ x=key['column'],
+ y=key['row'],
+ )
+
+ if key['width'] != 1:
+ qmk_key['w'] = key['width']
+ if key['height'] != 1:
+ qmk_key['h'] = key['height']
+ if 'name' in key and key['name']:
+ qmk_key['label'] = key['name'].split('\n', 1)[0]
+ else:
+ del (qmk_key['label'])
+
+ layout.append(qmk_key)
+
+ return layout
diff --git a/lib/python/qmk/tests/kle.txt b/lib/python/qmk/tests/kle.txt
new file mode 100644
index 0000000000..862a899ab9
--- /dev/null
+++ b/lib/python/qmk/tests/kle.txt
@@ -0,0 +1,5 @@
+["¬\n`","!\n1","\"\n2","£\n3","$\n4","%\n5","^\n6","&\n7","*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace"],
+[{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"],
+[{w:1.75},"Caps Lock","A","S","D","F","G","H","J","K","L",":\n;","@\n'","~\n#"],
+[{w:1.25},"Shift","|\n\\","Z","X","C","V","B","N","M","<\n,",">\n.","?\n/",{w:2.75},"Shift"],
+[{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 55b8d253f7..d91af992a8 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():
assert result.returncode == 0
assert 'general.color' in result.stdout
+def test_kle2json():
+ assert check_subcommand('kle2json', 'kle.txt', '-f').returncode == 0
def test_doctor():
result = check_subcommand('doctor')