def onDoGrep(self, dirname, filenamepatt, grepkey): # on Go in grep dialog: populate scrolled list with matches from PP4E.Tools.find import find from PP4E.Gui.Tour.scrolledlist import ScrolledList class ScrolledFilenames(ScrolledList): def runCommand(self, selection): # on list double-click file, line = selection.split(' [', 1)[0].split('@') editor = TextEditorMainPopup(loadFirst=file, winTitle=' grep match') editor.onGoto(int(line)) editor.text.focus_force() # no, really # should thread/queue/after me showinfo('PyEdit Wait', 'Ready to search files (a pause may follow)...') matches = [] for filepath in find(pattern=filenamepatt, startdir=dirname): try: for (linenum, linestr) in enumerate(open(filepath)): if grepkey in linestr: matches.append('%s@%d [%s]' % (filepath, linenum + 1, linestr)) except: print('Failed:', filepath) # Unicode errors, probably if not matches: showinfo('PyEdit', 'No matches found') else: popup = Tk() popup.title('PyEdit - grep matches: %r' % grepkey) ScrolledFilenames(parent=popup, options=matches)
def grepThreadProducer(self, filenamepatt, dirname, grepkey, myqueue): from PP4E.Tools.find import find matches = [] for filepath in find(pattern=filenamepatt, startdir=dirname): try: for (linenum, linestr) in enumerate(open(filepath)): if grepkey in linestr: message = '%s@%d [%s]' % (filepath, linenum + 1, linestr) matches.append(message) except UnicodeDecodeError: print('Unicode error in:', filepath) myqueue.put(matches)
def grepThreadProducer(self, filenamepatt, dirname, grepkey, myqueue): """ in a non-GUI parallel thread: queue find.find results list; could also queue matches as found, but need to keep window; """ from PP4E.Tools.find import find matches = [] for filepath in find(pattern=filenamepatt, startdir=dirname): try: for (linenum, linestr) in enumerate(open(filepath)): if grepkey in linestr: message = '%s@%d [%s]' % (filepath, linenum + 1, linestr) matches.append(message) except UnicodeDecodeError: print('Unicode error in:', filepath) myqueue.put(matches)
def grepThreadProducer(self, filenamepatt, dirname, grepkey, encoding, myqueue): from PP4E.Tools.find import find matches = [] try: for filepath in find(pattern=filenamepatt, startdir=dirname): try: textfile = open(filepath, encoding=encoding) for (linenum, linestr) in enumerate(textfile): if grepkey in linestr: msg = '%s@%d [%s]' % (filepath, linenum + 1, linestr) matches.append(msg) except UnicodeError as X: print('Unicode error in:', filepath, X) # eg: decode, bom except IOError as X: print('IO error in:', filepath, X) # eg: permission finally: myqueue.put(matches) # stop consumer loop on find excs: filenames?