keymap.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Generate a keymap.c from a configurator export.
  2. """
  3. import json
  4. import os
  5. import sys
  6. from milc import cli
  7. import qmk.keymap
  8. @cli.argument('-o', '--output', help='File to write to')
  9. @cli.argument('filename', help='Configurator JSON file')
  10. @cli.entrypoint('Create a keymap.c from a QMK Configurator export.')
  11. def main(cli):
  12. # Error checking
  13. if cli.args.filename == ('-'):
  14. cli.log.error('Reading from STDIN is not (yet) supported.')
  15. cli.print_usage()
  16. exit(1)
  17. if not os.path.exists(qmk.path.normpath(cli.args.filename)):
  18. cli.log.error('JSON file does not exist!')
  19. cli.print_usage()
  20. exit(1)
  21. # Environment processing
  22. if cli.args.output == ('-'):
  23. cli.args.output = None
  24. # Parse the configurator json
  25. with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
  26. user_keymap = json.load(fd)
  27. # Generate the keymap
  28. keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
  29. if cli.args.output:
  30. output_dir = os.path.dirname(cli.args.output)
  31. if not os.path.exists(output_dir):
  32. os.makedirs(output_dir)
  33. output_file = qmk.path.normpath(cli.args.output)
  34. with open(output_file, 'w') as keymap_fd:
  35. keymap_fd.write(keymap_c)
  36. cli.log.info('Wrote keymap to %s.', cli.args.output)
  37. else:
  38. print(keymap_c)