B
    2*™\r  ã               @   s¬   d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddd„ZG d	d
„ d
e
ƒZdd„ Zedkr¨ddlmZ edddd ddlmZ eeƒ dS )zŒGrep dialog for Find in Files functionality.

   Inherits from SearchDialogBase for GUI and uses searchengine
   to prepare search pattern.
é    N)Ú	StringVarÚ
BooleanVar)ÚCheckbutton)ÚSearchDialogBase)Úsearchenginec             C   sN   |   ¡ }t |¡}t|dƒs*t|||ƒ|_|j}|  dd¡}| | ||¡ dS )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.firstzsel.lastN)Z_rootr   ÚgetÚhasattrÚ
GrepDialogr   Úopen)ÚtextÚioÚflistÚrootÚengineZdialogÚsearchphrase© r   ú7C:\ALexclude\prg\programme\Python37\Lib\idlelib\grep.pyÚgrep   s    


r   c               @   s`   e Zd ZdZdZdZdZdd„ Zddd	„Zd
d„ Z	dd„ Z
dd„ Zddd„Zdd„ Zdd„ ZdS )r
   z$Dialog for searching multiple files.zFind in Files DialogZGrepr   c             C   s,   t  | ||¡ || _t|ƒ| _t|ƒ| _dS )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:
            globvar: Value of Text Entry widget for path to search.
            recvar: Boolean value of Checkbutton widget
                    for traversing through subdirectories.
        N)r   Ú__init__r   r   Úglobvarr   Úrecvar)Úselfr   r   r   r   r   r   r   .   s    
zGrepDialog.__init__Nc       	      C   sh   t  | ||¡ |r|jpd}nd}tj |¡\}}tj |¡\}}|sJd}| j tj 	|d| ¡¡ dS )z6Make dialog visible on top of others and ready to use.Ú z.pyÚ*N)
r   r   ÚfilenameÚosÚpathÚsplitÚsplitextr   ÚsetÚjoin)	r   r   r   r   r   ÚdirÚbaseÚheadÚtailr   r   r   r   >   s    zGrepDialog.openc             C   s"   t  | ¡ |  d| j¡d | _dS )z9Create base entry widgets and add widget for search path.z	In files:r   N)r   Úcreate_entriesZ
make_entryr   Zglobent)r   r   r   r   r&   K   s    
zGrepDialog.create_entriesc             C   s*   t |  ¡ d | jdd}|jddd dS )z0Add check button to recurse down subdirectories.r   zRecurse down subdirectories)Úvariabler   ÚtopZboth)ZsideÚfillN)r   Z
make_framer   Úpack)r   Zbtnr   r   r   Úcreate_other_buttonsP   s    zGrepDialog.create_other_buttonsc             C   s   t  | ¡ |  d| jd¡ dS )z6Create base command buttons and add button for search.zSearch Filesé   N)r   Úcreate_command_buttonsZmake_buttonÚdefault_command)r   r   r   r   r-   W   s    
z!GrepDialog.create_command_buttonsc             C   sj   | j  ¡ }|sdS | j ¡ }|s.| j ¡  dS ddlm} tj	}z|| j
ƒt_	|  ||¡ W d|t_	X dS )a
  Grep for search pattern in file path. The default command is bound
        to <Return>.

        If entry values are populated, set OutputWindow as stdout
        and perform search.  The search dialog is closed automatically
        when the search begins.
        Nr   )ÚOutputWindow)r   Zgetprogr   r   r(   ZbellZidlelib.outwinr/   ÚsysÚstdoutr   Úgrep_it)r   ZeventÚprogr   r/   Zsaver   r   r   r.   \   s    


zGrepDialog.default_commandc             C   sN  t j |¡\}}|  ||| j ¡ ¡}| ¡  |  ¡  | j 	¡ }t
d|›d|› dƒ d}yÚxº|D ]²}y€t|ddj}	xbt|	dƒD ]T\}
}|dd	… d
kr¤|d	d… }| |¡r€tj |› d|
› d|› d
¡ |d7 }q€W W d	Q R X W q` tk
r } zt
|ƒ W d	d	}~X Y q`X q`W t
|r*d|› dndƒ W n tk
rH   Y nX d	S )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 ...r   Úreplace)Úerrorsr,   éÿÿÿÿNÚ
z: zHits found: z'
(Hint: right-click to open locations.)zNo hits.)r   r   r   Ú	findfilesr   r   ÚsortÚcloser   ZgetpatÚprintr   Ú	enumerateÚsearchr0   r1   ÚwriteÚOSErrorÚAttributeError)r   r3   r   r"   r#   ÚlistZpatÚhitsÚfnÚfÚlinenoÚlineÚmsgr   r   r   r2   s   s.    


zGrepDialog.grep_itc          
   C   s¼   yt  |pt j¡}W n* tk
r> } zt|ƒ g S d}~X Y nX g }g }xH|D ]@}t j ||¡}	t j |	¡rx| |	¡ qNt	 	||¡rN| |	¡ qNW |r¸x |D ]}
| 
|  |
||¡¡ qœW |S )zŠReturn list of files in the dir that match the base pattern.

        If rec is True, recursively iterate through subdirectories.
        N)r   ÚlistdirÚcurdirr?   r;   r   r!   ÚisdirÚappendÚfnmatchÚextendr8   )r   r"   r#   ZrecÚnamesrG   rA   ZsubdirsÚnamerC   Zsubdirr   r   r   r8   •   s"    

zGrepDialog.findfiles)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtitleZiconZneedwrapbuttonr   r   r&   r+   r-   r.   r2   r8   r   r   r   r   r
   '   s   

"r
   c                sØ   ddl m}m}m‰m‰  ddlm}m} ddlm	} || ƒ}| 
d¡ tt|  ¡  d¡dd … ƒ\}}| d|› d|d › ¡ ||ƒ‰||ƒ}	|	 ¡  ||	d	d
‰ˆ ¡  ‡ ‡‡‡fdd„}
||	d|
d}| ¡  d S )Nr   )ÚToplevelÚTextÚSELÚEND)ÚFrameÚButton)ÚPyShellFileListzTest GrepDialogú+r,   é¯   é   )Zheightc                  s,   ˆ  ˆdˆ ¡ tˆˆd ˆ ˆdˆ ¡ d S )Nz1.0)r   )Ztag_addr   Z
tag_remover   )rX   rW   r   r   r   r   Úshow_grep_dialog¾   s    z&_grep_dialog.<locals>.show_grep_dialogzShow GrepDialog)r   Zcommand)ÚtkinterrU   rV   rW   rX   Útkinter.ttkrY   rZ   Zidlelib.pyshellr[   rT   ÚmapÚintZgeometryr   r*   )ÚparentrU   rV   rY   rZ   r[   r(   ÚxÚyÚframer_   Zbuttonr   )rX   rW   r   r   r   Ú_grep_dialog®   s    
 rh   Ú__main__)Úmainzidlelib.idle_test.test_grepé   F)Ú	verbosityÚexit)Úrun)NN)rS   rL   r   r0   r`   r   r   ra   r   Zidlelib.searchbaser   Zidlelibr   r   r
   rh   rP   Zunittestrj   Zidlelib.idle_test.htestrn   r   r   r   r   Ú<module>   s    
 