B
    ѻd:'                 @   s   d Z ddl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 e Zd	Zed
dZdd Zdd Zdd Zdd ZG dd deZdS )z(Provides wrapper for TensorFlow modules.    N)
monitoring)
tf_logging)fast_module_type)tf_decorator)
tf_inspect)all_renames_v2   z/tensorflow/api/compat/v1zcompat.v1 usagec             C   s   | t jkrd S t j|  S )N)r   Zsymbol_renames)name r
   W/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/util/module_wrapper.pyget_rename_v2    s    
r   c              C   sV   t  } | jjjdkstdx$tdD ]}| j}|dkr<P |} q(W d| jj| j	S )z|Extracts the caller filename and line number as a string.

  Returns:
    A string describing the caller source location.
  _tfmw_add_deprecation_warningzThis function should be called directly from _tfmw_add_deprecation_warning, as the caller is identified heuristically by chopping off the top stack frames.   Nz{}:{})
r   currentframef_backf_codeco_nameAssertionErrorrangeformatco_filenamef_lineno)frame_parentr
   r
   r   _call_location&   s    r   c             C   s   t dd | D S )Nc             s   s   | ]}|j d kV  qdS )
deprecatedN)decorator_name).0dr
   r
   r   	<genexpr>>   s    z1contains_deprecation_decorator.<locals>.<genexpr>)any)
decoratorsr
   r
   r   contains_deprecation_decorator=   s    r#   c             C   s\   t | \}} t|rdS t| r(dS t| s6dS t| dsDdS t | j\}}t|S )a%  Checks if given object has a deprecation decorator.

  We check if deprecation decorator is in decorators as well as
  whether symbol is a class whose __init__ method has a deprecation
  decorator.
  Args:
    symbol: Python object.

  Returns:
    True if symbol has deprecation decorator.
  TF__init__)r   unwrapr#   r   
isfunctionisclasshasattrr$   )symbolr"   Zinit_decoratorsr   r
   r
   r   has_deprecation_decoratorA   s    


r*   c                   sv   e Zd ZdZdZd fdd	Zdd Zd	d
 Zdd Zdd Z	 fddZ
dd Z fddZdd Zdd Z  ZS )TFModuleWrapperzGWrapper for TF modules to support deprecation messages and lazyloading.FNTc                s   t t| |j t| tj t| tj | j	
|j	 || _|| _|| _|| _|| _|jd| _| jrt| j | j_t| j | _n:t| jdr| jj| _n"dd t| jD | j_| jj| _t | _d| _d S )Nz
.compat.v1__all__c             S   s   g | ]}| d s|qS )r   )
startswith)r   attrr
   r
   r   
<listcomp>|   s    z,TFModuleWrapper.__init__.<locals>.<listcomp>r   )superr+   r$   __name__FastModuleTypeZset_getattr_callback_getattrZset_getattribute_callback_getattribute__dict__update_tfmw_wrapped_module_tfmw_module_name_tfmw_public_apis _tfmw_print_deprecation_warnings_tfmw_has_liteendswith_tfmw_is_compat_v1listkeysr,   r(   dirset_tfmw_deprecated_checked_tfmw_warning_count)selfwrappedmodule_namepublic_apisdeprecationhas_lite)	__class__r
   r   r$   _   s(    
zTFModuleWrapper.__init__c             C   s   | j tk r|| jkr| j| | jr6d| j|f }nd| }t|}|rt|st }|dst	
dt || |  j d7  _ dS dS )z@Print deprecation warning for attr with given name if necessary.ztf.%s.%sztf.%s<z;From %s: The name %s is deprecated. Please use %s instead.
r   TF)rC   _PER_MODULE_WARNING_LIMITrB   addr8   r   r*   r   r-   loggingwarning)rD   r	   r.   	full_namerenameZcall_locationr
   r
   r   r      s     


z-TFModuleWrapper._tfmw_add_deprecation_warningc             C   s   | j r(|dkr(tjs(dt_t d | j| }|d rXt|d }t	||d }nt|d }t
| j|| || j|< | || |S )zLazily loading the modules.appTr   r   )r=   r+   compat_v1_usage_recordedcompat_v1_usage_gaugeget_cellrA   r9   	importlibimport_modulegetattrsetattrr7   r5   _fastdict_insert)rD   r	   Zsymbol_loc_infomoduler.   r
   r
   r   _tfmw_import_module   s    

z#TFModuleWrapper._tfmw_import_modulec             C   s   t | d}|dkr@| jr@| |}t| jd| ||| |S t | |}|dsj|dsj|drx||| |S | jr| ||s||| |S )zImports and caches pre-defined API.

    Warns if necessary.

    This method is a replacement for __getattribute__(). It will be added into
    the extended python module as a callback to reduce API overhead.
    rZ   lite___tfmw_Z
_fastdict_)	object__getattribute__r;   r\   rY   r7   r-   r:   r   )rD   r	   Zfunc__fastdict_insertr.   r
   r
   r   r4      s     




zTFModuleWrapper._getattributec             C   sZ   yt | j|}W n2 tk
rB   | js( || jkr4 | |}Y nX | jrV| || |S )a  Imports and caches pre-defined API.

    Warns if necessary.

    This method is a replacement for __getattr__(). It will be added into the
    extended python module as a callback to reduce API overhead. Instead of
    relying on implicit AttributeError handling, this added callback function
    will
    be called explicitly from the extended C API if the default attribute lookup
    fails.
    )rX   r7   AttributeErrorr9   r\   r:   r   )rD   r	   r.   r
   r
   r   r3      s    
zTFModuleWrapper._getattrc                sl   | dsVt| j|| || j|< || jkr@|dkr@| j| | |rV| || tt	| 
|| d S )Nr_   r,   )r-   rY   r7   r5   r,   appendZ_fastdict_key_inrZ   r0   r+   __setattr__)rD   argval)rJ   r
   r   rd      s    


zTFModuleWrapper.__setattr__c             C   s@   | j r2tt| j  tdd t| jD S t| jS d S )Nc             S   s   g | ]}| d s|qS )r   )r-   )r   r.   r
   r
   r   r/     s    z+TFModuleWrapper.__dir__.<locals>.<listcomp>)r9   r>   rA   r?   unionr@   r7   )rD   r
   r
   r   __dir__  s    zTFModuleWrapper.__dir__c                s,   | drtt| | nt| j| d S )Nr_   )r-   r0   r+   __delattr__delattrr7   )rD   r	   )rJ   r
   r   ri     s    
zTFModuleWrapper.__delattr__c             C   s
   | j  S )N)r7   __repr__)rD   r
   r
   r   rk     s    zTFModuleWrapper.__repr__c             C   s   t j| jffS )N)rV   rW   r1   )rD   r
   r
   r   
__reduce__  s    zTFModuleWrapper.__reduce__)NTF)r1   
__module____qualname____doc__rS   r$   r   r\   r4   r3   rd   rh   ri   rk   rl   __classcell__r
   r
   )rJ   r   r+   Z   s      (r+   )ro   rV   tensorflow.python.eagerr   tensorflow.python.platformr   rN   tensorflow.python.utilr   r   r   Ztensorflow.tools.compatibilityr   Zget_fast_module_type_classr2   rL   	BoolGaugerT   r   r   r#   r*   r+   r
   r
   r
   r   <module>   s    