sys.getfilesystemencoding() if not None? see also Chapter6
footnote issue: 3.1 fnmatch always converts bytes per Latin-1;
"""
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?
def grepThreadConsumer(self, grepkey, encoding, myqueue, mypopup):
"""
in the main GUI thread: watch queue for results or [];
there may be multiple active grep threads/loops/queues;
there may be other types of threads/checkers in process,
especially when PyEdit is attached component (PyMailGUI);
"""
import queue
try:
matches = myqueue.get(block=False)
except queue.Empty:
myargs = (grepkey, encoding, myqueue, mypopup)
self.after(250, self.grepThreadConsumer, *myargs)
else:
mypopup.destroy() # close status
self.update() # erase it now
if not matches:
showinfo('PyEdit', 'Grep found no matches for: %r' % grepkey)
else:
self.grepMatchesList(matches, grepkey, encoding)
def grepMatchesList(self, matches, grepkey, encoding):
"""
populate list after successful matches;
we already know Unicode encoding from the search: use
it here when filename clicked, so open doesn't ask user;
"""
from PP4E.Gui.Tour.scrolledlist import ScrolledList
print('Matches for %s: %s' % (grepkey, len(matches)))
catch list double-click
class ScrolledFilenames(ScrolledList):
def runCommand(self, selection):
file, line = selection.split(' [', 1)[0].split('@')
editor = TextEditorMainPopup(
PyEdit: A Text Editor Program/Object | 709