U
    nuf                     @   s   d dl 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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 d dlmZ d dlmZ d dlmZ G dd deZG dd dZG dd deZG dd deeZdS )    N)Path)Optional)inference_state_method_cache)AbstractNameDefinition
ModuleName)GlobalNameFilterParserTreeFilter
DictFilterMergedFilter)compiled)	TreeValue)SubModuleName)values_from_qualified_names)create_simple_object)ValueSetModuleContextc                   @   s&   e Zd ZdZdZdddZdd ZdS )	_ModuleAttributeNamezA
    For module attributes like __file__, __str__ and so on.
    instanceNc                 C   s   || _ || _|| _d S N)parent_contextstring_name_string_value)selfparent_moduler   Zstring_value r   ?/tmp/pip-unpacked-wheel-ol4ehs9f/jedi/inference/value/module.py__init__   s    z_ModuleAttributeName.__init__c                 C   s2   | j d k	r$| j }tt| jj|gS t| jjS r   )r   r   r   r   inference_stater   Zget_string_value_set)r   sr   r   r   infer   s    
z_ModuleAttributeName.infer)N)__name__
__module____qualname____doc__api_typer   r    r   r   r   r   r      s   
r   c                   @   s   e Zd Ze dd ZdS )SubModuleDictMixinc                 C   s>   i }|   r:| jj|  }|D ]}t|  |||< q"|S )zf
        Lists modules in the directory of this module (if this module is a
        package).
        )
is_packager   Zcompiled_subprocessZiter_module_names
py__path__r   
as_context)r   namesZmodsnamer   r   r   sub_modules_dict&   s    z#SubModuleDictMixin.sub_modules_dictN)r!   r"   r#   r   r,   r   r   r   r   r&   %   s   r&   c                   @   sr   e Zd ZeZdddZdd Zdd Zdd	 Ze	e
 d
d Ze
 dd Zdd Ze
g dd Zdd ZdS )ModuleMixinNc                 c   sN   t t|  |dt|  V  t|  V  t|  V  |  E d H  d S )N)r   origin_scope)r
   r   r)   r   r	   r,   _module_attributes_dictiter_star_filters)r   r.   r   r   r   get_filters=   s    
zModuleMixin.get_filtersc                 C   s   t | jdd\}|S )Ntypes
ModuleType)r   r   )r   cr   r   r   py__class__I   s    zModuleMixin.py__class__c                 C   s   dS )NTr   r   r   r   r   	is_moduleM   s    zModuleMixin.is_modulec                 C   s   dS )NFr   r6   r   r   r   is_stubP   s    zModuleMixin.is_stubc                 C   s   |  | | jd S N)_module_name_classstring_namesr6   r   r   r   r+   S   s    zModuleMixin.namec                    sH   dddg}t  fdd|D }  }|d k	rDt dt||d< |S )N__package__r$   r!   c                 3   s   | ]}|t  |fV  qd S r   )r   ).0nr6   r   r   	<genexpr>\   s     z6ModuleMixin._module_attributes_dict.<locals>.<genexpr>__file__)dict
py__file__r   str)r   r*   dctpathr   r6   r   r/   X   s    
z#ModuleMixin._module_attributes_dictc                 c   s2   |   D ]$}t| d }|d k	s&t|V  qd S r   )star_importsnextr1   AssertionError)r   Zstar_modulefr   r   r   r0   b   s    zModuleMixin.iter_star_filtersc                 C   s|   ddl m} g }|  }| j D ]T}| r"|| j| d ||jd	 }|D ]}t
|trR|| 7 }qR||7 }q"|S )Nr   )Importerr:   )Zimport_pathmodule_contextlevel)Zjedi.inference.importsrK   r)   	tree_nodeZiter_importsZis_star_importr   	get_pathsrM   follow
isinstanceModuleValuerG   )r   rK   modulesrL   inewmoduler   r   r   rG   k   s     



zModuleMixin.star_importsc                 C   s   dS )z
        A module doesn't have a qualified name, but it's important to note that
        it's reachable and not `None`. With this information we can add
        qualified names on top for all value children.
        r   r   r6   r   r   r   get_qualified_names   s    zModuleMixin.get_qualified_names)N)r!   r"   r#   r   r;   r1   r5   r7   r8   propertyr   r+   r/   r0   rG   rW   r   r   r   r   r-   :   s   

		
r-   c                       sp   e Zd ZdZd fdd	Z fddZdd	 Zee d
ddZ	dd Z
dd Zdd Zdd Zdd Z  ZS )rR   rV   NFc                    sF   t  j|d |d || _|d kr(d | _n|j| _|| _|| _|| _d S )N)r   rN   )superr   file_io_pathrF   r<   
code_lines_is_package)r   r   Zmodule_noder\   rZ   r<   r'   	__class__r   r   r      s    zModuleValue.__init__c                    s$   | j d k	r| j jdkrdS t  S )Nz.pyiT)r[   suffixrY   r8   r6   r^   r   r   r8      s    zModuleValue.is_stubc                 C   s   | j d krd S d| j S )N.)r<   joinr6   r   r   r   
py__name__   s    
zModuleValue.py__name__)returnc                 C   s   | j dkrdS | j  S )z?
        In contrast to Python's __file__ can be None.
        N)r[   absoluter6   r   r   r   rC      s    
zModuleValue.py__file__c                 C   s   | j S r   )r]   r6   r   r   r   r'      s    zModuleValue.is_packagec                 C   s(   | j d krg S | jr| j S | j d d S r9   )r<   r]   r6   r   r   r   py__package__   s
    
zModuleValue.py__package__c                 C   s   | j s
dS d| jdd }d}|d |ks:|d |krt }| j D ],}tj|| jj	}tj
|rJ|| qJ|rt|S |  }|dk	sttj|gS )z
        In case of a package, this returns Python's __path__ attribute, which
        is a list of paths (strings).
        Returns None if the module is not a package.
        N 2   )zdeclare_namespace(__name__)zextend_path(__path__r      )r]   rb   r\   setr   Zget_sys_pathosrF   r+   r   isdiraddlistrC   rI   dirname)r   Zfirst_few_linesoptionspathsr   otherfiler   r   r   r(      s    zModuleValue.py__path__c                 C   s   t | S r   r   r6   r   r   r   _as_context   s    zModuleValue._as_contextc                 C   s.   d| j j|  | jjd | jjd |  f S )Nz<%s: %s@%s-%s is_stub=%s>r   )r_   r!   rc   rN   Z	start_posZend_posr8   r6   r   r   r   __repr__   s     
 
zModuleValue.__repr__)NNF)r!   r"   r#   r%   r   r8   rc   r   r   rC   r'   rf   r(   rt   ru   __classcell__r   r   r^   r   rR      s       	!rR   )rk   pathlibr   typingr   Zjedi.inference.cacher   Zjedi.inference.namesr   r   Zjedi.inference.filtersr   r   r	   r
   Zjedi.inferencer   Zjedi.inference.base_valuer   r   Zjedi.inference.helpersr   Zjedi.inference.compiledr   r   Zjedi.inference.contextr   r   r&   r-   rR   r   r   r   r   <module>   s    O