Last active
April 13, 2022 08:52
-
-
Save hjklapp/5bb7415315af4bb447a1daf0c11ecc6b to your computer and use it in GitHub Desktop.
Duplicate a keyboard layout in the keyboard-layout-editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import demjson | |
# Design the base layer in http://www.keyboard-layout-editor.com | |
# and copy the "json" from the raw data tab in KLE and save to file. | |
# This is NOT using the real json from KLE's "Export to json" feature. | |
# Paste the output of this program into the same raw data tab. | |
FILEPATH = "kyriaBase.json" | |
def readBaseLayout(): | |
with open(FILEPATH, 'r', encoding="utf-8") as f: | |
return demjson.decode('['+f.read()+']') | |
def copyLayout( | |
baseLayout, | |
x_offset=0, y_offset=0, | |
newLegend="", | |
keycapColor=None, keycapColorsToKeep=[] | |
): | |
if not keycapColor: | |
replaceColors = False | |
keycapColor="#cccccc" | |
else: | |
replaceColors = True | |
resetLayout = { | |
'c': keycapColor,# keycap color | |
't': '#000000', # text color | |
'g': False, # ghosted | |
'a': 7, # bit value for centering: 1 for X, 2 for Y, 4 for sideprinted | |
'r': 0, # rotation | |
'rx': x_offset, # x offset | |
'ry': y_offset, # y offset | |
'f': 6, # font size | |
} | |
result = [] | |
for row in baseLayout: | |
newRow = [] | |
for key in row: | |
if type(key) == dict: | |
newKey = dict(key) | |
if 'rx' in newKey: | |
newKey['rx'] += x_offset | |
if 'ry' in newKey: | |
newKey['ry'] += y_offset | |
if 'c' in newKey and replaceColors and newKey['c'] not in keycapColorsToKeep: | |
newKey['c'] = keycapColor | |
else: | |
newKey = newLegend | |
newRow.append(newKey) | |
result.append(newRow) | |
result[0].insert(0, resetLayout) | |
return result | |
def encodeKleTypes(json): | |
if type(json) == dict: | |
result = '{' | |
for key, value in json.items(): | |
result += key + ': ' + encodeKleTypes(value) + ', ' | |
if json: | |
result = result[:-2] | |
return result + '}' | |
if type(json) == list: | |
result = '[' | |
for item in json: | |
result += encodeKleTypes(item) + ', ' | |
if json: | |
result = result[:-2] | |
return result + ']' | |
if type(json) == str: | |
return '"' + repr(json)[1:-1].replace('"', '\\"').replace("\\'", "'") + '"' | |
if type(json) == bool: | |
if json: | |
return 'true' | |
else: | |
return 'false' | |
return str(json) | |
def encodeKle(jsonLayout): | |
return encodeKleTypes(jsonLayout)[1:-1] | |
def main(): | |
# base layout on top in the middle, then two columns below | |
# layoutSpecs = [ | |
# {"x_offset": 10, "y_offset": 0}, | |
# {"x_offset": 0, "y_offset": 6, "keycapColor": "#68cc78", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# {"x_offset": 20, "y_offset": 6, "keycapColor": "#9eb5f0", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# {"x_offset": 0, "y_offset": 12, "keycapColor": "#b684f5", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# {"x_offset": 20, "y_offset": 12, "keycapColor": "#faa184", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# {"x_offset": 0, "y_offset": 18, "keycapColor": "#59dece", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# {"x_offset": 20, "y_offset": 18, "keycapColor": "#e3df6f", "keycapColorsToKeep": ["#a3a0a0"]}, | |
# ] | |
# all layers in one column | |
layoutSpecs = [ | |
{"x_offset": 0, "y_offset": 0}, | |
{"x_offset": 0, "y_offset": 6, "keycapColor": "#68cc78", "keycapColorsToKeep": ["#a3a0a0"]}, | |
{"x_offset": 0, "y_offset": 12, "keycapColor": "#9eb5f0", "keycapColorsToKeep": ["#a3a0a0"]}, | |
{"x_offset": 0, "y_offset": 18, "keycapColor": "#b684f5", "keycapColorsToKeep": ["#a3a0a0"]}, | |
{"x_offset": 0, "y_offset": 24, "keycapColor": "#faa184", "keycapColorsToKeep": ["#a3a0a0"]}, | |
{"x_offset": 0, "y_offset": 30, "keycapColor": "#59dece", "keycapColorsToKeep": ["#a3a0a0"]}, | |
{"x_offset": 0, "y_offset": 36, "keycapColor": "#e3df6f", "keycapColorsToKeep": ["#a3a0a0"]}, | |
] | |
baseLayout = readBaseLayout() | |
result = [] | |
for layoutSpec in layoutSpecs: | |
nextLayout = copyLayout(baseLayout, **layoutSpec) | |
result.extend(nextLayout) | |
print(encodeKle(result)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone has ideas how to improve the pythonic-ness of my code, please comment!
I'm looking to improve my python.