keymap.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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', arg_only=True, help='File to write to')
  9. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  10. @cli.argument('filename', arg_only=True, help='Configurator JSON file')
  11. @cli.subcommand('Creates a keymap.c from a QMK Configurator export.')
  12. def json_keymap(cli):
  13. """Generate a keymap.c from a configurator export.
  14. 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.
  15. """
  16. # Error checking
  17. if cli.args.filename == ('-'):
  18. cli.log.error('Reading from STDIN is not (yet) supported.')
  19. cli.print_usage()
  20. exit(1)
  21. if not os.path.exists(qmk.path.normpath(cli.args.filename)):
  22. cli.log.error('JSON file does not exist!')
  23. cli.print_usage()
  24. exit(1)
  25. # Environment processing
  26. if cli.args.output == ('-'):
  27. cli.args.output = None
  28. # Parse the configurator json
  29. with open(qmk.path.normpath(cli.args.filename), 'r') as fd:
  30. user_keymap = json.load(fd)
  31. # Generate the keymap
  32. keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers'])
  33. if cli.args.output:
  34. output_dir = os.path.dirname(cli.args.output)
  35. if not os.path.exists(output_dir):
  36. os.makedirs(output_dir)
  37. output_file = qmk.path.normpath(cli.args.output)
  38. with open(output_file, 'w') as keymap_fd:
  39. keymap_fd.write(keymap_c)
  40. if not cli.args.quiet:
  41. cli.log.info('Wrote keymap to %s.', cli.args.output)
  42. else:
  43. print(keymap_c)