
    Xha1                        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j6                  ddj9                  d eD              z        Z ej6                  d      Zdedee   fdZ d Z!dede"fdZ#d Z$d"dedee   fdZ%d Z&d Z'd Z(d Z)d  Z*d! Z+y)#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              #   F   K   | ]  }t        j                  |        y wN)reescape).0ss     M/var/www/html/myenv/lib/python3.12/site-packages/IPython/core/completerlib.py	<genexpr>r   :   s      A!1 A   !z.*(\.ipy|\.ipynb|\.py[w]?)$pathreturnc                 |  	 | dk(  rd} t         j                  j                  t         j                  j                  |       rhg }t        j                  | d      D ]K  \  }}}|t        |       dz   d 		r!|j                  	fd|D               g |dd ;|j                  |       M n-	 t        t        |       j                  j                               }g }|D ]:  }t        j                  |      }|s|j                  |j                  d             < t        t!        |            S # t        $ r g }Y bw xY w)	z`
    Return the list containing the names of the modules available in the given
    folder.
     .T)followlinksr   Nc              3   0   K   | ]  } |        y wr    )r   fpjoinsubdirs     r   r   zmodule_list.<locals>.<genexpr>X   s     ?!U61-?s   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$   s
           @@r   module_listr;   D   s    rz GGLLE	ww}}T #%774T#B 	&D$#d)A+,'F?w??QW%	&	T*116689E
 G ,OOANN1776?+, G  	E	s   ,,D- -D;:D;c                     t               } | t        t        j                        S t	        | j
                  dd      ri }n| j
                  j                  di       }t        t        j                        }t               }d}t        j                  D ]  }	 ||   }|j%                  |        |r|| j
                  d<   t        t'        |            }|S # t        $ r t        |      }	 |j                  d       n# t        $ r Y nw xY w|dvr|||<   t               |z
  t        kD  r8|s6d}t        d       t        d       t        j                  j!                          t               |z
  t"        kD  rt        d	       g cY c S Y w xY w)
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.
    _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>   rootmodules
start_timestorer   r9   s          r   get_root_modulesrQ   l   su    
B	z C,,--ruugu%EEII&92>s//0KJE $	'-G$ 	7#)$* %6!"s;'(K+  	!$'Gz* 9$*1!$'v
"_4UHI ( )

  "v
"^3>?	 4	s7   CF)C;:F;	DFDA9FFattrc                     |r"	 t        | |      }t        j                  |      S |d d dk(  xr |dd  dk(   S # t        $ r Y yw xY w)NFr   __)rB   ModuleNotFoundErrorinspectismodule)modulerR   only_modulesmods       r   is_importabler\      sa    	&$'C $$8t#9RS	T(9:: # 		s   8 	AAc                 x    	 t        | |      }t        j                  |      S # t        $ r Y yt        $ r Y yw xY w)NTF)rB   AttributeError	TypeErrorrW   rX   )rY   rR   objs      r   is_possible_submodulera      sE    fd# C     s   # 	999r[   c           
        
 | j                  d      } 	 t        |       
dt        
dd      xs dv }g }t        
d      r|r|r5|j	                  t        
      D cg c]  }t        
||      r| c}       t        
dg       }|r|j	                  
fd|D               n|j	                  |       |rG
j                  }t        j                  j                  |      }||j	                  t        |             |D ch c]  }t        |t              s| }	}|	j                  d       t        |	      S #  g cY S xY wc c}w c c}w )zN
    Try to import given module and return list of potential completions.
    r   r?   __file__r   __all__c              3   <   K   | ]  }t        |      s|  y wr   )ra   )r   rR   r:   s     r   r   ztry_import.<locals>.<genexpr>   s     TD5J1d5S4Ts   )rstripr   rB   hasattrr+   dirr\   rc   r&   r   dirnamer;   
isinstancestrdiscardr,   )r[   rZ   	m_is_initcompletionsrR   m_allfile_	file_pathccompletions_setr:   s             @r   
try_importrt      s;    **S/C# wq*b9?R@IKAz"LYc!f Cd*1dLA " C 	D Ay"%ETETT5!

GGOOE*	 {956"-DQAs1CqDODJ'  /	C Es   D9 EEE9D?c                     t        t              rj                         fd}t               j	                  d||        y)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 r   r!   )selfeventrn   s     r   do_completez$quick_completer.<locals>.do_complete   s	        complete_command)str_keyN)rj   rk   splitr   set_hook)cmdrn   ry   s    ` r   quick_completerr      s<     +s#!'') M-kSIrz   c                    | j                  d      }t        |      }|dk(  r|d   dk(  rdgS |dk  r|d   dv r|dk(  r
t               S |d   j                  d      }t        |      d	k  r
t               S t        dj	                  |d
d       d      }|D cg c]  }dj	                  |d
d |gz          c}S |dk\  r|d   dk(  r|d   }t        |      S y
y
c c}w )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   NT)r}   r*   rQ   rt   r'   )linewordsnwordsr[   completion_listels         r   module_completionr      s     JJsOEZF {uQx6){ zuQx#AAQ;#%%AhnnS!s8a<#%%$SXXc#2h%7>4CDbSbRD)DD {uQx6)Ah# *{ Es   Cc                 ,    t        |j                        S )z@Give completions after user has typed 'import ...' or 'from ...')r   r   rw   rx   s     r   module_completerr     s     UZZ((rz   c                    t        |j                  d      }t        |      dkD  r0|j                  j                  d      s|d   j	                  d      }nd}t
        j
                  }t        j                  j                  }t        |      \  }}}t        d |D              r6 ||d	z         D cg c]!  }|j                  d
d       ||      rdndz   # }	}n ||d	z         D cg c]   } ||      s|j                  d
d      dz   " }
} ||dz          ||dz         z    ||dz         z    ||dz         z   D cg c]  }|j                  d
d       }}|
|z   }	|	D cg c]  }t        |||       c}S c c}w c c}w c c}w c c}w )zKComplete files that end in .py or .ipy or .ipynb for the %run command.
    F)strictr   r   r   z'"r   c              3   F   K   | ]  }t         j                  |        y wr   )magic_run_rer1   )r   rr   s     r   r   z&magic_run_completer.<locals>.<genexpr>7  s     
0Q<a 
0r   *\/z*.pyz*.ipyz*.ipynbz*.pyw)r   r   r*   endswithstripglobr&   r   r(   r   anyreplacer	   )rw   rx   compsrelpathlglobr(   tilde_expand	tilde_valr"   matchesr7   pysps                r   magic_run_completerr   !  s    ejj/EE
Q!4!4S!9)//%( IIEGGMME'27';$G\9 
0%
00%*73;%79 ! IId3'%(3C 9 9 493ERaq		$s#c)RRwv~.ww1GGgi'()+071B+CDE 		$s# E E * @GG!M!\95GG9 SE Hs   0&E9&E>4E>>F"Fc           
      L   t               }|j                  }|j                  j                  d      sd|j                  v r0| j                  j                  dd      }|r|j                         S g S |j                  dk(  rt        t        t        t        |j                  d         dz                     }d|z   d	z   }t        |j                  d         D cg c]  \  }}|||fz   }	}}t        |	      dkD  r|	S g S |j                  j                  d
      r=|j                  d   D 
cg c]$  }
d
t        j                  j                  |
      z   & c}
S t        |      \  }}}|j!                  dd      }g }t#        j"                  |dz         D cg c]6  }t        j                  j%                  |      r|j!                  dd      dz   8 c}D ]  }
d|
v rt&        |j)                  |
        |s~t        j                  j%                  |      rt+        |||      gS | j                  j                  di       }|D cg c]   }|j                  |j                        s|" }}|r|S t&        |D cg c]  }t+        |||       c}S c c}}w c c}
w c c}w c c}w c c}w )z:Completer function for cd, which only returns directories.z-bz -b 	bookmarksN-_dhr   z-%0zd [%s]z--r   r   r   r   )r   symbolr   r   rC   rD   r.   rk   r*   user_ns	enumerate
startswithr&   r   basenamer   r   r   r(   r
   r2   r	   )rw   rx   rM   r   bkmswidth_dhfmtir   entsdr   r   foundr"   bks	bkmatchesr   s                     r   cd_completerr   F  sQ   	BllG zz4 Fejj$8ww{{;-99;I||ss3s2::e#459:;<h((1"**U2C(DE1!uEEt9q=K	||t$46JJu4EFqrww''**FF (37';$G\9ood3'GE1573;1G #Aq! iiS!C' # !8 MQ 77==!!'<CDD ggkk+b) #B1q||ELL'AQB	B?DE!M!\95EEE F G# C Fs$   J*)J;J  J!J5J!c                 "    dj                         S )zA completer for %reset magicz-f -s in out array dhist)r}   r   s     r   reset_completerr   |  s    %++--rz   )F),__doc__r   rW   r&   r   r@   	importlibr   importlib.machineryr   r   	zipimportr   	completerr   r	   errorr
   utils._process_commonr   IPythonr   typingr   	_suffixesrH   rL   compiler'   r0   r   rk   r;   rQ   boolr\   ra   rt   r   r   r   r   r   r   r!   rz   r   <module>r      s
  "   	 	 
 # ,  ! 2  -   
 N	   BJJ * yy Ay AAB C	 rzz89%c %d3i %P,^	; 	;d 	;	!!C !S	 !JJ.J	)"HJ4Fl.rz   