o
    ˷e/                     @   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-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 ).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       | ]}t |V  qd S N)reescape.0s r   P/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/core/completerlib.py	<genexpr>:       r   z.*(\.ipy|\.ipynb|\.py[w]?)$c                    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   g }Y g }|D ]}t|}|rl||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   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.   	
r9   c                  C   s  t  } | du rttjS | jdi }ttj}t }d}tjD ]]}z|| }W nO tyw   t	|}z|
d W n	 tyC   Y nw |dvrL|||< t | tkrd|sdd}td td tj  t | tkrut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.
    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%   KeyErrorr9   remove
ValueErrorTIMEOUT_STORAGEprintstdoutflushTIMEOUT_GIVEUPr*   r2   )ipr:   rootmodules
start_timestorer%   r7   r   r   r   get_root_modulesk   sD   




rL   c                 C   s6   |r
t t| |S |d d dko|dd  dk S )Nr
   __)inspectismodulegetattr)moduleattronly_modulesr   r   r   is_importable   s   "rU   c                 C   s@   zt | |}W n ty   Y dS  ty   Y dS w t|S )NTF)rQ   AttributeError	TypeErrorrO   rP   )rR   rS   objr   r   r   is_possible_submodule   s   
rY   Fmodreturnc                    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|| |r_|ttj	 j
 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   )rU   r   rS   r8   rT   r   r   
<listcomp>   s    

ztry_import.<locals>.<listcomp>__all__c                 3   s    | ]
}t  |r|V  qd S r   )rY   r]   )r8   r   r   r      s    ztry_import.<locals>.<genexpr>c                 S   s   h | ]	}t |tr|qS r   )
isinstancestrr   cr   r   r   	<setcomp>   s    ztry_import.<locals>.<setcomp>)rstripr   rQ   hasattrr*   dirr9   r$   r%   dirnamer\   discardr+   )rZ   rT   	m_is_initcompletionsm_all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_completecomplete_command)str_keyN)ra   rb   splitr   set_hook)cmdrl   rt   r   rs   r   quick_completer   s   
rz   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 >   %aimportr}   importr   r   r
   NTc                    s$   g | ]}d   dd |g qS )r   Nr   )r&   )r   elrZ   r   r   r_         $ z%module_completion.<locals>.<listcomp>)rw   r)   rL   ro   r&   )linewordsnwords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   r{   r   z'"r   c                 s   r   r   )magic_run_rer/   rc   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   r_   ,  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_   0  s    z*.pyz*.ipyz*.ipynbz*.pywc                       g | ]}t | qS r   r   r   ptilde_expand	tilde_valr   r   r_   7      )r   r   r)   endswithstripglobr$   r%   r'   r   any)rq   rr   compsrelpathlglobmatchesr5   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 	bookmarksN-_dhr   z-%0zd [%s]c                    s   g | ]
\}} ||f qS r   r   )r   ir   )fmtr   r   r_   L      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   r   c                 S   s(   g | ]}t j|r|d dd qS r   )r$   r%   r'   r   r   r   r   r   r_   Y  s    
r   r{   c                    s   g | ]
}|  jr|qS r   )
startswithsymbolr   )rr   r   r   r_   h  r   c                    r   r   r   r   r   r   r   r_   n  r   )r   r   r   r   r>   r?   r-   rb   r)   user_ns	enumerater   r   r   r   r	   r0   r$   r%   r'   r   )rq   rr   rH   r   bkmswidth_dhentsfoundr   bks	bkmatchesr   )rr   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)rw   rp   r   r   r   reset_completerp  s   r   )F)+__doc__r   rO   r$   r   r<   	importlibr   importlib.machineryr   r   	zipimportr   	completerr   r   errorr	   utils._process_commonr   IPythonr   typingr   	_suffixesrC   rG   compiler&   r.   r   r9   rL   rU   rY   rb   ro   rz   r   r   r   r   r   r   r   r   r   <module>   sB   
(,"%%6