logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

info.py (9177B)


  1. """Keyboard information script.
  2. Compile an info.json for a particular keyboard and pretty-print it.
  3. """
  4. import sys
  5. import json
  6. from milc import cli
  7. from qmk.json_encoders import InfoJSONEncoder
  8. from qmk.constants import COL_LETTERS, ROW_LETTERS
  9. from qmk.decorators import automagic_keyboard, automagic_keymap
  10. from qmk.keyboard import keyboard_completer, keyboard_folder, render_layouts, render_layout, rules_mk
  11. from qmk.info import info_json, keymap_json
  12. from qmk.keymap import locate_keymap
  13. from qmk.path import is_keyboard
  14. UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith('utf')
  15. def _strip_api_content(info_json):
  16. # Ideally this would only be added in the API pathway.
  17. info_json.pop('platform', None)
  18. info_json.pop('platform_key', None)
  19. info_json.pop('processor_type', None)
  20. info_json.pop('protocol', None)
  21. info_json.pop('config_h_features', None)
  22. info_json.pop('keymaps', None)
  23. info_json.pop('keyboard_folder', None)
  24. info_json.pop('parse_errors', None)
  25. info_json.pop('parse_warnings', None)
  26. for layout in info_json.get('layouts', {}).values():
  27. layout.pop('filename', None)
  28. layout.pop('c_macro', None)
  29. layout.pop('json_layout', None)
  30. if 'matrix_pins' in info_json:
  31. info_json.pop('matrix_size', None)
  32. for feature in ['rgb_matrix', 'led_matrix']:
  33. if info_json.get(feature, {}).get("layout", None):
  34. info_json[feature].pop('led_count', None)
  35. return info_json
  36. def show_keymap(kb_info_json, title_caps=True):
  37. """Render the keymap in ascii art.
  38. """
  39. keymap_path = locate_keymap(cli.config.info.keyboard, cli.config.info.keymap)
  40. if keymap_path and keymap_path.suffix == '.json':
  41. keymap_data = json.load(keymap_path.open(encoding='utf-8'))
  42. # cater for layout-less keymap.json
  43. if 'layout' not in keymap_data:
  44. return
  45. layout_name = keymap_data['layout']
  46. layout_name = kb_info_json.get('layout_aliases', {}).get(layout_name, layout_name) # Resolve alias names
  47. for layer_num, layer in enumerate(keymap_data['layers']):
  48. if title_caps:
  49. cli.echo('{fg_cyan}Keymap %s Layer %s{fg_reset}:', cli.config.info.keymap, layer_num)
  50. else:
  51. cli.echo('{fg_cyan}keymap.%s.layer.%s{fg_reset}:', cli.config.info.keymap, layer_num)
  52. print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, layer))
  53. def show_layouts(kb_info_json, title_caps=True):
  54. """Render the layouts with info.json labels.
  55. """
  56. for layout_name, layout_art in render_layouts(kb_info_json, cli.config.info.ascii).items():
  57. title = f'Layout {layout_name.title()}' if title_caps else f'layouts.{layout_name}'
  58. cli.echo('{fg_cyan}%s{fg_reset}:', title)
  59. print(layout_art) # Avoid passing dirty data to cli.echo()
  60. def show_matrix(kb_info_json, title_caps=True):
  61. """Render the layout with matrix labels in ascii art.
  62. """
  63. for layout_name, layout in kb_info_json['layouts'].items():
  64. # Build our label list
  65. labels = []
  66. for key in layout['layout']:
  67. if 'matrix' in key:
  68. row = ROW_LETTERS[key['matrix'][0]]
  69. col = COL_LETTERS[key['matrix'][1]]
  70. labels.append(row + col)
  71. else:
  72. labels.append('')
  73. # Print the header
  74. if title_caps:
  75. cli.echo('{fg_blue}Matrix for "%s"{fg_reset}:', layout_name)
  76. else:
  77. cli.echo('{fg_blue}matrix_%s{fg_reset}:', layout_name)
  78. print(render_layout(kb_info_json['layouts'][layout_name]['layout'], cli.config.info.ascii, labels))
  79. def print_friendly_output(kb_info_json):
  80. """Print the info.json in a friendly text format.
  81. """
  82. cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
  83. cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
  84. if 'url' in kb_info_json:
  85. cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json.get('url', ''))
  86. if kb_info_json.get('maintainer', 'qmk') == 'qmk':
  87. cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
  88. else:
  89. cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json['maintainer'])
  90. cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  91. cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
  92. cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
  93. if 'layout_aliases' in kb_info_json:
  94. aliases = [f'{key}={value}' for key, value in kb_info_json['layout_aliases'].items()]
  95. cli.echo('{fg_blue}Layout aliases:{fg_reset} %s' % (', '.join(aliases),))
  96. def print_text_output(kb_info_json):
  97. """Print the info.json in a plain text format.
  98. """
  99. for key in sorted(kb_info_json):
  100. if key == 'layouts':
  101. cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  102. else:
  103. cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
  104. if cli.config.info.layouts:
  105. show_layouts(kb_info_json, False)
  106. if cli.config.info.matrix:
  107. show_matrix(kb_info_json, False)
  108. if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
  109. show_keymap(kb_info_json, False)
  110. def print_dotted_output(kb_info_json, prefix=''):
  111. """Print the info.json in a plain text format with dot-joined keys.
  112. """
  113. for key in sorted(kb_info_json):
  114. new_prefix = f'{prefix}.{key}' if prefix else key
  115. if key in ['parse_errors', 'parse_warnings']:
  116. continue
  117. elif key == 'layouts' and prefix == '':
  118. cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  119. elif isinstance(kb_info_json[key], dict):
  120. print_dotted_output(kb_info_json[key], new_prefix)
  121. elif isinstance(kb_info_json[key], list):
  122. cli.echo('{fg_blue}%s{fg_reset}: %s', new_prefix, ', '.join(map(str, sorted(kb_info_json[key]))))
  123. else:
  124. cli.echo('{fg_blue}%s{fg_reset}: %s', new_prefix, kb_info_json[key])
  125. def print_parsed_rules_mk(keyboard_name):
  126. rules = rules_mk(keyboard_name)
  127. for k in sorted(rules.keys()):
  128. print('%s = %s' % (k, rules[k]))
  129. return
  130. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Keyboard to show info for.')
  131. @cli.argument('-km', '--keymap', help='Keymap to show info for (Optional).')
  132. @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
  133. @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
  134. @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
  135. @cli.argument('--ascii', action='store_true', default=not UNICODE_SUPPORT, help='Render layout box drawings in ASCII only.')
  136. @cli.argument('-r', '--rules-mk', action='store_true', help='Render the parsed values of the keyboard\'s rules.mk file.')
  137. @cli.argument('-a', '--api', action='store_true', help='Show fully processed info intended for API consumption.')
  138. @cli.subcommand('Keyboard information.')
  139. @automagic_keyboard
  140. @automagic_keymap
  141. def info(cli):
  142. """Compile an info.json for a particular keyboard and pretty-print it.
  143. """
  144. # Determine our keyboard(s)
  145. if not cli.config.info.keyboard:
  146. cli.log.error('Missing parameter: --keyboard')
  147. cli.subcommands['info'].print_help()
  148. return False
  149. if not is_keyboard(cli.config.info.keyboard):
  150. cli.log.error('Invalid keyboard: "%s"', cli.config.info.keyboard)
  151. return False
  152. if bool(cli.args.rules_mk):
  153. print_parsed_rules_mk(cli.config.info.keyboard)
  154. return False
  155. # default keymap stored in config file should be ignored
  156. if cli.config_source.info.keymap == 'config_file':
  157. cli.config_source.info.keymap = None
  158. # Build the info.json file
  159. if cli.config.info.keymap:
  160. kb_info_json = keymap_json(cli.config.info.keyboard, cli.config.info.keymap)
  161. else:
  162. kb_info_json = info_json(cli.config.info.keyboard)
  163. if not cli.args.api:
  164. kb_info_json = _strip_api_content(kb_info_json)
  165. # Output in the requested format
  166. if cli.args.format == 'json':
  167. print(json.dumps(kb_info_json, cls=InfoJSONEncoder, sort_keys=True))
  168. return True
  169. elif cli.args.format == 'text':
  170. print_dotted_output(kb_info_json)
  171. title_caps = False
  172. elif cli.args.format == 'friendly':
  173. print_friendly_output(kb_info_json)
  174. title_caps = True
  175. else:
  176. cli.log.error('Unknown format: %s', cli.args.format)
  177. return False
  178. # Output requested extras
  179. if cli.config.info.layouts:
  180. show_layouts(kb_info_json, title_caps)
  181. if cli.config.info.matrix:
  182. show_matrix(kb_info_json, title_caps)
  183. if cli.config.info.keymap:
  184. show_keymap(kb_info_json, title_caps)