diff options
Diffstat (limited to 'lib/python/qmk/cli/kle2json.py')
-rwxr-xr-x | lib/python/qmk/cli/kle2json.py | 51 |
1 files changed, 18 insertions, 33 deletions
diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 3d1bb8c43..66d504bfc 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py | |||
@@ -3,25 +3,12 @@ | |||
3 | import json | 3 | import json |
4 | import os | 4 | import os |
5 | from pathlib import Path | 5 | from pathlib import Path |
6 | from decimal import Decimal | ||
7 | from collections import OrderedDict | ||
8 | 6 | ||
9 | from milc import cli | 7 | from milc import cli |
10 | from kle2xy import KLE2xy | 8 | from kle2xy import KLE2xy |
11 | 9 | ||
12 | from qmk.converter import kle2qmk | 10 | from qmk.converter import kle2qmk |
13 | 11 | from qmk.info_json_encoder import InfoJSONEncoder | |
14 | |||
15 | class CustomJSONEncoder(json.JSONEncoder): | ||
16 | def default(self, obj): | ||
17 | try: | ||
18 | if isinstance(obj, Decimal): | ||
19 | if obj % 2 in (Decimal(0), Decimal(1)): | ||
20 | return int(obj) | ||
21 | return float(obj) | ||
22 | except TypeError: | ||
23 | pass | ||
24 | return json.JSONEncoder.default(self, obj) | ||
25 | 12 | ||
26 | 13 | ||
27 | @cli.argument('filename', help='The KLE raw txt to convert') | 14 | @cli.argument('filename', help='The KLE raw txt to convert') |
@@ -52,24 +39,22 @@ def kle2json(cli): | |||
52 | cli.log.error('Could not parse KLE raw data: %s', raw_code) | 39 | cli.log.error('Could not parse KLE raw data: %s', raw_code) |
53 | cli.log.exception(e) | 40 | cli.log.exception(e) |
54 | return False | 41 | return False |
55 | keyboard = OrderedDict( | 42 | keyboard = { |
56 | keyboard_name=kle.name, | 43 | 'keyboard_name': kle.name, |
57 | url='', | 44 | 'url': '', |
58 | maintainer='qmk', | 45 | 'maintainer': 'qmk', |
59 | width=kle.columns, | 46 | 'width': kle.columns, |
60 | height=kle.rows, | 47 | 'height': kle.rows, |
61 | layouts={'LAYOUT': { | 48 | 'layouts': { |
62 | 'layout': 'LAYOUT_JSON_HERE' | 49 | 'LAYOUT': { |
63 | }}, | 50 | 'layout': kle2qmk(kle) |
64 | ) | 51 | } |
65 | # Initialize keyboard with json encoded from ordered dict | 52 | }, |
66 | keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=CustomJSONEncoder) | 53 | } |
67 | # Initialize layout with kle2qmk from converter module | 54 | |
68 | layout = json.dumps(kle2qmk(kle), separators=(', ', ':'), cls=CustomJSONEncoder) | ||
69 | # Replace layout in keyboard json | ||
70 | keyboard = keyboard.replace('"LAYOUT_JSON_HERE"', layout) | ||
71 | # Write our info.json | 55 | # Write our info.json |
72 | file = open(out_path / "info.json", "w") | 56 | keyboard = json.dumps(keyboard, indent=4, separators=(', ', ': '), sort_keys=False, cls=InfoJSONEncoder) |
73 | file.write(keyboard) | 57 | info_json_file = out_path / 'info.json' |
74 | file.close() | 58 | |
59 | info_json_file.write_text(keyboard) | ||
75 | cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path) | 60 | cli.log.info('Wrote out {fg_cyan}%s/info.json', out_path) |