
    Æe                     b   d 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 ddl	m
Z
 ddlmZmZmZmZmZmZmZmZ  ej(                  e      Zde
j.                  d	efd
Z	 	 	 ddej2                  dededed	eeddf   f
dZd	efdZdeded	ee   fdZ	 ddededed	eeddf   fdZdededed	efdZ y)zz
Functions to find importable names.

Can extract names from source code of a python file, .so object, or builtin module.
    N)import_module)	GeneratorList)ast   )ModuleCompiled
ModuleFile
ModuleInfoNameNameTypePackagePartialNameSourcenodereturnc                    t        | t        j                        rt        j                  S t        | t        j
                        rt        j                  S t        | t        j                        rt        j                  S t        j                  S )zGet the lsp type of a node.)	
isinstancer   ClassDefr   ClassFunctionDefFunctionAssignVariable)r   s    ?/usr/lib/python3/dist-packages/rope/contrib/autoimport/parse.pyget_type_astr      sZ    $%~~$(   $

#       modulepackage_name
underlinedprocess_importsc              #     K   	 t        j                  | j                               }t        j
                  |      D ]  }t        |t         j                        rm|j                  D ]]  }	 t        |t         j                        sJ |s|j                  j                  d      s!t        |j                  t        |             _ t        |t         j                   t         j"                  f      r@|s|j$                  j                  d      rt        |j$                  t        |             |st        |t         j&                        s|j(                  dk(  r%|j*                  r ||j*                  j-                  d      d   u rS|j.                  D ]o  }t        |t         j0                        r&|j2                  r|j2                  }	n|j$                  }	n|}	|s|	j                  d      rYt        |	t        |             q  y# t        $ r}t	        |       Y d}~yd}~ww xY w# t        t        f$ r Y w xY ww)z.Get all the names from a given file using ast.N_r   .)r   parse
read_bytesSyntaxErrorprintiter_child_nodesr   r   targetsr   id
startswithr   r   AttributeErrorAssertionErrorr   r   name
ImportFromlevelr   splitnamesaliasasname)
r   r   r   r    	root_nodeerrorr   targetr.   	real_names
             r   get_names_from_filer9   (   s    IIf//12	 $$Y/ "EdCJJ',, 
	%fchh777!)=)=c)B)"II(. 	
 s=>!5!5c!:!II &  D#..!AzzQ;;,$++2C2CC2H2K"K

 	EdCII.{{$(KK	$(II	 $IY%9%9#%>%id1CDD	E3"E  e '7 sm   I#H AI*AH2A
I$I4IB#I4I	H/H*%I*H//I2IIIIc                     t        j                  |       rt        j                  S t        j                  |       st        j
                  |       rt        j                  S t        j                  S )z Determine the type of an object.)inspectisclassr   r   
isfunction	isbuiltinr   r   )imported_objects    r   get_type_objectr@   Y   sK    '~~/*g.?.?.P   r   packagec                 X   t        | t              r4t        t        |j                  |j
                  | j                              S t        | t              rPt        | j                  |j                  | j                  | j                        D cg c]  }t        || |       c}S g S c c}w )z(Get all names from a module and package.)r   r    )r   r   listget_names_from_compiledr.   sourcer   r	   r9   filepathr    combine)r   rA   partial_names      r   	get_namesrI   b   s    &.)#GLL'..&BSBST
 	
 &*% !4!,, & 6 6	!
 GV\2
 	
 I
s   B'rE   c           
   #   *  K   ddg}| |v s| j                  d      r|sy|t        j                  t        j                  fvry	 t	        t        |             }t        j                  |      D ]|  \  }}|s|j                  d      rt        j                  |      s+t        j                  |      st        j                  |      sZt        t        |      | | |t        |             ~ y# t        $ r t        j                  |  d       Y yw xY ww)z
    Get the names from a compiled module.

    Instead of using ast, it imports the module.
    Parameters
    ----------
    package : str
        package to import. Must be in sys.path
    underlined : bool
        include underlined names
    builtinspython_crunr"   Nz. could not be imported for autoimport analysis)r+   r   BUILTINSTANDARDr   strr;   
getmembersr<   r=   r>   r   r@   ImportErrorloggerr6   )rA   rE   r   bannedr   r.   values          r   rD   rD   u   s     $ -(F&W//4Zfnnfoo66s7|,
 #--f5 		KD%!5OOE*))%0((/D	7GV_U=S 			  y NOPs6   A DC, .D?D&D,!DDDDr.   c                     t        |j                  |j                  | j                  | j                  |j                        S )z(Combine information to form a full name.)r   r.   modnamerE   	name_type)rA   r   r.   s      r   rG   rG      s)    		6>>7<<XXr   ) FF)F)!__doc__r;   loggingpathlib	importlibr   typingr   r   	rope.baser   defsr   r	   r
   r   r   r   r   r   	getLogger__name__rR   ASTr   PathrO   boolr9   r@   rI   rD   rG    r   r   <module>rf      s8      # " 	 	 	 
		8	$sww 8  !	.ELL.E.E .E 	.E
 {D$&'.Eb j 7 tDz , &&& & tT4 	&RYW Yj Y Y Yr   