Advertisement
Patasuss

Autodoc v0.2

Jun 30th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. #
  2. # AUTO-DOC
  3. # v0.2
  4. # Idea and code by Patrick Wilde
  5. #
  6. # This program generates a basic structure
  7. # of a LaTeX documentation. It analyzes .java files
  8. # and automatically finds all functions.
  9. # This script may be bugged, and the corresponding output
  10. # may not be correct. Use with caution and always check
  11. # the output.
  12. #
  13. # Please don't steal and don't claim it as your own. ;)
  14. #
  15. # Using Python 3.4
  16. #
  17.  
  18. #Trivial
  19. spaceWeight = 1
  20. tabWeight = 4
  21.  
  22. #Keywords used to identify functtion headers
  23. functionKeywords = ["public", "private", "static", "void", "int", "String", "char", "boolean", "byte", "double", "float"]
  24.  
  25. #Returns the indentation of the current line
  26. def getIndentation(line):
  27.     indentation = 0
  28.     for ch in line:
  29.         if(ch==' '):
  30.             indentation+=spaceWeight
  31.         elif(ch=='\t'):
  32.             indentation+=tabWeight
  33.         else:
  34.             break
  35.     return indentation
  36.  
  37. #Checks if the given line contains a function header
  38. def isFunctionHeader(line):
  39.     containsOneKeyword = False
  40.     for keyword in functionKeywords:
  41.         if(keyword in line):
  42.             containsOneKeyword = True
  43.             continue
  44.    
  45.     if(not containsOneKeyword):
  46.         return False
  47.    
  48.     if("(" in line and ")" in line and "{" in line):
  49.         return True
  50.    
  51.     return False
  52.  
  53. #Gets the functionname + parameters from the function header
  54. def getFunctionName(fString):
  55.     fName = ""
  56.     bracketIndex = fString.rfind(")")+1
  57.     lastChar = ''
  58.     ch = ''
  59.     openingBracketPassed = False
  60.     if(bracketIndex==-1):
  61.         return "N/A"
  62.     while(bracketIndex>0):
  63.         bracketIndex-=1
  64.         lastChar = ch
  65.         ch = fString[bracketIndex]
  66.         if( not((ch==' ' or ch=='\t') and lastChar=='(') ):
  67.             if(ch==' ' and openingBracketPassed):
  68.                 break
  69.             else:
  70.                 if(ch=='('):
  71.                     openingBracketPassed = True
  72.                 fName = ch + fName
  73.     return fName
  74.  
  75. #Extracts the classname from the filepath
  76. def getClassName(fileName):
  77.     fileName = fileName[0:-5]
  78.     slashIndex = fileName.rfind('/')
  79.    
  80.     if(slashIndex>-1):
  81.         fileName = fileName[slashIndex+1:]
  82.    
  83.     return fileName
  84.  
  85. #Generates the latex documentation code for a function (paragraph + filecode)
  86. def getLatexFunctionCode(fPath, fString, startLine, endLine):
  87.     code = ""
  88.     code += "\\paragraph{"+getFunctionName(fString)+"} Beschreibung der Funktion.\n"
  89.     code += "\\filecode{"+str(startLine)+"}{"+str(endLine)+"}{"+fPath+"}\n\n"
  90.     return code
  91.    
  92. #Generates the latex documentation code for a class
  93. def getLatexClassCode(fPath, endLine):
  94.     classname = getClassName(fPath)
  95.     code = ""
  96.     code = "%\n%"+classname.upper()+"\n%\n"
  97.     code += "\\subsection*{"+classname+"}\n Beschreibung der Klasse.\n "
  98.     #code += "\\filecode{1}{"+str(endLine)+"}{"+fPath+"} \n"
  99.     return code
  100.  
  101. #Generates the latex documentation code for an entire class
  102. def generateDoc(fileToOpen):
  103.     sourceFile = open(fileToOpen, "r")
  104.    
  105.     #0 - 'idle' mode
  106.     #1 - javadoc found, awaiting function line
  107.     #2 - found function, awaiting closing '}'
  108.     mode = 0
  109.     javadocLine = 0
  110.     functionIndentation = 0
  111.     functionString = ""
  112.     currLineNumber = 0
  113.     firstFunctionLineNumber = 0
  114.    
  115.     latexString = ""
  116.    
  117.     for currLine in sourceFile:
  118.         currLineNumber+=1
  119.         if(mode==0):
  120.             if("/**" in currLine):
  121.                 mode=1
  122.                 javadocLine=currLineNumber
  123.        
  124.         elif(mode==1):
  125.             if("*" in currLine):
  126.                 continue
  127.             elif("@" in currLine):
  128.                 continue
  129.             elif(isFunctionHeader(currLine)):
  130.                 mode=2
  131.                 functionIndentation = getIndentation(currLine)
  132.                 functionString = currLine
  133.             else:
  134.                 mode = 0
  135.  
  136.         elif(mode==2):
  137.                 if("}" in currLine and getIndentation(currLine)==functionIndentation):
  138.                    
  139.                     if(firstFunctionLineNumber==0):
  140.                         firstFunctionLineNumber = javadocLine
  141.                        
  142.                     latexString += getLatexFunctionCode(fileToOpen, functionString, javadocLine, currLineNumber)
  143.                     mode=0
  144.        
  145.         else:
  146.             print("%[AUTODOC] Invalid mode!")
  147.  
  148.     latexString = getLatexClassCode(fileToOpen, javadocLine-1) + latexString + "\n"
  149.     print(latexString)
  150.     sourceFile.close()
  151.  
  152. MainPath = "../Implementierung/uebung06/src/"
  153. filesWithoutPath = ["SccDotVisitor", "SystemStateChecker","SystemState", "TarjanCopyPasteProvider"]
  154.  
  155. files = []
  156.  
  157. for f in filesWithoutPath:
  158.     files.append(MainPath+f+".java")
  159.  
  160. for fileName in files:
  161.     generateDoc(fileName)
  162.  
  163. #generateDoc("../LabyrinthFactory.java")
  164.  
  165. #print(getClassName("../hello/my/name/is/dog/LabyrinthFactory.java"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement