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 __populateFromProject(self): """Populates find name dialog from the project files""" mainWindow = GlobalData().mainWindow for fname in GlobalData().project.filesList: if isPythonFile(fname): widget = mainWindow.getWidgetForFileName(fname) if widget is None: info = GlobalData().briefModinfoCache.get(fname) else: info = getBriefModuleInfoFromMemory( widget.getEditor().text) self.__populateInfo(info, fname)
def __populateFromProject( self ): " Populates find name dialog from the project files " mainWindow = GlobalData().mainWindow for fname in GlobalData().project.filesList: if detectFileType( fname ) in [ PythonFileType, Python3FileType ]: widget = mainWindow.getWidgetForFileName( fname ) if widget is None: info = GlobalData().briefModinfoCache.get( fname ) else: content = widget.getEditor().text() info = getBriefModuleInfoFromMemory( content ) self.__populateInfo( info, fname ) return
def __projectFiles(self, filters): """Project files list respecting the mask""" mainWindow = GlobalData().mainWindow files = [] for fname in GlobalData().project.filesList: if fname.endswith(sep): continue if self.__filterMatch(filters, fname): widget = mainWindow.getWidgetForFileName(fname) if widget is None: # Do not check for broken symlinks if isFileSearchable(fname, False): files.append(ItemToSearchIn(fname, "")) else: if widget.getType() in \ [MainWindowTabWidgetBase.PlainTextEditor]: files.append(ItemToSearchIn(fname, widget.getUUID())) QApplication.processEvents() if self.__cancelRequest: raise Exception("Cancel request") return files
def __populateFromProject(self): """Populates find name dialog from the project files""" mainWindow = GlobalData().mainWindow showTooltips = Settings()['findFileTooltips'] for fname in GlobalData().project.filesList: if fname.endswith(os.path.sep): continue mime, icon, _ = getFileProperties(fname) tooltip = "" if showTooltips and isPythonMime(mime): widget = mainWindow.getWidgetForFileName(fname) if widget is None: info = GlobalData().briefModinfoCache.get(fname) else: content = widget.getEditor().text info = getBriefModuleInfoFromMemory(content) if info.docstring is not None: tooltip = info.docstring.text newItem = FileItem(self.rootItem, icon, fname, tooltip) self.rootItem.appendChild(newItem) self.count += 1
def __projectFiles( self, filterRe ): " Project files list respecting the mask " mainWindow = GlobalData().mainWindow files = [] for fname in GlobalData().project.filesList: if fname.endswith( sep ): continue if filterRe is None or filterRe.match( fname ): widget = mainWindow.getWidgetForFileName( fname ) if widget is None: # Do not check for broken symlinks if isFileSearchable( fname, False ): files.append( ItemToSearchIn( fname, "" ) ) else: if widget.getType() in \ [ MainWindowTabWidgetBase.PlainTextEditor ]: files.append( ItemToSearchIn( fname, widget.getUUID() ) ) QApplication.processEvents() if self.__cancelRequest: raise Exception( "Cancel request" ) return files
def __dirFiles( self, path, filterRe, files ): " Files recursively for the dir " for item in os.listdir( path ): QApplication.processEvents() if self.__cancelRequest: raise Exception( "Cancel request" ) if os.path.isdir( path + item ): if item in [ ".svn", ".cvs" ]: # It does not make sense to search in revision control dirs continue anotherDir, isLoop = resolveLink( path + item ) if not isLoop: self.__dirFiles( anotherDir + sep, filterRe, files ) continue if not os.path.isfile( path + item ): continue realItem, isLoop = resolveLink( path + item ) if isLoop: continue if filterRe is None or filterRe.match( realItem ): found = False for itm in files: if itm.fileName == realItem: found = True break if not found: mainWindow = GlobalData().mainWindow widget = mainWindow.getWidgetForFileName( realItem ) if widget is None: if isFileSearchable( realItem ): files.append( ItemToSearchIn( realItem, "" ) ) else: if widget.getType() in \ [ MainWindowTabWidgetBase.PlainTextEditor ]: files.append( ItemToSearchIn( realItem, widget.getUUID() ) ) return
def __dirFiles(self, path, filterRe, files): " Files recursively for the dir " for item in os.listdir(path): QApplication.processEvents() if self.__cancelRequest: raise Exception("Cancel request") if os.path.isdir(path + item): if item in [".svn", ".cvs"]: # It does not make sense to search in revision control dirs continue anotherDir, isLoop = resolveLink(path + item) if not isLoop: self.__dirFiles(anotherDir + sep, filterRe, files) continue if not os.path.isfile(path + item): continue realItem, isLoop = resolveLink(path + item) if isLoop: continue if filterRe is None or filterRe.match(realItem): found = False for itm in files: if itm.fileName == realItem: found = True break if not found: mainWindow = GlobalData().mainWindow widget = mainWindow.getWidgetForFileName(realItem) if widget is None: if isFileSearchable(realItem): files.append(ItemToSearchIn(realItem, "")) else: if widget.getType() in \ [ MainWindowTabWidgetBase.PlainTextEditor ]: files.append( ItemToSearchIn(realItem, widget.getUUID())) return
def __populateFromProject(self): " Populates find name dialog from the project files " mainWindow = GlobalData().mainWindow showTooltips = Settings().findFileTooltips for fname in GlobalData().project.filesList: if fname.endswith(os.path.sep): continue fileType = detectFileType(fname) tooltip = "" if showTooltips and fileType in [PythonFileType, Python3FileType]: widget = mainWindow.getWidgetForFileName(fname) if widget is None: info = GlobalData().briefModinfoCache.get(fname) else: content = widget.getEditor().text() info = getBriefModuleInfoFromMemory(content) if info.docstring is not None: tooltip = info.docstring.text newItem = FileItem(self.rootItem, getFileIcon(fileType), fname, tooltip) self.rootItem.appendChild(newItem) self.count += 1 return
def __populateFromProject( self ): " Populates find name dialog from the project files " mainWindow = GlobalData().mainWindow showTooltips = Settings().findFileTooltips for fname in GlobalData().project.filesList: if fname.endswith( os.path.sep ): continue fileType = detectFileType( fname ) tooltip = "" if showTooltips and fileType in [ PythonFileType, Python3FileType ]: widget = mainWindow.getWidgetForFileName( fname ) if widget is None: info = GlobalData().briefModinfoCache.get( fname ) else: content = widget.getEditor().text() info = getBriefModuleInfoFromMemory( content ) if info.docstring is not None: tooltip = info.docstring.text newItem = FileItem( self.rootItem, getFileIcon( fileType ), fname, tooltip ) self.rootItem.appendChild( newItem ) self.count += 1 return
def __process(self): " Analysis process " self.__inProgress = True mainWindow = GlobalData().mainWindow editorsManager = mainWindow.editorsManagerWidget.editorsManager modified = editorsManager.getModifiedList( True) # True - only project files if modified: modNames = [modItem[0] for modItem in modified] label = "File" if len(modified) >= 2: label += "s" label += ": " logging.warning( "The analisys is performed for the content of saved files. " \ "The unsaved modifications will not be taken into account. " \ + label + ", ".join( modNames ) ) self.__updateFoundLabel() self.__progressBar.setRange(0, len(self.__srcModel.rootItem.childItems)) QApplication.processEvents() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) count = 0 candidates = [] for treeItem in self.__srcModel.rootItem.childItems: if self.__cancelRequest: break name = str(treeItem.data(0)).split('(')[0] path = os.path.realpath(treeItem.getPath()) lineNumber = int(treeItem.data(2)) absPosition = treeItem.sourceObj.absPosition count += 1 self.__progressBar.setValue(count) self.__infoLabel.setText(self.__formInfoLabel(name)) QApplication.processEvents() # Analyze the name found = False try: # True is for throwing exceptions locations = getOccurrences(path, absPosition, True) if len( locations ) == 1 and \ locations[ 0 ][ 1 ] == lineNumber: found = True index = getSearchItemIndex(candidates, path) if index < 0: widget = mainWindow.getWidgetForFileName(path) if widget is None: uuid = "" else: uuid = widget.getUUID() newItem = ItemToSearchIn(path, uuid) candidates.append(newItem) index = len(candidates) - 1 candidates[index].addMatch(name, lineNumber) except Exception, exc: # There is nothing interesting with exceptions here. # It seems to me that rope throws them in case if the same item # is declared multiple times in a file. I also suspect that # exceptions may come up in case of syntactic errors. # So I just suppress them. pass #logging.warning( "Error detected while analysing " + \ # self.__whatAsString() + " '" + name + \ # "'. Message: " + str( exc ) ) if found: self.__found += 1 self.__updateFoundLabel() QApplication.processEvents()
def __process(self): """Analysis process""" self.__inProgress = True mainWindow = GlobalData().mainWindow QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) # return code gives really nothing. So the error in running the utility # is detected by the stderr content. # Also, there could be a mix of messages for a project. Some files # could have syntax errors - there will be messages on stderr. The # other files are fine so there will be messages on stdout stdout, stderr = self.__run() self.candidates = [] for line in stdout.splitlines(): line = line.strip() if line: # Line is like file.py:2: unused variable 'a' (60% confidence) try: startIndex = line.find(':') if startIndex < 0: continue endIndex = line.find(':', startIndex + 1) if endIndex < 0: continue fileName = line[:startIndex] startIndex = line.find(':') if startIndex < 0: continue endIndex = line.find(':', startIndex + 1) if endIndex < 0: continue fileName = os.path.abspath(line[:startIndex]) lineno = int(line[startIndex + 1:endIndex]) message = line[endIndex + 1:].strip() except: continue index = getSearchItemIndex(self.candidates, fileName) if index < 0: widget = mainWindow.getWidgetForFileName(fileName) if widget is None: uuid = '' else: uuid = widget.getUUID() newItem = ItemToSearchIn(fileName, uuid) self.candidates.append(newItem) index = len(self.candidates) - 1 self.candidates[index].addMatch('', lineno, message) self.__found += 1 self.__updateFoundLabel() QApplication.processEvents() if self.__newSearch: # Do the action only for the new search. # Redo action will handle the results on its own if self.__found == 0: if stderr: logging.error('Error running vulture for ' + self.__path + ':\n' + stderr) else: logging.info('No unused candidates found') else: mainWindow.displayFindInFiles(VultureSearchProvider.getName(), self.candidates, {'path': self.__path}) QApplication.restoreOverrideCursor() self.__infoLabel.setText('Done') self.__inProgress = False self.accept()
def __process(self): """Analysis process""" self.__inProgress = True mainWindow = GlobalData().mainWindow editorsManager = mainWindow.editorsManagerWidget.editorsManager # True - only project files modified = editorsManager.getModifiedList(True) if modified: modNames = [modItem[0] for modItem in modified] label = "File" if len(modified) >= 2: label += "s" label += ": " logging.warning("The analisys is performed for the content " "of saved files. The unsaved modifications will " "not be taken into account. " + label + ", ".join(modNames)) self.__updateFoundLabel() self.__progressBar.setRange(0, len(self.__srcModel.rootItem.childItems)) QApplication.processEvents() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) count = 0 candidates = [] for treeItem in self.__srcModel.rootItem.childItems: if self.__cancelRequest: break name = str(treeItem.data(0)).split('(')[0] path = os.path.realpath(treeItem.getPath()) lineNumber = int(treeItem.data(2)) pos = treeItem.sourceObj.pos count += 1 self.__progressBar.setValue(count) self.__infoLabel.setText(self.__formInfoLabel(name)) QApplication.processEvents() # Analyze the name found = False definitions = getOccurrences(None, path, lineNumber, pos) if len(definitions) == 1: found = True index = getSearchItemIndex(candidates, path) if index < 0: widget = mainWindow.getWidgetForFileName(path) if widget is None: uuid = "" else: uuid = widget.getUUID() newItem = ItemToSearchIn(path, uuid) candidates.append(newItem) index = len(candidates) - 1 candidates[index].addMatch(name, lineNumber) if found: self.__found += 1 self.__updateFoundLabel() QApplication.processEvents() if self.__found == 0: # The analysis could be interrupted if not self.__cancelRequest: logging.info("No unused candidates found") else: mainWindow.displayFindInFiles("", candidates) QApplication.restoreOverrideCursor() self.__infoLabel.setText('Done') self.__inProgress = False self.accept()
def __process( self ): " Analysis process " self.__inProgress = True mainWindow = GlobalData().mainWindow editorsManager = mainWindow.editorsManagerWidget.editorsManager modified = editorsManager.getModifiedList( True ) # True - only project files if modified: modNames = [ modItem[ 0 ] for modItem in modified ] label = "File" if len( modified ) >= 2: label += "s" label += ": " logging.warning( "The analisys is performed for the content of saved files. " \ "The unsaved modifications will not be taken into account. " \ + label + ", ".join( modNames ) ) self.__updateFoundLabel() self.__progressBar.setRange( 0, len( self.__srcModel.rootItem.childItems ) ) QApplication.processEvents() QApplication.setOverrideCursor( QCursor( Qt.WaitCursor ) ) count = 0 candidates = [] for treeItem in self.__srcModel.rootItem.childItems: if self.__cancelRequest: break name = str( treeItem.data( 0 ) ).split( '(' )[ 0 ] path = os.path.realpath( treeItem.getPath() ) lineNumber = int( treeItem.data( 2 ) ) absPosition = treeItem.sourceObj.absPosition count += 1 self.__progressBar.setValue( count ) self.__infoLabel.setText( self.__formInfoLabel( name ) ) QApplication.processEvents() # Analyze the name found = False try: # True is for throwing exceptions locations = getOccurrences( path, absPosition, True ) if len( locations ) == 1 and \ locations[ 0 ][ 1 ] == lineNumber: found = True index = getSearchItemIndex( candidates, path ) if index < 0: widget = mainWindow.getWidgetForFileName( path ) if widget is None: uuid = "" else: uuid = widget.getUUID() newItem = ItemToSearchIn( path, uuid ) candidates.append( newItem ) index = len( candidates ) - 1 candidates[ index ].addMatch( name, lineNumber ) except Exception, exc: # There is nothing interesting with exceptions here. # It seems to me that rope throws them in case if the same item # is declared multiple times in a file. I also suspect that # exceptions may come up in case of syntactic errors. # So I just suppress them. pass #logging.warning( "Error detected while analysing " + \ # self.__whatAsString() + " '" + name + \ # "'. Message: " + str( exc ) ) if found: self.__found += 1 self.__updateFoundLabel() QApplication.processEvents()