o
    h                     @  s   U d Z ddlmZ ddlZddlZddlmZmZmZ ddl	Z	ddlm
Z
mZmZ dZeeZi Zded	< d+ddZd,ddZd-ddZd.ddZd/d d!Zd0d#d$Zd1d&d'Zejd2d)d*ZdS )3aX  
Primitives to deal with a concurrency supporting context, as introduced in
Python 3.7 as :mod:`contextvars`.

.. versionadded:: 20.1.0
.. versionchanged:: 21.1.0
   Reimplemented without using a single dict as context carrier for improved
   isolation. Every key-value pair is a separate `contextvars.ContextVar` now.

See :doc:`contextvars`.
    )annotationsN)Any	GeneratorMapping   )BindableLogger	EventDictWrappedLogger
structlog_z&dict[str, contextvars.ContextVar[Any]]_CONTEXT_VARSreturndict[str, Any]c                  C  sH   i } t  }|D ]}|jtr!|| tur!|| | |jtd < q| S )zh
    Return a copy of the *structlog*-specific context-local context.

    .. versionadded:: 21.2.0
    N)contextvarscopy_contextname
startswithSTRUCTLOG_KEY_PREFIXEllipsisSTRUCTLOG_KEY_PREFIX_LEN)rvctxk r   K/var/www/html/ai/venv/lib/python3.10/site-packages/structlog/contextvars.pyget_contextvars'   s   r   bound_loggerr   c                 C  s   t  }|t|  |S )z
    Return a copy of the current context-local context merged with the context
    from *bound_logger*.

    .. versionadded:: 21.2.0
    )r   update	structlogget_context)r   r   r   r   r   get_merged_contextvars7   s   r   loggerr	   method_namestr
event_dictr   c                 C  sH   t  }|D ]}|jtr!|| tur!||jtd ||  q|S )a  
    A processor that merges in a global (context-local) context.

    Use this as your first processor in :func:`structlog.configure` to ensure
    context-local context is included in all log calls.

    .. versionadded:: 20.1.0
    .. versionchanged:: 21.1.0 See toplevel note.
    N)r   r   r   r   r   r   
setdefaultr   )r    r!   r#   r   r   r   r   r   merge_contextvarsD   s   r%   Nonec                  C  s,   t  } | D ]}|jtr|t qdS )z
    Clear the context-local context.

    The typical use-case for this function is to invoke it early in request-
    handling code.

    .. versionadded:: 20.1.0
    .. versionchanged:: 21.1.0 See toplevel note.
    N)r   r   r   r   r   setr   )r   r   r   r   r   clear_contextvarsY   s   

r(   kwr   $Mapping[str, contextvars.Token[Any]]c               	   K  sj   i }|   D ],\}}t | }zt| }W n ty*   tj|td}|t|< Y nw ||||< q|S )a  
    Put keys and values into the context-local context.

    Use this instead of :func:`~structlog.BoundLogger.bind` when you want some
    context to be global (context-local).

    Return the mapping of `contextvars.Token`\s resulting
    from setting the backing :class:`~contextvars.ContextVar`\s.
    Suitable for passing to :func:`reset_contextvars`.

    .. versionadded:: 20.1.0
    .. versionchanged:: 21.1.0 Return the `contextvars.Token` mapping
        rather than None. See also the toplevel note.
    )default)itemsr   r   KeyErrorr   
ContextVarr   r'   )r)   r   r   vstructlog_kvarr   r   r   bind_contextvarsi   s   r2   contextvars.Token[Any]c                  K  s4   |   D ]\}}t | }t| }|| qdS )z\
    Reset contextvars corresponding to the given Tokens.

    .. versionadded:: 21.1.0
    N)r,   r   r   reset)r)   r   r/   r0   r1   r   r   r   reset_contextvars   s
   r5   keysc                  G  s0   | D ]}t  | }|tv rt| t qdS )a"  
    Remove *keys* from the context-local context if they are present.

    Use this instead of :func:`~structlog.BoundLogger.unbind` when you want to
    remove keys from a global (context-local) context.

    .. versionadded:: 20.1.0
    .. versionchanged:: 21.1.0 See toplevel note.
    N)r   r   r'   r   )r6   r   r0   r   r   r   unbind_contextvars   s   
r7   Generator[None, None, None]c               
   +  sx    t    fdd  |  @ D }tdi |  zdV  W t|    tdi | dS t|    tdi | w )z
    Bind *kw* to the current context-local context. Unbind or restore *kw*
    afterwards. Do **not** affect other keys.

    Can be used as a context manager or decorator.

    .. versionadded:: 21.4.0
    c                   s   i | ]}| | qS r   r   ).0r   contextr   r   
<dictcomp>   s    z%bound_contextvars.<locals>.<dictcomp>Nr   )r   r6   r2   r7   )r)   savedr   r:   r   bound_contextvars   s   
r>   )r   r   )r   r   r   r   )r    r	   r!   r"   r#   r   r   r   )r   r&   )r)   r   r   r*   )r)   r3   r   r&   )r6   r"   r   r&   )r)   r   r   r8   )__doc__
__future__r   
contextlibr   typingr   r   r   r   r   r   r	   r   lenr   r   __annotations__r   r   r%   r(   r2   r5   r7   contextmanagerr>   r   r   r   r   <module>   s&   






