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.

155 lines
5.9 KiB

  1. """Keyboard information script.
  2. Compile an info.json for a particular keyboard and pretty-print it.
  3. """
  4. import json
  5. from milc import cli
  6. from qmk.decorators import automagic_keyboard, automagic_keymap
  7. from qmk.keyboard import render_layouts, render_layout
  8. from qmk.keymap import locate_keymap
  9. from qmk.info import info_json
  10. from qmk.path import is_keyboard
  11. ROW_LETTERS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop'
  12. COL_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijilmnopqrstuvwxyz'
  13. def show_keymap(kb_info_json, title_caps=True):
  14. """Render the keymap in ascii art.
  15. """
  16. keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
  17. if keymap_path and keymap_path.suffix == '.json':
  18. if title_caps:
  19. cli.echo('{fg_blue}Keymap "%s"{fg_reset}:', cli.config.info.keymap)
  20. else:
  21. cli.echo('{fg_blue}keymap_%s{fg_reset}:', cli.config.info.keymap)
  22. keymap_data = json.load(keymap_path.open())
  23. layout_name = keymap_data['layout']
  24. for layer_num, layer in enumerate(keymap_data['layers']):
  25. if title_caps:
  26. cli.echo('{fg_cyan}Layer %s{fg_reset}:', layer_num)
  27. else:
  28. cli.echo('{fg_cyan}layer_%s{fg_reset}:', layer_num)
  29. print(render_layout(kb_info_json['layouts'][layout_name]['layout'], layer))
  30. def show_layouts(kb_info_json, title_caps=True):
  31. """Render the layouts with info.json labels.
  32. """
  33. for layout_name, layout_art in render_layouts(kb_info_json).items():
  34. title = layout_name.title() if title_caps else layout_name
  35. cli.echo('{fg_cyan}%s{fg_reset}:', title)
  36. print(layout_art) # Avoid passing dirty data to cli.echo()
  37. def show_matrix(kb_info_json, title_caps=True):
  38. """Render the layout with matrix labels in ascii art.
  39. """
  40. for layout_name, layout in kb_info_json['layouts'].items():
  41. # Build our label list
  42. labels = []
  43. for key in layout['layout']:
  44. if key['matrix']:
  45. row = ROW_LETTERS[key['matrix'][0]]
  46. col = COL_LETTERS[key['matrix'][1]]
  47. labels.append(row + col)
  48. else:
  49. labels.append('')
  50. # Print the header
  51. if title_caps:
  52. cli.echo('{fg_blue}Matrix for "%s"{fg_reset}:', layout_name)
  53. else:
  54. cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name)
  55. print(render_layout(kb_info_json['layouts'][layout_name]['layout'], labels))
  56. def print_friendly_output(kb_info_json):
  57. """Print the info.json in a friendly text format.
  58. """
  59. cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
  60. cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
  61. if 'url' in kb_info_json:
  62. cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json.get('url', ''))
  63. if kb_info_json.get('maintainer', 'qmk') == 'qmk':
  64. cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
  65. else:
  66. cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer'])
  67. cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown'))
  68. cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  69. if 'width' in kb_info_json and 'height' in kb_info_json:
  70. cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height']))
  71. cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
  72. cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
  73. if cli.config.info.layouts:
  74. show_layouts(kb_info_json, True)
  75. if cli.config.info.matrix:
  76. show_matrix(kb_info_json, True)
  77. if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
  78. show_keymap(kb_info_json, True)
  79. def print_text_output(kb_info_json):
  80. """Print the info.json in a plain text format.
  81. """
  82. for key in sorted(kb_info_json):
  83. if key == 'layouts':
  84. cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  85. else:
  86. cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
  87. if cli.config.info.layouts:
  88. show_layouts(kb_info_json, False)
  89. if cli.config.info.matrix:
  90. show_matrix(kb_info_json, False)
  91. if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
  92. show_keymap(kb_info_json, False)
  93. @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
  94. @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
  95. @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
  96. @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
  97. @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
  98. @cli.subcommand('Keyboard information.')
  99. @automagic_keyboard
  100. @automagic_keymap
  101. def info(cli):
  102. """Compile an info.json for a particular keyboard and pretty-print it.
  103. """
  104. # Determine our keyboard(s)
  105. if not cli.config.info.keyboard:
  106. cli.log.error('Missing paramater: --keyboard')
  107. cli.subcommands['info'].print_help()
  108. return False
  109. if not is_keyboard(cli.config.info.keyboard):
  110. cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
  111. return False
  112. # Build the info.json file
  113. kb_info_json = info_json(cli.config.info.keyboard)
  114. # Output in the requested format
  115. if cli.args.format == 'json':
  116. print(json.dumps(kb_info_json))
  117. elif cli.args.format == 'text':
  118. print_text_output(kb_info_json)
  119. elif cli.args.format == 'friendly':
  120. print_friendly_output(kb_info_json)
  121. else:
  122. cli.log.error('Unknown format: %s', cli.args.format)
  123. return False