B
    v9a7                 @   s   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dd	 Zd
d ZG dd de	Zdd Zedkrd dlmZ edddd d dlmZ ee dS )    N)	StringVar
BooleanVar)Checkbutton)SearchDialogBase)searchenginec             C   sN   |   }t|}t|ds*t||||_|j}| dd}|| || d S )N_grepdialogz	sel.firstzsel.last)Z_rootr   gethasattr
GrepDialogr   open)textioflistrootengineZdialogsearchphrase r   /usr/lib/python3.7/grep.pygrep   s    

r   c             C   s   t |  d S )N)print)msgr   r   r   
walk_error+   s    r   c             #   sB   x<t j| tdD ]*\ }} fdd|D E d H  |sP qW d S )N)onerrorc             3   s(   | ] }t  |rtj |V  qd S )N)fnmatchospathjoin).0name)dirpathpatternr   r   	<genexpr>9   s   zfindfiles.<locals>.<genexpr>)r   walkr   )folderr    	recursive_	filenamesr   )r   r    r   	findfiles0   s
    r'   c                   s\   e Zd ZdZdZdZ fddZdddZd	d
 Zdd Z	dd Z
dddZdd Z  ZS )r
   zFind in Files DialogZGrepr   c                s,   t  || || _t|| _t|| _d S )N)super__init__r   r   globvarr   recvar)selfr   r   r   )	__class__r   r   r)   G   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 )N z.py*)
r   r   filenamer   r   splitsplitextr*   setr   )	r,   r   r   r   r   dirbaseheadtailr   r   r   r   Z   s    zGrepDialog.openc             C   s"   t |  | d| jd | _d S )Nz	In files:r   )r   create_entriesZ
make_entryr*   Zglobent)r,   r   r   r   r8   p   s    
zGrepDialog.create_entriesc             C   s*   t |  d | jdd}|jddd d S )Nr   zRecurse down subdirectories)variabler   topZboth)Zsidefill)r   Z
make_framer+   pack)r,   Zbtnr   r   r   create_other_buttonsu   s    zGrepDialog.create_other_buttonsc             C   s    t |  | jd| jdd d S )NzSearch FilesT)Zisdef)r   create_command_buttonsZmake_buttondefault_command)r,   r   r   r   r>   |   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 )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   sR  t j|\}}|st j}tt||| j }|   | j	
 }td|d| d d}yx|D ]}yt|ddj}	xbt|	dD ]T\}
}|dd  d	kr|d d }||rtj| d
|
 d
| d	 |d7 }qW W d Q R X W qd tk
r } zt| W d d }~X Y qdX qdW t|r.d| dnd W n tk
rL   Y nX d S )Nz
Searching z in z ...r   replace)errors   
z: zHits found: z'
(Hint: right-click to open locations.)zNo hits.)r   r   r1   curdirsortedr'   r+   r   closer   Zgetpatr   r   	enumeratesearchrA   rB   writeOSErrorAttributeError)r,   rD   r   r#   ZfilepatZfilelistZpathitsfnflinenoliner   r   r   r   rC      s0    


zGrepDialog.grep_it)N)N)__name__
__module____qualname__titleZiconZneedwrapbuttonr)   r   r8   r=   r>   r?   rC   __classcell__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+rG         )Zheightc                  s,    d  td d  d S )Nz1.0)r   )Ztag_addr   Z
tag_remover   )r_   r^   r   r   r   r   show_grep_dialog   s    z&_grep_dialog.<locals>.show_grep_dialogzShow GrepDialog)r   Zcommand)tkinterr\   r]   r^   r_   tkinter.ttkr`   ra   Zidlelib.pyshellrb   rZ   mapintZgeometryr1   r<   )parentr\   r]   r`   ra   rb   r:   xyframerf   Zbuttonr   )r_   r^   r   r   r   _grep_dialog   s    
 ro   __main__)mainzidlelib.idle_test.test_grep   F)	verbosityexit)run)NN)r   r   rA   rg   r   r   rh   r   Zidlelib.searchbaser   Zidlelibr   r   r   r'   r
   ro   rW   Zunittestrq   Zidlelib.idle_test.htestru   r   r   r   r   <module>   s"   
 