a
    If7                     @   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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 )zGrep 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  Open 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.firstzsel.lastN)_rootr   gethasattr
GrepDialogr   open)textioflistrootengineZdialogsearchphrase r   lib/python3.9/idlelib/grep.pygrep   s    

r   c                 C   s   t |  dS )zHandle os.walk error.N)print)msgr   r   r   
walk_error+   s    r   c                 #   s@   t j| tdD ],\ }} fdd|D E dH  |s q<qdS )zGenerate file names in dir that match pattern.

    Args:
        folder: Root directory to search.
        pattern: File pattern to match.
        recursive: True to include subdirectories.
    )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>N)r   walkr   )folderr"   	recursive_	filenamesr   r    r   	findfiles0   s    r)   c                       s`   e Zd 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$Dialog for searching multiple files.zFind in Files DialogZGrepr   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:
            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__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 )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)
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 )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;   p   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)Zvariabler   topZboth)ZsideZfillN)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 )z<Create base command buttons and add button for Search Files.zSearch FilesT)ZisdefN)r   create_command_buttonsZmake_buttondefault_commandr<   r   r   r   r@   |   s    
z!GrepDialog.create_command_buttonsc                 C   sp   | j  }|sdS | j }|s.| j  dS ddlm} tj	}z"|| j
t_	| || W |t_	n|t_	0 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.outwinrB   sysstdoutr   grep_it)r.   Zeventprogr   rB   Zsaver   r   r   rA      s    


zGrepDialog.default_commandc                 C   s^  t j|\}}|st j}tt||| j }|   | j	
 }td|d| d d}z|D ]}zt|ddp}	t|	dD ]T\}
}|dd	 d
kr|d	d }||rtj| d|
 d| d
 |d7 }qW d	   n1 s0    Y  W qb ty$ } zt| W Y d	}~qbd	}~0 0 qbt|r<d| dnd W n tyX   Y n0 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).

        Args:
            prog: The compiled, cooked search pattern.
            path: String containing the search path.
        z
Searching z in z ...r   replace)errors   N
z: zHits found: z'
(Hint: right-click to open locations.)zNo hits.)r   r   r4   curdirsortedr)   r-   r	   closer   Zgetpatr   r   	enumeratesearchrC   rD   writeOSErrorAttributeError)r.   rF   r   r%   ZfilepatZfilelistZpathitsfnflinenoliner   r   r   r   rE      s6    

, zGrepDialog.grep_it)N)N)__name__
__module____qualname____doc__titleZiconZneedwrapbuttonr+   r   r;   r?   r@   rA   rE   __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+rI         )Zheightc                      s,    d  td d  d S )Nz1.0)r   )Ztag_addr   Z
tag_remover   rb   ra   r   r   r   r   show_grep_dialog   s    z&_grep_dialog.<locals>.show_grep_dialogzShow GrepDialog)r   Zcommand)tkinterr_   r`   ra   rb   tkinter.ttkrc   rd   Zidlelib.pyshellre   r]   mapintZgeometryr4   r>   )parentr_   r`   rc   rd   re   r=   xyframerj   Zbuttonr   ri   r   _grep_dialog   s    
 rs   __main__)mainzidlelib.idle_test.test_grep   F)	verbosityexit)run)NN)r\   r   r   rC   rk   r   r   rl   r   Zidlelib.searchbaser   Zidlelibr   r   r   r)   r   rs   rY   Zunittestru   Zidlelib.idle_test.htestry   r   r   r   r   <module>   s$   
 