U
    J+|jÈ  ã                   @   sT   d dl Z d dlZd dlZd dlZd dlmZmZmZ G dd„ dƒZG dd„ dƒZ	dS )é    N)ÚAnyÚDictÚUnionc                       sP   e Zd ZdZddœdd„Zdd„ Zeeddœ‡ fd	d
„Zeddœdd„Z	‡  Z
S )Ú_CVarzStorage utility for Local.N)Úreturnc                 C   s   t  d¡| _d S )Nzasgiref.local)ÚcontextvarsÚ
ContextVarÚ_data©Úself© r   úUc:\users\snmko\desktop\web_content\django_cbt\venv\Lib\site-packages\asgiref/local.pyÚ__init__   s    ÿz_CVar.__init__c                 C   sB   | j  i ¡}z
|| W S  tk
r<   t| ›d|›�ƒ‚Y nX d S ©Nz object has no attribute )r	   ÚgetÚKeyErrorÚAttributeError©r   ÚkeyÚstorage_objectr   r   r   Ú__getattr__   s
    
z_CVar.__getattr__)r   Úvaluer   c                    s:   |dkrt ƒ  ||¡S | j i ¡}|||< | j |¡ d S )Nr	   )ÚsuperÚ__setattr__r	   r   Úset)r   r   r   r   ©Ú	__class__r   r   r      s
    z_CVar.__setattr__)r   r   c                 C   s>   | j  i ¡}||kr(||= | j  |¡ nt| ›d|›�ƒ‚d S r   )r	   r   r   r   r   r   r   r   Ú__delattr__   s
    z_CVar.__delattr__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ústrr   r   r   Ú__classcell__r   r   r   r   r      s
   r   c                       sP   e Zd ZdZdeddœdd„Zejdd„ ƒZd	d
„ Z	‡ fdd„Z
dd„ Z‡  ZS )ÚLocala  Local storage for async tasks.

    This is a namespace object (similar to `threading.local`) where data is
    also local to the current async task (if there is one).

    In async threads, local means in the same sense as the `contextvars`
    module - i.e. a value set in an async frame will be visible:

    - to other async code `await`-ed from this frame.
    - to tasks spawned using `asyncio` utilities (`create_task`, `wait_for`,
      `gather` and probably others).
    - to code scheduled in a sync thread using `sync_to_async`

    In "sync" threads (a thread with no async event loop running), the
    data is thread-local, but additionally shared with async code executed
    via the `async_to_sync` utility, which schedules async code in a new thread
    and copies context across to that thread.

    If `thread_critical` is True, then the local will only be visible per-thread,
    behaving exactly like `threading.local` if the thread is sync, and as
    `contextvars` if the thread is async. This allows genuinely thread-sensitive
    code (such as DB handles) to be kept stricly to their initial thread and
    disable the sharing across `sync_to_async` and `async_to_sync` wrapped calls.

    Unlike plain `contextvars` objects, this utility is threadsafe.
    FN)Úthread_criticalr   c                 C   s0   || _ t ¡ | _|  |r$t ¡ | _ntƒ | _d S ©N)Ú_thread_criticalÚ	threadingÚRLockÚ_thread_lockÚlocalÚ_storager   )r   r%   r   r   r   r   D   s    
zLocal.__init__c              	   c   sp   | j rRzt ¡  W n tk
r.   | jV  Y qlX t| jdƒsFtƒ | j_| jjV  n| j� | jV  W 5 Q R X d S )NÚcvar)	r'   ÚasyncioÚget_running_loopÚRuntimeErrorr,   Úhasattrr   r-   r*   r
   r   r   r   Ú_lock_storageQ   s    
zLocal._lock_storagec              
   C   s*   |   ¡ �}t||ƒW  5 Q R £ S Q R X d S r&   )r2   Úgetattr©r   r   Ústorager   r   r   r   t   s    
zLocal.__getattr__c              	      s:   |dkrt ƒ  ||¡S |  ¡ �}t|||ƒ W 5 Q R X d S )N)Ú_localr,   r'   r*   )r   r   r2   Úsetattr)r   r   r   r5   r   r   r   r   x   s    
zLocal.__setattr__c              	   C   s"   |   ¡ �}t||ƒ W 5 Q R X d S r&   )r2   Údelattrr4   r   r   r   r   ~   s    
zLocal.__delattr__)F)r   r   r    r!   Úboolr   Ú
contextlibÚcontextmanagerr2   r   r   r   r#   r   r   r   r   r$   (   s   
"r$   )
r.   r:   r   r(   Útypingr   r   r   r   r$   r   r   r   r   Ú<module>   s    