PyXR

c:\python24\lib \ idlelib \ GrepDialog.py



0001 import os
0002 import fnmatch
0003 import sys
0004 from Tkinter import *
0005 import SearchEngine
0006 from SearchDialogBase import SearchDialogBase
0007 
0008 def grep(text, io=None, flist=None):
0009     root = text._root()
0010     engine = SearchEngine.get(root)
0011     if not hasattr(engine, "_grepdialog"):
0012         engine._grepdialog = GrepDialog(root, engine, flist)
0013     dialog = engine._grepdialog
0014     searchphrase = text.get("sel.first", "sel.last")
0015     dialog.open(text, searchphrase, io)
0016 
0017 class GrepDialog(SearchDialogBase):
0018 
0019     title = "Find in Files Dialog"
0020     icon = "Grep"
0021     needwrapbutton = 0
0022 
0023     def __init__(self, root, engine, flist):
0024         SearchDialogBase.__init__(self, root, engine)
0025         self.flist = flist
0026         self.globvar = StringVar(root)
0027         self.recvar = BooleanVar(root)
0028 
0029     def open(self, text, searchphrase, io=None):
0030         SearchDialogBase.open(self, text, searchphrase)
0031         if io:
0032             path = io.filename or ""
0033         else:
0034             path = ""
0035         dir, base = os.path.split(path)
0036         head, tail = os.path.splitext(base)
0037         if not tail:
0038             tail = ".py"
0039         self.globvar.set(os.path.join(dir, "*" + tail))
0040 
0041     def create_entries(self):
0042         SearchDialogBase.create_entries(self)
0043         self.globent = self.make_entry("In files:", self.globvar)
0044 
0045     def create_other_buttons(self):
0046         f = self.make_frame()
0047 
0048         btn = Checkbutton(f, anchor="w",
0049                 variable=self.recvar,
0050                 text="Recurse down subdirectories")
0051         btn.pack(side="top", fill="both")
0052         btn.select()
0053 
0054     def create_command_buttons(self):
0055         SearchDialogBase.create_command_buttons(self)
0056         self.make_button("Search Files", self.default_command, 1)
0057 
0058     def default_command(self, event=None):
0059         prog = self.engine.getprog()
0060         if not prog:
0061             return
0062         path = self.globvar.get()
0063         if not path:
0064             self.top.bell()
0065             return
0066         from OutputWindow import OutputWindow
0067         save = sys.stdout
0068         try:
0069             sys.stdout = OutputWindow(self.flist)
0070             self.grep_it(prog, path)
0071         finally:
0072             sys.stdout = save
0073 
0074     def grep_it(self, prog, path):
0075         dir, base = os.path.split(path)
0076         list = self.findfiles(dir, base, self.recvar.get())
0077         list.sort()
0078         self.close()
0079         pat = self.engine.getpat()
0080         print "Searching %r in %s ..." % (pat, path)
0081         hits = 0
0082         for fn in list:
0083             try:
0084                 f = open(fn)
0085             except IOError, msg:
0086                 print msg
0087                 continue
0088             lineno = 0
0089             while 1:
0090                 block = f.readlines(100000)
0091                 if not block:
0092                     break
0093                 for line in block:
0094                     lineno = lineno + 1
0095                     if line[-1:] == '\n':
0096                         line = line[:-1]
0097                     if prog.search(line):
0098                         sys.stdout.write("%s: %s: %s\n" % (fn, lineno, line))
0099                         hits = hits + 1
0100         if hits:
0101             if hits == 1:
0102                 s = ""
0103             else:
0104                 s = "s"
0105             print "Found", hits, "hit%s." % s
0106             print "(Hint: right-click to open locations.)"
0107         else:
0108             print "No hits."
0109 
0110     def findfiles(self, dir, base, rec):
0111         try:
0112             names = os.listdir(dir or os.curdir)
0113         except os.error, msg:
0114             print msg
0115             return []
0116         list = []
0117         subdirs = []
0118         for name in names:
0119             fn = os.path.join(dir, name)
0120             if os.path.isdir(fn):
0121                 subdirs.append(fn)
0122             else:
0123                 if fnmatch.fnmatch(name, base):
0124                     list.append(fn)
0125         if rec:
0126             for subdir in subdirs:
0127                 list.extend(self.findfiles(subdir, base, rec))
0128         return list
0129 
0130     def close(self, event=None):
0131         if self.top:
0132             self.top.grab_release()
0133             self.top.withdraw()
0134 

Generated by PyXR 0.9.4
SourceForge.net Logo