grep.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """Grep dialog for Find in Files functionality.
  2. Inherits from SearchDialogBase for GUI and uses searchengine
  3. to prepare search pattern.
  4. """
  5. import fnmatch
  6. import os
  7. import sys
  8. from tkinter import StringVar, BooleanVar
  9. from tkinter.ttk import Checkbutton # Frame imported in ...Base
  10. from idlelib.searchbase import SearchDialogBase
  11. from idlelib import searchengine
  12. # Importing OutputWindow here fails due to import loop
  13. # EditorWindow -> GrepDialog -> OutputWindow -> EditorWindow
  14. def grep(text, io=None, flist=None):
  15. """Open the Find in Files dialog.
  16. Module-level function to access the singleton GrepDialog
  17. instance and open the dialog. If text is selected, it is
  18. used as the search phrase; otherwise, the previous entry
  19. is used.
  20. Args:
  21. text: Text widget that contains the selected text for
  22. default search phrase.
  23. io: iomenu.IOBinding instance with default path to search.
  24. flist: filelist.FileList instance for OutputWindow parent.
  25. """
  26. root = text._root()
  27. engine = searchengine.get(root)
  28. if not hasattr(engine, "_grepdialog"):
  29. engine._grepdialog = GrepDialog(root, engine, flist)
  30. dialog = engine._grepdialog
  31. searchphrase = text.get("sel.first", "sel.last")
  32. dialog.open(text, searchphrase, io)
  33. def walk_error(msg):
  34. "Handle os.walk error."
  35. print(msg)
  36. def findfiles(folder, pattern, recursive):
  37. """Generate file names in dir that match pattern.
  38. Args:
  39. folder: Root directory to search.
  40. pattern: File pattern to match.
  41. recursive: True to include subdirectories.
  42. """
  43. for dirpath, _, filenames in os.walk(folder, onerror=walk_error):
  44. yield from (os.path.join(dirpath, name)
  45. for name in filenames
  46. if fnmatch.fnmatch(name, pattern))
  47. if not recursive:
  48. break
  49. class GrepDialog(SearchDialogBase):
  50. "Dialog for searching multiple files."
  51. title = "Find in Files Dialog"
  52. icon = "Grep"
  53. needwrapbutton = 0
  54. def __init__(self, root, engine, flist):
  55. """Create search dialog for searching for a phrase in the file system.
  56. Uses SearchDialogBase as the basis for the GUI and a
  57. searchengine instance to prepare the search.
  58. Attributes:
  59. flist: filelist.Filelist instance for OutputWindow parent.
  60. globvar: String value of Entry widget for path to search.
  61. globent: Entry widget for globvar. Created in
  62. create_entries().
  63. recvar: Boolean value of Checkbutton widget for
  64. traversing through subdirectories.
  65. """
  66. super().__init__(root, engine)
  67. self.flist = flist
  68. self.globvar = StringVar(root)
  69. self.recvar = BooleanVar(root)
  70. def open(self, text, searchphrase, io=None):
  71. """Make dialog visible on top of others and ready to use.
  72. Extend the SearchDialogBase open() to set the initial value
  73. for globvar.
  74. Args:
  75. text: Multicall object containing the text information.
  76. searchphrase: String phrase to search.
  77. io: iomenu.IOBinding instance containing file path.
  78. """
  79. SearchDialogBase.open(self, text, searchphrase)
  80. if io:
  81. path = io.filename or ""
  82. else:
  83. path = ""
  84. dir, base = os.path.split(path)
  85. head, tail = os.path.splitext(base)
  86. if not tail:
  87. tail = ".py"
  88. self.globvar.set(os.path.join(dir, "*" + tail))
  89. def create_entries(self):
  90. "Create base entry widgets and add widget for search path."
  91. SearchDialogBase.create_entries(self)
  92. self.globent = self.make_entry("In files:", self.globvar)[0]
  93. def create_other_buttons(self):
  94. "Add check button to recurse down subdirectories."
  95. btn = Checkbutton(
  96. self.make_frame()[0], variable=self.recvar,
  97. text="Recurse down subdirectories")
  98. btn.pack(side="top", fill="both")
  99. def create_command_buttons(self):
  100. "Create base command buttons and add button for Search Files."
  101. SearchDialogBase.create_command_buttons(self)
  102. self.make_button("Search Files", self.default_command, isdef=True)
  103. def default_command(self, event=None):
  104. """Grep for search pattern in file path. The default command is bound
  105. to <Return>.
  106. If entry values are populated, set OutputWindow as stdout
  107. and perform search. The search dialog is closed automatically
  108. when the search begins.
  109. """
  110. prog = self.engine.getprog()
  111. if not prog:
  112. return
  113. path = self.globvar.get()
  114. if not path:
  115. self.top.bell()
  116. return
  117. from idlelib.outwin import OutputWindow # leave here!
  118. save = sys.stdout
  119. try:
  120. sys.stdout = OutputWindow(self.flist)
  121. self.grep_it(prog, path)
  122. finally:
  123. sys.stdout = save
  124. def grep_it(self, prog, path):
  125. """Search for prog within the lines of the files in path.
  126. For the each file in the path directory, open the file and
  127. search each line for the matching pattern. If the pattern is
  128. found, write the file and line information to stdout (which
  129. is an OutputWindow).
  130. Args:
  131. prog: The compiled, cooked search pattern.
  132. path: String containing the search path.
  133. """
  134. folder, filepat = os.path.split(path)
  135. if not folder:
  136. folder = os.curdir
  137. filelist = sorted(findfiles(folder, filepat, self.recvar.get()))
  138. self.close()
  139. pat = self.engine.getpat()
  140. print(f"Searching {pat!r} in {path} ...")
  141. hits = 0
  142. try:
  143. for fn in filelist:
  144. try:
  145. with open(fn, errors='replace') as f:
  146. for lineno, line in enumerate(f, 1):
  147. if line[-1:] == '\n':
  148. line = line[:-1]
  149. if prog.search(line):
  150. sys.stdout.write(f"{fn}: {lineno}: {line}\n")
  151. hits += 1
  152. except OSError as msg:
  153. print(msg)
  154. print(f"Hits found: {hits}\n(Hint: right-click to open locations.)"
  155. if hits else "No hits.")
  156. except AttributeError:
  157. # Tk window has been closed, OutputWindow.text = None,
  158. # so in OW.write, OW.text.insert fails.
  159. pass
  160. def _grep_dialog(parent): # htest #
  161. from tkinter import Toplevel, Text, SEL, END
  162. from tkinter.ttk import Frame, Button
  163. from idlelib.pyshell import PyShellFileList
  164. top = Toplevel(parent)
  165. top.title("Test GrepDialog")
  166. x, y = map(int, parent.geometry().split('+')[1:])
  167. top.geometry(f"+{x}+{y + 175}")
  168. flist = PyShellFileList(top)
  169. frame = Frame(top)
  170. frame.pack()
  171. text = Text(frame, height=5)
  172. text.pack()
  173. def show_grep_dialog():
  174. text.tag_add(SEL, "1.0", END)
  175. grep(text, flist=flist)
  176. text.tag_remove(SEL, "1.0", END)
  177. button = Button(frame, text="Show GrepDialog", command=show_grep_dialog)
  178. button.pack()
  179. if __name__ == "__main__":
  180. from unittest import main
  181. main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
  182. from idlelib.idle_test.htest import run
  183. run(_grep_dialog)