generator.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """This script generates the XAP protocol documentation.
  2. """
  3. import hjson
  4. from qmk.constants import QMK_FIRMWARE
  5. from qmk.xap.common import get_xap_definition_files, update_xap_definitions
  6. def _update_type_docs(overall):
  7. defs = overall['type_docs']
  8. type_docs = []
  9. for (k, v) in sorted(defs.items(), key=lambda x: x[0]):
  10. type_docs.append(f'| _{k}_ | {v} |')
  11. desc_str = "\n".join(type_docs)
  12. overall['documentation']['!type_docs!'] = f'''\
  13. | Name | Definition |
  14. | -- | -- |
  15. {desc_str}
  16. '''
  17. def _update_term_definitions(overall):
  18. defs = overall['term_definitions']
  19. term_descriptions = []
  20. for (k, v) in sorted(defs.items(), key=lambda x: x[0]):
  21. term_descriptions.append(f'| _{k}_ | {v} |')
  22. desc_str = "\n".join(term_descriptions)
  23. overall['documentation']['!term_definitions!'] = f'''\
  24. | Name | Definition |
  25. | -- | -- |
  26. {desc_str}
  27. '''
  28. def _update_response_flags(overall):
  29. flags = overall['response_flags']['bits']
  30. for n in range(0, 8):
  31. if str(n) not in flags:
  32. flags[str(n)] = {"name": "-", "description": "-"}
  33. header = '| ' + " | ".join([f'Bit {n}' for n in range(7, -1, -1)]) + ' |'
  34. dividers = '|' + "|".join(['--' for n in range(7, -1, -1)]) + '|'
  35. bit_names = '| ' + " | ".join([flags[str(n)]['name'] for n in range(7, -1, -1)]) + ' |'
  36. bit_descriptions = ''
  37. for n in range(7, -1, -1):
  38. bit_desc = flags[str(n)]
  39. if bit_desc['name'] != '-':
  40. desc = bit_desc['description']
  41. bit_descriptions = bit_descriptions + f'\n* `Bit {n}`: {desc}'
  42. overall['documentation']['!response_flags!'] = f'''\
  43. {header}
  44. {dividers}
  45. {bit_names}
  46. {bit_descriptions}
  47. '''
  48. def generate_docs():
  49. """Generates the XAP protocol documentation by merging the definitions files, and producing the corresponding Markdown document under `/docs/`.
  50. """
  51. docs_list = []
  52. overall = None
  53. for file in get_xap_definition_files():
  54. overall = update_xap_definitions(overall, hjson.load(file.open(encoding='utf-8')))
  55. try:
  56. if 'type_docs' in overall:
  57. _update_type_docs(overall)
  58. if 'term_definitions' in overall:
  59. _update_term_definitions(overall)
  60. if 'response_flags' in overall:
  61. _update_response_flags(overall)
  62. except:
  63. print(hjson.dumps(overall))
  64. exit(1)
  65. output_doc = QMK_FIRMWARE / "docs" / f"{file.stem}.md"
  66. docs_list.append(output_doc)
  67. with open(output_doc, "w", encoding='utf-8') as out_file:
  68. for e in overall['documentation']['order']:
  69. out_file.write(overall['documentation'][e].strip())
  70. out_file.write('\n\n')
  71. output_doc = QMK_FIRMWARE / "docs" / f"xap_protocol.md"
  72. with open(output_doc, "w", encoding='utf-8') as out_file:
  73. out_file.write('''\
  74. # XAP Protocol Reference
  75. ''')
  76. for file in reversed(sorted(docs_list)):
  77. ver = file.stem[4:]
  78. out_file.write(f'* [XAP Version {ver}]({file.name})\n')