gendocs.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. ########################################################################
  2. #
  3. # Documentation generator for panda.
  4. #
  5. # How to use this module:
  6. #
  7. # from direct.directscripts import gendocs
  8. # gendocs.generate(version, indirlist, directdirlist, docdir, header, footer, urlprefix, urlsuffix)
  9. #
  10. # - version is the panda version number
  11. #
  12. # - indirlist is the name of a directory, or a list of directories,
  13. # containing the "xxx.in" files that interrogate generates. No
  14. # slash at end.
  15. #
  16. # - directdirlist is the name of a directory, or a list of
  17. # directories, containing the source code for "direct," as well as
  18. # for other Python-based trees that should be included in the
  19. # documentation pages. No slash at end.
  20. #
  21. # - docdir is the name of a directory into which HTML files
  22. # will be emitted. No slash at end.
  23. #
  24. # - header is a string that will be placed at the front of
  25. # every HTML page.
  26. #
  27. # - footer is a string that will be placed at the end of
  28. # every HTML page.
  29. #
  30. # - urlprefix is a string that will be appended to the front of
  31. # every URL.
  32. #
  33. # - urlsuffix is a string that will be appended to the end of
  34. # every URL.
  35. #
  36. ########################################################################
  37. #
  38. # The major subsystems are:
  39. #
  40. # * The module that loads interrogate databases.
  41. #
  42. # * The module that loads python parse-trees.
  43. #
  44. # * The "code database", which provides a single access point
  45. # for both interrogate databases and python parse trees.
  46. #
  47. # * The HTML generator.
  48. #
  49. ########################################################################
  50. import os, sys, parser, symbol, token, re
  51. ########################################################################
  52. #
  53. # assorted utility functions
  54. #
  55. ########################################################################
  56. SECHEADER = re.compile("^[A-Z][a-z]+\\s*:")
  57. JUNKHEADER = re.compile("^((Function)|(Access))\\s*:")
  58. IMPORTSTAR = re.compile("^from\\s+([a-zA-Z0-9_.]+)\\s+import\\s+[*]\\s*$")
  59. IDENTIFIER = re.compile("[a-zA-Z0-9_]+")
  60. FILEHEADER = re.compile(
  61. r"""^// Filename: [a-zA-Z.]+
  62. // Created by: [a-zA-Z. ()0-9]+(
  63. //)?
  64. ////////////////////////////////////////////////////////////////////
  65. //
  66. // PANDA 3D SOFTWARE
  67. // Copyright \(c\) Carnegie Mellon University. All rights reserved.
  68. //
  69. // All use of this software is subject to the terms of the revised BSD
  70. // license. You should have received a copy of this license along
  71. // with this source code in a file named "LICENSE."
  72. //
  73. ////////////////////////////////////////////////////////////////////""")
  74. def readFile(fn):
  75. try:
  76. srchandle = open(fn, "r")
  77. data = srchandle.read()
  78. srchandle.close()
  79. return data
  80. except:
  81. sys.exit("Cannot read "+fn)
  82. def writeFile(wfile, data):
  83. try:
  84. dsthandle = open(wfile, "wb")
  85. dsthandle.write(data)
  86. dsthandle.close()
  87. except:
  88. sys.exit("Cannot write "+wfile)
  89. def writeFileLines(wfile, lines):
  90. try:
  91. dsthandle = open(wfile, "wb")
  92. for x in lines:
  93. dsthandle.write(x)
  94. dsthandle.write("\n")
  95. dsthandle.close()
  96. except:
  97. sys.exit("Cannot write "+wfile)
  98. def findFiles(dirlist, ext, ign, list):
  99. if isinstance(dirlist, str):
  100. dirlist = [dirlist]
  101. for dir in dirlist:
  102. for file in os.listdir(dir):
  103. full = dir + "/" + file
  104. if full not in ign and file not in ign:
  105. if (os.path.isfile(full)):
  106. if (file.endswith(ext)):
  107. list.append(full)
  108. elif (os.path.isdir(full)):
  109. findFiles(full, ext, ign, list)
  110. def pathToModule(result):
  111. if (result[-3:]==".py"): result=result[:-3]
  112. result = result.replace("/src/","/")
  113. result = result.replace("/",".")
  114. return result
  115. def textToHTML(comment, sep, delsection=None):
  116. sections = [""]
  117. included = {}
  118. for line in comment.split("\n"):
  119. line = line.lstrip(" ").lstrip(sep).lstrip(" ").rstrip("\r").rstrip(" ")
  120. if (line == ""):
  121. sections.append("")
  122. elif (line[0]=="*") or (line[0]=="-"):
  123. sections.append(line)
  124. sections.append("")
  125. elif (SECHEADER.match(line)):
  126. sections.append(line)
  127. else:
  128. sections[-1] = sections[-1] + " " + line
  129. total = ""
  130. for sec in sections:
  131. if (sec != ""):
  132. sec = sec.replace("&","&")
  133. sec = sec.replace("<","&lt;")
  134. sec = sec.replace(">","&gt;")
  135. sec = sec.replace(" "," ")
  136. sec = sec.replace(" "," ")
  137. if (delsection != None) and (delsection.match(sec)):
  138. included[sec] = 1
  139. if sec not in included:
  140. included[sec] = 1
  141. total = total + sec + "<br>\n"
  142. return total
  143. def linkTo(link, text):
  144. return '<a href="' + link + '">' + text + '</a>'
  145. def convertToPythonFn(fn):
  146. result = ""
  147. lastc = 0
  148. for c in fn:
  149. if (c!="_"):
  150. if (lastc=="_"):
  151. result = result + c.upper()
  152. else:
  153. result = result + c
  154. lastc = c
  155. return result
  156. def removeFileLicense(content):
  157. # Removes the license part at the top of a file.
  158. return re.sub(FILEHEADER, "", content).strip()
  159. ########################################################################
  160. #
  161. # Interrogate Database Tokenizer
  162. #
  163. ########################################################################
  164. class InterrogateTokenizer:
  165. """
  166. A big string, with a "parse pointer", and routines to
  167. extract integers and strings. The token syntax is that
  168. used by interrogate databases.
  169. """
  170. def __init__(self, fn):
  171. self.fn = fn
  172. self.pos = 0
  173. self.data = readFile(fn)
  174. def readint(self):
  175. neg = 0
  176. while (self.data[self.pos].isspace()):
  177. self.pos += 1
  178. if (self.data[self.pos] == "-"):
  179. neg = 1
  180. self.pos += 1
  181. if (self.data[self.pos].isdigit()==0):
  182. print("File position " + str(self.pos))
  183. print("Text: " + self.data[self.pos:self.pos+50])
  184. sys.exit("Syntax error in interrogate file format 0")
  185. value = 0
  186. while (self.data[self.pos].isdigit()):
  187. value = value*10 + int(self.data[self.pos])
  188. self.pos += 1
  189. if (neg): value = -value
  190. return value
  191. def readstring(self):
  192. length = self.readint()
  193. if (self.data[self.pos].isspace()==0):
  194. sys.exit("Syntax error in interrogate file format 1")
  195. self.pos += 1
  196. body = self.data[self.pos:self.pos+length]
  197. if (len(body) != length):
  198. sys.exit("Syntax error in interrogate file format 2")
  199. self.pos += length
  200. return body
  201. ########################################################################
  202. #
  203. # Interrogate Database Storage/Parsing
  204. #
  205. ########################################################################
  206. def parseInterrogateIntVec(tokzr):
  207. length = tokzr.readint()
  208. result = []
  209. for i in range(length):
  210. result.append(tokzr.readint())
  211. return result
  212. class InterrogateFunction:
  213. def __init__(self, tokzr, db):
  214. self.db = db
  215. self.index = tokzr.readint()
  216. self.componentname = tokzr.readstring()
  217. self.flags = tokzr.readint()
  218. self.classindex = tokzr.readint()
  219. self.scopedname = tokzr.readstring()
  220. self.cwrappers = parseInterrogateIntVec(tokzr)
  221. self.pythonwrappers = parseInterrogateIntVec(tokzr)
  222. self.comment = tokzr.readstring()
  223. self.prototype = tokzr.readstring()
  224. class InterrogateEnumValue:
  225. def __init__(self, tokzr):
  226. self.name = tokzr.readstring()
  227. self.scopedname = tokzr.readstring()
  228. self.value = tokzr.readint()
  229. class InterrogateDerivation:
  230. def __init__(self, tokzr):
  231. self.flags = tokzr.readint()
  232. self.base = tokzr.readint()
  233. self.upcast = tokzr.readint()
  234. self.downcast = tokzr.readint()
  235. class InterrogateType:
  236. def __init__(self, tokzr, db):
  237. self.db = db
  238. self.index = tokzr.readint()
  239. self.componentname = tokzr.readstring()
  240. self.flags = tokzr.readint()
  241. self.scopedname = tokzr.readstring()
  242. self.truename = tokzr.readstring()
  243. self.outerclass = tokzr.readint()
  244. self.atomictype = tokzr.readint()
  245. self.wrappedtype = tokzr.readint()
  246. self.constructors = parseInterrogateIntVec(tokzr)
  247. self.destructor = tokzr.readint()
  248. self.elements = parseInterrogateIntVec(tokzr)
  249. self.methods = parseInterrogateIntVec(tokzr)
  250. self.casts = parseInterrogateIntVec(tokzr)
  251. self.derivations = []
  252. nderivations = tokzr.readint()
  253. for i in range(nderivations):
  254. self.derivations.append(InterrogateDerivation(tokzr))
  255. self.enumvalues = []
  256. nenumvalues = tokzr.readint()
  257. for i in range(nenumvalues):
  258. self.enumvalues.append(InterrogateEnumValue(tokzr))
  259. self.nested = parseInterrogateIntVec(tokzr)
  260. self.comment = tokzr.readstring()
  261. class InterrogateParameter:
  262. def __init__(self, tokzr):
  263. self.name = tokzr.readstring()
  264. self.parameterflags = tokzr.readint()
  265. self.type = tokzr.readint()
  266. class InterrogateWrapper:
  267. def __init__(self, tokzr, db):
  268. self.db = db
  269. self.index = tokzr.readint()
  270. self.componentname = tokzr.readstring()
  271. self.flags = tokzr.readint()
  272. self.function = tokzr.readint()
  273. self.returntype = tokzr.readint()
  274. self.returnvaluedestructor = tokzr.readint()
  275. self.uniquename = tokzr.readstring()
  276. self.parameters = []
  277. nparameters = tokzr.readint()
  278. for i in range(nparameters):
  279. self.parameters.append(InterrogateParameter(tokzr))
  280. class InterrogateDatabase:
  281. def __init__(self, tokzr):
  282. self.fn = tokzr.fn
  283. self.magic = tokzr.readint()
  284. version1 = tokzr.readint()
  285. version2 = tokzr.readint()
  286. if (version1 != 2) or (version2 != 2):
  287. sys.exit("This program only understands interrogate file format 2.2")
  288. self.library = tokzr.readstring()
  289. self.libhash = tokzr.readstring()
  290. self.module = tokzr.readstring()
  291. self.functions = {}
  292. self.wrappers = {}
  293. self.types = {}
  294. self.namedtypes = {}
  295. count_functions = tokzr.readint()
  296. for i in range(count_functions):
  297. fn = InterrogateFunction(tokzr, self)
  298. self.functions[fn.index] = fn
  299. count_wrappers = tokzr.readint()
  300. for i in range(count_wrappers):
  301. wr = InterrogateWrapper(tokzr, self)
  302. self.wrappers[wr.index] = wr
  303. count_types = tokzr.readint()
  304. for i in range(count_types):
  305. tp = InterrogateType(tokzr, self)
  306. self.types[tp.index] = tp
  307. self.namedtypes[tp.scopedname] = tp
  308. ########################################################################
  309. #
  310. # Pattern Matching for Python Parse Trees
  311. #
  312. ########################################################################
  313. def printTree(tree, indent):
  314. spacing = " "[:indent]
  315. if isinstance(tree, tuple) and isinstance(tree[0], int):
  316. if tree[0] in symbol.sym_name:
  317. for i in range(len(tree)):
  318. if (i==0):
  319. print(spacing + "(symbol." + symbol.sym_name[tree[0]] + ",")
  320. else:
  321. printTree(tree[i], indent+1)
  322. print(spacing + "),")
  323. elif tree[0] in token.tok_name:
  324. print(spacing + "(token." + token.tok_name[tree[0]] + ", '" + tree[1] + "'),")
  325. else:
  326. print(spacing + str(tree))
  327. else:
  328. print(spacing + str(tree))
  329. COMPOUND_STMT_PATTERN = (
  330. symbol.stmt,
  331. (symbol.compound_stmt, ['compound'])
  332. )
  333. DOCSTRING_STMT_PATTERN = (
  334. symbol.stmt,
  335. (symbol.simple_stmt,
  336. (symbol.small_stmt,
  337. (symbol.expr_stmt,
  338. (symbol.testlist,
  339. (symbol.test,
  340. (symbol.or_test,
  341. (symbol.and_test,
  342. (symbol.not_test,
  343. (symbol.comparison,
  344. (symbol.expr,
  345. (symbol.xor_expr,
  346. (symbol.and_expr,
  347. (symbol.shift_expr,
  348. (symbol.arith_expr,
  349. (symbol.term,
  350. (symbol.factor,
  351. (symbol.power,
  352. (symbol.atom,
  353. (token.STRING, ['docstring'])
  354. ))))))))))))))))),
  355. (token.NEWLINE, '')
  356. ))
  357. DERIVATION_PATTERN = (
  358. symbol.test,
  359. (symbol.or_test,
  360. (symbol.and_test,
  361. (symbol.not_test,
  362. (symbol.comparison,
  363. (symbol.expr,
  364. (symbol.xor_expr,
  365. (symbol.and_expr,
  366. (symbol.shift_expr,
  367. (symbol.arith_expr,
  368. (symbol.term,
  369. (symbol.factor,
  370. (symbol.power,
  371. (symbol.atom,
  372. (token.NAME, ['classname'])
  373. ))))))))))))))
  374. ASSIGNMENT_STMT_PATTERN = (
  375. symbol.stmt,
  376. (symbol.simple_stmt,
  377. (symbol.small_stmt,
  378. (symbol.expr_stmt,
  379. (symbol.testlist,
  380. (symbol.test,
  381. (symbol.or_test,
  382. (symbol.and_test,
  383. (symbol.not_test,
  384. (symbol.comparison,
  385. (symbol.expr,
  386. (symbol.xor_expr,
  387. (symbol.and_expr,
  388. (symbol.shift_expr,
  389. (symbol.arith_expr,
  390. (symbol.term,
  391. (symbol.factor,
  392. (symbol.power,
  393. (symbol.atom,
  394. (token.NAME, ['varname']),
  395. ))))))))))))))),
  396. (token.EQUAL, '='),
  397. (symbol.testlist, ['rhs']))),
  398. (token.NEWLINE, ''),
  399. ))
  400. class ParseTreeInfo:
  401. docstring = ''
  402. name = ''
  403. def __init__(self, tree, name, file):
  404. """
  405. The code can be a string (in which case it is parsed), or it
  406. can be in parse tree form already.
  407. """
  408. self.name = name
  409. self.file = file
  410. self.class_info = {}
  411. self.function_info = {}
  412. self.assign_info = {}
  413. self.derivs = {}
  414. if isinstance(tree, str):
  415. try:
  416. tree = parser.suite(tree+"\n").totuple()
  417. if (tree):
  418. found, vars = self.match(DOCSTRING_STMT_PATTERN, tree[1])
  419. if found:
  420. self.docstring = vars["docstring"]
  421. except:
  422. print("CAUTION --- Parse failed: " + name)
  423. if isinstance(tree, tuple):
  424. self.extract_info(tree)
  425. def match(self, pattern, data, vars=None):
  426. """
  427. pattern
  428. Pattern to match against, possibly containing variables.
  429. data
  430. Data to be checked and against which variables are extracted.
  431. vars
  432. Dictionary of variables which have already been found. If not
  433. provided, an empty dictionary is created.
  434. The `pattern' value may contain variables of the form ['varname']
  435. which are allowed to parseTreeMatch anything. The value that is
  436. parseTreeMatched is returned as part of a dictionary which maps
  437. 'varname' to the parseTreeMatched value. 'varname' is not required
  438. to be a string object, but using strings makes patterns and the code
  439. which uses them more readable. This function returns two values: a
  440. boolean indicating whether a parseTreeMatch was found and a
  441. dictionary mapping variable names to their associated values.
  442. """
  443. if vars is None:
  444. vars = {}
  445. if type(pattern) is list: # 'variables' are ['varname']
  446. vars[pattern[0]] = data
  447. return 1, vars
  448. if type(pattern) is not tuple:
  449. return (pattern == data), vars
  450. if len(data) != len(pattern):
  451. return 0, vars
  452. for pattern, data in map(None, pattern, data):
  453. same, vars = self.match(pattern, data, vars)
  454. if not same:
  455. break
  456. return same, vars
  457. def extract_info(self, tree):
  458. # extract docstring
  459. found = 0
  460. if len(tree) == 2:
  461. found, vars = self.match(DOCSTRING_STMT_PATTERN[1], tree[1])
  462. elif len(tree) >= 4:
  463. found, vars = self.match(DOCSTRING_STMT_PATTERN, tree[3])
  464. if found:
  465. self.docstring = eval(vars['docstring'])
  466. # discover inner definitions
  467. for node in tree[1:]:
  468. found, vars = self.match(ASSIGNMENT_STMT_PATTERN, node)
  469. if found:
  470. self.assign_info[vars['varname']] = 1
  471. found, vars = self.match(COMPOUND_STMT_PATTERN, node)
  472. if found:
  473. cstmt = vars['compound']
  474. if cstmt[0] == symbol.funcdef:
  475. name = cstmt[2][1]
  476. # Workaround for a weird issue with static and classmethods
  477. if name == "def":
  478. name = cstmt[3][1]
  479. self.function_info[name] = ParseTreeInfo(cstmt and cstmt[-1] or None, name, self.file)
  480. self.function_info[name].prototype = self.extract_tokens("", cstmt[4])
  481. else:
  482. self.function_info[name] = ParseTreeInfo(cstmt and cstmt[-1] or None, name, self.file)
  483. self.function_info[name].prototype = self.extract_tokens("", cstmt[3])
  484. elif cstmt[0] == symbol.classdef:
  485. name = cstmt[2][1]
  486. self.class_info[name] = ParseTreeInfo(cstmt and cstmt[-1] or None, name, self.file)
  487. self.extract_derivs(self.class_info[name], cstmt)
  488. def extract_derivs(self, classinfo, tree):
  489. if (len(tree)==8):
  490. derivs = tree[4]
  491. for deriv in derivs[1:]:
  492. found, vars = self.match(DERIVATION_PATTERN, deriv)
  493. if (found):
  494. classinfo.derivs[vars["classname"]] = 1
  495. def extract_tokens(self, str, tree):
  496. if (isinstance(tree, tuple)):
  497. if tree[0] in token.tok_name:
  498. str = str + tree[1]
  499. if (tree[1]==","): str=str+" "
  500. elif tree[0] in symbol.sym_name:
  501. for sub in tree[1:]:
  502. str = self.extract_tokens(str, sub)
  503. return str
  504. ########################################################################
  505. #
  506. # The code database contains:
  507. #
  508. # - a list of InterrogateDatabase objects representing C++ modules.
  509. # - a list of ParseTreeInfo objects representing python modules.
  510. #
  511. # Collectively, these make up all the data about all the code.
  512. #
  513. ########################################################################
  514. class CodeDatabase:
  515. def __init__(self, cxxlist, pylist):
  516. self.types = {}
  517. self.funcs = {}
  518. self.goodtypes = {}
  519. self.funcExports = {}
  520. self.typeExports = {}
  521. self.varExports = {}
  522. self.globalfn = []
  523. self.formattedprotos = {}
  524. print("Reading C++ source files")
  525. for cxx in cxxlist:
  526. tokzr = InterrogateTokenizer(cxx)
  527. idb = InterrogateDatabase(tokzr)
  528. for type in idb.types.values():
  529. if (type.flags & 8192) or type.scopedname not in self.types:
  530. self.types[type.scopedname] = type
  531. if (type.flags & 8192) and (type.atomictype == 0) and (type.scopedname.count(" ")==0) and (type.scopedname.count(":")==0):
  532. self.goodtypes[type.scopedname] = type
  533. self.typeExports.setdefault("pandac.PandaModules", []).append(type.scopedname)
  534. for func in idb.functions.values():
  535. type = idb.types.get(func.classindex)
  536. func.pyname = convertToPythonFn(func.componentname)
  537. if (type == None):
  538. self.funcs["GLOBAL."+func.pyname] = func
  539. self.globalfn.append("GLOBAL."+func.pyname)
  540. self.funcExports.setdefault("pandac.PandaModules", []).append(func.pyname)
  541. else:
  542. self.funcs[type.scopedname+"."+func.pyname] = func
  543. print("Reading Python sources files")
  544. for py in pylist:
  545. pyinf = ParseTreeInfo(readFile(py), py, py)
  546. mod = pathToModule(py)
  547. for type in pyinf.class_info.keys():
  548. typinf = pyinf.class_info[type]
  549. self.types[type] = typinf
  550. self.goodtypes[type] = typinf
  551. self.typeExports.setdefault(mod, []).append(type)
  552. for func in typinf.function_info.keys():
  553. self.funcs[type+"."+func] = typinf.function_info[func]
  554. for func in pyinf.function_info.keys():
  555. self.funcs["GLOBAL."+func] = pyinf.function_info[func]
  556. self.globalfn.append("GLOBAL."+func)
  557. self.funcExports.setdefault(mod, []).append(func)
  558. for var in pyinf.assign_info.keys():
  559. self.varExports.setdefault(mod, []).append(var)
  560. def getClassList(self):
  561. return list(self.goodtypes.keys())
  562. def getGlobalFunctionList(self):
  563. return self.globalfn
  564. def getClassComment(self, cn):
  565. type = self.types.get(cn)
  566. if (isinstance(type, InterrogateType)):
  567. return textToHTML(type.comment,"/")
  568. elif (isinstance(type, ParseTreeInfo)):
  569. return textToHTML(type.docstring,"#")
  570. else:
  571. return ""
  572. def getClassParents(self, cn):
  573. type = self.types.get(cn)
  574. if (isinstance(type, InterrogateType)):
  575. parents = []
  576. for deriv in type.derivations:
  577. basetype = type.db.types[deriv.base]
  578. parents.append(basetype.scopedname)
  579. return parents
  580. elif (isinstance(type, ParseTreeInfo)):
  581. return list(type.derivs.keys())
  582. else:
  583. return []
  584. def getClassConstants(self, cn):
  585. type = self.types.get(cn)
  586. if (isinstance(type, InterrogateType)):
  587. result = []
  588. for subtype in type.nested:
  589. enumtype = type.db.types[subtype]
  590. if (len(enumtype.enumvalues)):
  591. for enumvalue in enumtype.enumvalues:
  592. name = convertToPythonFn(enumvalue.name)
  593. result.append((name, "("+enumtype.componentname+")"))
  594. result.append(("",""))
  595. return result
  596. else:
  597. return []
  598. def buildInheritance(self, inheritance, cn):
  599. if (inheritance.count(cn) == 0):
  600. inheritance.append(cn)
  601. for parent in self.getClassParents(cn):
  602. self.buildInheritance(inheritance, parent)
  603. def getInheritance(self, cn):
  604. inheritance = []
  605. self.buildInheritance(inheritance, cn)
  606. return inheritance
  607. def getClassImport(self, cn):
  608. type = self.types.get(cn)
  609. if (isinstance(type, InterrogateType)):
  610. return "pandac.PandaModules"
  611. else:
  612. return pathToModule(type.file)
  613. def getClassConstructors(self, cn):
  614. # Only detects C++ constructors, not Python constructors, since
  615. # those are treated as ordinary methods.
  616. type = self.types.get(cn)
  617. result = []
  618. if (isinstance(type, InterrogateType)):
  619. for constructor in type.constructors:
  620. func = type.db.functions[constructor]
  621. if (func.classindex == type.index):
  622. result.append(type.scopedname+"."+func.pyname)
  623. return result
  624. def getClassMethods(self, cn):
  625. type = self.types.get(cn)
  626. result = []
  627. if (isinstance(type, InterrogateType)):
  628. for method in type.methods:
  629. func = type.db.functions[method]
  630. if (func.classindex == type.index):
  631. result.append(type.scopedname+"."+func.pyname)
  632. elif (isinstance(type, ParseTreeInfo)):
  633. for method in type.function_info.keys():
  634. result.append(type.name + "." + method)
  635. return result
  636. def getFunctionName(self, fn):
  637. func = self.funcs.get(fn)
  638. if (isinstance(func, InterrogateFunction)):
  639. return func.pyname
  640. elif (isinstance(func, ParseTreeInfo)):
  641. return func.name
  642. else:
  643. return fn
  644. def getFunctionImport(self, fn):
  645. func = self.funcs.get(fn)
  646. if (isinstance(func, InterrogateFunction)):
  647. return "pandac.PandaModules"
  648. else:
  649. return pathToModule(func.file)
  650. def getFunctionPrototype(self, fn, urlprefix, urlsuffix):
  651. func = self.funcs.get(fn)
  652. if (isinstance(func, InterrogateFunction)):
  653. if fn in self.formattedprotos:
  654. proto = self.formattedprotos[fn]
  655. else:
  656. proto = func.prototype
  657. proto = proto.replace(" inline "," ")
  658. if (proto.startswith("inline ")): proto = proto[7:]
  659. proto = proto.replace("basic_string< char >", "string")
  660. proto = textToHTML(proto,"")
  661. if "." in fn:
  662. for c in self.goodtypes.keys():
  663. if c != fn.split(".")[0] and (c in proto):
  664. proto = re.sub("\\b%s\\b" % c, linkTo(urlprefix+c+urlsuffix, c), proto)
  665. self.formattedprotos[fn] = proto
  666. return proto
  667. elif (isinstance(func, ParseTreeInfo)):
  668. return textToHTML("def "+func.name+func.prototype,"")
  669. return fn
  670. def getFunctionComment(self, fn):
  671. func = self.funcs.get(fn)
  672. if (isinstance(func, InterrogateFunction)):
  673. return textToHTML(removeFileLicense(func.comment), "/", JUNKHEADER)
  674. elif (isinstance(func, ParseTreeInfo)):
  675. return textToHTML(func.docstring, "#")
  676. return fn
  677. def isFunctionPython(self, fn):
  678. func = self.funcs.get(fn)
  679. if (isinstance(func, InterrogateFunction)):
  680. return False
  681. elif (isinstance(func, ParseTreeInfo)):
  682. return True
  683. return False
  684. def getFuncExports(self, mod):
  685. return self.funcExports.get(mod, [])
  686. def getTypeExports(self, mod):
  687. return self.typeExports.get(mod, [])
  688. def getVarExports(self, mod):
  689. return self.varExports.get(mod, [])
  690. ########################################################################
  691. #
  692. # The "Class Rename Dictionary" - Yech.
  693. #
  694. ########################################################################
  695. CLASS_RENAME_DICT = {
  696. # No longer used, now empty.
  697. }
  698. ########################################################################
  699. #
  700. # HTML generation
  701. #
  702. ########################################################################
  703. def makeCodeDatabase(indirlist, directdirlist):
  704. if isinstance(directdirlist, str):
  705. directdirlist = [directdirlist]
  706. ignore = {}
  707. ignore["__init__.py"] = 1
  708. for directdir in directdirlist:
  709. ignore[directdir + "/src/directscripts"] = 1
  710. ignore[directdir + "/src/extensions"] = 1
  711. ignore[directdir + "/src/extensions_native"] = 1
  712. ignore[directdir + "/src/ffi"] = 1
  713. ignore[directdir + "/built"] = 1
  714. cxxfiles = []
  715. pyfiles = []
  716. findFiles(indirlist, ".in", ignore, cxxfiles)
  717. findFiles(directdirlist, ".py", ignore, pyfiles)
  718. return CodeDatabase(cxxfiles, pyfiles)
  719. def generateFunctionDocs(code, method, urlprefix, urlsuffix):
  720. name = code.getFunctionName(method)
  721. proto = code.getFunctionPrototype(method, urlprefix, urlsuffix)
  722. comment = code.getFunctionComment(method)
  723. if (comment == ""): comment = "Undocumented function.<br>\n"
  724. chunk = '<table bgcolor="e8e8e8" border=0 cellspacing=0 cellpadding=5 width="100%"><tr><td>' + "\n"
  725. chunk = chunk + '<a name="' + name + '"><b>' + name + "</b></a><br>\n"
  726. chunk = chunk + proto + "<br>\n"
  727. chunk = chunk + comment
  728. chunk = chunk + "</td></tr></table><br>\n"
  729. return chunk
  730. def generateLinkTable(link, text, cols, urlprefix, urlsuffix):
  731. column = (len(link)+cols-1)/cols
  732. percent = 100 / cols
  733. result = '<table width="100%">\n'
  734. for i in range(column):
  735. line = ""
  736. for j in range(cols):
  737. slot = i + column*j
  738. linkval = ""
  739. textval = ""
  740. if (slot < len(link)): linkval = link[slot]
  741. if (slot < len(text)): textval = text[slot]
  742. if (i==0):
  743. line = line + '<td width="' + str(percent) + '%">' + linkTo(urlprefix+linkval+urlsuffix, textval) + "</td>"
  744. else:
  745. line = line + '<td>' + linkTo(urlprefix+linkval+urlsuffix, textval) + "</td>"
  746. result = result + "<tr>" + line + "</tr>\n"
  747. result = result + "</table>\n"
  748. return result
  749. def generate(pversion, indirlist, directdirlist, docdir, header, footer, urlprefix, urlsuffix):
  750. code = makeCodeDatabase(indirlist, directdirlist)
  751. classes = code.getClassList()[:]
  752. classes.sort(None, str.lower)
  753. xclasses = classes[:]
  754. print("Generating HTML pages")
  755. for type in classes:
  756. body = "<h1>" + type + "</h1>\n"
  757. comment = code.getClassComment(type)
  758. body = body + "<ul>\nfrom " + code.getClassImport(type) + " import " + type + "</ul>\n\n"
  759. body = body + "<ul>\n" + comment + "</ul>\n\n"
  760. inheritance = code.getInheritance(type)
  761. body = body + "<h2>Inheritance:</h2>\n<ul>\n"
  762. for inh in inheritance:
  763. line = " " + linkTo(urlprefix+inh+urlsuffix, inh) + ": "
  764. for parent in code.getClassParents(inh):
  765. line = line + linkTo(urlprefix+parent+urlsuffix, parent) + " "
  766. body = body + line + "<br>\n"
  767. body = body + "</ul>\n"
  768. for sclass in inheritance:
  769. methods = code.getClassMethods(sclass)[:]
  770. methods.sort(None, str.lower)
  771. constructors = code.getClassConstructors(sclass)
  772. if (len(methods) > 0 or len(constructors) > 0):
  773. body = body + "<h2>Methods of "+sclass+":</h2>\n<ul>\n"
  774. if len(constructors) > 0:
  775. fn = code.getFunctionName(constructors[0])
  776. body = body + '<a href="#' + fn + '">' + fn + " (Constructor)</a><br>\n"
  777. for method in methods:
  778. fn = code.getFunctionName(method)
  779. body = body + '<a href="#' + fn + '">' + fn + "</a><br>\n"
  780. body = body + "</ul>\n"
  781. for sclass in inheritance:
  782. enums = code.getClassConstants(sclass)[:]
  783. if (len(enums) > 0):
  784. body = body + "<h2>Constants in "+sclass+":</h2>\n<ul><table>\n"
  785. for (value, comment) in enums:
  786. body = body + "<tr><td>" + value + "</td><td>" + comment + "</td></tr>\n"
  787. body = body + "</table></ul>"
  788. for sclass in inheritance:
  789. constructors = code.getClassConstructors(sclass)
  790. for constructor in constructors:
  791. body = body + generateFunctionDocs(code, constructor, urlprefix, urlsuffix)
  792. methods = code.getClassMethods(sclass)[:]
  793. methods.sort(None, str.lower)
  794. for method in methods:
  795. body = body + generateFunctionDocs(code, method, urlprefix, urlsuffix)
  796. body = header + body + footer
  797. writeFile(docdir + "/" + type + ".html", body)
  798. if type in CLASS_RENAME_DICT:
  799. modtype = CLASS_RENAME_DICT[type]
  800. writeFile(docdir + "/" + modtype + ".html", body)
  801. xclasses.append(modtype)
  802. xclasses.sort(None, str.lower)
  803. index = "<h1>List of Classes - Panda " + pversion + "</h1>\n"
  804. index = index + generateLinkTable(xclasses, xclasses, 3, urlprefix, urlsuffix)
  805. fnlist = code.getGlobalFunctionList()[:]
  806. fnlist.sort(None, str.lower)
  807. fnnames = []
  808. for i in range(len(fnlist)):
  809. fnnames.append(code.getFunctionName(fnlist[i]))
  810. index = header + index + footer
  811. writeFile(docdir + "/classes.html", index)
  812. index = "<h1>List of Global Functions - Panda " + pversion + "</h1>\n"
  813. index = index + generateLinkTable(fnnames, fnnames, 3,"#","")
  814. for func in fnlist:
  815. index = index + generateFunctionDocs(code, func, urlprefix, urlsuffix)
  816. index = header + index + footer
  817. writeFile(docdir + "/functions.html", index)
  818. table = {}
  819. for type in classes:
  820. for method in code.getClassMethods(type)[:]:
  821. name = code.getFunctionName(method)
  822. prefix = name[0].upper()
  823. if prefix not in table:
  824. table[prefix] = {}
  825. if name not in table[prefix]:
  826. table[prefix][name] = []
  827. table[prefix][name].append(type)
  828. index = "<h1>List of Methods - Panda " + pversion + "</h1>\n"
  829. prefixes = list(table.keys())
  830. prefixes.sort(None, str.lower)
  831. for prefix in prefixes:
  832. index = index + linkTo("#"+prefix, prefix) + " "
  833. index = index + "<br><br>"
  834. for prefix in prefixes:
  835. index = index + '<a name="' + prefix + '">' + "\n"
  836. names = list(table[prefix].keys())
  837. names.sort(None, str.lower)
  838. for name in names:
  839. line = '<b>' + name + ":</b><ul>\n"
  840. ctypes = table[prefix][name]
  841. ctypes.sort(None, str.lower)
  842. for type in ctypes:
  843. line = line + "<li>" + linkTo(urlprefix+type+urlsuffix+"#"+name, type) + "\n"
  844. line = line + "</ul>\n"
  845. index = index + line + "\n"
  846. index = header + index + footer
  847. writeFile(docdir + "/methods.html", index)
  848. index = "<h1>Panda " + pversion + "</h1>\n"
  849. index = index + "<ul>\n"
  850. index = index + "<li>" + linkTo(urlprefix+"classes"+urlsuffix, "List of all Classes") + "\n"
  851. index = index + "</ul>\n"
  852. index = index + "<ul>\n"
  853. index = index + "<li>" + linkTo(urlprefix+"functions"+urlsuffix, "List of all Global Functions") + "\n"
  854. index = index + "</ul>\n"
  855. index = index + "<ul>\n"
  856. index = index + "<li>" + linkTo(urlprefix+"methods"+urlsuffix, "List of all Methods (very long)") + "\n"
  857. index = index + "</ul>\n"
  858. writeFile(docdir + "/index.html", index)
  859. ########################################################################
  860. #
  861. # IMPORT repair
  862. #
  863. ########################################################################
  864. def expandImports(indirlist, directdirlist, fixdirlist):
  865. code = makeCodeDatabase(indirlist, directdirlist)
  866. fixfiles = []
  867. findFiles(fixdirlist, ".py", {}, fixfiles)
  868. for fixfile in fixfiles:
  869. if (os.path.isfile(fixfile+".orig")):
  870. text = readFile(fixfile+".orig")
  871. else:
  872. text = readFile(fixfile)
  873. writeFile(fixfile+".orig", text)
  874. text = text.replace("\r","")
  875. lines = text.split("\n")
  876. used = {}
  877. for id in IDENTIFIER.findall(text):
  878. used[id] = 1
  879. result = []
  880. for line in lines:
  881. mat = IMPORTSTAR.match(line)
  882. if (mat):
  883. module = mat.group(1)
  884. if (fixfile.count("/")!=0) and (module.count(".")==0):
  885. modfile = os.path.dirname(fixfile)+"/"+module+".py"
  886. if (os.path.isfile(modfile)):
  887. module = pathToModule(modfile)
  888. typeExports = code.getTypeExports(module)
  889. funcExports = code.getFuncExports(module)
  890. varExports = code.getVarExports(module)
  891. if (len(typeExports)+len(funcExports)+len(varExports)==0):
  892. result.append(line)
  893. print(fixfile + " : " + module + " : no exports")
  894. else:
  895. print(fixfile + " : " + module + " : repairing")
  896. for x in funcExports:
  897. fn = code.getFunctionName(x)
  898. if fn in used:
  899. result.append("from "+module+" import "+fn)
  900. for x in typeExports:
  901. if x in used:
  902. result.append("from "+module+" import "+x)
  903. for x in varExports:
  904. if x in used:
  905. result.append("from "+module+" import "+x)
  906. else:
  907. result.append(line)
  908. writeFileLines(fixfile, result)