sys_pub.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. import os
  5. import io
  6. import re
  7. import json
  8. import shutil
  9. source_path = r"../luat"
  10. if len(sys.argv) >= 3:
  11. source_path = sys.argv[1]
  12. print("path:")
  13. print(source_path)
  14. file_list = []
  15. for home, dirs, files in os.walk(source_path):
  16. for filename in files:
  17. if filename.endswith(".c"):
  18. file_list.append(os.path.join(home, filename))
  19. for home, dirs, files in os.walk(source_path+"/../lua"):
  20. for filename in files:
  21. if filename.endswith(".c"):
  22. file_list.append(os.path.join(home, filename))
  23. for home, dirs, files in os.walk(source_path+"/../components"):
  24. for filename in files:
  25. if filename.endswith(".c"):
  26. file_list.append(os.path.join(home, filename))
  27. for home, dirs, files in os.walk(source_path+"/../bsp/rtt"):
  28. for filename in files:
  29. if filename.endswith(".c"):
  30. file_list.append(os.path.join(home, filename))
  31. #注释的格式:
  32. # /*
  33. # @sys_pub mod
  34. # 第一行写明消息的用途,如:WIFI扫描结束
  35. # WLAN_SCAN_DONE (该topic的完整名称)
  36. # @string 第一个传递的数据,@后跟数据类型,空格后跟数据解释,如果没有就别写这几行
  37. # @number 第二个数据
  38. # ...根据实际,列出所有传递的数据
  39. # @usage
  40. # --使用的例子,可多行
  41. # sys.taskInit(function()
  42. # xxxxxxxxxx
  43. # xxxxxxx
  44. # sys.waitUntil("WLAN_SCAN_DONE")
  45. # xxxxxxxxxx
  46. # end)
  47. # */
  48. modules = []
  49. #数据结构:
  50. # modules = {
  51. # 'mod': [
  52. # {
  53. # 'topic':'WLAN_SCAN_DONE',
  54. # 'summary': 'WIFI扫描结束',
  55. # 'return': [
  56. # {
  57. # 'type': 'string',
  58. # 'summary': '第一个传递的数据'
  59. # },
  60. # {
  61. # 'type': 'number',
  62. # 'summary': '第二个数据'
  63. # }
  64. # ],
  65. # 'usage': 'sys.taskInit(function()...'
  66. # },
  67. # ...
  68. # ],
  69. # ...
  70. # }
  71. print("found %d files" % len(file_list))
  72. modules = {}
  73. for file in file_list:
  74. text = ""
  75. try:
  76. f = io.open(file,"r",encoding="utf-8")
  77. text = f.read()
  78. f.close()
  79. except:
  80. #print("read %s fail, maybe not use utf8" % file)
  81. continue
  82. #后面的数据
  83. lines = text.splitlines()
  84. line_now = 0
  85. while line_now<len(lines)-3:
  86. #匹配api完整名称行
  87. name = re.search(r" *@sys_pub *(.+) *",lines[line_now+1],re.I)
  88. if lines[line_now].startswith("/*") and name:
  89. mod = name.group(1)#模块名
  90. api = {}
  91. api["topic"] = re.search(r" *(.+) *",lines[line_now+3],re.I).group(1)
  92. api["summary"] = re.search(r" *(.+) *",lines[line_now+2],re.I).group(1)
  93. line_now += 4
  94. api["return"] = []
  95. api["usage"] = ""
  96. arg_re = r" *@([^ ]+) +(.+) *"
  97. usage_re = r" *@usage *"
  98. #匹配返回参数
  99. while True:
  100. arg = re.search(arg_re,lines[line_now],re.I)
  101. arg_return = re.search(usage_re,lines[line_now],re.I)
  102. if arg and not arg_return:
  103. api["return"].append({'type':arg.group(1),'summary':arg.group(2)})
  104. line_now+=1
  105. else:
  106. break
  107. #匹配用法例子
  108. while True:
  109. arg = re.search(usage_re,lines[line_now],re.I)
  110. if arg:
  111. line_now+=1
  112. while lines[line_now].find("*/") < 0:
  113. api["usage"] += lines[line_now]+"\n"
  114. line_now+=1
  115. else:
  116. line_now+=2
  117. break
  118. if not (mod in modules):
  119. modules[mod] = []
  120. modules[mod].append(api)
  121. else:
  122. line_now += 1
  123. ################## 接口数据提取完毕 ##################
  124. doc = open("../../luatos-wiki/api/sys_pub.md", "a+",encoding='utf-8')
  125. doc.write("# 📮 sys系统消息\n")
  126. doc.write("\n\n")
  127. doc.write("此处列举了LuatOS框架中自带的系统消息列表\n\n")
  128. doc.write("\n\n")
  129. for _, (name,module) in enumerate(modules.items()):
  130. doc.write("## "+name+"\n\n")
  131. doc.write("\n\n")
  132. doc.write("["+name+"接口文档页](https://wiki.luatos.com/api/"+name+".html)\n\n")
  133. doc.write("\n\n")
  134. for pub in module:
  135. doc.write("### "+pub["topic"]+"\n\n")
  136. doc.write(pub["summary"]+"\n\n")
  137. doc.write("**额外返回参数**\n\n")
  138. if len(pub["return"]) > 0:
  139. doc.write("|返回参数类型|解释|\n|-|-|\n")
  140. for arg in pub["return"]:
  141. doc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  142. doc.write("\n")
  143. else:
  144. doc.write("无\n\n")
  145. doc.write("**例子**\n\n")
  146. if len(pub["usage"]) == 0:
  147. doc.write("无\n\n")
  148. else:
  149. doc.write("```lua\n"+pub["usage"]+"\n```\n\n")
  150. doc.write("---\n\n")
  151. doc.close()