U
    -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,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   Z/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/IPython/core/completerlib.py	<genexpr>:   s     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X 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pjoinsubdirr   r   r   W   s     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*    

r7   c                  C   s   t  } | dkrttjS | jdi }ttj}t }d}tjD ]}z|| }W n tk
r   t	|}z|
d W n tk
r   Y nX |dkr|||< t | tkr|sd}td td tj  t | tkrtd	 g  Y   S Y nX || 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#   KeyErrorr7   remove
ValueErrorTIMEOUT_STORAGEprintstdoutflushTIMEOUT_GIVEUPr(   r0   )ipr8   Zrootmodules
start_timestorer#   r5   r   r   r   get_root_modulesk   s>    




rI   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    rR   c                 C   sD   zt | |}W n* tk
r$   Y dS  tk
r8   Y dS X t|S )NTF)rN   AttributeError	TypeErrorrL   rM   )rO   rP   objr   r   r   is_possible_submodule   s    rV   F)modreturnc                    s   |  d} zt|  W n   g  Y S X dt ddp8dk}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   r9   __file__r   c                    s   g | ]}t  |r|qS r   )rR   r   rP   r6   rQ   r   r   
<listcomp>   s    ztry_import.<locals>.<listcomp>__all__c                 3   s   | ]}t  |r|V  qd S r   )rV   rZ   )r6   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   rN   hasattrr(   dirr7   r"   r#   dirnamerY   discardr)   )rW   rQ   Z	m_is_initcompletionsZm_allZcompletions_setr   r[   r   
try_import   s$    



ri   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rh   r   r   do_complete   s    z$quick_completer.<locals>.do_completeZcomplete_command)Zstr_keyN)r^   r_   splitr   Zset_hook)cmdrh   rn   r   rm   r   quick_completer   s    
rq   c                    s   |  d}t|}|dkr,|d dkr,dgS |dk r|d dk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 >   rt   %aimportimportr   r   r
   NTc                    s$   g | ]}d   dd |g qS )r   Nrw   )r$   )r   elrW   r   r   r\      s     z%module_completion.<locals>.<listcomp>)ro   r'   rI   ri   r$   )linewordsZnwordsZcompletion_listr   ry   r   module_completion   s    	
r|   c                 C   s
   t |jS )z@Give completions after user has typed 'import ...' or 'from ...')r|   rz   rj   r   r   r   module_completer  s    	r}   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   rr   rw   z'"r   c                 s   s   | ]}t |V  qd S r   )magic_run_rer-   r`   r   r   r   r   +  s     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\   /  s      c                 S   s   g | ]}| d dqS r   r   r   r   r   r   r\   0  s   z*.pyz*.ipyz*.ipynbz*.pywc                    s   g | ]}t | qS r   r   r   ptilde_expand	tilde_valr   r   r\   7  s     )r   rz   r'   endswithstripglobr"   r#   r%   r   any)rk   rl   compsrelpathZlglobmatchesr3   Zpysr   )r%   r   r   r   magic_run_completer  s*    



r   c                    s|  t  } j} jds"d jk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|k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   r\   L  s     z cd_completer.<locals>.<listcomp>--c                 S   s   g | ]}d t j| qS )r   )r"   r#   basename)r   dr   r   r   r\   R  s     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   rr   c                    s   g | ]}|  jr|qS r   )
startswithsymbolr   )rl   r   r   r\   h  s      c                    s   g | ]}t | qS r   r   r   r   r   r   r\   n  s     )r   r   rz   r   r<   r=   r+   r_   r'   Zuser_ns	enumerater   r   r   r   r	   r.   r"   r#   r%   r   )rk   rl   rF   r   ZbkmsZwidth_dhZentsfoundr   ZbksZ	bkmatchesr   )rl   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)ro   rj   r   r   r   reset_completerp  s    r   )F)+__doc__r   rL   r"   r   r:   	importlibr   Zimportlib.machineryr   r   	zipimportr   Z	completerr   r   errorr	   Zutils._process_commonr   ZIPythonr   typingr   Z	_suffixesrA   rE   compiler$   r,   r   r7   rI   rR   rV   r_   ri   rq   r|   r}   r   r   r   r   r   r   r   <module>   s@   
(,"%%6