info.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(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(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(info_json, title_caps=True):
  38. """Render the layout with matrix labels in ascii art.
  39. """
  40. for layout_name, layout in 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(info_json['layouts'][layout_name]['layout'], labels))
  56. @cli.argument('-kb', '--keyboard', help='Keyboard to show info for.')
  57. @cli.argument('-km', '--keymap', help='Show the layers for a JSON keymap too.')
  58. @cli.argument('-l', '--layouts', action='store_true', help='Render the layouts.')
  59. @cli.argument('-m', '--matrix', action='store_true', help='Render the layouts with matrix information.')
  60. @cli.argument('-f', '--format', default='friendly', arg_only=True, help='Format to display the data in (friendly, text, json) (Default: friendly).')
  61. @cli.subcommand('Keyboard information.')
  62. @automagic_keyboard
  63. @automagic_keymap
  64. def info(cli):
  65. """Compile an info.json for a particular keyboard and pretty-print it.
  66. """
  67. # Determine our keyboard(s)
  68. if not is_keyboard(cli.config.info.keyboard):
  69. cli.log.error('Invalid keyboard: %s!', cli.config.info.keyboard)
  70. exit(1)
  71. # Build the info.json file
  72. kb_info_json = info_json(cli.config.info.keyboard)
  73. # Output in the requested format
  74. if cli.args.format == 'json':
  75. print(json.dumps(kb_info_json))
  76. exit()
  77. if cli.args.format == 'text':
  78. for key in sorted(kb_info_json):
  79. if key == 'layouts':
  80. cli.echo('{fg_blue}layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  81. else:
  82. cli.echo('{fg_blue}%s{fg_reset}: %s', key, kb_info_json[key])
  83. if cli.config.info.layouts:
  84. show_layouts(kb_info_json, False)
  85. if cli.config.info.matrix:
  86. show_matrix(kb_info_json, False)
  87. if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
  88. show_keymap(kb_info_json, False)
  89. elif cli.args.format == 'friendly':
  90. cli.echo('{fg_blue}Keyboard Name{fg_reset}: %s', kb_info_json.get('keyboard_name', 'Unknown'))
  91. cli.echo('{fg_blue}Manufacturer{fg_reset}: %s', kb_info_json.get('manufacturer', 'Unknown'))
  92. if 'url' in kb_info_json:
  93. cli.echo('{fg_blue}Website{fg_reset}: %s', kb_info_json['url'])
  94. if kb_info_json.get('maintainer') == 'qmk':
  95. cli.echo('{fg_blue}Maintainer{fg_reset}: QMK Community')
  96. else:
  97. cli.echo('{fg_blue}Maintainer{fg_reset}: %s', kb_info_json.get('maintainer', 'qmk'))
  98. cli.echo('{fg_blue}Keyboard Folder{fg_reset}: %s', kb_info_json.get('keyboard_folder', 'Unknown'))
  99. cli.echo('{fg_blue}Layouts{fg_reset}: %s', ', '.join(sorted(kb_info_json['layouts'].keys())))
  100. if 'width' in kb_info_json and 'height' in kb_info_json:
  101. cli.echo('{fg_blue}Size{fg_reset}: %s x %s' % (kb_info_json['width'], kb_info_json['height']))
  102. cli.echo('{fg_blue}Processor{fg_reset}: %s', kb_info_json.get('processor', 'Unknown'))
  103. cli.echo('{fg_blue}Bootloader{fg_reset}: %s', kb_info_json.get('bootloader', 'Unknown'))
  104. if cli.config.info.layouts:
  105. show_layouts(kb_info_json, True)
  106. if cli.config.info.matrix:
  107. show_matrix(kb_info_json, True)
  108. if cli.config_source.info.keymap and cli.config_source.info.keymap != 'config_file':
  109. show_keymap(kb_info_json, True)
  110. else:
  111. cli.log.error('Unknown format: %s', cli.args.format)