o
    2hg/                     @   s   U d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ i Ze	ee
e f ed< dae Zd	d
 Zdd Zdd Zdd Zdd ZG dd dZG dd deZG dd dZdd ZdS )zThis module implements a post import hook mechanism styled after what is
described in PEP-369. Note that it doesn't cope with modules being reloaded.

    N)	find_spec)CallableDictList   )BaseObjectProxy_post_import_hooksFc                        fdd}|S )Nc                    sF     d\}}| d}t| tj| }|D ]}t||}q|| S )N:.)split
__import__sysmodulesgetattr)modulemodule_namefunctionattrscallbackattrname I/var/www/html/chatgem/venv/lib/python3.10/site-packages/wrapt/importer.pyimport_hook!   s   

z4_create_import_hook_from_string.<locals>.import_hookr   )r   r   r   r   r   _create_import_hook_from_string    s   	r   c                 C   s   t | tr	t| } t( tsdatjdt  tj	
|d}|du r,t|g |  W d   n1 s6w   Y  |durE| | dS dS )aU  
    Register a post import hook for the target module `name`. The `hook`
    function will be called once the module is imported and will be passed the
    module as argument. If the module is already imported, the `hook` will be
    called immediately. If you also want to defer loading of the module containing
    the `hook` function until required, you can specify the `hook` as a string in
    the form 'module:function'. This will result in a proxy hook function being
    registered which will defer loading of the specified module containing the
    callback function until required.
    Tr   N)
isinstancestrr   _post_import_hooks_lock_post_import_hooks_initr   	meta_pathinsertImportHookFinderr   getr   
setdefaultappend)hookr   r   r   r   r   register_post_import_hook-   s   
r(   c                    r	   )Nc                    s\    j d}|d }t| tj| }t|dkr*|d d}|D ]}t||}q"|| S )Nr
   r   r   r   )valuer   r   r   r   lenr   )r   entrypoint_valuer   r   r   r   
entrypointr   r   r   ^   s   
z8_create_import_hook_from_entrypoint.<locals>.import_hookr   )r-   r   r   r,   r   #_create_import_hook_from_entrypoint]   s   r.   c                 C   sZ   z	t jj| d}W n ty   t j | d}Y nw |D ]}| }t||j qdS )z
    Discover and register post import hooks defined as package entry points
    in the specified `group`. The group should be a string that matches the
    entry point group name used in the package metadata.
    )groupr   N)	importlibmetadataentry_points	TypeErrorr$   loadr(   r   )r/   entrypointsr-   r   r   r   r   discover_post_import_hooksm   s   r6   c                 C   sR   t | dd}t t|d}W d   n1 sw   Y  |D ]}||  q dS )z
    Notify that a `module` has been loaded and invoke any post import hooks
    registered against the module. If the module is not registered, this
    function does nothing.
    __name__Nr   )r   r   r   pop)r   r   hooksr'   r   r   r   notify_module_loaded   s   
r:   c                   @   s   e Zd Zdd ZdS )_ImportHookLoaderc                 C   s   t j| }t| |S N)r   r   r:   selffullnamer   r   r   r   load_module   s   
z_ImportHookLoader.load_moduleN)r7   
__module____qualname__r@   r   r   r   r   r;      s    r;   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )_ImportHookChainedLoaderc                    s`   t t| | t|dr| d| j t|dr | d| j t|dr.| d| j d S d S )Nr@   create_moduleexec_module)superrC   __init__hasattr__self_setattr___self_load_module_self_create_module_self_exec_module)r>   loader	__class__r   r   rG      s   


z!_ImportHookChainedLoader.__init__c                 C   sz   G dd d}t |d|d | fv r!z| j|_W n	 ty    Y nw t |dd d ur9t |jdd | u r;| j|j_d S d S d S )Nc                   @   s   e Zd ZdS )z<_ImportHookChainedLoader._self_set_loader.<locals>.UNDEFINEDN)r7   rA   rB   r   r   r   r   	UNDEFINED   s    rP   
__loader____spec__rM   )r   __wrapped__rQ   AttributeErrorrR   rM   )r>   r   rP   r   r   r   _self_set_loader   s   z)_ImportHookChainedLoader._self_set_loaderc                 C   s"   | j |}| | t| |S r<   )rS   r@   rU   r:   r=   r   r   r   rJ      s   
z*_ImportHookChainedLoader._self_load_modulec                 C   s   | j |S r<   )rS   rD   )r>   specr   r   r   rK      s   z,_ImportHookChainedLoader._self_create_modulec                 C   s"   |  | | j| t| d S r<   )rU   rS   rE   r:   )r>   r   r   r   r   rL      s   
z*_ImportHookChainedLoader._self_exec_module)	r7   rA   rB   rG   rU   rJ   rK   rL   __classcell__r   r   rN   r   rC      s    

rC   c                   @   s(   e Zd Zdd ZdddZd	ddZdS )
r#   c                 C   s
   i | _ d S r<   )in_progress)r>   r   r   r   rG      s   
zImportHookFinder.__init__Nc                 C   s   t  |tvr	 W d    d S W d    n1 sw   Y  || jv r&d S d| j|< z&tt|dd }|rDt|tsKt|W | j|= S W | j|= d S W | j|= d S | j|= w NTrM   )r   r   rX   r   r   r   rC   )r>   r?   pathrM   r   r   r   find_module   s$   

	
zImportHookFinder.find_modulec                 C   s   t  |tvr	 W d    d S W d    n1 sw   Y  || jv r&d S d| j|< zt|}t|dd }|rBt|tsBt||_|W | j|= S | j|= w rY   )r   r   rX   r   r   r   rC   rM   )r>   r?   rZ   targetrV   rM   r   r   r   r     s    	


zImportHookFinder.find_specr<   )NN)r7   rA   rB   rG   r[   r   r   r   r   r   r#      s    
(r#   c                    r	   )aU  
    Returns a decorator that registers the decorated function as a post import
    hook for the module specified by `name`. The function will be called once
    the module with the specified name is imported, and will be passed the
    module as argument. If the module is already imported, the function will
    be called immediately.
    c                    s   t |   | S r<   )r(   )r'   r   r   r   registerH  s   
zwhen_imported.<locals>.registerr   )r   r]   r   r   r   when_imported?  s   	r^   )__doc__importlib.metadatar0   r   	threadingimportlib.utilr   typingr   r   r   	__wrapt__r   r   r   __annotations__r    RLockr   r   r(   r.   r6   r:   r;   rC   r#   r^   r   r   r   r   <module>   s&    
0	:\