o
    Zha1                     @   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   P/var/www/html/lang_env/lib/python3.10/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
   NTc                    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 rt|| q}|stj|rt|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   Z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