keymap.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. """Generate a keymap.c from a configurator export.
  13. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
  14. """
  15. # Error checking
  16. if cli.args.filename == ('-'):
  17. cli.log.error('Reading from STDIN is not (yet) supported.')
  18. cli.print_usage()
  19. exit(1)
  20. if not os.path.exists(qmk.path.normpath(cli.args.filename)):
  21. cli.log.error('JSON file does not exist!')
  22. cli.print_usage()
  23. exit(1)
  24. # Environment processing
  25. if cli.args.output == ('-'):
  26. cli.args.output = None
  27. # Parse the configurator json
  28. with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
  29. user_keymap = json.load(fd)
  30. # Generate the keymap
  31. keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
  32. if cli.args.output:
  33. output_dir = os.path.dirname(cli.args.output)
  34. if not os.path.exists(output_dir):
  35. os.makedirs(output_dir)
  36. output_file = qmk.path.normpath(cli.args.output)
  37. with open(output_file, 'w') as keymap_fd:
  38. keymap_fd.write(keymap_c)
  39. cli.log.info('Wrote keymap to %s.', cli.args.output)
  40. else:
  41. print(keymap_c)