You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3 KiB

  1. """Generate a keymap.json from a keymap.c file.
  2. """
  3. import json
  4. import sys
  5. from milc import cli
  6. import qmk.keymap
  7. import qmk.path
  8. @cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
  9. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  10. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  11. @cli.argument('-kb', '--keyboard', arg_only=True, required=True, help='The keyboard\'s name')
  12. @cli.argument('-km', '--keymap', arg_only=True, required=True, help='The keymap\'s name')
  13. @cli.argument('filename', arg_only=True, help='keymap.c file')
  14. @cli.subcommand('Creates a keymap.json from a keymap.c file.')
  15. def c2json(cli):
  16. """Generate a keymap.json from a keymap.c file.
  17. This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided.
  18. """
  19. cli.args.filename = qmk.path.normpath(cli.args.filename)
  20. # Error checking
  21. if not cli.args.filename.exists():
  22. cli.log.error('C file does not exist!')
  23. cli.print_usage()
  24. exit(1)
  25. if str(cli.args.filename) == '-':
  26. # TODO(skullydazed/anyone): Read file contents from STDIN
  27. cli.log.error('Reading from STDIN is not (yet) supported.')
  28. cli.print_usage()
  29. exit(1)
  30. # Environment processing
  31. if cli.args.output == ('-'):
  32. cli.args.output = None
  33. # Parse the keymap.c
  34. keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
  35. # Generate the keymap.json
  36. try:
  37. keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'])
  38. except KeyError:
  39. cli.log.error('Something went wrong. Try to use --no-cpp.')
  40. sys.exit(1)
  41. if cli.args.output:
  42. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  43. if cli.args.output.exists():
  44. cli.args.output.replace(cli.args.output.name + '.bak')
  45. cli.args.output.write_text(json.dumps(keymap_json))
  46. if not cli.args.quiet:
  47. cli.log.info('Wrote keymap to %s.', cli.args.output)
  48. else:
  49. print(json.dumps(keymap_json))