update_doc_baseclasses.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ##############################################################################
  2. # Name: misc/scripts/update_doc_baseclasses.py
  3. # Purpose: Warns about missing classes in the "Derived from"
  4. # sections in the doc files
  5. # Created: 2007-07-28
  6. # Copyright: (c) 2007 Francesco Montorsi
  7. # Licence: wxWindows licence
  8. ##############################################################################
  9. from update_doc_utils import scanTexFiles
  10. # classes whose docs cannot be fixed automatically
  11. # because of:
  12. # 1) multiple inheritance
  13. # 2) other strange things specific of these classes
  14. EXCEPTIONS=['wxNotebook','wxChoicebook','wxListbook','wxToolbook','wxTreebook','wxURLDataObject','wxHScrolledWindow','wxVScrolledWindow','wxVarHVScrollHelper','wxHVScrolledWindow','wxFileStream']
  15. def myCallback(classname, texFileName, content, i):
  16. # now search the base classes for this class
  17. baseclasses = []
  18. for j in range(i,len(content)):
  19. line = content[j]
  20. if line.startswith("\wxheading{Derived from}"):
  21. # take all lines contained between this \wxheading and the next one
  22. # as base classes
  23. for k in range(j+1,len(content)):
  24. line = content[k]
  25. if "\wxheading" in line:
  26. break
  27. elif "\helpref" in line:
  28. baseclasses.append(line)
  29. break # we've already processed the 'derived from' section for this class
  30. if baseclasses == []:
  31. print " no base classes declared for class %s" % classname
  32. return True # keep going on with next \class tags
  33. # polish baseclasses list
  34. for i in range(len(baseclasses)):
  35. s = baseclasses[i]
  36. baseclasses[i] = s[s.find("{")+1:s.find("}")]
  37. # store the baseclasses
  38. tree[classname] = baseclasses
  39. treetex[classname] = texFileName
  40. treepos[classname] = j+1
  41. print " class '%s' has %d base class(es): %s" % (classname, len(baseclasses), ','.join(baseclasses))
  42. return True
  43. tree = dict()
  44. treetex = dict()
  45. treepos = dict()
  46. count = scanTexFiles(myCallback)
  47. print "\nProcessed %d files." % count
  48. print "\nNow starting to compare the base class lists.\n"
  49. def getFullListOfBaseClasses(classname):
  50. if classname not in tree or classname=='':
  51. return []
  52. # only use the first base class of info taken from .tex files
  53. # i.e. we assume that at least the first class declared as base class is always correct
  54. baseclass = tree[classname][0]
  55. return [baseclass] + getFullListOfBaseClasses(baseclass)
  56. # now compare the theorical list of base classes with the list of base
  57. # classes taken from the .tex files
  58. fixed=0
  59. tofix=set()
  60. for classname in tree:
  61. theorical=getFullListOfBaseClasses(classname)
  62. real=tree[classname]
  63. # compare them
  64. if real!=theorical:
  65. print "* for class '%s' documented in '%s'" % (classname,treetex[classname])
  66. print " theorical list: %s" % theorical
  67. print " declared list: %s" % real
  68. if classname in EXCEPTIONS:
  69. tofix.add(treetex[classname])
  70. print " cannot fix automatically (blacklisted class!)\n"
  71. continue
  72. # fix it!
  73. file = open(treetex[classname], "r")
  74. content = file.readlines()
  75. #print "old content is:"
  76. #print ''.join(content)
  77. # remove old \helpref lines
  78. startidx = treepos[classname]
  79. count = 0
  80. while count < len(tree[classname]):
  81. # we want to remove n \helpref lines, where 'n' is the
  82. # number of base classes declared
  83. if content[startidx].startswith('\helpref'):
  84. del content[startidx]
  85. count = count+1
  86. else:
  87. startidx = startidx+1 # probably an empty line
  88. # insert new ones
  89. if len(theorical)>1:
  90. for j in range(len(theorical)-1):
  91. c = theorical[j]
  92. content.insert(startidx+j, "\helpref{%s}{%s}\\\\\n" % (c, c.lower()))
  93. else:
  94. j=-1
  95. c = theorical[j+1]
  96. content.insert(startidx+j+1, "\helpref{%s}{%s}\n" % (c, c.lower()))
  97. #print "new content is:"
  98. #print ''.join(content)
  99. # save the file
  100. file = open(treetex[classname], "w")
  101. file.write(''.join(content))
  102. file.flush()
  103. print " fixed the .tex file\n"
  104. fixed=fixed+1
  105. print "Total number of errors reported: %d" % fixed
  106. print "There are %d files to fix manually:\n%s" % (len(tofix), '\n'.join(tofix))