def get(self, path): """ Provides the required modinfo """ path = os.path.abspath(str(path)) try: modInfo = self.__cache[path] if not os.path.exists(path): del self.__cache[path] raise Exception("Cannot open " + path) lastModTime = os.path.getmtime(path) if lastModTime <= modInfo[0]: return modInfo[1] # update the key info = getBriefModuleInfoFromFile(path) self.__cache[path] = [lastModTime, info] return info except KeyError: if not os.path.exists(path): raise Exception("Cannot open " + path) info = getBriefModuleInfoFromFile(path) self.__cache[path] = [os.path.getmtime(path), info] return info
def getImportedNameDefinitionLine( path, name, info = None ): """ Searches for the given name in the given file and provides its line number. -1 if not found. Only top level names are searched through. """ if info is None: mainWindow = GlobalData().mainWindow widget = mainWindow.getWidgetForFileName( os.path.realpath( path ) ) if widget is None: # The file is not opened now info = getBriefModuleInfoFromFile( path ) else: editor = widget.getEditor() info = getBriefModuleInfoFromMemory( editor.text() ) # Check the object names for classObj in info.classes: if classObj.name == name: return classObj.line for funcObj in info.functions: if funcObj.name == name: return funcObj.line for globalObj in info.globals: if globalObj.name == name: return globalObj.line return -1
def test_lone_import( self ): " Test for lone import keyword " pythonFile = self.dir + "loneimport.py" info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile ) self.failUnless( info.isOK != True ) f = open( pythonFile ) content = f.read() f.close() info = cdmbriefparser.getBriefModuleInfoFromMemory( content ) self.failUnless( info.isOK != True ) return
def test_lone_import(self): " Test for lone import keyword " pythonFile = self.dir + "loneimport.py" info = cdmbriefparser.getBriefModuleInfoFromFile(pythonFile) self.failUnless(info.isOK != True) f = open(pythonFile) content = f.read() f.close() info = cdmbriefparser.getBriefModuleInfoFromMemory(content) self.failUnless(info.isOK != True) return
def test_print_func(self): " Test print statements " pythonFile = self.dir + "print2.py" info = cdmbriefparser.getBriefModuleInfoFromFile(pythonFile) if not info.isOK: outFileName = pythonFile.replace(".py", ".out") outFile = open(outFileName, "w") for item in info.errors: outFile.write("\n" + item) for item in info.lexerErrors: outFile.write("\n" + item) outFile.close() self.failUnless(info.isOK == True) return
def test_print_func( self ): " Test print statements " pythonFile = self.dir + "print2.py" info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile ) if not info.isOK: outFileName = pythonFile.replace( ".py", ".out" ) outFile = open( outFileName, "w" ) for item in info.errors: outFile.write( "\n" + item ) for item in info.lexerErrors: outFile.write( "\n" + item ) outFile.close() self.failUnless( info.isOK == True ) return
def main(): " main function for the netschedule multi test " parser = OptionParser( """ %prog <file name> """ ) parser.add_option( "-m", "--use-memory-buffer", action="store_true", dest="memory", default=False, help="Read the whole file first and " \ "then parse it (default: False)" ) # parse the command line options options, args = parser.parse_args() if len( args ) != 1: return parserError( parser, "One argument is expected" ) fileName = os.path.abspath( args[ 0 ] ) if not os.path.exists( fileName ): raise Exception( "Cannot find file to parse. Expected here: " + \ fileName ) info = None if options.memory: content = file( fileName ).read() info = getBriefModuleInfoFromMemory( content ) else: info = getBriefModuleInfoFromFile( fileName ) print info.niceStringify() if info.isOK: print "No errors found" else: print "Errors found" if len( info.lexerErrors ) > 0: print "Lexer errors:" print "\n".join( info.lexerErrors ) else: print "No lexer errors" if len( info.errors ) > 0: print "Parser errors:" print "\n".join( info.errors ) else: print "No parser errors" return 0
def cdmpyparserTest( files ): " Loop for the codimension parser " count = 0 for item in files: # print "Processing " + item + " ..." tempObj = cdmbriefparser.getBriefModuleInfoFromFile( item ) #if not tempObj.isOK: # print "Failed to parse: " + item # for item in tempObj.errors: # print " P: " + item # for item in tempObj.lexerErrors: # print " L: " + item count += 1 print "cdmpyparser: processed " + str(count) + " file(s)" return
def test_wrong_indent( self ): " Test wrong indent " pythonFile = self.dir + "wrong_indent.py" info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile ) self.failUnless( info.isOK != True ) outFileName = pythonFile.replace( ".py", ".out" ) outFile = open( outFileName, "w" ) outFile.write( info.niceStringify() ) for item in info.errors: outFile.write( "\n" + item ) outFile.close() okFileName = pythonFile.replace( ".py", ".ok" ) self.failUnless( files_equal( outFileName, okFileName ), "wrong indent test failed" ) return
def test_wrong_indent(self): " Test wrong indent " pythonFile = self.dir + "wrong_indent.py" info = cdmbriefparser.getBriefModuleInfoFromFile(pythonFile) self.failUnless(info.isOK != True) outFileName = pythonFile.replace(".py", ".out") outFile = open(outFileName, "w") outFile.write(info.niceStringify()) for item in info.errors: outFile.write("\n" + item) outFile.close() okFileName = pythonFile.replace(".py", ".ok") self.failUnless(files_equal(outFileName, okFileName), "wrong indent test failed") return
def cdmpyparserTest( files ): " Loop for the codimension parser " errorCount = 0 count = 0 for item in files: # print "Processing " + item + " ..." tempObj = cdmbriefparser.getBriefModuleInfoFromFile( item ) if SHOW_ERRORS: if not tempObj.isOK: errorCount += 1 print "Failed to parse: " + item for item in tempObj.errors: print item for item in tempObj.lexerErrors: print " L: " + item count += 1 print "cdmpyparser: processed " + str(count) + " file(s)" print "cdmpyparser: number of errors: " + str(errorCount) return
def meat(self, pythonFile, errorMsg): " The test process meat " info = cdmbriefparser.getBriefModuleInfoFromFile(pythonFile) self.failUnless(info.isOK == True) f = open(pythonFile) content = f.read() f.close() info = cdmbriefparser.getBriefModuleInfoFromMemory(content) self.failUnless(info.isOK == True) outFileName = pythonFile.replace(".py", ".out") outFile = open(outFileName, "w") outFile.write(info.niceStringify()) outFile.close() okFileName = pythonFile.replace(".py", ".ok") self.failUnless(files_equal(outFileName, okFileName), errorMsg) return
def meat( self, pythonFile, errorMsg ): " The test process meat " info = cdmbriefparser.getBriefModuleInfoFromFile( pythonFile ) self.failUnless( info.isOK == True ) f = open( pythonFile ) content = f.read() f.close() info = cdmbriefparser.getBriefModuleInfoFromMemory( content ) self.failUnless( info.isOK == True ) outFileName = pythonFile.replace( ".py", ".out" ) outFile = open( outFileName, "w" ) outFile.write( info.niceStringify() ) outFile.close() okFileName = pythonFile.replace( ".py", ".ok" ) self.failUnless( files_equal( outFileName, okFileName ), errorMsg ) return
def __onMcCabeObject( self, objName, fileName ): " Called when the user activated McCabe item " info = None mainWindow = GlobalData().mainWindow widget = mainWindow.getWidgetByUUID( self.__reportUUID ) if widget is None: if fileName == "": logging.error( "The unsaved buffer has been closed" ) return # No widget, but we know the file name info = getBriefModuleInfoFromFile( fileName ) else: # The widget was found editor = widget.getEditor() # The editor content has been modified, so re-parse the buffer info = getBriefModuleInfoFromMemory( editor.text() ) parts = objName.split( '.' ) currentIndex = 0 functionsContainer = info.functions classesContainer = info.classes line = -1 if objName == "__main__" and len( parts ) == 1: # Special case - global file scope line = 1 currentIndex = 1 while currentIndex < len( parts ): found = False for func in functionsContainer: if func.name == parts[ currentIndex ]: if currentIndex == len( parts ) - 1: # Found, jump to the line line = func.line break functionsContainer = func.functions classesContainer = func.classes found = True break if line != -1: break if found: currentIndex += 1 continue for klass in classesContainer: if klass.name == parts[ currentIndex ]: if currentIndex == len( parts ) - 1: # Found, jump to the line line = klass.line break functionsContainer = klass.functions classesContainer = klass.classes found = True if line != -1: break if found: currentIndex += 1 continue # Not found logging.error( "Cannot find the " + objName ) return # Here we have the line number if widget is None: GlobalData().mainWindow.openFile( fileName, line ) else: editor = widget.getEditor() editor.gotoLine( line ) editor.setFocus() return
def __getImportedObjects( moduleName, fileName ): " Provides a list of objects to be imported from " buildSystemWideModulesList() modulePath = None moduleName = str( moduleName ) if moduleName in __systemwideModules: modulePath = __systemwideModules[ moduleName ] if modulePath is None or moduleName in Settings().dirSafeModules: # it could be a built-in module return getImportNames( moduleName ) else: # Not a system wide, try search in the project # or current directories if moduleName.startswith( "." ): # That's a relative import if not fileName: # File name must be known for a relative import return set() dotCount = 0 while moduleName.startswith( "." ): dotCount += 1 moduleName = moduleName[ 1: ] # Strip as many paths as dots instruct baseDir = os.path.dirname( fileName ) while dotCount > 1: baseDir = os.path.dirname( baseDir ) dotCount -= 1 specificModules = getProjectSpecificModules( baseDir, True ) else: specificModules = getProjectSpecificModules( fileName ) if moduleName in specificModules: modulePath = specificModules[ moduleName ] if modulePath is None: return set() binarySuffix = __isBinaryModule( modulePath ) if binarySuffix is not None: try: modName = os.path.basename( modulePath ) modObj = imp.load_module( modName, None, modulePath, binarySuffix ) return set( dir( modObj ) ) except: # Failed to load a binary module return set() # It's not a binary module, so parse it and make a list of objects. # Check first if the module is loaded into the editor mainWindow = GlobalData().mainWindow editorsManager = mainWindow.editorsManagerWidget.editorsManager widget = editorsManager.getWidgetForFileName( modulePath ) if widget is None: # Not loaded, so parse it from a file info = getBriefModuleInfoFromFile( modulePath ) else: # Parse it from memory because it could be changed editor = widget.getEditor() info = getBriefModuleInfoFromMemory( editor.text() ) return __getParsedModuleNames( info )
def __onMcCabeObject(self, objName, fileName): " Called when the user activated McCabe item " info = None mainWindow = GlobalData().mainWindow widget = mainWindow.getWidgetByUUID(self.__reportUUID) if widget is None: if fileName == "": logging.error("The unsaved buffer has been closed") return # No widget, but we know the file name info = getBriefModuleInfoFromFile(fileName) else: # The widget was found editor = widget.getEditor() # The editor content has been modified, so re-parse the buffer info = getBriefModuleInfoFromMemory(editor.text()) parts = objName.split('.') currentIndex = 0 functionsContainer = info.functions classesContainer = info.classes line = -1 if objName == "__main__" and len(parts) == 1: # Special case - global file scope line = 1 currentIndex = 1 while currentIndex < len(parts): found = False for func in functionsContainer: if func.name == parts[currentIndex]: if currentIndex == len(parts) - 1: # Found, jump to the line line = func.line break functionsContainer = func.functions classesContainer = func.classes found = True break if line != -1: break if found: currentIndex += 1 continue for klass in classesContainer: if klass.name == parts[currentIndex]: if currentIndex == len(parts) - 1: # Found, jump to the line line = klass.line break functionsContainer = klass.functions classesContainer = klass.classes found = True if line != -1: break if found: currentIndex += 1 continue # Not found logging.error("Cannot find the " + objName) return # Here we have the line number if widget is None: GlobalData().mainWindow.openFile(fileName, line) else: editor = widget.getEditor() editor.gotoLine(line) editor.setFocus() return
def __getImportedObjects(moduleName, fileName): " Provides a list of objects to be imported from " buildSystemWideModulesList() modulePath = None moduleName = str(moduleName) if moduleName in __systemwideModules: modulePath = __systemwideModules[moduleName] if modulePath is None or moduleName in Settings().dirSafeModules: # it could be a built-in module return getImportNames(moduleName) else: # Not a system wide, try search in the project # or current directories if moduleName.startswith("."): # That's a relative import if not fileName: # File name must be known for a relative import return set() dotCount = 0 while moduleName.startswith("."): dotCount += 1 moduleName = moduleName[1:] # Strip as many paths as dots instruct baseDir = os.path.dirname(fileName) while dotCount > 1: baseDir = os.path.dirname(baseDir) dotCount -= 1 specificModules = getProjectSpecificModules(baseDir, True) else: specificModules = getProjectSpecificModules(fileName) if moduleName in specificModules: modulePath = specificModules[moduleName] if modulePath is None: return set() binarySuffix = __isBinaryModule(modulePath) if binarySuffix is not None: try: modName = os.path.basename(modulePath) modObj = imp.load_module(modName, None, modulePath, binarySuffix) return set(dir(modObj)) except: # Failed to load a binary module return set() # It's not a binary module, so parse it and make a list of objects. # Check first if the module is loaded into the editor mainWindow = GlobalData().mainWindow editorsManager = mainWindow.editorsManagerWidget.editorsManager widget = editorsManager.getWidgetForFileName(modulePath) if widget is None: # Not loaded, so parse it from a file info = getBriefModuleInfoFromFile(modulePath) else: # Parse it from memory because it could be changed editor = widget.getEditor() info = getBriefModuleInfoFromMemory(editor.text()) return __getParsedModuleNames(info)