B
    7r\p                 @   s   d dl Z d dl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mZmZ d dlmZ d dlmZ d dlmZmZ G d	d
 d
eZG dd deeZG dd deZdS )    N)python_bytes_to_unicode)evaluator_method_cache)iter_modulesall_suffixes)GlobalNameFilterContextNameMixinAbstractNameDefinitionParserTreeFilter
DictFilterMergedFilter)compiled)TreeContext)SubModuleNameinfer_importc               @   s$   e Zd ZdZdZdd Zdd ZdS )_ModuleAttributeNamezA
    For module attributes like __file__, __str__ and so on.
    instancec             C   s   || _ || _d S )N)parent_contextstring_name)selfparent_moduler    r   ;lib/python3.7/site-packages/jedi/evaluate/context/module.py__init__   s    z_ModuleAttributeName.__init__c             C   s   t | jjS )N)r   Zget_string_context_setr   	evaluator)r   r   r   r   infer   s    z_ModuleAttributeName.inferN)__name__
__module____qualname____doc__api_typer   r   r   r   r   r   r      s   r   c               @   s$   e Zd ZdZdd Zedd ZdS )
ModuleName)   r   c             C   s   || _ || _d S )N)Z_context_name)r   contextnamer   r   r   r       s    zModuleName.__init__c             C   s   | j S )N)r"   )r   r   r   r   r   $   s    zModuleName.string_nameN)r   r   r   	start_posr   propertyr   r   r   r   r   r       s   r    c                   s   e Zd ZdZdZ fddZd!ddZeg dd Ze d	d
 Z	e
dd Ze
e dd Zdd Zdd Zdd Zdd Zdd Ze
dd Ze dd Zdd Zdd  Z  ZS )"ModuleContextmoduleNc                s&   t t| j|d |d || _|| _d S )N)r   	tree_node)superr'   r   _path
code_lines)r   r   Zmodule_nodepathr,   )	__class__r   r   r   -   s    
zModuleContext.__init__c             c   sd   t t| j| ||dt| | jV  t|  V  t|  V  x |  D ]}t	|
|V  qHW d S )N)r#   until_positionorigin_scope)r   r	   r   r   r)   r
   _sub_modules_dict_module_attributes_dictstar_importsnextget_filters)r   Zsearch_globalr/   r0   Zstar_moduler   r   r   r5   6   s    zModuleContext.get_filtersc             C   sj   g }x`| j  D ]R}| r| d d }t| |}x"|D ]}t|tr<|| 7 }q<W ||7 }qW |S )N)r)   Ziter_importsZis_star_importZ	get_pathsr   
isinstancer'   r3   )r   modulesir$   newr(   r   r   r   r3   H   s    


zModuleContext.star_importsc                s"   ddddg}t  fdd|D S )N__file____package__r   r   c             3   s   | ]}|t  |fV  qd S )N)r   ).0n)r   r   r   	<genexpr>Y   s    z8ModuleContext._module_attributes_dict.<locals>.<genexpr>)dict)r   namesr   )r   r   r2   U   s    z%ModuleContext._module_attributes_dictc             C   sL   | j dkrdS ttjjfd }td| | j }tdd|dS dS )z& This is used for the goto functions. N    z#([^%s]*?)(%s__init__)?(\.py|\.so)?$z\.[a-z]+-\d{2}[mud]{0,3}$r!   )	r+   reescapeosr-   sepsearchsubgroup)r   rG   rr   r   r   _string_name[   s
    
zModuleContext._string_namec             C   s   t | | jS )N)r    rL   )r   r   r   r   r$   f   s    zModuleContext.namec             C   sP   xJt  D ]@}d| }|  }|dk	r||r|  dt| d  S qW dS )zt
        :return: The path to the directory of a package. None in case it's not
                 a package.
        r   Nr!   )r   
py__file__endswithlen)r   suffixZendingrM   r   r   r   _get_init_directoryk   s    z!ModuleContext._get_init_directoryc             C   s2   x,| j j D ]\}}|| kr|dkr|S qW dS )NrB   __main__)r   Zmodule_cacheZiterate_modules_with_names)r   r$   r(   r   r   r   
py__name__x   s    zModuleContext.py__name__c             C   s   | j dkrdS tj| j S )z?
        In contrast to Python's __file__ can be None.
        N)r+   rF   r-   abspath)r   r   r   r   rM      s    
zModuleContext.py__file__c             C   s*   |   d krtdd|  S |  S d S )Nz	\.?[^.]+$rB   )rQ   rD   rI   rS   )r   r   r   r   py__package__   s    zModuleContext.py__package__c       	   	   C   s   | j  }|  }tj|dkrt|dz}t| dd}d}|d |ksZ|d |krt	 }x4|D ],}tj
|| jj}tj|rf|| qfW |rt|S W d Q R X |  gS )Nz__init__.pyrbreplace)errors)zdeclare_namespace(__name__)zextend_path(__path__r   r!   )r   Zget_sys_pathrM   rF   r-   basenameopenr   readsetjoinr$   r   isdiraddlistrQ   )	r   search_pathZ	init_pathfZcontentZoptionspathssotherr   r   r   _py__path__   s    

zModuleContext._py__path__c             C   s$   |   }|dkrtdn| jS dS )aS  
        Not seen here, since it's a property. The callback actually uses a
        variable, so use it like::

            foo.py__path__(sys_path)

        In case of a package, this returns Python's __path__ attribute, which
        is a list of paths (strings).
        Raises an AttributeError if the module is not a package.
        Nz'Only packages have __path__ attributes.)rQ   AttributeErrorrf   )r   r-   r   r   r   
py__path__   s    
zModuleContext.py__path__c             C   sd   i }y
| j }W n tk
r"   Y n>X x:| D ]0}t|g}x |D ]\}}}t| |||< q@W q,W |S )zf
        Lists modules in the directory of this module (if this module is a
        package).
        )rh   rg   r   r   )r   rA   methodr-   ZmodsZmodule_loaderr$   Zis_pkgr   r   r   r1      s    

	zModuleContext._sub_modules_dictc             C   s   t | jdS )NZMODULE_CLASS)r   Zget_special_objectr   )r   r   r   r   py__class__   s    zModuleContext.py__class__c             C   s&   d| j j| j| jjd | jjd f S )Nz<%s: %s@%s-%s>r   )r.   r   rL   r)   r%   Zend_pos)r   r   r   r   __repr__   s    
zModuleContext.__repr__)NN)r   r   r   r   r   r   r5   r   r3   r2   r&   rL   r$   rQ   rS   rM   rU   rf   rh   r1   rj   rk   __classcell__r   r   )r.   r   r'   )   s$   	
	r'   )rD   rF   Zparsor   Zjedi.evaluate.cacher   Zjedi._compatibilityr   r   Zjedi.evaluate.filtersr   r   r   r	   r
   r   Zjedi.evaluater   Zjedi.evaluate.base_contextr   Zjedi.evaluate.importsr   r   r   r    r'   r   r   r   r   <module>   s    