converter.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Functions to convert to and from QMK formats
  2. """
  3. from milc import cli
  4. def kle2qmk(kle):
  5. """Convert a KLE layout to QMK's layout format.
  6. """
  7. layout = []
  8. top_left_corner = None
  9. # Iterate through the KLE classifying keys by layout
  10. for row in kle:
  11. for key in row:
  12. if key['decal']:
  13. continue
  14. if key['label_style'] in [0, 4]:
  15. matrix, _, _, alt_layout, layout_name, _, keycode = key['name'].split('\n')
  16. else:
  17. cli.log.error('Unknown label style: %s', key['label_style'])
  18. continue
  19. matrix = list(map(int, matrix.split(',', 1)))
  20. qmk_key = {
  21. 'label': keycode,
  22. 'x': key['column'],
  23. 'y': key['row'],
  24. 'matrix': matrix,
  25. }
  26. if not top_left_corner and (not alt_layout or alt_layout.endswith(',0')):
  27. top_left_corner = key['column'], key['row']
  28. # Figure out what layout this key is part of
  29. # FIXME(skullydazed): In the future this will populate `layout_options` in info.json
  30. if alt_layout:
  31. alt_group, layout_index = map(int, alt_layout.split(',', 1))
  32. if layout_index != 0:
  33. continue
  34. # Set the key size
  35. if key['width'] != 1:
  36. qmk_key['w'] = key['width']
  37. if key['height'] != 1:
  38. qmk_key['h'] = key['height']
  39. layout.append(qmk_key)
  40. # Adjust the keys to account for the top-left corner
  41. for key in layout:
  42. key['x'] -= top_left_corner[0]
  43. key['y'] -= top_left_corner[1]
  44. return layout