converter.py 1.7 KB

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