2019-11-13 05:55:41 +01:00
|
|
|
"""Convert raw KLE to JSON
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
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
|
2019-11-20 23:54:18 +01:00
|
|
|
return json.JSONEncoder.default(self, obj)
|
2019-11-13 05:55:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('filename', help='The KLE raw txt to convert')
|
|
|
|
@cli.argument('-f', '--force', action='store_true', help='Flag to overwrite current info.json')
|
2020-03-23 21:59:44 +01:00
|
|
|
@cli.subcommand('Convert a KLE layout to a Configurator JSON', hidden=False if cli.config.user.developer else True)
|
2019-11-13 05:55:41 +01:00
|
|
|
def kle2json(cli):
|
|
|
|
"""Convert a KLE layout to QMK's layout format.
|
2019-11-13 06:24:56 +01:00
|
|
|
""" # If filename is a path
|
2019-11-13 05:55:41 +01:00
|
|
|
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():
|
2020-04-18 22:00:56 +02:00
|
|
|
return cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path)
|
2019-11-13 05:55:41 +01:00
|
|
|
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:
|
2020-04-18 22:00:56 +02:00
|
|
|
cli.log.error('File {fg_cyan}%s/info.json{style_reset_all} already exists, use -f or --force to overwrite.', out_path)
|
2019-11-13 06:24:56 +01:00
|
|
|
return False
|
2019-11-13 05:55:41 +01:00
|
|
|
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
|
2019-11-13 06:24:56 +01:00
|
|
|
keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder)
|
2019-11-13 05:55:41 +01:00
|
|
|
# Initialize layout with kle2qmk from converter module
|
2019-11-13 06:24:56 +01:00
|
|
|
layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder)
|
2019-11-13 05:55:41 +01:00
|
|
|
# Replace layout in keyboard json
|
|
|
|
keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout)
|
|
|
|
# Write our info.json
|
2020-04-18 22:00:56 +02:00
|
|
|
file = open(out_path + "/info.json", "w")
|
2019-11-13 05:55:41 +01:00
|
|
|
file.write(keyboard)
|
|
|
|
file.close()
|
2020-04-18 22:00:56 +02:00
|
|
|
cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path)
|