B
    '˜t\ù	  ã               @   s„   d Z ddlmZ ddlmZ ddlmZ ddddd	gZeƒ Zda	d
d„ Z
G dd„ deƒZG dd„ deƒZdd„ ZG dd	„ d	eƒZdS )a¸  
Task local storage for coroutines.

Example::

    # Create a new task local.
    my_local = TaskLocal()

    # Set/retrieve/erase:
    my_local.set(value)

    value = my_local.get()

    my_local.delete()

A new scope can be created as follows::

    with context():
        ...

Within this scope, a new value can be assigned, which is only visible within
the scope. The scope as passed along when code is sent to an executor and back.
é    )Úunicode_literals)Úlocal)ÚwrapsÚcontextÚget_context_idÚwrap_in_current_contextÚ	TaskLocalÚTaskLocalNotSetErrorc               C   s    yt jS  tk
r   dS X dS )z( Return the current context ID or None. r   N)Ú_storageÚ
context_idÚAttributeError© r   r   ú?lib/python3.7/site-packages/prompt_toolkit/eventloop/context.pyr   )   s    c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
r   z5
    Context manager that activates a new scope.
    Nc             C   s"   |d k	r|| _ ntd7 at| _ d S )Né   )ÚidÚ_last_context_id)Úselfr   r   r   r   Ú__init__5   s    zcontext.__init__c             C   s6   yt j| _W n tk
r&   d | _Y nX | jt _| jS )N)r
   r   Ú_previous_idr   r   )r   r   r   r   Ú	__enter__>   s    zcontext.__enter__c             G   s   | j d krt`n| j t_d S )N)r   r
   r   )r   Úar   r   r   Ú__exit__G   s    
zcontext.__exit__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   1   s   
		c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )r   z<
    Like a thread local, but tied to the current task.
    c             C   s
   i | _ d S )N)r
   )r   r   r   r   r   R   s    zTaskLocal.__init__c             C   s.   yt ƒ }| j| S  tk
r(   t‚Y nX d S )N)r   r
   ÚKeyErrorr	   )r   Úctxr   r   r   ÚgetU   s
    
zTaskLocal.getc             C   s   t ƒ }|| j|< d S )N)r   r
   )r   Úvaluer   r   r   r   Úset\   s    zTaskLocal.setc             C   s,   t ƒ }y| j|= W n tk
r&   Y nX d S )N)r   r
   r   )r   r   r   r   r   Údelete`   s
    zTaskLocal.deleteN)r   r   r   r   r   r   r    r!   r   r   r   r   r   N   s
   c                s,   t ˆƒst‚tƒ ‰ tˆƒ‡ ‡fdd„ƒ}|S )zq
    Decorator that takes a function, and ensures that when it's called, the
    current context will apply.
    c           	      s   t ˆ ƒ ˆ| |ŽS Q R X d S )N)r   )r   Úkw)Úctx_idÚfuncr   r   Únew_funcp   s    
z)wrap_in_current_context.<locals>.new_func)ÚcallableÚAssertionErrorr   r   )r$   r%   r   )r#   r$   r   r   h   s    c               @   s   e Zd ZdS )r	   N)r   r   r   r   r   r   r   r	   w   s   N)r   Z
__future__r   Z	threadingr   Ú	functoolsr   Ú__all__r
   r   r   Úobjectr   r   r   Ú	Exceptionr	   r   r   r   r   Ú<module>   s   