diff options
| author | Cody Bender <50554676+cfbender@users.noreply.github.com> | 2019-11-12 21:55:41 -0700 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-11-12 20:55:41 -0800 |
| commit | 7329c2d02d38f40a23d38f789de34057fd2acd42 (patch) | |
| tree | bb4e0640164b71d60714b964a72025517c2ade61 /lib/python/kle2xy.py | |
| parent | 00fb1bd1f0550645997b61870d7d092494265a60 (diff) | |
| download | qmk_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/kle2xy.py')
| -rw-r--r-- | lib/python/kle2xy.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/lib/python/kle2xy.py b/lib/python/kle2xy.py new file mode 100644 index 000000000..ea16a4b5e --- /dev/null +++ b/lib/python/kle2xy.py | |||
| @@ -0,0 +1,155 @@ | |||
| 1 | """ Original code from https://github.com/skullydazed/kle2xy | ||
| 2 | """ | ||
| 3 | |||
| 4 | import hjson | ||
| 5 | from decimal import Decimal | ||
| 6 | |||
| 7 | class KLE2xy(list): | ||
| 8 | """Abstract interface for interacting with a KLE layout. | ||
| 9 | """ | ||
| 10 | def __init__(self, layout=None, name='', invert_y=True): | ||
| 11 | super(KLE2xy, self).__init__() | ||
| 12 | |||
| 13 | self.name = name | ||
| 14 | self.invert_y = invert_y | ||
| 15 | self.key_width = Decimal('19.05') | ||
| 16 | self.key_skel = { | ||
| 17 | 'decal': False, | ||
| 18 | 'border_color': 'none', | ||
| 19 | 'keycap_profile': '', | ||
| 20 | 'keycap_color': 'grey', | ||
| 21 | 'label_color': 'black', | ||
| 22 | 'label_size': 3, | ||
| 23 | 'label_style': 4, | ||
| 24 | 'width': Decimal('1'), 'height': Decimal('1'), | ||
| 25 | 'x': Decimal('0'), 'y': Decimal('0') | ||
| 26 | } | ||
| 27 | self.rows = Decimal(0) | ||
| 28 | self.columns = Decimal(0) | ||
| 29 | |||
| 30 | if layout: | ||
| 31 | self.parse_layout(layout) | ||
| 32 | |||
| 33 | @property | ||
| 34 | def width(self): | ||
| 35 | """Returns the width of the keyboard plate. | ||
| 36 | """ | ||
| 37 | return (Decimal(self.columns) * self.key_width) + self.key_width/2 | ||
| 38 | |||
| 39 | @property | ||
| 40 | def height(self): | ||
| 41 | """Returns the height of the keyboard plate. | ||
| 42 | """ | ||
| 43 | return (self.rows * self.key_width) + self.key_width/2 | ||
| 44 | |||
| 45 | @property | ||
| 46 | def size(self): | ||
| 47 | """Returns the size of the keyboard plate. | ||
| 48 | """ | ||
| 49 | return (self.width, self.height) | ||
| 50 | |||
| 51 | def attrs(self, properties): | ||
| 52 | """Parse the keyboard properties dictionary. | ||
| 53 | """ | ||
| 54 | # FIXME: Store more than just the keyboard name. | ||
| 55 | if 'name' in properties: | ||
| 56 | self.name = properties['name'] | ||
| 57 | |||
| 58 | def parse_layout(self, layout): | ||
| 59 | # Wrap this in a dictionary so hjson will parse KLE raw data | ||
| 60 | layout = '{"layout": [' + layout + ']}' | ||
| 61 | layout = hjson.loads(layout)['layout'] | ||
| 62 | |||
| 63 | # Initialize our state machine | ||
| 64 | current_key = self.key_skel.copy() | ||
| 65 | current_row = Decimal(0) | ||
| 66 | current_col = Decimal(0) | ||
| 67 | current_x = 0 | ||
| 68 | current_y = self.key_width / 2 | ||
| 69 | |||
| 70 | if isinstance(layout[0], dict): | ||
| 71 | self.attrs(layout[0]) | ||
| 72 | layout = layout[1:] | ||
| 73 | |||
| 74 | for row_num, row in enumerate(layout): | ||
| 75 | self.append([]) | ||
| 76 | |||
| 77 | # Process the current row | ||
| 78 | for key in row: | ||
| 79 | if isinstance(key, dict): | ||
| 80 | if 'w' in key and key['w'] != Decimal(1): | ||
| 81 | current_key['width'] = Decimal(key['w']) | ||
| 82 | if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1: | ||
| 83 | # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25} | ||
| 84 | current_key['isoenter'] = True | ||
| 85 | if 'h' in key and key['h'] != Decimal(1): | ||
| 86 | current_key['height'] = Decimal(key['h']) | ||
| 87 | if 'a' in key: | ||
| 88 | current_key['label_style'] = self.key_skel['label_style'] = int(key['a']) | ||
| 89 | if current_key['label_style'] < 0: | ||
| 90 | current_key['label_style'] = 0 | ||
| 91 | elif current_key['label_style'] > 9: | ||
| 92 | current_key['label_style'] = 9 | ||
| 93 | if 'f' in key: | ||
| 94 | font_size = int(key['f']) | ||
| 95 | if font_size > 9: | ||
| 96 | font_size = 9 | ||
| 97 | elif font_size < 1: | ||
| 98 | font_size = 1 | ||
| 99 | current_key['label_size'] = self.key_skel['label_size'] = font_size | ||
| 100 | if 'p' in key: | ||
| 101 | current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p'] | ||
| 102 | if 'c' in key: | ||
| 103 | current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c'] | ||
| 104 | if 't' in key: | ||
| 105 | # FIXME: Need to do better validation, plus figure out how to support multiple colors | ||
| 106 | if '\n' in key['t']: | ||
| 107 | key['t'] = key['t'].split('\n')[0] | ||
| 108 | if key['t'] == "0": | ||
| 109 | key['t'] = "#000000" | ||
| 110 | current_key['label_color'] = self.key_skel['label_color'] = key['t'] | ||
| 111 | if 'x' in key: | ||
| 112 | current_col += Decimal(key['x']) | ||
| 113 | current_x += Decimal(key['x']) * self.key_width | ||
| 114 | if 'y' in key: | ||
| 115 | current_row += Decimal(key['y']) | ||
| 116 | current_y += Decimal(key['y']) * self.key_width | ||
| 117 | if 'd' in key: | ||
| 118 | current_key['decal'] = True | ||
| 119 | |||
| 120 | else: | ||
| 121 | current_key['name'] = key | ||
| 122 | current_key['row'] = current_row | ||
| 123 | current_key['column'] = current_col | ||
| 124 | |||
| 125 | # Determine the X center | ||
| 126 | x_center = (current_key['width'] * self.key_width) / 2 | ||
| 127 | current_x += x_center | ||
| 128 | current_key['x'] = current_x | ||
| 129 | current_x += x_center | ||
| 130 | |||
| 131 | # Determine the Y center | ||
| 132 | y_center = (current_key['height'] * self.key_width) / 2 | ||
| 133 | y_offset = y_center - (self.key_width / 2) | ||
| 134 | current_key['y'] = (current_y + y_offset) | ||
| 135 | |||
| 136 | # Tend to our row/col count | ||
| 137 | current_col += current_key['width'] | ||
| 138 | if current_col > self.columns: | ||
| 139 | self.columns = current_col | ||
| 140 | |||
| 141 | # Invert the y-axis if neccesary | ||
| 142 | if self.invert_y: | ||
| 143 | current_key['y'] = -current_key['y'] | ||
| 144 | |||
| 145 | # Store this key | ||
| 146 | self[-1].append(current_key) | ||
| 147 | current_key = self.key_skel.copy() | ||
| 148 | |||
| 149 | # Move to the next row | ||
| 150 | current_x = 0 | ||
| 151 | current_y += self.key_width | ||
| 152 | current_col = Decimal(0) | ||
| 153 | current_row += Decimal(1) | ||
| 154 | if current_row > self.rows: | ||
| 155 | self.rows = Decimal(current_row) | ||
