a
    `*jK-                     @   s   d Z ddlZddlZddlmZ ddlmZ dd Zdd	 ZdddZ	G dd dZ
dddZdd Zdd ZdddZdd ZdS )z=Utilities for monkey patching and wrapping object attributes.    N   FunctionWrapper)register_post_import_hookc                 C   sn   t | trt|  tj|  } | }|d}|d }dd }|||}|dd D ]}|}|||}qP|||fS )a  
    Resolves the dotted path supplied as `name` to an attribute on a target
    object. The `target` can be a module, class, or instance of a class. If the
    `target` argument is a string, it is assumed to be the name of a module,
    which will be imported if necessary and then used as the target object.
    Returns a tuple containing the parent object holding the attribute lookup
    resolved to, the attribute name (path prefix removed if present), and the
    original attribute value.
    .r   c                 S   sN   t | r@t | D ] }|t|v rt||   S qt| |S t| |S d S N)inspectisclassgetmrovarsgetattr)parent	attributecls r   I/var/www/html/assistant/venv/lib/python3.9/site-packages/wrapt/patches.pylookup_attribute-   s    

z&resolve_path.<locals>.lookup_attributer   N)
isinstancestr
__import__sysmodulessplit)targetnamer   pathr   r   originalr   r   r   resolve_path   s    




r   c                 C   s   t | || dS )zv
    Convenience function for applying a patch to an attribute. This maps to
    the standard setattr() function.
    N)setattr)r   r   replacementr   r   r   apply_patch@   s    r    r   c           	      C   sB   |du ri }t | |\}}}||g|R i |}t||| |S )a  
    Wraps an object which is the attribute of a target object with a wrapper
    object created by the `factory` function. The `target` can be a module,
    class, or instance of a class. In the special case of `target` being a
    string, it is assumed to be the name of a module, with the module being
    imported if necessary and then used as the target object. The `name` is a
    string representing the dotted path to the attribute. The `factory` function
    should accept the original object and may accept additional positional and
    keyword arguments which will be set by unpacking input arguments using
    `*args` and `**kwargs` calling conventions. The factory function should
    return a new object that will replace the original object.
    N)r   r    )	r   r   factoryargskwargsr   r   r   wrapperr   r   r   wrap_objectI   s    r%   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )AttributeWrapperz\A descriptor that intercepts access to an instance attribute to apply
    a wrapper factory.c                 C   s   || _ || _|| _|| _d S r   )r   r!   r"   r#   )selfr   r!   r"   r#   r   r   r   __init__l   s    zAttributeWrapper.__init__c                 C   s(   |j | j }| j|g| jR i | jS r   )__dict__r   r!   r"   r#   )r'   instanceownervaluer   r   r   __get__r   s    zAttributeWrapper.__get__c                 C   s   ||j | j< d S r   r)   r   )r'   r*   r,   r   r   r   __set__v   s    zAttributeWrapper.__set__c                 C   s   |j | j= d S r   r.   )r'   r*   r   r   r   
__delete__y   s    zAttributeWrapper.__delete__N)__name__
__module____qualname____doc__r(   r-   r/   r0   r   r   r   r   r&   h   s
   r&   c           	      C   sH   |du ri }| dd\}}t| |d }t||||}t||| |S )a|  
    Wraps an object which is the attribute of a class instance with a wrapper
    object created by the `factory` function. It does this by patching the
    class, not the instance, with a descriptor that intercepts access to the
    instance attribute. The `module` can be a module, class, or instance of a
    class. In the special case of `module` being a string, it is assumed to be
    the name of a module, with the module being imported if necessary and then
    used as the target object. The `name` is a string representing the dotted
    path to the attribute. The `factory` function should accept the original
    object and may accept additional positional and keyword arguments which will
    be set by unpacking input arguments using `*args` and `**kwargs` calling
    conventions. The factory function should return a new object that will
    replace the original object.
    Nr   r      )rsplitr   r&   r    )	moduler   r!   r"   r#   r   r   r   r$   r   r   r   wrap_object_attribute}   s    r8   c                    s    fdd}t  |S )a  
    Creates a decorator for wrapping a function with a `wrapper` function.
    The decorator which is returned may also be applied to any other callable
    objects such as lambda functions, methods, classmethods, and staticmethods,
    or objects which implement the `__call__()` method. The `wrapper` function
    should accept the `wrapped` function, `instance`, `args`, and `kwargs`,
    arguments and return the result of calling the wrapped function or some
    other appropriate value.
    c                    sH   |d }|d u r }n(t |r. d |}n |t|}t||S )Nr   r   r	   r-   typer   )wrappedr*   r"   r#   target_wrappedtarget_wrapperr$   r   r   _wrapper   s    
z"function_wrapper.<locals>._wrapperr   r$   r?   r   r>   r   function_wrapper   s    
rA   c                    sl   t | tr\| dr\| dd } | tjv r@ttj|   tfS  fdd}t||  dS t|  tfS )a  
    Wraps a function which is the attribute of a target object with a `wrapper`
    function. The `target` can be a module, class, or instance of a class. In
    the special case of `target` being a string, it is assumed to be the name
    of a module, with the module being imported if necessary. If the `target`
    is a string with a trailing ``?``, the wrapping will be deferred until the
    module is imported. If the module is already imported, the wrapping will be
    applied immediately. The `name` is a string representing the dotted path to
    the attribute. The `wrapper` function should accept the `wrapped` function,
    `instance`, `args`, and `kwargs` arguments, and would return the result of
    calling the wrapped attribute or some other appropriate value. Returns the
    wrapped target function if the wrapping was applied immediately, or ``None``
    if the wrapping was deferred.
    ?Nc                    s   t |  tf d S r   r%   r   r7   r   r$   r   r   callback   s    z'wrap_function_wrapper.<locals>.callbackr   r   endswithr   r   r%   r   r   )r   r   r$   rG   r   rF   r   wrap_function_wrapper   s    

rJ   c                    s    fdd}|S )a0  
    Creates a decorator which can be applied to a wrapper function, where the
    wrapper function will be used to wrap a function which is the attribute of
    a target object. The decorator returns the original wrapper function. The
    `target` can be a module, class, or instance of a class. In the special case
    of `target` being a string, it is assumed to be the name of a module, with
    the module being imported if necessary. If the `target` is a string with a
    trailing ``?``, the wrapping will be deferred until the module is imported.
    If the module is already imported, the wrapping will be applied immediately.
    The `name` is a string representing the dotted path to the attribute. The
    `enabled` argument can be a boolean or a callable that returns a boolean.
    When a callable is provided, it will be called each time the wrapper is
    invoked to determine if the wrapper function should be executed or whether
    the wrapped function should be called directly. If `enabled` is not
    provided, the wrapper is enabled by default.
    c                    sz   t trddrdd d }|tjv rFttj| t f  S  fdd}t||  S tt f  S )NrB   rC   c                    s   t | t f d S r   rD   rE   )enabledr   r$   r   r   rG      s    z:patch_function_wrapper.<locals>._wrapper.<locals>.callbackrH   )r$   _targetrG   rK   r   r   r>   r   r?      s    

z(patch_function_wrapper.<locals>._wrapperr   )r   r   rK   r?   r   rM   r   patch_function_wrapper   s    rN   c                    s    fdd}|S )a  Creates a decorator that patches a target function with a wrapper
    function, but only for the duration of the call that the decorator was
    applied to. The `target` can be a module, class, or instance of a class.
    In the special case of `target` being a string, it is assumed to be the name
    of a module, with the module being imported if necessary. The `name` is a
    string representing the dotted path to the attribute.
    c                    s    fdd}t  |S )Nc                    sX   |d }|d u r n(t |r.d | n|t|  fdd}t||S )Nr   c              
      sV   t  \}}}t|}t||| z| |i |W t||| S t||| 0 d S r   )r   r   r   )r;   r*   r"   r#   r   r   r   r   )r   r   r=   r   r   _execute  s    
zRtransient_function_wrapper.<locals>._decorator.<locals>._wrapper.<locals>._executer9   )r;   r*   r"   r#   r<   rO   )r   r   r$   )r=   r   r?     s    
	z@transient_function_wrapper.<locals>._decorator.<locals>._wrapperr   r@   r   r   r>   r   
_decorator  s    z.transient_function_wrapper.<locals>._decoratorr   )r   r   rQ   r   rP   r   transient_function_wrapper   s    	rR   )r   N)r   N)N)r4   r   r   Z	__wrapt__r   importerr   r   r    r%   r&   r8   rA   rJ   rN   rR   r   r   r   r   <module>   s   4	

 
(