api_get.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import re
  2. import io
  3. import os
  4. def get_file_list(paths, ext = ".c"):
  5. file_list = []
  6. for path in paths:
  7. for home, _, files in os.walk(path):
  8. for filename in files:
  9. if filename.endswith(ext):
  10. file_list.append(os.path.join(home, filename))
  11. return file_list
  12. #注释的格式:
  13. # /*
  14. # @module 模块的调用名
  15. # @summary 模块的简短描述信息
  16. # @version 版本号,可选
  17. # @data 日期,可选
  18. # */
  19. # /*
  20. # @api/function module.function(调用时用到的完整函数名)
  21. # @string 第一个参数,@后跟参数类型,空格后跟参数解释
  22. # @number[opt=nil] 第二个参数,默认值为nil
  23. # @table[opt={}] 第三个参数,默认值为{}
  24. # ...根据实际,列出所有参数
  25. # @return 类型 返回的第一个值,这里是解释
  26. # @return string 返回的第二个值,类型为string
  27. # ...根据实际,列处所有返回值
  28. # @demo demo路径
  29. # @video 视频链接
  30. # @usage
  31. # --使用的例子,可多行
  32. # lcoal a,b,c = module.function("test",nil,{1,2,3})
  33. # */
  34. # static int l_module_function(lua_State *L) {
  35. # //一堆代码
  36. # }
  37. #
  38. #//@const NONE number 无校验
  39. ########################################################
  40. #数据结构:
  41. # modules = [
  42. # {
  43. # 'module': 'adc',
  44. # 'summary': '数模转换',
  45. # 'url': 'https://xxxxxx',
  46. # 'demo': 'adc',
  47. # 'video': 'https://xxxxx',
  48. # 'usage': '--xxxxxxx',
  49. # 'const': [
  50. # {
  51. # 'var':'uart.NONE',
  52. # 'type':'number',
  53. # 'summary':'无校验',
  54. # },
  55. # ],
  56. # 'api':[
  57. # {
  58. # 'api':'adc.read(id)',
  59. # 'summary': '读取adc通道',
  60. # 'args': [
  61. # {
  62. # 'type': 'int',
  63. # 'summary': '通道id,与具体设备有关,通常从0开始'
  64. # }
  65. # ],
  66. # 'return': [
  67. # {
  68. # 'type': 'int',
  69. # 'summary': '原始值'
  70. # },
  71. # {
  72. # 'type': 'int',
  73. # 'summary': '计算后的值'
  74. # }
  75. # ],
  76. # 'usage': '-- 打开adc通道2,并读取\nif adc.open(2) then...'
  77. # },
  78. # ]
  79. # }
  80. # ]
  81. def get_modules(file_list, start="/*", end="*/"):
  82. modules = []
  83. for file in file_list:
  84. text = ""
  85. try:
  86. f = io.open(file,"r",encoding="utf-8")
  87. text = f.read()
  88. f.close()
  89. except:
  90. #print("read fail, maybe not use utf8")
  91. continue
  92. module = {}
  93. file = file.replace("\\","/")
  94. if file.rfind("luat/") >= 0:
  95. file = file[file.rfind("luat/"):]
  96. module["url"] = "https://gitee.com/openLuat/LuatOS/tree/master/"+file
  97. else:
  98. module["url"] = ""
  99. # 注释头
  100. r = re.search(re.escape(start) + r" *\n *@module *(\w+)\n *@summary *(.+)\n",text,re.I|re.M)
  101. if r:
  102. module["module"] = r.group(1)
  103. module["summary"] = r.group(2)
  104. module["usage"] = ""
  105. module["demo"] = ""
  106. module["video"] = ""
  107. module["api"] = []
  108. module["const"] = []
  109. else:
  110. continue
  111. for mstep in range(len(modules)-1,-1,-1):
  112. if modules[mstep]["module"] == module["module"]:
  113. module["api"] = modules[mstep]["api"]
  114. del modules[mstep]
  115. module["url"] = ""
  116. #后面的数据
  117. lines = text.splitlines()
  118. line_now = 0
  119. isGotApi = False #是否已经有过接口? 或者是否第一段注释已结束?
  120. while line_now<len(lines)-3:
  121. if lines[line_now].find(end) >= 0:
  122. isGotApi = True #第一段注释结束了,不用找例子了
  123. if not isGotApi:#库自带的例子
  124. if re.search(" *@demo *.+",lines[line_now],re.I):
  125. module["demo"] = "https://gitee.com/openLuat/LuatOS/tree/master/demo/"
  126. module["demo"] += re.search(" *@demo * (.+) *",lines[line_now],re.I).group(1)
  127. line_now+=1
  128. continue
  129. if re.search(" *@video *.+",lines[line_now],re.I):
  130. module["video"] = re.search(" *@video * (.+) *",lines[line_now],re.I).group(1)
  131. line_now+=1
  132. continue
  133. if re.search(" *@usage *",lines[line_now],re.I):
  134. line_now+=1
  135. while lines[line_now].find(end) < 0:
  136. module["usage"] += lines[line_now]+"\n"
  137. line_now+=1
  138. isGotApi = True
  139. continue
  140. #匹配api完整名称行
  141. name = re.search(r" *@api *(.+) *",lines[line_now+2],re.I)
  142. if not name:
  143. name = re.search(r" *@function *(.+) *",lines[line_now+2],re.I)
  144. #匹配常量
  145. const_re = re.search(r"[ \-]*//@const +(.+?) +(.+?) +(.+)",lines[line_now],re.I)
  146. if const_re:
  147. const = {}
  148. const["var"] = module["module"]+"."+const_re.group(1)
  149. const["type"] = const_re.group(2)
  150. const["summary"] = const_re.group(3)
  151. module["const"].append(const)
  152. if lines[line_now].startswith(start) and name:
  153. api = {}
  154. api["api"] = name.group(1)
  155. api["summary"] = re.search(r" *(.+) *",lines[line_now+1],re.I).group(1)
  156. line_now += 3
  157. api["args"] = []
  158. api["return"] = []
  159. api["usage"] = ""
  160. arg_re = r" *@([^ ]+) +(.+) *"
  161. return_re = r" *@return *([^ ]+) +(.+) *"
  162. isGotApi = True
  163. #匹配输入参数
  164. while True:
  165. arg = re.search(arg_re,lines[line_now],re.I)
  166. arg_return = re.search(return_re,lines[line_now],re.I)
  167. if arg and not arg_return:
  168. api["args"].append({'type':arg.group(1),'summary':arg.group(2)})
  169. line_now+=1
  170. else:
  171. break
  172. #匹配返回值
  173. while True:
  174. arg = re.search(return_re,lines[line_now],re.I)
  175. if arg:
  176. api["return"].append({'type':arg.group(1),'summary':arg.group(2)})
  177. line_now+=1
  178. else:
  179. break
  180. #匹配用法例子
  181. while True:
  182. arg = re.search(" *@usage *",lines[line_now],re.I)
  183. if arg:
  184. line_now+=1
  185. while lines[line_now].find(end) < 0:
  186. api["usage"] += lines[line_now]+"\n"
  187. line_now+=1
  188. else:
  189. line_now+=2
  190. break
  191. module["api"].append(api)
  192. else:
  193. line_now += 1
  194. #没有api的包,不导入
  195. if len(module["api"]) > 0:
  196. modules.append(module)
  197. print(module["module"])
  198. #按名字排个序
  199. def sorfFnc(k):
  200. return k["module"]
  201. modules.sort(key=sorfFnc)
  202. return modules