B uW@sdZddlZddlZddlZddlmZmZddlmZddl m Z ddl m Z dddZ d d Zd d ZGd dde ZddZedkrddlmZeddddddlmZeedS)zGrep dialog for Find in Files functionality. Inherits from SearchDialogBase for GUI and uses searchengine to prepare search pattern. N) StringVar BooleanVar) Checkbutton)SearchDialogBase) searchenginecCsN|}t|}t|ds*t||||_|j}|dd}||||dS)aOpen the Find in Files dialog. Module-level function to access the singleton GrepDialog instance and open the dialog. If text is selected, it is used as the search phrase; otherwise, the previous entry is used. Args: text: Text widget that contains the selected text for default search phrase. io: iomenu.IOBinding instance with default path to search. flist: filelist.FileList instance for OutputWindow parent. _grepdialogz sel.firstzsel.lastN)Z_rootrgethasattr GrepDialogropen)textioflistrootengineZdialog searchphraser1/opt/alt/python37/lib64/python3.7/idlelib/grep.pygreps   rcCs t|dS)zHandle os.walk error.N)print)msgrrr walk_error+src#sBx9szfindfiles..N)rwalkr)folderr recursive_ filenamesr)rr r findfiles0s  r'cs`eZdZdZdZdZdZfddZddd Zd d Z d d Z ddZ dddZ ddZ ZS)r z$Dialog for searching multiple files.zFind in Files DialogZGreprcs,t||||_t||_t||_dS)a-Create search dialog for searching for a phrase in the file system. Uses SearchDialogBase as the basis for the GUI and a searchengine instance to prepare the search. Attributes: flist: filelist.Filelist instance for OutputWindow parent. globvar: String value of Entry widget for path to search. globent: Entry widget for globvar. Created in create_entries(). recvar: Boolean value of Checkbutton widget for traversing through subdirectories. N)super__init__rrglobvarrrecvar)selfrrr) __class__rrr)Gs zGrepDialog.__init__Nc Csht||||r|jpd}nd}tj|\}}tj|\}}|sJd}|jtj |d|dS)a_Make dialog visible on top of others and ready to use. Extend the SearchDialogBase open() to set the initial value for globvar. Args: text: Multicall object containing the text information. searchphrase: String phrase to search. io: iomenu.IOBinding instance containing file path. z.py*N) rr filenamerrsplitsplitextr*setr) r,r rr rdirbaseheadtailrrrr Zs  zGrepDialog.opencCs"t||d|jd|_dS)z9Create base entry widgets and add widget for search path.z In files:rN)rcreate_entriesZ make_entryr*Zglobent)r,rrrr8ps zGrepDialog.create_entriescCs*t|d|jdd}|jddddS)z0Add check button to recurse down subdirectories.rzRecurse down subdirectories)Zvariabler topZboth)ZsideZfillN)rZ make_framer+pack)r,Zbtnrrrcreate_other_buttonsuszGrepDialog.create_other_buttonscCs t||jd|jdddS)z. If entry values are populated, set OutputWindow as stdout and perform search. The search dialog is closed automatically when the search begins. Nr) OutputWindow) rZgetprogr*rr9ZbellZidlelib.outwinr>sysstdoutrgrep_it)r,Zeventprogrr>Zsaverrrr=s     zGrepDialog.default_commandc CsRtj|\}}|stj}tt|||j}||j }t d|d|dd}yx|D]}yt |ddj} xbt | dD]T\} } | dd d kr| d d} || rtj|d | d | d |d7}qWWd QRXWqdtk r} z t | Wd d } ~ XYqdXqdWt |r.d |d ndWntk rLYnXd S)aSearch for prog within the lines of the files in path. For the each file in the path directory, open the file and search each line for the matching pattern. If the pattern is found, write the file and line information to stdout (which is an OutputWindow). Args: prog: The compiled, cooked search pattern. path: String containing the search path. z Searching z in z ...rreplace)errorsN z: z Hits found: z' (Hint: right-click to open locations.)zNo hits.)rrr1curdirsortedr'r+rcloserZgetpatrr enumeratesearchr?r@writeOSErrorAttributeError) r,rBrr#ZfilepatZfilelistZpathitsfnflinenolinerrrrrAs0     zGrepDialog.grep_it)N)N)__name__ __module__ __qualname____doc__titleZiconZneedwrapbuttonr)r r8r;r<r=rA __classcell__rr)r-rr @s   r c sddlm}m}mmddlm}m}ddlm }||}| dt t | ddd\}}| d|d|d||||} | || d d fd d } || d | d} | dS)Nr)ToplevelTextSELEND)FrameButton)PyShellFileListzTest GrepDialog+rE)Zheightcs,dtdddS)Nz1.0)r)Ztag_addrZ tag_remover)r^r]rr rrshow_grep_dialogs z&_grep_dialog..show_grep_dialogzShow GrepDialog)r Zcommand)tkinterr[r\r]r^ tkinter.ttkr_r`Zidlelib.pyshellrarYmapintZgeometryr1r:) parentr[r\r_r`rar9xyframereZbuttonr)r^r]rr r _grep_dialogs    rn__main__)mainzidlelib.idle_test.test_grepF) verbosityexit)run)NN)rXrrr?rfrrrgrZidlelib.searchbaserZidlelibrrrr'r rnrUZunittestrpZidlelib.idle_test.htestrtrrrrs$