a
    Fb/                     @   s<  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d Zdd Z dd Z!dd Z"d,e#ee# dddZ$d d! Z%d"d# Z&d$d% Z'd&d' Z(d(d) Z)d*d+ Z*dS )-z\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   s   | ]}t |V  qd S N)reescape.0s r   8lib/python3.9/site-packages/IPython/core/completerlib.py	<genexpr>:       r   z.*(\.ipy|\.ipynb|\.py[w]?)$c                    s   | dkrd} t jj t j| rg }t j| ddD ]T\}}}|t| d d r|| fdd|D  g |dd< q2|| q2n(ztt| j	
 }W n   g }Y n0 g }|D ]"}t|}|r||d	 qt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ZpjoinZsubdirr   r   r   W   r   zmodule_list.<locals>.<genexpr>name)ospathjoinisdirwalklenextendlistr   _fileskeys	import_rematchappendgroupset)r"   filesrootdirsnondirsmodulesr   mr   r   r   module_listC   s*    

r6   c                  C   s  t  } | du rttjS | jdi }ttj}t }d}tjD ]}z|| }W n ty   t	|}z|
d W n ty   Y n0 |dvr|||< t | tkr|sd}td td tj  t | tkrtd	 g  Y   S Y n0 || 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.
    Nrootmodules_cacheF__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dbgetr   r"   KeyErrorr6   remove
ValueErrorTIMEOUT_STORAGEprintstdoutflushTIMEOUT_GIVEUPr'   r/   )ipr7   ZrootmodulesZ
start_timeZstorer"   r4   r   r   r   get_root_modulesk   s>    




rF   c                 C   s:   |rt t| |S |d d dko2|dd  dk S d S )Nr
   __)inspectismodulegetattr)moduleattronly_modulesr   r   r   is_importable   s    rO   c                 C   s@   zt | |}W n& ty"   Y dS  ty4   Y dS 0 t|S )NTF)rK   AttributeError	TypeErrorrI   rJ   )rL   rM   objr   r   r   is_possible_submodule   s    rS   F)modreturnc                    s   |  d} zt|  W n   g  Y S 0 dt ddp8dv }g }t drRrR|rp| fddt D  t dg }r| fdd	|D  n
|| |r|ttj	 j
 d
d |D }|d t|S )zN
    Try to import given module and return list of potential completions.
    r   r8   __file__r   c                    s   g | ]}t  |r|qS r   )rO   r   rM   r5   rN   r   r   
<listcomp>   s   ztry_import.<locals>.<listcomp>__all__c                 3   s   | ]}t  |r|V  qd S r   )rS   rW   )r5   r   r   r      r   ztry_import.<locals>.<genexpr>c                 S   s   h | ]}t |tr|qS r   )
isinstancestrr   cr   r   r   	<setcomp>   r   ztry_import.<locals>.<setcomp>)rstripr   rK   hasattrr'   dirr6   r!   r"   dirnamerV   discardr(   )rT   rN   Z	m_is_initcompletionsZm_allZcompletions_setr   rX   r   
try_import   s$    



rf   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re   r   r   do_complete   s    z$quick_completer.<locals>.do_completeZcomplete_command)Zstr_keyN)r[   r\   splitr   Zset_hook)cmdre   rk   r   rj   r   quick_completer   s    
rn   c                    s   |  d}t|}|dkr,|d dkr,dgS |dk r|d dv r|dkrNt S |d  d t d	k rnt S td d
d d} fdd|D S |dkr|d dkr|d  t 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 >   rq   z%aimportimportr   r   r
   NTc                    s$   g | ]}d   dd |g qS )r   Nrs   )r#   )r   ZelrT   r   r   rY      r   z%module_completion.<locals>.<listcomp>)rl   r&   rF   rf   r#   )lineZwordsZnwordsZcompletion_listr   rt   r   module_completion   s    	
rv   c                 C   s
   t |jS )z@Give completions after user has typed 'import ...' or 'from ...')rv   ru   rg   r   r   r   module_completer  s    	rw   c                    s   t |jdd}t|dkr6|jds6|d d}nd}tj}tjj t	|\}t
dd	 |D r 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   ro   rs   z'"r   c                 s   s   | ]}t |V  qd S r   )magic_run_rer,   r]   r   r   r   r   +  r   z&magic_run_completer.<locals>.<genexpr>c                    s(   g | ] }| d d |rdnd qS )\/r   replacer   r$   r   r   rY   ,  s   z'magic_run_completer.<locals>.<listcomp>*c                    s$   g | ]} |r| d dd qS rz   r{   r|   r   r~   r   r   rY   /  r   c                 S   s   g | ]}| d dqS r   r|   r   r   r   r   rY   0  s   z*.pyz*.ipyz*.ipynbz*.pywc                    s   g | ]}t | qS r   r   r   ptilde_expand	tilde_valr   r   rY   7  r   )r   ru   r&   endswithstripglobr!   r"   r$   r   any)rh   ri   compsrelpathZlglobZmatchesr2   Zpysr   )r$   r   r   r   magic_run_completer  s*    



r   c                    s|  t  } j} jds"d jv r@| jdd}|r<| S g S  jdkrtttt|j	d d }d| d	 fd
dt
|j	d D }t|dkr|S g S  jdrdd |j	d D S t|\}|dd}g }dd t|d D D ]}d|v rt|| q|shtj|r:t|gS | jdi }	 fdd|	D }
|
rd|
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   rY   L  r   z cd_completer.<locals>.<listcomp>--c                 S   s   g | ]}d t j| qS )r   )r!   r"   basename)r   dr   r   r   rY   R  r   rz   r{   c                 S   s(   g | ] }t j|r|d dd qS r   )r!   r"   r$   r}   r   r   r   r   rY   Y  s   r   ro   c                    s   g | ]}|  jr|qS r   )
startswithsymbolr   )ri   r   r   rY   h  r   c                    s   g | ]}t | qS r   r   r   r   r   r   rY   n  r   )r   r   ru   r   r;   r<   r*   r\   r&   Zuser_ns	enumerater   r   r}   r   r	   r-   r!   r"   r$   r   )rh   ri   rE   r   ZbkmsZwidth_dhZentsfoundr   ZbksZ	bkmatchesr   )ri   r   r   r   r   cd_completer:  s@    

r   c                 C   s   d  S )zA completer for %reset magicz-f -s in out array dhist)rl   rg   r   r   r   reset_completerp  s    r   )F)+__doc__r   rI   r!   r   r9   	importlibr   importlib.machineryr   r   	zipimportr   Z	completerr   r   errorr	   Zutils._process_commonr   ZIPythonr   typingr   Z	_suffixesr@   rD   compiler#   r+   ry   r6   rF   rO   rS   r\   rf   rn   rv   rw   r   r   r   r   r   r   r   <module>   s@   
(,"%%6