B
    \'                 @   sb   d Z ddlZddlZddlmZ ddlmZ G dd deZG dd	 d	e	Z
G d
d dejZdS )z%A shim module for deprecated imports
    N)import_module   )import_itemc               @   s   e Zd ZdZdS )ShimWarningzFA warning to show when a module has moved, and a shim is in its place.N)__name__
__module____qualname____doc__ r
   r
   7lib/python3.7/site-packages/IPython/utils/shimmodule.pyr      s   r   c               @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )ShimImporterzImport hook for a shim.
    
    This ensures that submodule imports return the real target module,
    not a clone that will confuse `is` and `isinstance` checks.
    c             C   s   || _ || _d S )N)srcmirror)selfr   r   r
   r
   r   __init__   s    zShimImporter.__init__c             C   s   | j |t| jd  S )z#get the name of the mirrored moduleN)r   lenr   )r   fullnamer
   r
   r   _mirror_name   s    zShimImporter._mirror_nameNc             C   sT   | | jd rP| |}yt|}W n tk
r:   dS X t|tjsLdS | S dS )z6Return self if we should be used to import the module..N)
startswithr   r   r   ImportError
isinstancetypes
ModuleType)r   r   pathmirror_namemodr
   r
   r   find_module   s    
zShimImporter.find_modulec             C   s    |  |}t|}|tj|< |S )z:Import the mirrored module, and insert it into sys.modules)r   r   sysmodules)r   r   r   r   r
   r
   r   load_module-   s    

zShimImporter.load_module)N)r   r   r   r	   r   r   r   r    r
   r
   r
   r   r      s
   
r   c                   sP   e Zd Z fddZedd Zedd Zdd Zed	d
 Zdd Z	  Z
S )
ShimModulec                s`   | d| _| dd }|r0|ddd |d< tt| j|| |r\tjt	|| jd d S )Nr   r   r   r   name)r   r   )
pop_mirrorrsplitsuperr!   r   r   	meta_pathappendr   )r   argskwargsr   )	__class__r
   r   r   7   s    zShimModule.__init__c             C   s   g S )Nr
   )r   r
   r
   r   __path__C   s    zShimModule.__path__c             C   s   t | jjS )z&Don't produce __spec__ until requested)r   r%   __spec__)r   r
   r
   r   r.   G   s    zShimModule.__spec__c             C   s   t t| jS )N)dirr   r%   )r   r
   r
   r   __dir__L   s    zShimModule.__dir__c             C   s8   t | j}y|jS  tk
r2   dd t|D S X dS )z Ensure __all__ is always definedc             S   s   g | ]}| d s|qS )_)r   ).0r#   r
   r
   r   
<listcomp>V   s    z&ShimModule.__all__.<locals>.<listcomp>N)r   r%   __all__AttributeErrorr/   )r   r   r
   r
   r   r4   O   s
    
zShimModule.__all__c             C   s8   d| j |f }yt|S  tk
r2   t|Y nX d S )Nz%s.%s)r%   r   r   r5   )r   keyr#   r
   r
   r   __getattr__X   s
    zShimModule.__getattr__)r   r   r   r   propertyr-   r.   r0   r4   r7   __classcell__r
   r
   )r,   r   r!   5   s   	r!   )r	   r   r   	importlibr   Zimportstringr   Warningr   objectr   r   r!   r
   r
   r
   r   <module>   s   %