def changeAnnotationState_(self, doAnnotate): ''' Change the class annotation state. ''' LANG = ProgramExecution.Languages currentProject = getCurrentProject() mySourceCodeParser = SourceCodeParser(currentProject.getLanguage(), currentProject.getMainFile(), currentProject.getMainFunction()) auxFileName = self.fileToAnnotate_ + ".aux" shutil.copy(self.fileToAnnotate_, auxFileName) try: if currentProject.getLanguage() == LANG.C_PLUS_PLUS: if doAnnotate: mySourceCodeParser.annotateCppClasses(auxFileName, self.fileToAnnotate_, self.headerToInclude_, [self.className_]) else: mySourceCodeParser.unannotateCppClasses(auxFileName, self.fileToAnnotate_, [self.className_]) else: assert currentProject.getLanguage() == LANG.PYTHON classNameStr = self.headerToInclude_ + '.' + self.className_ if doAnnotate: mySourceCodeParser.annotatePythonObject(classNameStr, False) else: mySourceCodeParser.unAnnotatePythonObject(classNameStr, False) except Exception: QMessageBox.about(None, 'ERROR','Error al (des)anotar la clase. Traceback:\n\n' + traceback.format_exc()) shutil.copy(auxFileName, self.fileToAnnotate_) os.remove(auxFileName)
def testCppGetAllClassesAndFunctions(self): MY_HEADER = "MyFunctions.h" myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, MY_HEADER) mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.C_PLUS_PLUS, myFunctionsFileName) classes, functions = mySourceCodeParser.getAllClassesAndFunctions( myFunctionsFileName) expectedFunctions = [ 'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction', 'noParamsFunction', 'mySubstring', 'processVector', 'processIntMap' ] expectedClasses = [ 'MyClass', 'ClassWithConstructor', 'ClassWithStaticMethods' ] self.assertEqual(expectedFunctions, functions) self.assertEqual(expectedClasses, classes)
def loadMainFunctionComboBox_(self): ''' Load combo box for main function. It uses a SourceCodeParser instance to get all the possible global functions. ''' language = self.getSelectedLanguage_() self.mainFunctionComboBox.clear() mainFile = str(self.mainFileEditBox.toPlainText()) if self.validateMainFile_(mainFile, warnToUser = False): mySourceCodeParser = SourceCodeParser(language, mainFile) _, functions = mySourceCodeParser.getAllClassesAndFunctions(mainFile) for funcName in functions: self.mainFunctionComboBox.addItem(funcName) if self.isEditMode_(): mainFunctionIndex = self.mainFunctionComboBox.findText(self.projectToEdit_.getMainFunction()) self.mainFunctionComboBox.setCurrentIndex(mainFunctionIndex)
def testPythonGetAllClassesAndFunctions(self): mainFileName = 'mainFileForTests.py' myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, "MyFunctions.py") mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.PYTHON, mainFileName) classes, functions = mySourceCodeParser.getAllClassesAndFunctions( myFunctionsFileName, "myFunctions") expectedFunctions = [ 'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction', 'noParamsFunction', 'mySubstring', 'processList', 'processDict' ] expectedClasses = [ 'MyClass', 'ClassWithDummyParameters', 'NonAnnotatedClass', 'ClassWithConstructor', 'ClassWithStaticAndClassMethods' ] self.assertEqual(expectedFunctions, functions) self.assertEqual(expectedClasses, classes)
def loadMainFunctionComboBox_(self): ''' Load combo box for main function. It uses a SourceCodeParser instance to get all the possible global functions. ''' language = self.getSelectedLanguage_() self.mainFunctionComboBox.clear() mainFile = str(self.mainFileEditBox.toPlainText()) if self.validateMainFile_(mainFile, warnToUser=False): mySourceCodeParser = SourceCodeParser(language, mainFile) _, functions = mySourceCodeParser.getAllClassesAndFunctions( mainFile) for funcName in functions: self.mainFunctionComboBox.addItem(funcName) if self.isEditMode_(): mainFunctionIndex = self.mainFunctionComboBox.findText( self.projectToEdit_.getMainFunction()) self.mainFunctionComboBox.setCurrentIndex(mainFunctionIndex)
def __init__(self, fileToAnnotate, headerToInclude): ''' Constructor. ''' TreeItemComposite.__init__(self, TreeItemType.FILE, fileToAnnotate, headerToInclude) self.fileToAnnotate_ = fileToAnnotate self.headerToInclude_ = headerToInclude currentProject = getCurrentProject() self.parser_ = SourceCodeParser(currentProject.getLanguage(), currentProject.getMainFile(), currentProject.getMainFunction())
def processAnnotatePythonObject_(self, doAdd, testingFunctionName, pythonObjectName, itsAModule, functionName=None): mainFileName = os.path.join(TEST_FILES_FOLDER, testingFunctionName + '.py') expectedFileName = os.path.join( TEST_FILES_FOLDER, testingFunctionName + "_expected" + '.py') if not doAdd: aux = mainFileName mainFileName = expectedFileName expectedFileName = aux mainFileBackup = mainFileName + ".mybkp" try: shutil.copy(mainFileName, mainFileBackup) #Verify second (un)annotation yields the same file for i in range(2): mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.PYTHON, mainFileName) if doAdd: mySourceCodeParser.annotatePythonObject( pythonObjectName, itsAModule, functionName) else: mySourceCodeParser.unAnnotatePythonObject( pythonObjectName, itsAModule, functionName) self.compareFileContents_(mainFileName, expectedFileName) finally: if os.path.exists(mainFileBackup): shutil.copy(mainFileBackup, mainFileName) os.remove(mainFileBackup)
def processAnnotateMainTestingFunction_(self, doAdd, language, functionName): dumpFileName = "call_graph.json" assert language in (ProgramExecution.Languages.PYTHON, ProgramExecution.Languages.C_PLUS_PLUS) sourceExt = ".py" if language == ProgramExecution.Languages.PYTHON else ".cpp" mainFileName = os.path.join(TEST_FILES_FOLDER, functionName + sourceExt) expectedFileName = os.path.join(TEST_FILES_FOLDER, functionName + "_expected" + sourceExt) if not doAdd: aux = mainFileName mainFileName = expectedFileName expectedFileName = aux mainFileBackup = mainFileName + ".mybkp" try: shutil.copy(mainFileName, mainFileBackup) #Verify second (un)annotation yields the same file for i in range(2): mySourceCodeParser = SourceCodeParser(language, mainFileName) if doAdd: mySourceCodeParser.annotateMainFile(dumpFileName) else: mySourceCodeParser.unAnnotateMainFile() self.compareFileContents_(mainFileName, expectedFileName) finally: if os.path.exists(mainFileBackup): shutil.copy(mainFileBackup, mainFileName) os.remove(mainFileBackup)
def changeAnnotationState_(self, doAnnotate): ''' Change the class annotation state. ''' LANG = ProgramExecution.Languages currentProject = getCurrentProject() mySourceCodeParser = SourceCodeParser(currentProject.getLanguage(), currentProject.getMainFile(), currentProject.getMainFunction()) auxFileName = self.fileToAnnotate_ + ".aux" shutil.copy(self.fileToAnnotate_, auxFileName) try: if currentProject.getLanguage() == LANG.C_PLUS_PLUS: if doAnnotate: mySourceCodeParser.annotateCppClasses( auxFileName, self.fileToAnnotate_, self.headerToInclude_, [self.className_]) else: mySourceCodeParser.unannotateCppClasses( auxFileName, self.fileToAnnotate_, [self.className_]) else: assert currentProject.getLanguage() == LANG.PYTHON classNameStr = self.headerToInclude_ + '.' + self.className_ if doAnnotate: mySourceCodeParser.annotatePythonObject( classNameStr, False) else: mySourceCodeParser.unAnnotatePythonObject( classNameStr, False) except Exception: QMessageBox.about( None, 'ERROR', 'Error al (des)anotar la clase. Traceback:\n\n' + traceback.format_exc()) shutil.copy(auxFileName, self.fileToAnnotate_) os.remove(auxFileName)
def accept(self): ''' User accept. If data is valid, store the project with the help of the ProjectManager. ''' ok = self.validateProject_() if not ok: return theProjectsManager = project.projectsManagerInstance() name = str(self.projectNameTextEdit.toPlainText()) projectsFolder = str(self.projectsLocationEditBox.toPlainText()) folder = os.path.join(projectsFolder, name) language = self.getSelectedLanguage_() solutionFile = str(self.solutionEditBox.toPlainText()) cppProject = str(self.cppProjectEditBox.toPlainText()) mainFile = str(self.mainFileEditBox.toPlainText()) mainFunction = str(self.mainFunctionComboBox.currentText()) compilingCommand = str(self.compilingCommandEditBox.toPlainText()) exeCommand = str(self.exeCommandTextEdit.toPlainText()) aProject = project.Project(name, folder, language, solutionFile, mainFile, mainFunction, exeCommand, [], cppProject, compilingCommand) #Annotate main file before adding it to the tree if self.isEditMode_(): #If main function changed, unannotate previous main function previousMainFunction = self.projectToEdit_.getMainFunction() if mainFunction != previousMainFunction: mySourceCodeParser = SourceCodeParser(language, mainFile, previousMainFunction) mySourceCodeParser.unAnnotateMainFile() mySourceCodeParser = SourceCodeParser(language, mainFile, mainFunction) mainBackupFileName = mainFile + ".bkp" shutil.copyfile(mainFile, mainBackupFileName) dumpFileName = os.path.join(aProject.getExecutionsFolder(), name + ".json") mySourceCodeParser.annotateMainFile(dumpFileName) if language == ProgramExecution.Languages.C_PLUS_PLUS: self.addBraCustomInfoToCppProject_(cppProject) theProjectsManager.storeProject(aProject) theProjectsManager.setCurrentProjectName(aProject.getName()) QDialog.accept(self)
def removeProject(self, projectName): ''' Remove a project. ''' if projectName == self.currentProjectName_: self.currentProjectName_ = None theProject = self.projects_[projectName] #Unannotate Files theLanguage = theProject.getLanguage() treeFiles = theProject.getTreeFiles() mainFile = theProject.getMainFile() mainFunction = theProject.getMainFunction() aSourceCodeParser = SourceCodeParser(theLanguage, mainFile, mainFunction) if theLanguage == ProgramExecution.Languages.C_PLUS_PLUS: for sourceFile, _ in treeFiles: #Unannotate file sourceFileTmp = sourceFile + '.tmp' shutil.copy(sourceFile, sourceFileTmp) try: aSourceCodeParser.unAnnotateCppFile(sourceFile, sourceFileTmp) shutil.copy(sourceFileTmp, sourceFile) finally: if os.path.exists(sourceFileTmp): os.remove(sourceFileTmp) aSourceCodeParser.unAnnotateMainFile() cppProject = theProject.getCppProject() cppProjectBkp = getCppProjectFileBackup(cppProject) if os.path.exists(cppProjectBkp): shutil.copy(cppProjectBkp, cppProject) os.remove(cppProjectBkp) projectFolder = theProject.getFolder() removeFolder(projectFolder) del self.projects_[projectName] ProjectSerializer.saveProjectsList(self.projects_)
def processCppTestingFunction_(self, doAdd, functionName, classNames=None, functionNames=None): mainFileName = os.path.join(TEST_FILES_FOLDER, "testAnnotateMainCpp.cpp") originalFileName = os.path.join(TEST_FILES_FOLDER, functionName + ".h") resultFileName = os.path.join(TEST_FILES_FOLDER, functionName + "_result.h") expectedFileName = os.path.join(TEST_FILES_FOLDER, functionName + "_expected.h") if not doAdd: aux = originalFileName originalFileName = expectedFileName expectedFileName = aux mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.C_PLUS_PLUS, mainFileName) headerToInclude = functionName + ".h" #Verify second (un)annotation yields the same file try: originalFileBkp = originalFileName + '.bkp' shutil.copy(originalFileName, originalFileBkp) for i in range(2): try: ok = False if doAdd: if classNames is None and functionNames is None: mySourceCodeParser.annotateCppFile( originalFileName, resultFileName, headerToInclude) else: if classNames is not None: if functionNames is not None: assert len(classNames) == 1 mySourceCodeParser.annotateCppClassFunctions( originalFileName, resultFileName, headerToInclude, classNames[0], functionNames) else: mySourceCodeParser.annotateCppClasses( originalFileName, resultFileName, headerToInclude, classNames) if functionNames is not None and classNames is None: mySourceCodeParser.annotateCppFunctions( originalFileName, resultFileName, headerToInclude, functionNames) else: if classNames is None and functionNames is None: mySourceCodeParser.unannotateCppFile( originalFileName, resultFileName) else: if classNames is not None: mySourceCodeParser.unannotateCppClasses( originalFileName, resultFileName, classNames) if functionNames is not None and classNames is None: mySourceCodeParser.unannotateCppFunctions( originalFileName, resultFileName, functionNames) self.compareFileContents_(resultFileName, expectedFileName) ok = True finally: if i == 0 and ok: shutil.copy(resultFileName, originalFileName) if os.path.exists(resultFileName): os.remove(resultFileName) finally: if os.path.exists(originalFileBkp): shutil.copy(originalFileBkp, originalFileName) os.remove(originalFileBkp)
def testPythonGetAllClassesAndFunctionsAnnotations(self): expectedFunctions = [ 'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction', 'noParamsFunction', 'mySubstring', 'processList', 'processDict' ] expectedClasses = [ 'MyClass', 'ClassWithDummyParameters', 'NonAnnotatedClass', 'ClassWithConstructor', 'ClassWithStaticAndClassMethods' ] def verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions): def verifyContainer(container, expectedContainer, expectedAnnotations): i = 0 for pythonObj, annotState in container: self.assertEqual(pythonObj, expectedContainer[i]) status = AnnotationState.ANNOTATED if pythonObj in expectedAnnotations else AnnotationState.NOT_ANNOTATED self.assertEqual(annotState, status) i += 1 verifyContainer(classes, expectedClasses, expectedAnnotClasses) verifyContainer(functions, expectedFunctions, expectedAnnotFunctions) #Check without annotations mainFileName = 'mainFileForTests.py' myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, "MyFunctions.py") mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.PYTHON, mainFileName) classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( myFunctionsFileName, "MyFunctions") #No annotations expected, just the function and classes names verifyStates(classes, functions, expectedAnnotClasses=[], expectedAnnotFunctions=[]) #Now, annotate a couple of functions and classes mainFileBackup = mainFileName + ".mybkp" try: shutil.copy(mainFileName, mainFileBackup) #Annotate main file mySourceCodeParser.annotateMainFile("dumpFileName.json") #Annotate a couple of classes mySourceCodeParser.annotatePythonObject("MyFunctions.MyClass", False) mySourceCodeParser.annotatePythonObject( "MyFunctions.ClassWithConstructor", False) expectedAnnotClasses = ['MyClass', 'ClassWithConstructor'] #Annotate a couple of functions mySourceCodeParser.annotatePythonObject("MyFunctions", True, 'subtract') mySourceCodeParser.annotatePythonObject("MyFunctions", True, 'outerFunction') expectedAnnotFunctions = ['subtract', 'outerFunction'] #Get states, and verify annotations classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( myFunctionsFileName, "MyFunctions") verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions) finally: if os.path.exists(mainFileBackup): shutil.copy(mainFileBackup, mainFileName) os.remove(mainFileBackup)
def testCppGetAllClassesAndFunctionsAnnotations(self): MY_HEADER = "MyFunctions.h" expectedFunctions = [ 'myPrint', 'add', 'subtract', 'innerFunction', 'outerFunction', 'noParamsFunction', 'mySubstring', 'processVector', 'processIntMap' ] expectedClasses = [ 'MyClass', 'ClassWithConstructor', 'ClassWithStaticMethods' ] def verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions): def verifyContainer(container, expectedContainer, expectedAnnotations): i = 0 for obj, annotState in container: self.assertEqual(obj, expectedContainer[i]) status = AnnotationState.ANNOTATED if obj in expectedAnnotations else AnnotationState.NOT_ANNOTATED self.assertEqual(annotState, status) i += 1 verifyContainer(classes, expectedClasses, expectedAnnotClasses) verifyContainer(functions, expectedFunctions, expectedAnnotFunctions) #Check without annotations mainFileName = os.path.join(TEST_FILES_FOLDER, "testAnnotateMainCpp.cpp") myFunctionsFileName = os.path.join(TEST_FILES_FOLDER, MY_HEADER) mySourceCodeParser = SourceCodeParser( ProgramExecution.Languages.C_PLUS_PLUS, mainFileName) classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( myFunctionsFileName, ) #No annotations expected, just the function and classes names verifyStates(classes, functions, expectedAnnotClasses=[], expectedAnnotFunctions=[]) #Now, annotate a couple of functions and classes myFunctionsFileBackup = myFunctionsFileName + ".mybkp" resultFileName = os.path.join(TEST_FILES_FOLDER, myFunctionsFileName + "_result.h") resultFileName2 = resultFileName + ".2" try: shutil.copy(myFunctionsFileName, myFunctionsFileBackup) #Annotate a couple of classes expectedAnnotClasses = ['MyClass', 'ClassWithConstructor'] mySourceCodeParser.annotateCppClasses(myFunctionsFileName, resultFileName, MY_HEADER, expectedAnnotClasses) #Annotate a couple of functions expectedAnnotFunctions = ['subtract', 'outerFunction'] mySourceCodeParser.annotateCppFunctions(resultFileName, resultFileName2, MY_HEADER, expectedAnnotFunctions) #Get states, and verify annotations classes, functions = mySourceCodeParser.getAllClassesAndFunctionsAnnotations( resultFileName2, MY_HEADER) verifyStates(classes, functions, expectedAnnotClasses, expectedAnnotFunctions) finally: if os.path.exists(myFunctionsFileBackup): shutil.copy(myFunctionsFileBackup, myFunctionsFileName) os.remove(myFunctionsFileBackup) if os.path.exists(resultFileName): os.remove(resultFileName) if os.path.exists(resultFileName2): os.remove(resultFileName2)