o
    ADi                     @  s   U d Z ddlm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ZddlmZmZmZ dZeeZed	Zd
ed	< i Zded< d.ddZd/ddZd0ddZd1ddZd2d#d$Zd3d&d'Zd4d)d*Zejd5d,d-ZdS )6a  
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.
.. versionchanged:: 23.3.0
   Callsite parameters are now also collected under asyncio.

See :doc:`contextvars`.
    )annotationsN)	FrameType)Any	GeneratorMapping   )BindableLogger	EventDictWrappedLogger
structlog__ASYNC_CALLING_STACKz!contextvars.ContextVar[FrameType]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   N/var/www/Datamplify/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_contextvars>   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_contextvarsK   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_contextvars`   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_contextvarsp   s   r4   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   r1   r2   r3   r   r   r   reset_contextvars   s
   r7   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   )r8   r   r2   r   r   r   unbind_contextvars   s   
r9   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   r8   r4   r9   )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+   r5   r   r(   )r8   r$   r   r(   )r+   r   r   r:   )__doc__
__future__r   
contextlibr   typesr   typingr   r   r   r   r   r	   r
   r   lenr   r0   r   __annotations__r   r   r!   r'   r*   r4   r7   r9   contextmanagerr@   r   r   r   r   <module>   s,   







