
    cM                        d 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m	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZmZmZmZmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 dee&         de4dej5        deeddf         fdZ6dee&         de4dee7         dee&         fdZ8 G d d          Z9dS )zAutoImport module for rope.    N)OrderedDict)FutureProcessPoolExecutoras_completed)chain)Path)	GeneratorIterableIteratorListOptionalSetTuple)
exceptionslibutilsresourceobserver
taskhandle)Project)Resource)models)
ModuleFileNameNameTypePackagePackageTypeSearchResultSource)	get_names)	get_filesget_modname_from_pathget_package_tuplesort_and_deduplicatesort_and_deduplicate_tuple)importutilspackages
underlinedjob_setreturnc              #   "  K   t                      5 }| D ]a}t          ||          D ]N}|                    |j                   |                                 |                    t          ||          V  Ob	 ddd           dS # 1 swxY w Y   dS )zGet all names as futures.N)r   r   started_jobmodname	incrementsubmitr   )r%   r&   r'   executorpackagemodules         >lib/python3.11/site-packages/rope/contrib/autoimport/sqlite.pyget_future_namesr2   %   s      
		 B( 	B 	BG#GZ88 B B##FN333!!###ooiAAAAAAB	BB B B B B B B B B B B B B B B B B Bs   A%BBBexistingc                 z    |rdt           dt          ffd}ndt           dt          ffd}t          ||           S )z!Filter list of packages to parse.r/   r(   c                     | j         vS Nnamer/   r3   s    r1   filter_packagez'filter_packages.<locals>.filter_package7   s    <x//    c                 L    | j         vo| j                             d           S )N_)r8   
startswithr9   s    r1   r:   z'filter_packages.<locals>.filter_package<   s)    <x/T8O8OPS8T8T4TTr;   )r   boolfilter)r%   r&   r3   r:   s     ` r1   filter_packagesrA   1   s      U	0G 	0 	0 	0 	0 	0 	0 	0 	0
	UG 	U 	U 	U 	U 	U 	U 	U .(+++r;   c                      e Zd ZU dZej        ed<   eed<   eed<   e	ed<   d=defdZ
d	 Zd
efdZd>dededeeeef                  fdZ	 	 d?dededeee                  deeddf         fdZ	 d>dededeeddf         fdZ	 d>dededeeddf         fdZdee         fdZdee         fdZdeee         ee	         f         fdZddej        fdeee                  dedej        fdZ dej        ddfdeee                  dej        dedee         fdZ!dee         de"e	         fdZ#defd Z$d! Z%d" Z&d# Z'd$ Z(	 d@d%eded&efd'Z)d( Z*d%ed)efd*Z+dAd&efd+Z,dee-         fd,Z.dee	         fd-Z/d.ee	         fd/Z0dee         fd0Z1d1 Z2d2e3fd3Z4e5dede6fd4            Z7d2e8e         fd5Z9defd6Z:d7edee	         fd8Z;	 d>d%edede<fd9Z=d:e>j?        fd;Z@d:e>j?        fd<ZAdS )B
AutoImportzA class for finding the module that provides a name.

    This class maintains a cache of global names in python modules.
    Note that this cache is not accurate and might be out of date.

    
connectionr&   projectproject_packageTFc                    || _         t          t          |j        j                  |          }|J |j        J || _        || _        |s|j        d}n)t          t          |j        j                  dz            }t          j        |          | _        |                                  |r=t          j        | j        | j        | j                  }|                    |           dS dS )a  Construct an AutoImport object.

        Parameters
        ___________
        project : rope.base.project.Project
            the project to use for project imports
        observe : bool
            if true, listen for project changes and update the cache.
        underlined : bool
            If `underlined` is `True`, underlined names are cached, too.
        memory : bool
            if true, don't persist to disk
        Nz:memory:zautoimport.db)changedmovedremoved)rE   r!   r   root	real_pathpathrF   r&   
ropefolderstrsqlite3connectrD   	_setup_dbr   ResourceObserver_changed_moved_removedadd_observer)selfrE   observer&   memoryrF   db_pathobservers           r1   __init__zAutoImport.__init__O   s     +D1G,H,H'RR***#///.$ 	PW' 	P GG$w1;<<NOOG!/'22 	+'8T[$-  H   *****		+ 	+r;   c                     t           j                            | j                   t           j                            | j                   | j                                         d S r6   )r   r   create_tablerD   r   commitrX   s    r1   rR   zAutoImport._setup_dbp   sL      111##DO444     r;   startingc                     |                      t          j        j                            ddd          |f                                          }t          |          S )aa  
        Find modules that have a global name that starts with `starting`.

        For a more complete list, use the search or search_full methods.

        Parameters
        __________
        starting : str
            what all the names should start with
        Return
        __________
        Return a list of ``(name, module)`` tuples
        r8   r0   source)_executer   r   import_assistselectfetchallr#   )rX   rb   resultss      r1   rf   zAutoImport.import_assistu   sX     --K%,,VXxHH8+
 

(** 	 *
 
 	
r;   r8   exact_matchr(   c                 `    d |                      ||          D             }t          |          S )z
        Search both modules and names for an import string.

        This is a simple wrapper around search_full with basic sorting based on Source.

        Returns a sorted list of import statement, modname pairs
        c                 "    g | ]\  }}}}|||fS  rm   ).0	statementimport_namerd   types        r1   
<listcomp>z%AutoImport.search.<locals>.<listcomp>   s7     /
 /
 /
4	; V,/
 /
 /
r;   )search_fullr#   )rX   r8   rj   ri   s       r1   searchzAutoImport.search   sE    /
 /
8<8H8Hk9 9/
 /
 /
 *'222r;   Nignored_namesc              #      K   |t                      }t          |                     ||                    }|                    |                     ||                    }|D ]}|j        |vr|V  dS )a  
        Search both modules and names for an import string.

        Parameters
        __________
        name: str
            Name to search for
        exact_match: bool
            If using exact_match, only search for that name.
            Otherwise, search for any name starting with that name.
        ignored_names : Set[str]
            Will ignore any names in this set

        Return
        __________
        Unsorted Generator of SearchResults. Each is guaranteed to be unique.
        N)set_search_nameunion_search_moduler8   )rX   r8   rj   ru   ri   results         r1   rs   zAutoImport.search_full   s      .  	"EEMd''k::;;-- 3 3D+ F FGG 	 	F{-/ 	 	r;   c              #      K   |s|dz   }|                      t          j        j                            dddd          |f          D ]!\  }}}}t          d| d| |||          V  "dS )	z
        Search both names for available imports.

        Returns the import statement, import name, source, and type.
        %r8   r0   rd   rq   from  import N)re   r   r   search_by_name_likerg   r   )rX   r8   rj   rp   r0   rd   	name_types          r1   rx   zAutoImport._search_name   s        	#:D6:mmK+2268XvVVG7
 7
 	 	2K
 9F99K99	    		 	r;   c              #   8  K   |s|dz   }|                      t          j        j                            dd          |f          D ]j\  }}|                    d          }|d         }|d         }|dd         D ]}|dz  }||z  }t          d| d	| ||t          j        j	                  V  k|                      t          j        j
                            dd          |f          D ]0\  }}d|v r
t          d
| ||t          j        j	                  V  1dS )z
        Search both modules for available imports.

        Returns the import statement, import name, source, and type.
        r}   r0   rd   .r      r~   r   zimport N)re   r   r   search_submodule_likerg   splitr   r   Modulevaluesearch_module_like)	rX   r8   rj   r0   rd   partsrp   	remainingparts	            r1   rz   zAutoImport._search_module   s{       	#:D"mmK-44XxHH4'
 
 	 	NFF LL%%E)KaIad " "S 	T!		<I<<{<<O)	     #mmK*11(HEEw
 
 	 	NFF f} "&""FFHO4I     	 	r;   c                     |                      t          j        j                            dd          |f                                          }t          |          S )z0Get the list of modules that have global `name`.r0   rd   )re   r   r   r   rg   rh   r"   )rX   r8   ri   s      r1   get_moduleszAutoImport.get_modules   sM    --K+228XFF
 

(** 	 $G,,,r;   c                     |                      t          j        j                            d                                                    S )z(Get the list of all cached global names.r8   )re   r   r   objectsrg   rh   ra   s    r1   get_all_nameszAutoImport.get_all_names   s3    }}V[077??@@IIKKKr;   c                 *   |                      t          j        j                                                                                  }|                      t          j        j                                                                                  }||fS )zDump the entire database.)re   r   r   r   select_starrh   r   )rX   name_resultspackage_resultss      r1   	_dump_allzAutoImport._dump_all   si    }}V[%8%D%D%F%FGGPPRR--(>(J(J(L(LMMVVXX_,,r;   	resourcestask_handlec           	         || j                                         }|                    dt          |                    }|                     t
          j        j        | j        j	        f           g }t                      5 }|D ]i}|                    d|j                    |                     ||          }|                    |                    t           || j                             j	 ddd           n# 1 swxY w Y   t#          |          D ]=}	|                     |	                                           |                                 >| j                                         dS )zGenerate global name cache for project files.

        If `resources` is a list of `rope.base.resource.File`, only
        those files are searched; otherwise all python modules in the
        project are cached.
        NGenerating autoimport cachezWorking on )rE   get_python_filescreate_jobsetlenre   r   r   delete_by_package_namerF   r8   r   r*   rM   _resource_to_moduleappendr-   r   r   
_add_namesr{   finished_jobrD   r`   )
rX   r   r&   r   r'   futuresr.   filer0   futures
             r1   generate_cachezAutoImport.generate_cache  s     	85577I++)3y>>
 
 	N1D4H4M3O	
 	
 	
  "" 	Yh! Y Y##$=$)$=$=>>>11$
CCxy&$BVWWXXXXY	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y
 #7++ 	# 	#FOOFMMOO,,,  """"     s   ?A-C99C= C=modulessingle_threadc                    || j         n|}||                                 n!t          |                     |                    }|                                 }t          t          |||                    }|sdS |                     |           |                    dd          }|ro|D ]k}t          ||          D ]X}	|	                    |	j
                   t          |	|          D ]+}
|                     |
           |                                 ,Yln\t          t          |||                    D ]=}|                     |                                           |                                 >| j                                         dS )a}  
        Generate global name cache for external modules listed in `modules`.

        If no modules are provided, it will generate a cache for every module available.
        This method searches in your sys.path and configured python folders.
        Do not use this for generating your own project's internal names,
        use generate_resource_cache for that instead.
        Nr   r   )r&   _get_available_packageslist_get_packages_from_modules_get_packages_from_cacherA   _add_packagesr   r   r*   r+   r   	_add_namer   r   r2   r   r{   rD   r`   )rX   r   r   r   r&   r%   r3   r'   r/   r0   r8   future_names               r1   generate_modules_cachez!AutoImport.generate_modules_cache$  s    )3JT__

 @D((***d55g>>?? 	 0022*hGGHH 	F8$$$++,I1MM 	'# / /'<< / /F''777 )&' : : / /t,,,,,....///  , :w??    ' '  2 2 4 4555$$&&&&     r;   c              #   L   K   |D ]}|                      |          }||V  d S r6   )_find_package_path)rX   r   r+   r/   s       r1   r   z%AutoImport._get_packages_from_modulesQ  sH       	 	G--g66G MMMM		 	r;   r0   c                 \    |                      |           |                     |g           dS )z<Update a module in the cache, or add it if it doesn't exist.N)_del_if_existr   )rX   r0   s     r1   update_modulezAutoImport.update_moduleX  s2    6"""##VH-----r;   c                 j    | j                                          | j                                          dS )zClose the autoimport database.N)rD   r`   closera   s    r1   r   zAutoImport.close]  s0       r;   c                    g }|                      t          j        j                            d          |f                                          }|D ]}t          j        t          j	                  5  |d         }|
                    | j        j         d          r(d                    |                    d                    }| j                            |          }||v rb||         }|                                \  }}|A|                                                                }	|	||                    |	|f           ddd           n# 1 swxY w Y   |S )z/Return a list of ``(resource, lineno)`` tuples.r0   r   r   N)re   r   r   r   rg   rh   
contextlibsuppressr   ModuleNotFoundErrorr>   rF   r8   joinr   rE   
get_moduleget_definition_locationget_resourcer   )
rX   r8   r{   r   r0   module_namepymodulepynamelinenoresources
             r1   get_name_locationszAutoImport.get_name_locationsb  s   --K+228<<tg
 

(** 	  	> 	>F$Z%CDD > >$Qi))T-A-F*I*I*IJJ C"%((;+<+<S+A+A"B"BK<22;??8# >%d^F%+%C%C%E%ENFF >#)#4#4#6#6#C#C#E#E# >F >"MM8V*<===> > > > > > > > > > > > > > > s   1CEE	E	c                 8   |                      t          j        j                                                   |                      t          j        j                                                   |                                  | j                                         dS )zClear all entries in global-name cache.

        It might be a good idea to use this function before
        regenerating global names.

        N)	re   r   r   r   
drop_tabler   rR   rD   r`   ra   s    r1   clear_cachezAutoImport.clear_cachew  st     	fk)4466777fn,7799:::     r;   c                    t          j        d|          }||d|                                         }	 t          j        | j        |          }n# t          j        $ r Y dS w xY wd}t          j	        |dff          }t          j
        | j        |          }|                    |           |                                }|                    |          }|                    dd|          dz   }|S )z5Guess at what line the new import should be inserted.z^(def|class)\s+Nr   __rope_testmodule_rope
r   )rert   startr   get_string_modulerE   r   ModuleSyntaxErrorr$   NormalImportget_module_imports
add_importget_changed_sourceindexcount)	rX   codematchr   testmodname
importinfomodule_importsoffsetr   s	            r1   find_insertion_linezAutoImport.find_insertion_line  s    	,d33 	)%++--(D	1$,EEHH+ 	 	 	11	. -T/B.DEE
$7hOO!!*---0022K((D!V,,q0s   A A#"A#r   r`   c                    |r|n| j         }|                     ||          }|                     |j        d           t	          || j                  D ]}|                     |           |r| j                                         dS dS )z0Update the cache for global names in `resource`.F)r   r`   N)	r&   r   r   r+   r   rF   r   rD   r`   )rX   r   r&   r`   r0   r8   s         r1   update_resourcezAutoImport.update_resource  s     $.BZZ4?
))(J??v~eDDDfd&:;; 	! 	!DNN4     	%O""$$$$$	% 	%r;   c                 \    |                                 s|                     |           d S d S r6   )	is_folderr   )rX   r   s     r1   rT   zAutoImport._changed  s:    !!## 	+  *****	+ 	+r;   newresourcec                     |                                 sF|                     |          j        }|                     |           |                     |           d S d S r6   )r   r   r+   r   r   )rX   r   r   r+   s       r1   rU   zAutoImport._moved  sd    !!## 	...x88@Gw'''  -----	. 	.r;   c                     |                      t          j        j        |f           |r| j                                         d S d S r6   )re   r   r   delete_by_module_namerD   r`   )rX   r   r`   s      r1   r   zAutoImport._del_if_exist  sI    fk7+HHH 	%O""$$$$$	% 	%r;   c                     dt           dt          fd}| j                                        }t	          d |          }t          ||          }t          t          j        |                    S )Nfolderr(   c                 Z    |                                  o|                                 dk    S )Nz/usr/bin)is_diras_posixr   s    r1   filter_foldersz6AutoImport._get_python_folders.<locals>.filter_folders  s$    ==??Fv'8'8J'FFr;   c                 *    t          | j                  S r6   )r   rL   r   s    r1   <lambda>z0AutoImport._get_python_folders.<locals>.<lambda>  s    $v/?*@*@ r;   )	r   r?   rE   get_python_path_foldersmapr@   r   r   fromkeys)rX   r   foldersfolder_pathss       r1   _get_python_folderszAutoImport._get_python_folders  sz    	G4 	GD 	G 	G 	G 	G ,6688@@'JJnl;;K(66777r;   c                     d t           j        D             }|                                 D ]F}|                                D ]/}t	          || j                  }||                    |           0G|S )Nc                 Z    g | ](}t          |t          j        d t          j                  )S r6   )r   r   BUILTINr   )rn   r0   s     r1   rr   z6AutoImport._get_available_packages.<locals>.<listcomp>  s;     #
 #
 #
 FFND+2EFF#
 #
 #
r;   )sysbuiltin_module_namesr   iterdirr!   rE   r   )rX   r%   r   r/   package_tuples        r1   r   z"AutoImport._get_available_packages  s    #
 #
2#
 #
 #
 ..00 	/ 	/F!>>++ / / 1'4< H H  ....	/
 r;   r%   c                     d |D             }|                      t          j        j                                        |           d S )Nc                 D    g | ]}|j         t          |j                  fS rm   )r8   rO   rM   )rn   ps     r1   rr   z,AutoImport._add_packages.<locals>.<listcomp>  s'    888!QV%888r;   )_executemanyr   r   r   insert_into)rX   r%   datas      r1   r   zAutoImport._add_packages  sB    88x888&.0<<>>EEEEEr;   c                     t          t          |                     t          j        j                                                                                             }|                    | j	        j
                   |S r6   )r   r   re   r   r   r   r   rh   r   rF   r8   )rX   r3   s     r1   r   z#AutoImport._get_packages_from_cache  sa    "4==!7!C!C!E!EFFOOQQR
 
 	,1222r;   c                     |                                 s1|                     |          j        }|                     |           d S d S r6   )r   r   r+   r   )rX   r   r+   s      r1   rV   zAutoImport._removed  sP    !!## 	(..x88@Gw'''''	( 	(r;   namesc                 T    |                      |                                           d S r6   )r   r{   rX   r  s     r1   _add_future_nameszAutoImport._add_future_names  s"    '''''r;   c                 V    | j         | j        | j        | j        j        | j        j        fS r6   )r8   r+   r/   rd   r   r   r7   s    r1   _convert_namezAutoImport._convert_name  s-     ILLKN 
 	
r;   c                      |E                      t          j        j                                         fd|D                        d S d S )Nc                 :    g | ]}                     |          S rm   )r
  )rn   r8   rX   s     r1   rr   z)AutoImport._add_names.<locals>.<listcomp>  s'    <<<d##D))<<<r;   )r   r   r   r   r  r  s   ` r1   r   zAutoImport._add_names  sb     	#//11<<<<e<<<    	 	r;   c                     |                      t          j        j                                        |                     |                     d S r6   )re   r   r   r   r  r
  )rX   r8   s     r1   r   zAutoImport._add_name  s;    fk)55779K9KD9Q9QRRRRRr;   target_namec                     |t           j        v r&t          |t          j        d t
          j                  S |                                 D ]D}|                                D ]-}t          || j	                  }||\  }}}}||k    r|c c S .Ed S r6   )
r   r   r   r   r   r   r   r   r!   rE   )	rX   r  r   r/   r   r8   rd   package_pathpackage_types	            r1   r   zAutoImport._find_package_path  s    #22 	S;k>QRRR..00 	) 	)F!>>++ ) ) 1'4< H H  ;H8flL;& )(((((()) tr;   c                     | j         j        sJ |r|n| j        }t          |j                  }t          || j         j        d          }t          ||||j        dk              S )NF)add_package_namez__init__.py)rF   rM   r&   r   rL   r    r   r8   )rX   r   r&   resource_pathresource_modnames        r1   r   zAutoImport._resource_to_module  s     #((((#-BZZ4?
"8#566 !64/4u!
 !
 !
 -/	
 
 	
r;   queryc                 p    t          |t          j                  sJ  | j        j        |j        g|R i |S r6   )
isinstancer   
FinalQueryrD   execute_queryrX   r  argskwargss       r1   re   zAutoImport._execute  sB    %!233333&t&u|EdEEEfEEEr;   c                 p    t          |t          j                  sJ  | j        j        |j        g|R i |S r6   )r  r   r  rD   executemanyr  r  s       r1   r   zAutoImport._executemany  sB    %!233333*t*5<I$III&IIIr;   )TFT)F)FN)FT)T)B__name__
__module____qualname____doc__rP   
Connection__annotations__r?   r   r   r]   rR   rO   rf   r   r   rt   r   r   r	   r   rs   rx   rz   r   r   r   r   r   DEFAULT_TASK_HANDLEr   BaseTaskHandler   r   r   r   r   r   r   r   r   r   rT   rU   r   r   r   r   r   r   rV   r   r  staticmethodtupler
  r
   r   r   r   r   r   r   r  re   r   rm   r;   r1   rC   rC   B   s          """"+ + + + + +B! ! !

c 
 
 
 
*3 33 3T 3d5c?>S 3 3 3 3& ",0	    C)	
 
<t+	,   @ .3 &*	<t+	,   0 .3" ""&*"	<t+	," " " "H-49 - - - -LtCy L L L L-5dT']!:; - - - - /3 1;1O	! !DN+! !  .	! ! ! !B (,1;1O#%)+! +!$s)$+!  .+! 	+!
 TN+! +! +! +!Z$s) @Q    .C . . . .
     
  *
! 
! 
!  & LP
% 
% 
%.2
%DH
% 
% 
% 
%+ + +.x .h . . . .% % % % % %
8T$Z 8 8 8 8g    Fd7m F F F F$s)    ( ( (
(v ( ( ( ( 
D 
U 
 
 
 \
    Sd S S S Sc hw6G     6;
 
 
.2
	
 
 
 
&Ff/ F F F FJ&"3 J J J J J Jr;   rC   ):r$  r   r   rP   r   collectionsr   concurrent.futuresr   r   r   	itertoolsr   pathlibr   typingr	   r
   r   r   r   r   r   	rope.baser   r   r   r   rope.base.projectr   rope.base.resourcesr   rope.contrib.autoimportr   rope.contrib.autoimport.defsr   r   r   r   r   r   r   rope.contrib.autoimport.parser   rope.contrib.autoimport.utilsr   r    r!   r"   r#   rope.refactorr$   r?   
BaseJobSetr2   rO   rA   rC   rm   r;   r1   <module>r9     s   ! !     				  



 # # # # # # H H H H H H H H H H             L L L L L L L L L L L L L L L L L L H H H H H H H H H H H H % % % % % % ( ( ( ( ( ( * * * * * *                  4 3 3 3 3 3              & % % % % %	B7m	B)-	B8B8M	BvtT!"	B 	B 	B 	B,w,-1,=A#Y,g, , , ,"RJ RJ RJ RJ RJ RJ RJ RJ RJ RJr;   