3  fV@sdZddlZddlZddlZddlmZmZddlmZddl m Z ddl m Z dddZ Gd d d e Zd d Zed krddlmZeddddddlmZeedS)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|j}tj|}t|ds*t||||_|j}|jdd}|j|||dS)a&Create or find singleton GrepDialog instance. 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/python36/lib64/python3.6/idlelib/grep.pygreps    rc@s`eZdZdZdZdZdZddZddd Zd d Z d d Z ddZ dddZ ddZ ddZdS)r z$Dialog for searching multiple files.zFind in Files DialogZGreprcCs,tj|||||_t||_t||_dS)aCreate 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: globvar: Value of Text Entry widget for path to search. recvar: Boolean value of Checkbutton widget for traversing through subdirectories. N)r__init__rrglobvarrrecvar)selfrrrrrrr.s  zGrepDialog.__init__Nc Cshtj||||r|jpd}nd}tjj|\}}tjj|\}}|sJd}|jjtjj |d|dS)z6Make dialog visible on top of others and ready to use.z.py*N) rr filenameospathsplitsplitextrsetjoin) rr rr rdirbaseheadtailrrrr >s zGrepDialog.opencCs"tj||jd|jd|_dS)z9Create base entry widgets and add widget for search path.z In files:rN)rcreate_entriesZ make_entryrZglobent)rrrrr&Ks zGrepDialog.create_entriescCs*t|jd|jdd}|jddddS)z0Add check button to recurse down subdirectories.rzRecurse down subdirectories)Zvariabler topZboth)ZsideZfillN)rZ make_framerpack)rZbtnrrrcreate_other_buttonsPszGrepDialog.create_other_buttonscCstj||jd|jddS)z6Create base command buttons and add button for search.z Search FilesN)rcreate_command_buttonsZ make_buttondefault_command)rrrrr+Ws z!GrepDialog.create_command_buttonsc Csj|jj}|sdS|jj}|s.|jjdSddlm}tj }z||j t_ |j ||Wd|t_ XdS)a Grep for search pattern in file path. The default command is bound to . If entry values are populated, set OutputWindow as stdout and perform search. The search dialog is closed automatically when the search begins. Nr) OutputWindow) rZgetprogrrr'ZbellZidlelib.outwinr-sysstdoutrgrep_it)rZeventprogrr-Zsaverrrr,\s     zGrepDialog.default_commandc CsNtjj|\}}|j|||jj}|j|j|jj }t d|d|dd}yx|D]}yt |ddj} xbt | dD]T\} } | ddd kr| dd} |j | rtjj|d | d | d |d7}qWWdQRXWq`tk r} zt | WYdd} ~ Xq`Xq`Wt |r*d |d nd Wntk rHYnXdS)a+Search 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). z Searching z in z ...rreplace)errorsr*N z: z Hits found: z' (Hint: right-click to open locations.)zNo hits.r5)rrr findfilesrrsortcloserZgetpatprintr enumeratesearchr.r/writeOSErrorAttributeError) rr1rr"r#listZpathitsfnflinenolinemsgrrrr0ss.    zGrepDialog.grep_itc Csytj|ptj}Wn(tk r<}z t|gSd}~XnXg}g}xH|D]@}tjj||} tjj| rv|j| qLt j ||rL|j| qLW|rx |D]} |j |j | ||qW|S)zReturn list of files in the dir that match the base pattern. If rec is True, recursively iterate through subdirectories. N) rlistdircurdirr=r9rr!isdirappendfnmatchextendr6) rr"r#ZrecnamesrEr?ZsubdirsnamerAZsubdirrrrr6s"     zGrepDialog.findfiles)N)N)__name__ __module__ __qualname____doc__titleZiconZneedwrapbuttonrr r&r)r+r,r0r6rrrrr 's  "r c sddlm}m}mmddlm}ddlm}||}|j dt t |j j ddd\}}|j d|d|d||||d d jfd d }||d |d} | jdS)Nr)ToplevelTextSELEND)Button)PyShellFileListzTest GrepDialog+r*)Zheightcs,jdtdjddS)Nz1.0)r)Ztag_addrZ tag_remover)rVrUrr rrshow_grep_dialogs z&_grep_dialog..show_grep_dialogzShow GrepDialog)r Zcommand)tkinterrSrTrUrV tkinter.ttkrWZidlelib.pyshellrXrRmapintZgeometryrr() parentrSrTrWrXr'xyr\Zbuttonr)rVrUrr r _grep_dialogs     rd__main__)mainzidlelib.idle_test.test_grepF) verbosityexit)run)NN)rQrJrr.r]rrr^rZidlelib.searchbaserZidlelibrrr rdrNZunittestrfZidlelib.idle_test.htestrjrrrrs