json.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Create a keymap directory from a configurator export.
  2. """
  3. import json
  4. import os
  5. import sys
  6. import subprocess
  7. from milc import cli
  8. import qmk.keymap
  9. import qmk.path
  10. @cli.argument('filename', help='Configurator JSON export')
  11. @cli.entrypoint('Compile a QMK Configurator export.')
  12. def main(cli):
  13. """Compile a QMK Configurator export.
  14. This command creates a new keymap from a configurator export, overwriting an existing keymap if one exists.
  15. FIXME(skullydazed): add code to check and warn if the keymap already exists
  16. """
  17. # Error checking
  18. if cli.args.filename == ('-'):
  19. cli.log.error('Reading from STDIN is not (yet) supported.')
  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. exit(1)
  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_path = qmk.path.keymap(user_keymap['keyboard'])
  29. cli.log.info('Creating {fg_cyan}%s{style_reset_all} keymap in {fg_cyan}%s', user_keymap['keymap'], keymap_path)
  30. qmk.keymap.write(user_keymap['keyboard'], user_keymap['keymap'], user_keymap['layout'], user_keymap['layers'])
  31. cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
  32. # Compile the keymap
  33. command = ['make', ':'.join((user_keymap['keyboard'], user_keymap['keymap']))]
  34. cli.log.info('Compiling keymap with {fg_cyan}%s\n\n', ' '.join(command))
  35. subprocess.run(command)