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 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)
Esempio n. 3
0
    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 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)