
    c                      n    d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZ  G d d          Zd ZdS )a  
IMPORTANT: This is a deprecated implementation of autoimport using pickle-based
storage.

This pickle-based autoimport is provided only for backwards compatibility
purpose and will be removed and the sqlite backend will be the new default
implementation in the future.

If you are still using this module, you should migrate to the new and improved
sqlite-based storage backend (rope.contrib.autoimport.sqlite.AutoImport).
    N)builtins
exceptionslibutilspynames	pyobjectsresourceobserver	resources
taskhandle)importutilsc                       e Zd ZdZddZd Zd Zd Zd Zd	d	e	j
        fd
Zd	e	j
        fdZd Zd ZddZddZd Zd Zd Zd Zd Zd Zd	S )
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.

    TFc                 8   || _         || _        |j                            d          | _        | j        i | _        |j                            | j                   t          j        | j	        | j
        | j                  }|r|                    |           dS dS )zConstruct an AutoImport object

        If `observe` is `True`, listen for project changes and update
        the cache.

        If `underlined` is `True`, underlined names are cached, too.
        globalnamesN)changedmovedremoved)project
underlined
data_files	read_datanamesadd_write_hook_writer   ResourceObserver_changed_moved_removedadd_observer)selfr   observer   observers        >lib/python3.11/site-packages/rope/contrib/autoimport/pickle.py__init__zAutoImport.__init__&   s     $'11-@@
: 	DJ))$+666#4Mdm
 
 
  	+  *****	+ 	+    c                 |    g }| j         D ]/|                    fd| j                  D                        0|S )zReturn a list of ``(name, module)`` tuples

        This function tries to find modules that have a global name
        that starts with `starting`.
        c              3   J   K   | ]}|                               |fV  d S N)
startswith).0global_namemodulestartings     r"   	<genexpr>z+AutoImport.import_assist.<locals>.<genexpr>D   sP        ))(33f%     r$   )r   extend)r   r,   resultr+   s    ` @r"   import_assistzAutoImport.import_assist;   sp     j 	 	FMM     #':f#5       r$   c                 .      fd j         D             S )z2Return the list of modules that have global `name`c                 2    g | ]}j         |         v |S  r   )r)   r+   namer   s     r"   
<listcomp>z*AutoImport.get_modules.<locals>.<listcomp>N   s*    NNN644:f;M3MNNNNr$   r4   )r   r5   s   ``r"   get_moduleszAutoImport.get_modulesL   s#    NNNNNTZNNNNr$   c                     t                      }| j        D ]/}|                    t          | j        |                              0|S )z*Return the list of all cached global names)setr   update)r   r/   r+   s      r"   get_all_nameszAutoImport.get_all_namesP   sE    j 	3 	3FMM#dj0112222r$   c                    g }| j         D ]}|| j         |         v rt          j        t          j                  5  | j                            |          }||v rb||         }|                                \  }}|A|                                                                }|||	                    ||f           ddd           n# 1 swxY w Y   |S )z.Return a list of ``(resource, lineno)`` tuplesN)
r   
contextlibsuppressr   ModuleNotFoundErrorr   
get_moduleget_definition_locationget_resourceappend)r   r5   r/   r+   pymodulepynamelinenoresources           r"   get_name_locationszAutoImport.get_name_locationsW   s@   j 
	B 
	BFtz&)) 	B()GHH B B#|66v>>Hx' B!)$)/)G)G)I)I! B'-'8'8':':'G'G'I'IH' B B &x.@ A A AB B B B B B B B B B B B B B B s   BCC
	C
	Nc                    || j                                         }|                    dt          |                    }|D ]I}|                    d|j        z             |                     ||           |                                 Jd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.

        NzGenerating autoimport cacheWorking on <%s>)r   get_python_filescreate_jobsetlenstarted_jobpathupdate_resourcefinished_job)r   r	   r   task_handlejob_setfiles         r"   generate_cachezAutoImport.generate_cacheg   s      	85577I++)3y>>
 
  	# 	#D 1DI =>>>  z222  """"	# 	#r$   c                    |                     dt          |                    }|D ]}|                    d|z             |                    d          rM| j                            |dd                   }|r(t          |          D ]}|                     ||           n|                     ||           |	                                 dS )z:Generate global name cache for modules listed in `modules`z'Generating autoimport cache for modulesrJ   z.*N)
rL   rM   rN   endswithr   find_module
submodulesrP   update_modulerQ   )r   modulesr   rR   rS   modnamemodsubs           r"   generate_modules_cachez!AutoImport.generate_modules_cache~   s     ++5s7||
 
  		# 		#G 1G ;<<<%% 8l..wss|<< >)# > >,,S*====""7J777  """"		# 		#r$   c                 8    | 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)r   clearr   s    r"   clear_cachezAutoImport.clear_cache   s     	
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 )z4Guess at what line the new import should be insertedz^(def|class)\s+N   __rope_testmodule_rope
r   )researchstartr   get_string_moduler   r   ModuleSyntaxErrorr   NormalImportget_module_imports
add_importget_changed_sourceindexcount)	r   codematchrD   testmodname
importinfomodule_importsoffsetrF   s	            r"   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#c                     t          j        t          j                  5  | j                            |          }|                     |          }|                     |||           ddd           dS # 1 swxY w Y   dS )z/Update the cache for global names in `resource`N)r=   r>   r   rm   r   get_pymodule_module_name
_add_names)r   rG   r   rD   r]   s        r"   rP   zAutoImport.update_resource   s     !=>> 	; 	;|00::H''11GOOHgz:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   AA33A7:A7c                     t          j        t          j                  5  | j                            |          }|                     |||           ddd           dS # 1 swxY w Y   dS )zjUpdate the cache for global names in `modname` module

        `modname` is the name of a module.
        N)r=   r>   r   r?   r   r@   r~   )r   r]   r   rD   s       r"   r[   zAutoImport.update_module   s    
  !?@@ 	; 	;|..w77HOOHgz:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   2AA"%A"c                 *    t          j        |          S r'   )r   r]   r   rG   s     r"   r}   zAutoImport._module_name   s    )))r$   c                    || j         }g }t          |t          j                  r|                                }n|                                }|                                D ]\  }}|s|                    d          rt          |t          j	        t          j
        f          r|                    |           t          |t          j                  r|                    |           || j        |<   d S )N_)r   
isinstancer   PyDefinedObject_get_structural_attributesget_attributesitemsr(   r   AssignedNameDefinedNamerC   r   BuiltinModuler   )r   rD   r]   r   globals
attributesr5   rE   s           r"   r~   zAutoImport._add_names   s     	)Jh	 9:: 	3!<<>>JJ!0022J&,,.. 	% 	%LD& $//#"6"6 &7#79L"MNN %t$$$(H$:;; %t$$$%
7r$   c                 P    | j         j                            d| j                   d S )Nr   )r   r   
write_datar   rc   s    r"   r   zAutoImport._write   s%    **=$*EEEEEr$   c                 \    |                                 s|                     |           d S d S r'   )	is_folderrP   r   s     r"   r   zAutoImport._changed   s:    !!## 	+  *****	+ 	+r$   c                     |                                 s=|                     |          }|| j        v r| j        |= |                     |           d S d S r'   )r   r}   r   rP   )r   rG   newresourcer]   s       r"   r   zAutoImport._moved   sf    !!## 	.''11G$*$ (Jw'  -----		. 	.r$   c                     |                                 s(|                     |          }|| j        v r| j        |= d S d S d S r'   )r   r}   r   )r   rG   r]   s      r"   r   zAutoImport._removed   s\    !!## 	(''11G$*$ (Jw'''	( 	(( (r$   )TFr'   )__name__
__module____qualname____doc__r#   r0   r7   r;   rH   r
   DEFAULT_TASK_HANDLErU   r`   rd   rz   rP   r[   r}   r~   r   r   r   r   r3   r$   r"   r   r      sT        + + + +*  "O O O    $ 2	# # # #0 #'J4R# # # #$    $; ; ; ;; ; ; ;* * *& & &"F F F+ + +. . .( ( ( ( (r$   r   c                 D   t          | t          j                  r6| j                            d          r| j        dk    r| hS t                      S |                     d          st                      S | h}|                                 D ]}|t          |          z  }|S )Nz.pyz__init__.py)	r   r	   Filer5   rX   r9   	has_childget_childrenrZ   )r^   r/   childs      r"   rZ   rZ      s    #y~&& 8U## 	M(A 	5Luu=='' uuUF!!## $ $*U###Mr$   )r   r=   ri   	rope.baser   r   r   r   r   r   r	   r
   rope.refactorr   r   rZ   r3   r$   r"   <module>r      s   
 
     					 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 & % % % % %C( C( C( C( C( C( C( C(L
 
 
 
 
r$   