o
    û›ha1  ã                   @   sV  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
m
Z
 ddlm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lmZ e	ƒ Zd
ZdZe dd dd„ eD ƒ¡ ¡Ze d¡Zdedee fdd„Z dd„ Z!dede"fdd„Z#dd„ Z$d/d edee fd!d"„Z%d#d$„ Z&d%d&„ Z'd'd(„ Z(d)d*„ Z)d+d,„ Z*d-d.„ Z+dS )0z\Implementations for various useful completers.

These are all loaded by default by IPython.
é    N)Úimport_module)Úall_suffixes)Útime)Úzipimporteré   )Úexpand_userÚcompress_user)ÚTryNexté   )Ú	arg_split)Úget_ipython)ÚListé   z?(?P<name>[^\W\d]\w*?)(?P<package>[/\\]__init__)?(?P<suffix>%s)$ú|c                 c   ó   � | ]}t  |¡V  qd S ©N)ÚreÚescape©Ú.0Ús© r   úOc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/completerlib.pyÚ	<genexpr>:   ó   € r   z.*(\.ipy|\.ipynb|\.py[w]?)$ÚpathÚreturnc                    sð   | dkrd} t jj‰ t j | ¡rEg }t j| dd�D ]*\}}}|t| ƒd d… ‰ˆr>| ‡ ‡fdd„|D ƒ¡ g |dd…< q| |¡ qnztt| ƒj	 
¡ ƒ}W n ty[   g }Y nw g }|D ]}t |¡}|rq| | d	¡¡ q`tt|ƒƒS )
z`
    Return the list containing the names of the modules available in the given
    folder.
    Ú Ú.T)Úfollowlinksr   Nc                 3   s   � | ]}ˆ ˆ|ƒV  qd S r   r   ©r   Úf©ÚpjoinÚsubdirr   r   r   X   r   zmodule_list.<locals>.<genexpr>Úname)Úosr   ÚjoinÚisdirÚwalkÚlenÚextendÚlistr   Ú_filesÚkeysÚ	ExceptionÚ	import_reÚmatchÚappendÚgroupÚset)r   ÚfilesÚrootÚdirsÚnondirsÚmodulesr!   Úmr   r"   r   Úmodule_listD   s0   ú	ÿ
€r;   c                  C   s.  t ƒ } | du rttjƒS t| jddƒri }n| j di ¡}ttjƒ}tƒ }d}tjD ]]}z|| }W nO t	y�   t
|ƒ}z| d¡ W n	 tyM   Y nw |dvrV|||< tƒ | tkrn|snd}tdƒ td	ƒ tj ¡  tƒ | tkrtd
ƒ g  Y   S Y nw | |¡ q*|r�|| jd< tt|ƒƒ}|S )zº
    Returns a list containing the names of all the modules available in the
    folders of the pythonpath.

    ip.db['rootmodules_cache'] maps sys.path entries to list of modules.
    NZ_mockFÚrootmodules_cacheÚ__init__)r   r   Tz/
Caching the list of root modules, please wait!z@(This will only be done once - type '%rehashx' to reset cache!)
z%This is taking too long, we give up.
)r   r,   ÚsysÚbuiltin_module_namesÚgetattrÚdbÚgetr   r   ÚKeyErrorr;   ÚremoveÚ
ValueErrorÚTIMEOUT_STORAGEÚprintÚstdoutÚflushÚTIMEOUT_GIVEUPr+   r4   )Úipr<   ZrootmodulesÚ
start_timeÚstorer   r9   r   r   r   Úget_root_modulesl   sH   


ÿ
þò
rN   Úattrc                 C   sT   |rzt | |ƒ}W n
 ty   Y dS w t |¡S |d d… dko(|dd … dk S )NFr
   Ú__éþÿÿÿ)r@   ÚModuleNotFoundErrorÚinspectÚismodule)ÚmodulerO   Úonly_modulesÚmodr   r   r   Úis_importable›   s   þ
"rX   c                 C   s@   zt | |ƒ}W n ty   Y dS  ty   Y dS w t |¡S )NTF)r@   ÚAttributeErrorÚ	TypeErrorrS   rT   )rU   rO   Úobjr   r   r   Úis_possible_submodule¦   s   þ
r\   FrW   c                    sî   |   d¡} zt| ƒ‰ W n   g  Y S dtˆ ddƒpdv }g }tˆ dƒr(ˆr(|r7| ‡ ‡fdd„tˆ ƒD ƒ¡ tˆ dg ƒ}ˆrL| ‡ fdd	„|D ƒ¡ n| |¡ |rgˆ j}tj 	|¡}|d
urg| t
|ƒ¡ dd„ |D ƒ}| d¡ t|ƒS )zN
    Try to import given module and return list of potential completions.
    r   r=   Ú__file__r   c                    s   g | ]
}t ˆ |ˆƒr|‘qS r   )rX   ©r   rO   ©r:   rV   r   r   Ú
<listcomp>À   s    

ÿztry_import.<locals>.<listcomp>Ú__all__c                 3   s   � | ]
}t ˆ |ƒr|V  qd S r   )r\   r^   )r:   r   r   r   Å   s   € ztry_import.<locals>.<genexpr>Nc                 S   s   h | ]	}t |tƒr|’qS r   )Ú
isinstanceÚstr©r   Úcr   r   r   Ú	<setcomp>Î   s    ztry_import.<locals>.<setcomp>)Úrstripr   r@   Úhasattrr+   Údirr]   r&   r   Údirnamer;   Údiscardr,   )rW   rV   Z	m_is_initÚcompletionsZm_allÚfile_Ú	file_pathZcompletions_setr   r_   r   Ú
try_import²   s*   


ro   c                    s4   t ˆ tƒr	ˆ  ¡ ‰ ‡ fdd„}tƒ jd|| d� dS )a�   Easily create a trivial completer for a command.

    Takes either a list of completions, or all completions in string (that will
    be split on whitespace).

    Example::

        [d:\ipython]|1> import ipy_completers
        [d:\ipython]|2> ipy_completers.quick_completer('foo', ['bar','baz'])
        [d:\ipython]|3> foo b<TAB>
        bar baz
        [d:\ipython]|3> foo ba
    c                    s   ˆ S r   r   ©ÚselfÚevent©rl   r   r   Údo_completeé   s   z$quick_completer.<locals>.do_completeZcomplete_command)Zstr_keyN)rb   rc   Úsplitr   Zset_hook)Úcmdrl   rt   r   rs   r   Úquick_completer×   s   
rw   c                    sÄ   |   d¡}t|ƒ}|dkr|d dkrdgS |dk rL|d dv rL|dkr'tƒ S |d   d¡‰ tˆ ƒd	k r7tƒ S td ˆ d
d… ¡dƒ}‡ fdd„|D ƒS |dkr^|d dkr`|d ‰ tˆ ƒS d
S d
S )z¡
    Returns a list containing the completion possibilities for an import line.

    The line looks like this :
    'import xml.d'
    'from xml.dom import'
    ú é   r   Úfromzimport >   z%aimportrz   Úimportr   r   r
   NéÿÿÿÿTc                    s$   g | ]}d   ˆ dd… |g ¡‘qS )r   Nr|   )r'   )r   Úel©rW   r   r   r`     ó   $ z%module_completion.<locals>.<listcomp>)ru   r*   rN   ro   r'   )ÚlineÚwordsZnwordsZcompletion_listr   r~   r   Úmodule_completionî   s    
	þr‚   c                 C   s
   t |jƒS )z@Give completions after user has typed 'import ...' or 'from ...')r‚   r€   rp   r   r   r   Úmodule_completer  s   
	rƒ   c                    sô   t |jdd�}t|ƒdkr|j d¡s|d  d¡}nd}tj}tjj‰ t	|ƒ\}‰‰t
dd	„ |D ƒƒrB‡ fd
d„||d ƒD ƒ}n.‡ fdd„||d ƒD ƒ}dd„ ||d ƒ||d ƒ ||d ƒ ||d ƒ D ƒ}|| }‡‡fdd„|D ƒS )zKComplete files that end in .py or .ipy or .ipynb for the %run command.
    F)Ústrictr   rx   r|   z'"r   c                 s   r   r   )Úmagic_run_rer1   rd   r   r   r   r   7  r   z&magic_run_completer.<locals>.<genexpr>c                    s(   g | ]}|  d d¡ˆ |ƒrdnd ‘qS )ú\ú/r   ©Úreplacer    ©r(   r   r   r`   8  s     ÿz'magic_run_completer.<locals>.<listcomp>Ú*c                    s$   g | ]}ˆ |ƒr|  d d¡d ‘qS ©r†   r‡   rˆ   r    rŠ   r   r   r`   ;  r   c                 S   s   g | ]}|  d d¡‘qS rŒ   rˆ   r    r   r   r   r`   <  s    ÿz*.pyz*.ipyz*.ipynbz*.pywc                    ó   g | ]}t |ˆ ˆƒ‘qS r   ©r   ©r   Úp©Útilde_expandÚ	tilde_valr   r   r`   C  ó    )r   r€   r*   ÚendswithÚstripÚglobr&   r   r(   r   Úany)rq   rr   ÚcompsÚrelpathZlglobÚmatchesr7   Zpysr   )r(   r’   r“   r   Úmagic_run_completer!  s*   

ÿ
ÿ
ÿÿrœ   c                    st  t ƒ }ˆ j}ˆ j d¡sdˆ jv r | j dd¡}|r| ¡ S g S ˆ jdkrRtttt|j	d ƒd ƒƒƒ}d| d	 ‰‡fd
d„t
|j	d ƒD ƒ}t|ƒdkrP|S g S ˆ j d¡rbdd„ |j	d D ƒS t|ƒ\}‰‰| dd¡}g }dd„ t |d ¡D ƒD ]}d|v r…t‚| |¡ q}|s°tj |¡ršt|ˆˆƒgS | j di ¡}	‡ fdd„|	D ƒ}
|
r®|
S t‚‡‡fdd„|D ƒS )z:Completer function for cd, which only returns directories.z-bz -b Z	bookmarksNú-Z_dhr   z-%0zd [%s]c                    s   g | ]
\}}ˆ ||f ‘qS r   r   )r   Úir   )Úfmtr   r   r`   X  ó    z cd_completer.<locals>.<listcomp>ú--c                 S   s   g | ]
}d t j |¡ ‘qS )r¡   )r&   r   Úbasename)r   Údr   r   r   r`   ^  r    r†   r‡   c                 S   s(   g | ]}t j |¡r| d d¡d ‘qS rŒ   )r&   r   r(   r‰   r    r   r   r   r`   e  s    
ÿr‹   rx   c                    s   g | ]
}|  ˆ j¡r|‘qS r   )Ú
startswithÚsymbolr   )rr   r   r   r`   t  r    c                    r�   r   rŽ   r�   r‘   r   r   r`   z  r”   )r   r¥   r€   r•   rA   rB   r.   rc   r*   Zuser_nsÚ	enumerater¤   r   r‰   r—   r	   r2   r&   r   r(   r   )rq   rr   rK   rš   ZbkmsZwidth_dhZentsÚfoundr£   ZbksZ	bkmatchesr   )rr   rŸ   r’   r“   r   Úcd_completerF  s@   
r¨   c                 C   s   d  ¡ S )zA completer for %reset magicz-f -s in out array dhist)ru   rp   r   r   r   Úreset_completer|  s   r©   )F),Ú__doc__r—   rS   r&   r   r>   Ú	importlibr   Úimportlib.machineryr   r   Ú	zipimportr   Z	completerr   r   Úerrorr	   Zutils._process_commonr   ZIPythonr   Útypingr   Z	_suffixesrF   rJ   Úcompiler'   r0   r…   rc   r;   rN   ÚboolrX   r\   ro   rw   r‚   rƒ   rœ   r¨   r©   r   r   r   r   Ú<module>   sB   ý
(/%%%6