Debian Codegraph @

scripts/python/nasal_api_doc.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (C) 2012 Adrian Musceac
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 import os, sys, glob
21 import io
22 import re, string
23
24 """Script which generates an API documentation file for Nasal libraries
25 located inside $FGROOT/Nasal/
26 Usage: nasal_api_doc.py parse [path to $FGROOT/Nasal/]
27 Or configure the local path below, and omit the path argument in the command.
28 The API doc in HTML format is generated in the current working directory"""
29
30 ########### Local $FGROOT/Nasal/ path ##########
31 NASAL_PATH="../fgfs/fgdata/Nasal/"
32
33
34 def get_files(nasal_dir):
35 if nasal_dir[-1]!='/':
36 nasal_dir+='/'
37 try:
38 os.stat(nasal_dir)
39 except:
40 print("The path does not exist")
41 sys.exit()
42 fgroot_dir = nasal_dir.rstrip('/').replace('Nasal','')
43
44 with open(fgroot_dir + 'version', 'r', encoding="utf-8") as f_version:
45 version = f_version.read().strip()
46
47 top_level = []
48 modules = []
49 top_namespaces = []
50 files_list = os.listdir(nasal_dir)
51 for f in files_list:
52 if f.find(".nas")!=-1:
53 top_level.append(f)
54 continue
55 if os.path.isdir(nasal_dir + f):
56 modules.append(f)
57 top_level.sort()
58 modules.sort()
59 if len(top_level) ==0:
60 print("This does not look like the correct $FGROOT/Nasal path")
61 sys.exit()
62 if len(modules)==0:
63 print("Warning: could not find any submodules")
64 for f in top_level:
65 namespace=f.replace(".nas","")
66 functions=parse_file(nasal_dir + f)
67 top_namespaces.append([namespace,functions])
68 for m in modules:
69 files=glob.glob(nasal_dir+m+"/*.nas")
70 for f in files:
71 functions=parse_file(f)
72 top_namespaces.append([m,functions])
73
74 output_text(top_namespaces,modules,version)
75
76
77 def output_text(top_namespaces, modules, version):
78 with open('./nasal_api_doc.html', 'w', encoding="utf-8") as fw:
79 output_text_aux(fw, top_namespaces, modules, version)
80
81
82 def output_text_aux(fw, top_namespaces, modules, version):
83 buf='<html><head>\
84 <title>Nasal API</title>\
85 <style>\n\
86 a.main_module_link {margin-left:30px;display:block;float:left;}\
87 div.container {background-color:#eee;clear:left;margin-top:20px;}\
88 h2.namespace_title {padding-left:20px;color:#fff;background-color:#8888AC}\
89 h4.class_function {padding-left:20px;background-color:#eee;color:#000033}\
90 h4.class_definition {padding-left:20px;background-color:#eee;color:#000033}\
91 h4.function {padding-left:20px;background-color:#eee;color:#000033}\
92 hr {margin-left:30px;margin-right:30px;}\
93 div.comments {padding-left:40px;display:inline;font-size:12px;}\
94 </style>\n\
95 </head><body style="width:1024px;">'
96
97 buf+='<h1 style="padding-left:20px;display:block;color:#fff;background-color:#555588;">\
98 Nasal $FGROOT Library<br/><span style="font-size:12px;">Flightgear version: '+version+'\
99 <br/>This file is generated automatically by scripts/python/nasal_api_doc.py\
100 </span></h1>\
101 <br/><a href="http://plausible.org/nasal">Nasal documentation</a>&nbsp;&nbsp;\
102 <a href="http://wiki.flightgear.org/Nasal_scripting_language">Flightgear Nasal documentation</a>\n<div style="float:right;">&nbsp;'
103 buf+='<h2 style="font-size:14px;height:450px;width:250px;overflow:scroll;display:block;position:fixed;top:20px;right:20px;background-color:#8888AC;border:1px solid black;">\n'
104 done=[]
105 for namespace in top_namespaces:
106 color='0000cc'
107 if namespace[0] in modules:
108 color='cc0000'
109 if namespace[0] not in done:
110 buf+='<a class="main_module_link" style="color:'+color+'" href="#'+namespace[0]+'">'+namespace[0]+'</a>&nbsp;<br/>\n'
111 done.append(namespace[0])
112 buf+='</h2></div>\n'
113 done2=[]
114 for namespace in top_namespaces:
115 if namespace[0] not in done2:
116 buf+='<div class="container" style="">\n'
117 buf += '<h2 class="namespace_title"><a name="'+namespace[0]+'">'+namespace[0]+'</a></h2>\n'
118 done2.append(namespace[0])
119 for functions in namespace[1]:
120 class_func=functions[0].split('.')
121 if len(class_func)>1:
122 f_name=''
123 if class_func[1].find('_')==0:
124 f_name='<font color="#0000cc">'+class_func[1]+'</font>'
125 else:
126 f_name=class_func[1]
127 if class_func[1]!='':
128 buf+= '<div><h4 class="class_function"><b>'\
129 +namespace[0]+'</b>'+ "." + '<b><i>'+class_func[0]+'</i></b>'+'<b>.'+f_name+'</b>'+' ( <font color="#cc0000">'+ functions[1]+ '</font> )' +'</h4>\n'
130 else:
131 buf+= '<div><h4 class="class_definition"><b>'\
132 +namespace[0]+'</b>'+ "." + '<b><i><u><font color="#000000">'+class_func[0]+'</font></u></i></b>' +'</h4>\n'
133 else:
134 if functions[0].find('_')==0:
135 f_name='<font color="#0000cc">'+functions[0]+'</font>'
136 else:
137 f_name=functions[0]
138 buf+= '<div><h4 class="function"><b>'\
139 +namespace[0]+'</b>'+ "." + '<b>'+f_name+'</b>'+ ' ( <font color="#cc0000">'+ functions[1]+ '</font> )' +'</h4>\n'
140 for comment in functions[2]:
141 if comment.find('=====')!=-1:
142 buf+='<hr/>'
143 else:
144 tempComment = comment.replace('#','').replace('<','&lt;').replace('>','&gt;')
145 if tempComment.strip()!="":
146 buf+= '<div class="comments">'+tempComment+'</div><br/>\n'
147 buf+='</div>\n'
148 if namespace[0] not in done2:
149 buf+='</div>\n'
150 buf+='</body></html>'
151 fw.write(buf)
152
153
154 def parse_file(filename):
155 with open(filename, 'r', encoding="utf-8") as fr:
156 content = fr.readlines()
157
158 i=0
159 retval=[]
160 classname=""
161 for line in content:
162 match=re.search(r'^var\s+([A-Za-z0-9_-]+)\s*=\s*func\s*\(?([A-Za-z0-9_\s,=.\n-]*)\)?',line)
163 if match is not None:
164 func_name=match.group(1)
165 comments=[]
166 param=match.group(2)
167 if(line.find(')')==-1 and line.find('(')!=-1):
168 k=i+1
169 while(content[k].find(')')==-1):
170 param+=content[k].rstrip('\n')
171 k+=1
172 param+=content[k].split(')')[0]
173 j=i-1
174 count=0
175 while ( j>i-35 and j>-1):
176 if count>3:
177 break
178 if len(content[j])<2:
179 j-=1
180 count+=1
181 continue
182 if re.search(r'^\s*#',content[j]) is not None:
183 comments.append(content[j].rstrip('\n'))
184 j-=1
185 else:
186 break
187 if(len(comments)>1):
188 comments.reverse()
189 retval.append((func_name, param,comments))
190 i+=1
191 continue
192
193 match3=re.search(r'^var\s*([A-Za-z0-9_-]+)\s*=\s*{\s*(\n|})',line)
194 if match3 is not None:
195 classname=match3.group(1)
196
197 comments=[]
198
199 j=i-1
200 count=0
201 while ( j>i-35 and j>-1):
202 if count>3:
203 break
204 if len(content[j])<2:
205 j-=1
206 count+=1
207 continue
208 if re.search(r'^\s*#',content[j]) is not None:
209 comments.append(content[j].rstrip('\n'))
210 j-=1
211 else:
212 break
213 if(len(comments)>1):
214 comments.reverse()
215 retval.append((classname+'.', '',comments))
216 i+=1
217 continue
218
219 match2=re.search(r'^\s*([A-Za-z0-9_-]+)\s*:\s*func\s*\(?([A-Za-z0-9_\s,=.\n-]*)\)?',line)
220 if match2 is not None:
221 func_name=match2.group(1)
222 comments=[]
223 param=match2.group(2)
224 if(line.find(')')==-1 and line.find('(')!=-1):
225 k=i+1
226 while(content[k].find(')')==-1):
227 param+=content[k].rstrip('\n')
228 k+=1
229 param+=content[k].split(')')[0]
230 j=i-1
231 count=0
232 while ( j>i-35 and j>-1):
233 if count>3:
234 break
235 if len(content[j])<2:
236 j-=1
237 count+=1
238 continue
239 if re.search(r'^\s*#',content[j]) is not None:
240 comments.append(content[j].rstrip('\n'))
241 j-=1
242 else:
243 break
244 if(len(comments)>1):
245 comments.reverse()
246 if classname =='':
247 continue
248 retval.append((classname+'.'+func_name, param,comments))
249 i+=1
250 continue
251
252 match4=re.search(r'^([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)\s*=\s*func\s*\(?([A-Za-z0-9_\s,=\n.-]*)\)?',line)
253 if match4 is not None:
254 classname=match4.group(1)
255 func_name=match4.group(2)
256 comments=[]
257 param=match4.group(3)
258 if(line.find(')')==-1 and line.find('(')!=-1):
259 k=i+1
260 while(content[k].find(')')==-1):
261 param+=content[k].rstrip('\n')
262 k+=1
263 param+=content[k].split(')')[0]
264 j=i-1
265 count=0
266 while ( j>i-35 and j>-1):
267 if count>3:
268 break
269 if len(content[j])<2:
270 j-=1
271 count+=1
272 continue
273 if re.search(r'^\s*#',content[j]) is not None:
274 comments.append(content[j].rstrip('\n'))
275 j-=1
276 else:
277 break
278 if(len(comments)>1):
279 comments.reverse()
280 retval.append((classname+'.'+func_name, param,comments))
281 i+=1
282 continue
283
284 i+=1
285 return retval
286
287
288
289 if __name__ == "__main__":
290 if len(sys.argv) <2:
291 print('Usage: nasal_api_doc.py parse [path to $FGROOT/Nasal/]')
292 sys.exit()
293 else:
294 if sys.argv[1]=='parse':
295 if len(sys.argv) <3:
296 nasal_path=NASAL_PATH
297 else:
298 nasal_path=sys.argv[2]
299 get_files(nasal_path)
300 else:
301 print('Usage: nasal_api_doc.py parse [path to $FGROOT/Nasal/]')
302 sys.exit()