o
    2h'                     @   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vrd S t j|  S N)r   symbol_renames)name r   `/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/util/module_wrapper.pyget_rename_v2    s   

r   c                  C   sT   t  } | jjjdksJ dtdD ]}| j}|du r n|} q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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| sdS t| ds"d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#   init_decoratorsr   r   r   r   has_deprecation_decoratorA   s   


r,   c                       s|   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rIt| j | j_t| j | _nt| jdrU| jj| _ndd t| jD | j_| jj| _t | _d| _d S )Nz
.compat.v1__all__c                 S      g | ]	}| d s|qS r   
startswithr   attrr   r   r   
<listcomp>{       z,TFModuleWrapper.__init__.<locals>.<listcomp>r   )superr-   r%   __name__FastModuleTypeset_getattr_callback_getattr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 rC|| jvrC| j| | jrd| j|f }nd| }t|}|rCt|sCt }|dsCt	
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)rL   _PER_MODULE_WARNING_LIMITrK   addrA   r   r,   r   r2   loggingwarning)rM   r   r4   	full_namerename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 r,t|d }t	||d }nt|d }t
| j|| || j|< | || |S )zLazily loading the modules.appTr   r   )rF   r-   compat_v1_usage_recordedcompat_v1_usage_gaugeget_cellrJ   rB   	importlibimport_modulegetattrsetattrr@   r>   _fastdict_insert)rM   r   symbol_loc_infomoduler4   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s5|ds5|dr<||| |S | jrE| ||sJ||| |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.
    re   lite___tfmw_
_fastdict_)	object__getattribute__rD   rh   rd   r@   r2   rC   r   )rM   r   func__fastdict_insertr4   r   r   r   r=      s$   





zTFModuleWrapper._getattributec                 C   sX   zt | j|}W n ty    | js || jvr | |}Y nw | jr*| || |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.
    )rc   r@   AttributeErrorrB   rh   rC   r   )rM   r   r4   r   r   r   r;      s   
	zTFModuleWrapper._getattrc                    sl   | ds+t| j|| || j|< || jvr |dkr | j| | |r+| || tt	| 
|| d S )Nrk   r.   )r2   rd   r@   r>   r.   append_fastdict_key_inre   r7   r-   __setattr__)rM   argvalrS   r   r   rs      s   


zTFModuleWrapper.__setattr__c                 C   s<   | j rtt| j  tdd t| jD S t| jS )Nc                 S   r/   r0   r1   r3   r   r   r   r5     r6   z+TFModuleWrapper.__dir__.<locals>.<listcomp>)rB   rG   rJ   rH   unionrI   r@   rM   r   r   r   __dir__  s   
zTFModuleWrapper.__dir__c                    sZ   | drtt| | d S t| j| | j| || jv r&| j	| | 
| d S )Nrk   )r2   r7   r-   __delattr__delattrr@   r>   popr.   remove_fastdict_pop)rM   r   rS   r   r   ry     s   

zTFModuleWrapper.__delattr__c                 C   s
   | j  S r	   )r@   __repr__rw   r   r   r   r~     s   
zTFModuleWrapper.__repr__c                 C   s   t j| jffS r	   )ra   rb   r8   rw   r   r   r   
__reduce__  s   zTFModuleWrapper.__reduce__)NTF)r8   
__module____qualname____doc__r^   r%   r   rh   r=   r;   rs   rx   ry   r~   r   __classcell__r   r   rS   r   r-   Z   s     &(r-   )r   ra   tensorflow.python.eagerr   tensorflow.python.platformr   rX   tensorflow.python.utilr   r   r   tensorflow.tools.compatibilityr   get_fast_module_type_classr9   rV   	BoolGauger_   r   r   r$   r,   r-   r   r   r   r   <module>   s$   