documentGenerator.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. snippet_path = r"snippet.json"
  11. if len(sys.argv) >= 3:
  12. source_path = sys.argv[1]
  13. snippet_path = sys.argv[2]
  14. print("path:")
  15. print(source_path)
  16. print(snippet_path)
  17. file_list = []
  18. for home, dirs, files in os.walk(source_path):
  19. for filename in files:
  20. if filename.endswith(".c"):
  21. file_list.append(os.path.join(home, filename))
  22. for home, dirs, files in os.walk(source_path+"/../lua"):
  23. for filename in files:
  24. if filename.endswith(".c"):
  25. file_list.append(os.path.join(home, filename))
  26. for home, dirs, files in os.walk(source_path+"/../components"):
  27. for filename in files:
  28. if filename.endswith(".c"):
  29. file_list.append(os.path.join(home, filename))
  30. #注释的格式:
  31. # /*
  32. # @module 模块的调用名
  33. # @summary 模块的简短描述信息
  34. # @version 版本号,可选
  35. # @data 日期,可选
  36. # */
  37. # /*
  38. # @api/function module.function(调用时用到的完整函数名)
  39. # @string 第一个参数,@后跟参数类型,空格后跟参数解释
  40. # @number[opt=nil] 第二个参数,默认值为nil
  41. # @table[opt={}] 第三个参数,默认值为{}
  42. # ...根据实际,列出所有参数
  43. # @return 类型 返回的第一个值,这里是解释
  44. # @return string 返回的第二个值,类型为string
  45. # ...根据实际,列处所有返回值
  46. # @usage
  47. # --使用的例子,可多行
  48. # lcoal a,b,c = module.function("test",nil,{1,2,3})
  49. # */
  50. # static int l_module_function(lua_State *L) {
  51. # //一堆代码
  52. # }
  53. modules = []
  54. #数据结构:
  55. # modules = [
  56. # {
  57. # 'module': 'adc',
  58. # 'summary': '数模转换',
  59. # 'url': 'https://xxxxxx',
  60. # 'api':[
  61. # {
  62. # 'api':'adc.read(id)',
  63. # 'summary': '读取adc通道',
  64. # 'args': [
  65. # {
  66. # 'type': 'int',
  67. # 'summary': '通道id,与具体设备有关,通常从0开始'
  68. # }
  69. # ],
  70. # 'return': [
  71. # {
  72. # 'type': 'int',
  73. # 'summary': '原始值'
  74. # },
  75. # {
  76. # 'type': 'int',
  77. # 'summary': '计算后的值'
  78. # }
  79. # ],
  80. # 'usage': '-- 打开adc通道2,并读取\nif adc.open(2) then...'
  81. # },
  82. # ]
  83. # }
  84. # ]
  85. print("found %d files" % len(file_list))
  86. modules = []
  87. for file in file_list:
  88. text = ""
  89. try:
  90. f = io.open(file,"r",encoding="utf-8")
  91. text = f.read()
  92. f.close()
  93. except:
  94. #print("read fail, maybe not use utf8")
  95. continue
  96. module = {}
  97. file = file.replace("\\","/")
  98. if file.rfind("luat/") >= 0:
  99. file = file[file.rfind("luat/"):]
  100. module["url"] = "https://gitee.com/openLuat/LuatOS/tree/master/"+file
  101. else:
  102. module["url"] = ""
  103. # 注释头
  104. r = re.search(r"/\* *\n *@module *(\w+)\n *@summary *(.+)\n",text,re.I|re.M)
  105. if r:
  106. module["module"] = r.group(1)
  107. module["summary"] = r.group(2)
  108. module["api"] = []
  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. #后面的数据
  116. lines = text.splitlines()
  117. line_now = 0
  118. while line_now<len(lines)-3:
  119. #匹配api完整名称行
  120. name = re.search(r" *@api *(.+) *",lines[line_now+2],re.I)
  121. if lines[line_now].startswith("/*") and name:
  122. api = {}
  123. api["api"] = name.group(1)
  124. api["summary"] = re.search(r" *(.+) *",lines[line_now+1],re.I).group(1)
  125. line_now += 3
  126. api["args"] = []
  127. api["return"] = []
  128. api["usage"] = ""
  129. arg_re = r" *@([^ ]+) +(.+) *"
  130. return_re = r" *@return *([^ ]+) +(.+) *"
  131. #匹配输入参数
  132. while True:
  133. arg = re.search(arg_re,lines[line_now],re.I)
  134. arg_return = re.search(return_re,lines[line_now],re.I)
  135. if arg and not arg_return:
  136. api["args"].append({'type':arg.group(1),'summary':arg.group(2)})
  137. line_now+=1
  138. else:
  139. break
  140. #匹配返回值
  141. while True:
  142. arg = re.search(return_re,lines[line_now],re.I)
  143. if arg:
  144. api["return"].append({'type':arg.group(1),'summary':arg.group(2)})
  145. line_now+=1
  146. else:
  147. break
  148. #匹配用法例子
  149. while True:
  150. arg = re.search(" *@usage *",lines[line_now],re.I)
  151. if arg:
  152. line_now+=1
  153. while lines[line_now].find("*/") < 0:
  154. api["usage"] += lines[line_now]+"\n"
  155. line_now+=1
  156. else:
  157. line_now+=2
  158. break
  159. module["api"].append(api)
  160. else:
  161. line_now += 1
  162. #没有api的包,不导入
  163. if len(module["api"]) > 0:
  164. modules.append(module)
  165. print(module["module"])
  166. #按名字排个序
  167. def sorfFnc(k):
  168. return k["module"]
  169. modules.sort(key=sorfFnc)
  170. ################## 接口数据提取完毕 ##################
  171. #生成自动补全文件
  172. snippet = {'_G': {'body': '_G(${0:...})', 'description': '_G', 'prefix': '_G'}, '_VERSION': {'body': '_VERSION(${0:...})', 'description': '_VERSION', 'prefix': '_VERSION'}, 'assert': {'body': 'assert(${1:v}${2:[, message]})', 'description': 'assert()', 'prefix': 'assert'}, 'collectgarbage': {'body': 'collectgarbage(${1:[opt]}${2:[, arg]})', 'description': 'collectgarbage()', 'prefix': 'collectgarbage'}, 'coroutine.create': {'body': 'coroutine.create( ${1:function} )', 'description': 'coroutine.create', 'prefix': 'coroutine.create'}, 'coroutine.isyieldable': {'body': 'coroutine.isyieldable( )', 'description': 'coroutine.isyieldable', 'prefix': 'coroutine.isyieldable'}, 'coroutine.resume': {'body': 'coroutine.resume( ${1:co}${2:[, val1, ···]} )', 'description': 'coroutine.resume', 'prefix': 'coroutine.resume'}, 'coroutine.running': {'body': 'coroutine.running( )', 'description': 'coroutine.running', 'prefix': 'coroutine.running'}, 'coroutine.status': {'body': 'coroutine.status( ${1:co} )', 'description': 'coroutine.status', 'prefix': 'coroutine.status'}, 'coroutine.wrap': {'body': 'coroutine.wrap( ${1:function} )', 'description': 'coroutine.wrap', 'prefix': 'coroutine.wrap'}, 'coroutine.yield': {'body': 'coroutine.yield( ${1:...} )', 'description': 'coroutine.yield', 'prefix': 'coroutine.yield'}, 'debug.debug': {'body': 'debug.debug()', 'description': 'debug.debug ()', 'prefix': 'debug.debug'}, 'debug.getfenv': {'body': 'debug.getfenv(${0:...})', 'description': 'debug.getfenv (o)', 'prefix': 'debug.getfenv'}, 'debug.gethook': {'body': 'debug.gethook( ${1:[thread]} )', 'description': 'debug.gethook ([thread])', 'prefix': 'debug.gethook'}, 'debug.getinfo': {'body': 'debug.getinfo( ${1:[thread],}${2:f}${3:[, what]} )', 'description': 'debug.getinfo ([thread,] f [, what])', 'prefix': 'debug.getinfo'}, 'debug.getlocal': {'body': 'debug.getlocal( ${1:[thread],}${2:f}${3:[, local]} )', 'description': 'debug.getlocal ([thread,] f, local)', 'prefix': 'debug.getlocal'}, 'debug.getmetatable': {'body': 'debug.getmetatable( ${1:value} )', 'description': 'debug.getmetatable (value)', 'prefix': 'debug.getmetatable'}, 'debug.getregistry': {'body': 'debug.getregistry()', 'description': 'debug.getregistry ()', 'prefix': 'debug.getregistry'}, 'debug.getupvalue': {'body': 'debug.getupvalue( ${1:f}, ${2:up} )', 'description': 'debug.getupvalue (f, up)', 'prefix': 'debug.getupvalue'}, 'debug.getuservalue': {'body': 'debug.getuservalue(${0:...})', 'description': 'debug.getuservalue (u)', 'prefix': 'debug.getuservalue'}, 'debug.getuservalue ': {'body': 'debug.getuservalue ( ${1:u} )', 'description': 'debug.getuservalue (u)', 'prefix': 'debug.getuservalue '}, 'debug.setfenv': {'body': 'debug.setfenv(${0:...})', 'description': 'debug.setfenv (object, table)', 'prefix': 'debug.setfenv'}, 'debug.sethook': {'body': 'debug.sethook( ${1:[thead,]}${2:hook}, ${3:mask}${4:[, count]} )', 'description': 'debug.sethook ([thread,] hook, mask [, count])', 'prefix': 'debug.sethook'}, 'debug.setlocal': {'body': 'debug.setlocal( ${1:[thead,]}${2:level}, ${3:local}, ${4:value} )', 'description': 'debug.setlocal ([thread,] level, local, value)', 'prefix': 'debug.setlocal'}, 'debug.setmetatable': {'body': 'debug.setmetatable( ${1:value}, ${2:table} )', 'description': 'debug.setmetatable (value, table)', 'prefix': 'debug.setmetatable'}, 'debug.setupvalue': {'body': 'debug.setupvalue( ${1:f}, ${2:up}, ${3:value} )', 'description': 'debug.setupvalue (f, up, value)', 'prefix': 'debug.setupvalue'}, 'debug.setuservalue': {'body': 'debug.setuservalue( ${1:udata}, ${2:value} )', 'description': 'debug.setuservalue (udata, value)', 'prefix': 'debug.setuservalue'}, 'debug.traceback': {'body': 'debug.traceback( ${1:[thread,]}${2:[message]}${3:[, level]} )', 'description': 'debug.traceback ([thread,] [message [, level]])', 'prefix': 'debug.traceback'}, 'debug.upvalueid': {'body': 'debug.upvalueid( ${1:f}, ${2:n})', 'description': 'debug.upvalueid (f, n)', 'prefix': 'debug.upvalueid'}, 'debug.upvaluejoin': {'body': 'debug.upvaluejoin( ${1:f1}, ${2:n1}, ${3:f2}, ${4:n2} )', 'description': 'debug.upvaluejoin (f1, n1, f2, n2)', 'prefix': 'debug.upvaluejoin'}, 'dofile': {'body': 'dofile(${1:[filename]})', 'description': 'dofile ([filename])', 'prefix': 'dofile'}, 'elif': {'body': 'else if ${1:condition} then\n\t${0:-- body}\n', 'description': 'elif', 'prefix': 'elif'}, 'error': {'body': 'error(${0:...})', 'description': 'error (message [, level])', 'prefix': 'error'}, 'file:close': {'body': 'file:close(${0:...})', 'description': 'file:close ()', 'prefix': 'file:close'}, 'file:flush': {'body': 'file:flush(${0:...})', 'description': 'file:flush ()', 'prefix': 'file:flush'}, 'file:lines': {'body': 'file:lines(${0:...})', 'description': 'file:lines ()', 'prefix': 'file:lines'}, 'file:read': {'body': 'file:read(${0:...})', 'description': 'file:read (...)', 'prefix': 'file:read'}, 'file:seek': {'body': 'file:seek(${0:...})', 'description': 'file:seek ([whence] [, offset])', 'prefix': 'file:seek'}, 'file:setvbuf': {'body': 'file:setvbuf(${0:...})', 'description': 'file:setvbuf (mode [, size])', 'prefix': 'file:setvbuf'}, 'file:write': {'body': 'file:write(${0:...})', 'description': 'file:write (...)', 'prefix': 'file:write'}, 'for': {'body': 'for ${1:i}=${2:1},${3:10} do\n\t${0:print(i)}\nend', 'description': 'for i=1,10', 'prefix': 'for'}, 'fori': {'body': 'for ${1:i},${2:v} in ipairs(${3:table_name}) do\n\t${0:print(i,v)}\nend', 'description': 'for i,v in ipairs()', 'prefix': 'fori'}, 'forp': {'body': 'for ${1:k},${2:v} in pairs(${3:table_name}) do\n\t${0:print(k,v)}\nend', 'description': 'for k,v in pairs()', 'prefix': 'forp'}, 'fun': {'body': 'function ${1:function_name}( ${2:...} )\n\t${0:-- body}\nend', 'description': 'function', 'prefix': 'fun'}, 'function': {'body': 'function ${1:function_name}( ${2:...} )\n\t${0:-- body}\nend', 'description': 'function', 'prefix': 'function'}, 'getfenv': {'body': 'getfenv(${0:...})', 'description': 'getfenv ([f])', 'prefix': 'getfenv'}, 'getmetatable': {'body': 'getmetatable(${1:object})', 'description': 'getmetatable (object)', 'prefix': 'getmetatable'}, 'if': {'body': 'if ${1:condition} then\n\t${0:-- body}\nend', 'description': 'if', 'prefix': 'if'}, 'ifel': {'body': 'if ${1:condition} then\n\t${2:-- body}\nelse\n\t${0:-- body}\nend', 'description': 'ifel', 'prefix': 'ifel'}, 'io.close': {'body': 'io.close(${0:...})', 'description': 'io.close ([file])', 'prefix': 'io.close'}, 'io.flush': {'body': 'io.flush(${0:...})', 'description': 'io.flush ()', 'prefix': 'io.flush'}, 'io.input': {'body': 'io.input(${0:...})', 'description': 'io.input ([file])', 'prefix': 'io.input'}, 'io.lines': {'body': 'io.lines(${0:...})', 'description': 'io.lines ([filename])', 'prefix': 'io.lines'}, 'io.open': {'body': 'io.open(${0:...})', 'description': 'io.open (filename [, mode])', 'prefix': 'io.open'}, 'io.output': {'body': 'io.output(${0:...})', 'description': 'io.output ([file])', 'prefix': 'io.output'}, 'io.popen': {'body': 'io.popen(${0:...})', 'description': 'io.popen (prog [, mode])', 'prefix': 'io.popen'}, 'io.read': {'body': 'io.read(${0:...})', 'description': 'io.read (...)', 'prefix': 'io.read'}, 'io.tmpfile': {'body': 'io.tmpfile(${0:...})', 'description': 'io.tmpfile ()', 'prefix': 'io.tmpfile'}, 'io.type': {'body': 'io.type(${0:...})', 'description': 'io.type (obj)', 'prefix': 'io.type'}, 'io.write': {'body': 'io.write(${0:...})', 'description': 'io.write (...)', 'prefix': 'io.write'}, 'ipairs': {'body': 'ipairs(${0:...})', 'description': 'ipairs (t)', 'prefix': 'ipairs'}, 'load': {'body': 'load(${0:...})', 'description': 'load (func [, chunkname])', 'prefix': 'load'}, 'loadfile': {'body': 'loadfile(${0:...})', 'description': 'loadfile ([filename])', 'prefix': 'loadfile'}, 'loadstring': {'body': 'loadstring(${0:...})', 'description': 'loadstring (string [, chunkname])', 'prefix': 'loadstring'}, 'local': {'body': 'local ${1:x} = ${0:1}', 'description': 'local x = 1', 'prefix': 'local'}, 'math.abs': {'body': 'math.abs( ${1:x} )', 'description': 'math.abs', 'prefix': 'math.abs'}, 'math.acos': {'body': 'math.acos( ${1:x} )', 'description': 'math.acos', 'prefix': 'math.acos'}, 'math.asin': {'body': 'math.asin( ${1:x} )', 'description': 'math.asin', 'prefix': 'math.asin'}, 'math.atan': {'body': 'math.atan( ${1:y}${2:[, x]} )', 'description': 'math.atan', 'prefix': 'math.atan'}, 'math.atan2': {'body': 'math.atan2(${0:...})', 'description': 'math.atan2 (y, x)', 'prefix': 'math.atan2'}, 'math.ceil': {'body': 'math.ceil( ${1:x} )', 'description': 'math.ceil', 'prefix': 'math.ceil'}, 'math.cos': {'body': 'math.cos( ${1:x} )', 'description': 'math.cos', 'prefix': 'math.cos'}, 'math.cosh': {'body': 'math.cosh(${0:...})', 'description': 'math.cosh (x)', 'prefix': 'math.cosh'}, 'math.deg': {'body': 'math.deg( ${1:x} )', 'description': 'math.deg', 'prefix': 'math.deg'}, 'math.exp': {'body': 'math.exp( ${1:x} )', 'description': 'math.exp', 'prefix': 'math.exp'}, 'math.floor': {'body': 'math.floor( ${1:x} )', 'description': 'math.floor', 'prefix': 'math.floor'}, 'math.fmod': {'body': 'math.fmod( ${1:x},${2:y} )', 'description': 'math.fmod', 'prefix': 'math.fmod'}, 'math.frexp': {'body': 'math.frexp(${0:...})', 'description': 'math.frexp (x)', 'prefix': 'math.frexp'}, 'math.huge': {'body': 'math.huge(${0:...})', 'description': 'math.huge', 'prefix': 'math.huge'}, 'math.ldexp': {'body': 'math.ldexp(${0:...})', 'description': 'math.ldexp (m, e)', 'prefix': 'math.ldexp'}, 'math.log': {'body': 'math.log( ${1:x}${2:[, base]} )', 'description': 'math.log', 'prefix': 'math.log'}, 'math.log10': {'body': 'math.log10(${0:...})', 'description': 'math.log10 (x)', 'prefix': 'math.log10'}, 'math.math.randomseed': {'body': 'math.math.randomseed( ${1:x} )', 'description': 'math.math.randomseed', 'prefix': 'math.math.randomseed'}, 'math.max': {'body': 'math.max( ${1:x},${2:...} )', 'description': 'math.max', 'prefix': 'math.max'}, 'math.maxinteger': {'body': 'math.maxinteger(${0:...})', 'description': 'math.maxinteger', 'prefix': 'math.maxinteger'}, 'math.min': {'body': 'math.min( ${1:x},${2:...} )', 'description': 'math.min', 'prefix': 'math.min'}, 'math.mininteger': {'body': 'math.mininteger(${0:...})', 'description': 'math.mininteger', 'prefix': 'math.mininteger'}, 'math.modf': {'body': 'math.modf( ${1:x} )', 'description': 'math.modf', 'prefix': 'math.modf'}, 'math.pi': {'body': 'math.pi(${0:...})', 'description': 'math.pi', 'prefix': 'math.pi'}, 'math.pow': {'body': 'math.pow(${0:...})', 'description': 'math.pow (x, y)', 'prefix': 'math.pow'}, 'math.rad': {'body': 'math.rad(${0:...})', 'description': 'math.rad (x)', 'prefix': 'math.rad'}, 'math.random': {'body': 'math.random( ${1:[m]}${2:[, n]} )', 'description': 'math.random', 'prefix': 'math.random'}, 'math.randomseed': {'body': 'math.randomseed(${0:...})', 'description': 'math.randomseed (x)', 'prefix': 'math.randomseed'}, 'math.sin': {'body': 'math.sin( ${1:x} )', 'description': 'math.sin', 'prefix': 'math.sin'}, 'math.sinh': {'body': 'math.sinh(${0:...})', 'description': 'math.sinh (x)', 'prefix': 'math.sinh'}, 'math.sqrt': {'body': 'math.sqrt( ${1:x} )', 'description': 'math.sqrt', 'prefix': 'math.sqrt'}, 'math.tan': {'body': 'math.tan( ${1:x} )', 'description': 'math.tan', 'prefix': 'math.tan'}, 'math.tanh': {'body': 'math.tanh(${0:...})', 'description': 'math.tanh (x)', 'prefix': 'math.tanh'}, 'math.tointeger': {'body': 'math.tointeger( ${1:x} )', 'description': 'math.tointeger', 'prefix': 'math.tointeger'}, 'math.type': {'body': 'math.type( ${1:x} )', 'description': 'math.type', 'prefix': 'math.type'}, 'math.ult': {'body': 'math.ult(${0:...})', 'description': 'math.ult (m, n)', 'prefix': 'math.ult'}, 'module': {'body': 'module(${0:...})', 'description': 'module (name [, ...])', 'prefix': 'module'}, 'next': {'body': 'next(${1:table}${2:[, index]})', 'description': 'next (table [, index])', 'prefix': 'next'}, 'os.clock': {'body': 'os.clock(${0:...})', 'description': 'os.clock ()', 'prefix': 'os.clock'}, 'os.date': {'body': 'os.date(${0:...})', 'description': 'os.date ([format [, time]])', 'prefix': 'os.date'}, 'os.difftime': {'body': 'os.difftime(${0:...})', 'description': 'os.difftime (t2, t1)', 'prefix': 'os.difftime'}, 'os.execute': {'body': 'os.execute(${0:...})', 'description': 'os.execute ([command])', 'prefix': 'os.execute'}, 'os.exit': {'body': 'os.exit(${0:...})', 'description': 'os.exit ([code])', 'prefix': 'os.exit'}, 'os.getenv': {'body': 'os.getenv(${0:...})', 'description': 'os.getenv (varname)', 'prefix': 'os.getenv'}, 'os.remove': {'body': 'os.remove(${0:...})', 'description': 'os.remove (filename)', 'prefix': 'os.remove'}, 'os.rename': {'body': 'os.rename(${0:...})', 'description': 'os.rename (oldname, newname)', 'prefix': 'os.rename'}, 'os.setlocale': {'body': 'os.setlocale(${0:...})', 'description': 'os.setlocale (locale [, category])', 'prefix': 'os.setlocale'}, 'os.time': {'body': 'os.time(${0:...})', 'description': 'os.time ([table])', 'prefix': 'os.time'}, 'os.tmpname': {'body': 'os.tmpname(${0:...})', 'description': 'os.tmpname ()', 'prefix': 'os.tmpname'}, 'package.config': {'body': 'package.config(${0:...})', 'description': 'package.config', 'prefix': 'package.config'}, 'package.cpath': {'body': 'package.cpath(${0:...})', 'description': 'package.cpath', 'prefix': 'package.cpath'}, 'package.loaded': {'body': 'package.loaded(${0:...})', 'description': 'package.loaded', 'prefix': 'package.loaded'}, 'package.loaders': {'body': 'package.loaders(${0:...})', 'description': 'package.loaders', 'prefix': 'package.loaders'}, 'package.loadlib': {'body': 'package.loadlib(${0:...})', 'description': 'package.loadlib (libname, funcname)', 'prefix': 'package.loadlib'}, 'package.path': {'body': 'package.path(${0:...})', 'description': 'package.path', 'prefix': 'package.path'}, 'package.preload': {'body': 'package.preload(${0:...})', 'description': 'package.preload', 'prefix': 'package.preload'}, 'package.searchers': {'body': 'package.searchers(${0:...})', 'description': 'package.searchers', 'prefix': 'package.searchers'}, 'package.searchpath': {'body': 'package.searchpath(${0:...})', 'description': 'package.searchpath (name, path [, sep [, rep]])', 'prefix': 'package.searchpath'}, 'package.seeall': {'body': 'package.seeall(${0:...})', 'description': 'package.seeall (module)', 'prefix': 'package.seeall'}, 'pairs': {'body': 'pairs(${0:...})', 'description': 'pairs (t)', 'prefix': 'pairs'}, 'pcall': {'body': 'pcall(${0:...})', 'description': 'pcall (f, arg1, ...)', 'prefix': 'pcall'}, 'print': {'body': 'print(${1:...})', 'description': 'print(...)', 'prefix': 'print'}, 'require': {'body': 'require"${1:module}"', 'description': 'require()', 'prefix': 'require'}, 'ret': {'body': 'return ${1:...}', 'description': 'return ...', 'prefix': 'ret'}, 'select': {'body': 'select(${1:index}, ${2:...})', 'description': 'select (index, ···)', 'prefix': 'select'}, 'setfenv': {'body': 'setfenv(${0:...})', 'description': 'setfenv (f, table)', 'prefix': 'setfenv'}, 'setmetatable': {'body': 'setmetatable(${1:table}, ${2:metatable})', 'description': 'setmetatable (table, metatable)', 'prefix': 'setmetatable'}, 'tonumber': {'body': 'tonumber(${1:e}${2:[, base]})', 'description': 'tonumber (e [, base])', 'prefix': 'tonumber'}, 'tostring': {'body': 'tostring(${1:v})', 'description': 'tostring (v)', 'prefix': 'tostring'}, 'type': {'body': 'type(${1:v})', 'description': 'type (v)', 'prefix': 'type'}, 'unpack': {'body': 'unpack(${0:...})', 'description': 'unpack (list [, i [, j]])', 'prefix': 'unpack'}, 'xpcall': {'body': 'xpcall(${0:...})', 'description': 'xpcall (f, err)', 'prefix': 'xpcall'},"sys.publish(...)": {"body": "sys.publish(${1:...})","description": "发布内部消息,存储在内部消息队列中\r\n返回值:\r\n无\r\n例子:\r\npublish(\"NET_STATUS_IND\")\r\n","prefix": "sys.publish(...)"},"sys.restart(r)": {"body": "sys.restart(${1:r})","description": "软件重启\r\n返回值:\r\n无\r\n例子:\r\nsys.restart('程序超时软件重启')\r\n","prefix": "sys.restart(r)"},"sys.run()": {"body": "sys.run()","description": "run()从底层获取core消息并及时处理相关消息,查询定时器并调度各注册成功的任务线程运行和挂起\r\n返回值:\r\n无\r\n例子:\r\nsys.run()\r\n","prefix": "sys.run()"},"sys.setRollBack(flag, secs)": {"body": "sys.setRollBack(${1:flag},${2: secs})","description": "设置“lua脚本运行出错时,是否回退原始烧写版本”的功能开关。如果没有调用此接口设置,默认回滚\r\n返回值:\r\nnil\r\n例子:\r\nsys.setRollBack(true)\r\nsys.setRollBack(false)\r\n","prefix": "sys.setRollBack(flag, secs)"},"sys.subscribe(id, callback)": {"body": "sys.subscribe(${1:id},${2: callback})","description": "订阅消息\r\n返回值:\r\n无\r\n例子:\r\nsubscribe(\"NET_STATUS_IND\", callback)\r\n","prefix": "sys.subscribe(id, callback)"},"sys.taskInit(fun, ...)": {"body": "sys.taskInit(${1:fun},${2: ...})","description": "创建一个任务线程,在模块最末行调用该函数并注册模块中的任务函数,main.lua导入该模块即可\r\n返回值:\r\nco 返回该任务的线程号\r\n例子:\r\nsys.taskInit(task1,'a','b')\r\n","prefix": "sys.taskInit(fun, ...)"},"sys.timerIsActive(val, ...)": {"body": "sys.timerIsActive(${1:val},${2: ...})","description": "判断某个定时器是否处于开启状态\r\n返回值:\r\nnumber 开启状态返回true,否则nil","prefix": "sys.timerIsActive(val, ...)"},"sys.timerLoopStart(fnc, ms, ...)": {"body": "sys.timerLoopStart(${1:fnc},${2: ms},${3: ...})","description": "开启一个循环定时器\r\n返回值:\r\nnumber 定时器ID,如果失败,返回nil","prefix": "sys.timerLoopStart(fnc, ms, ...)"},"sys.timerStart(fnc, ms, ...)": {"body": "sys.timerStart(${1:fnc},${2: ms},${3: ...})","description": "开启一个定时器\r\n返回值:\r\nnumber 定时器ID,如果失败,返回nil","prefix": "sys.timerStart(fnc, ms, ...)"},"sys.timerStop(val, ...)": {"body": "sys.timerStop(${1:val},${2: ...})","description": "关闭定时器\r\n返回值:\r\n无\r\n例子:\r\ntimerStop(1)\r\n","prefix": "sys.timerStop(val, ...)"},"sys.timerStopAll(fnc)": {"body": "sys.timerStopAll(${1:fnc})","description": "关闭同一回调函数的所有定时器\r\n返回值:\r\n无\r\n例子:\r\ntimerStopAll(cbFnc)\r\n","prefix": "sys.timerStopAll(fnc)"},"sys.unsubscribe(id, callback)": {"body": "sys.unsubscribe(${1:id},${2: callback})","description": "取消订阅消息\r\n返回值:\r\n无\r\n例子:\r\nunsubscribe(\"NET_STATUS_IND\", callback)\r\n","prefix": "sys.unsubscribe(id, callback)"},"sys.wait(ms)": {"body": "sys.wait(${1:ms})","description": "Task任务延时函数,只能用于任务函数中\r\n返回值:\r\n定时结束返回nil,被其他线程唤起返回调用线程传入的参数\r\n例子:\r\nsys.wait(30)\r\n","prefix": "sys.wait(ms)"},"sys.waitUntil(id, ms)": {"body": "sys.waitUntil(${1:id},${2: ms})","description": "Task任务的条件等待函数(包括事件消息和定时器消息等条件),只能用于任务函数中。\r\n返回值:\r\nresult 接收到消息返回true,超时返回false\r\n例子:\r\nresult, data = sys.waitUntil(\"SIM_IND\", 120000)\r\n","prefix": "sys.waitUntil(id, ms)"},"sys.waitUntilExt(id, ms)": {"body": "sys.waitUntilExt(${1:id},${2: ms})","description": "Task任务的条件等待函数扩展(包括事件消息和定时器消息等条件),只能用于任务函数中。\r\n返回值:\r\nmessage 接收到消息返回message,超时返回false\r\n例子:\r\nresult, data = sys.waitUntilExt(\"SIM_IND\", 120000)\r\n","prefix": "sys.waitUntilExt(id, ms)"},"table.concat(t,s)": {"body": "table.concat(${1:t},${2: s})","description": "表连接\r\n返回值:\r\n 字符串\r\n例子:\r\nfruits = {\"banana\",\"orange\",\"apple\"}\r\n-- 返回 table 连接后的字符串\r\nprint(\"连接后的字符串 \",table.concat(fruits))\r\n--连接后的字符串 bananaorangeapple\r\n\r\n-- 指定连接字符\r\nprint(\"连接后的字符串 \",table.concat(fruits,\", \"))\r\n--连接后的字符串 banana, orange, apple\r\n\r\n-- 指定索引来连接 table\r\nprint(\"连接后的字符串 \",table.concat(fruits,\", \", 2,3))\r\n--连接后的字符串 orange, apple\r\n","prefix": "table.concat()"},"table.gsort(t, f)": {"body": "table.gsort(${1:t},${2: f})","description": "返回一个迭代器函数,每次调用函数都会返回hash表的排序后的键值对\r\n返回值:\r\nfunction.\r\n例子:\r\ntest = {a=1,f=9,d=2,c=8,b=5}\r\nfor name,line in pairsByKeys(test) do print(name,line) end\r\n","prefix": "table.gsort(t, f)"},"table.insert(t,d)": {"body": "table.insert(${1:t},${2: d})","description": "添加table参数\r\n返回值:\r\n table\r\n例子:\r\nfruits = {\"banana\",\"orange\",\"apple\"}\r\n\r\n-- 在末尾插入\r\ntable.insert(fruits,\"mango\")\r\nprint(\"索引为 4 的元素为 \",fruits[4])\r\n--索引为 4 的元素为 mango\r\n\r\n-- 在索引为 2 的键处插入\r\ntable.insert(fruits,2,\"grapes\")\r\nprint(\"索引为 2 的元素为 \",fruits[2])\r\n--索引为 2 的元素为 grapes\r\n\r\nprint(\"最后一个元素为 \",fruits[5])\r\n--最后一个元素为 mango\r\n","prefix": "table.insert()"},"table.remove(t,i)": {"body": "table.remove(${1:t},${2: i})","description": "移除table参数\r\n返回值:\r\n table","prefix": "table.remove()"},"string.byte()": {"body": "string.byte()","description": "接收控制字符,返回ASCII码\r\n返回值:\r\n ASCII码\r\n例子:\r\nstring.byte(\"ABCD\") --65\r\nstring.byte(\"ABCD\",4) --68\r\n","prefix": "string.byte()"},"string.char()": {"body": "string.char()","description": "接收ASCII码,返回对应的控制字符\r\n返回值:\r\n 字符\r\n例子:\r\nstring.char(97,98,99,100) --abcd\r\n","prefix": "string.char()"},"string.find()": {"body": "string.find()","description": "查找第一个字符串 s 中匹配到的 pattern\r\n返回值:\r\n 返回匹配字符具体位置。不存在则返回 nil\r\n例子:\r\nstring.find(\"Hello Lua user\", \"Lua\", 1) --7 9\r\n","prefix": "string.find()"},"string.format()": {"body": "string.format()","description": "格式化字符串\r\n返回值:\r\n 字符串\r\n例子:\r\nstring.format(\"the value is:%d\",4) --the value is:4\r\n","prefix": "string.format()"},"string.gsub()": {"body": "string.gsub()","description": "字符串替换","prefix": "string.gsub()"},"string.len()": {"body": "string.len()","description": "计算字符串长度\r\n返回值:\r\n 字符串长度\r\n例子:\r\nstring.len(\"abc\") --3\r\n","prefix": "string.len()"},"string.lower()": {"body": "string.lower()","description": "字符串全部转为小写字母\r\n返回值:\r\n 小写字母字符串","prefix": "string.lower()"},"string.rep()": {"body": "string.rep()","description": "字符串拷贝\r\n返回值:\r\n 字符串\r\n例子:\r\nstring.rep(\"abcd\",2) --abcdabcd\r\n","prefix": "string.rep()"},"string.reverse()": {"body": "string.reverse()","description": "字符串反转\r\n返回值:\r\n 反转后的字符串\r\n例子:\r\nstring.reverse(\"Luat\") --tauL\r\n","prefix": "string.reverse()"},"string.split(str, delimiter)": {"body": "string.split(${1:str},${2: delimiter})","description": "按照指定分隔符分割字符串\r\n返回值:\r\n分割后的字符串列表\r\n例子:\r\n\"123,456,789\":split(',') -> {'123','456','789'}\r\n","prefix": "string.split(str, delimiter)"},"string.upper()": {"body": "string.upper()","description": "字符串全部转为大写字母\r\n返回值:\r\n 大写字母字符串","prefix": "string.upper()"},}
  173. for module in modules:
  174. for api in module["api"]:
  175. api_str = api["api"][0:api["api"].find("(")]
  176. body = api["api"].replace(" ","")
  177. arg_s = re.search(r"\((.+)\)",body)#内部参数取出来,加上编号搞
  178. if arg_s:
  179. body = api_str + "("
  180. arg_s = arg_s.group(1).split(",")
  181. n = 1
  182. for i in arg_s:
  183. body += "${"+str(n)+":"+i+"},"
  184. n+=1
  185. body = body[0:-1]+")"
  186. snippet[api_str] = {
  187. 'body': body,
  188. 'description': api["summary"],
  189. 'prefix': api_str,
  190. }
  191. s = io.open(snippet_path,"w")
  192. s.write(json.dumps(snippet))
  193. s.close()
  194. try:
  195. shutil.rmtree("../../luatos-wiki/api/")
  196. os.mkdir("../../luatos-wiki/api/")
  197. except:
  198. pass
  199. doc = open("../../luatos-wiki/api/index.rst", "a+",encoding='utf-8')
  200. doc.write("C库接口\n")
  201. doc.write("==============\n\n")
  202. doc.write("请点击左侧列表,查看各个接口。如需搜索,请直接使用搜索框进行搜索。\n\n")
  203. doc.write(".. toctree::\n\n")
  204. for module in modules:
  205. mdoc = open("../../luatos-wiki/api/"+module["module"]+".md", "a+",encoding='utf-8')
  206. mdoc.write("# "+module["module"]+" - "+module["summary"]+"\n\n")
  207. if len(module["url"]) > 0:
  208. mdoc.write("> 本页文档由[这个文件]("+module["url"]+")自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!\n\n")
  209. doc.write(" "+module["module"]+"\n")
  210. for api in module["api"]:
  211. mdoc.write("## "+api["api"]+"\n\n")
  212. mdoc.write(api["summary"]+"\n\n")
  213. mdoc.write("**参数**\n\n")
  214. if len(api["args"]) > 0:
  215. mdoc.write("|传入值类型|解释|\n|-|-|\n")
  216. for arg in api["args"]:
  217. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  218. mdoc.write("\n")
  219. else:
  220. mdoc.write("无\n\n")
  221. mdoc.write("**返回值**\n\n")
  222. if len(api["return"]) > 0:
  223. mdoc.write("|返回值类型|解释|\n|-|-|\n")
  224. for arg in api["return"]:
  225. mdoc.write("|"+arg["type"].replace("|","\|")+"|"+arg["summary"].replace("|","\|")+"|\n")
  226. mdoc.write("\n")
  227. else:
  228. mdoc.write("无\n\n")
  229. mdoc.write("**例子**\n\n")
  230. if len(api["usage"]) == 0:
  231. mdoc.write("无\n\n")
  232. else:
  233. mdoc.write("```lua\n"+api["usage"]+"\n```\n\n")
  234. mdoc.write("---\n\n")
  235. mdoc.close()
  236. os.system("pandoc -o ./../../luatos-wiki/api/"+module["module"]+".rst ./../../luatos-wiki/api/"+module["module"]+".md")
  237. os.remove("./../../luatos-wiki/api/"+module["module"]+".md")
  238. doc.close()