File: //proc/thread-self/root/lib/python3/dist-packages/twisted/python/__pycache__/context.cpython-38.pyc
U
W[� � @ sn d Z ddlmZmZ ddlmZ ddlmZ i Zej Z
eG dd� d��ZG dd� de�Z
d d
� Zee
� � dS )z�
Dynamic pseudo-scoping for Python.
Call functions with context.call({key: value}, func); func and
functions that it calls will be able to use 'context.get(key)' to
retrieve 'value'.
This is thread-safe.
� )�division�absolute_import)�local)� _oldStylec @ s* e Zd ZdZdd� Zdd� Zd dd�ZdS )
�ContextTrackera�
A L{ContextTracker} provides a way to pass arbitrary key/value data up and
down a call stack without passing them as parameters to the functions on
that call stack.
This can be useful when functions on the top and bottom of the call stack
need to cooperate but the functions in between them do not allow passing the
necessary state. For example::
from twisted.python.context import call, get
def handleRequest(request):
call({'request-id': request.id}, renderRequest, request.url)
def renderRequest(url):
renderHeader(url)
renderBody(url)
def renderHeader(url):
return "the header"
def renderBody(url):
return "the body (request id=%r)" % (get("request-id"),)
This should be used sparingly, since the lack of a clear connection between
the two halves can result in code which is difficult to understand and
maintain.
@ivar contexts: A C{list} of C{dict}s tracking the context state. Each new
L{ContextTracker.callWithContext} pushes a new C{dict} onto this stack
for the duration of the call, making the data available to the function
called and restoring the previous data once it is complete..
c C s t g| _d S �N)�defaultContextDict�contexts��self� r �8/usr/lib/python3/dist-packages/twisted/python/context.py�__init__= s zContextTracker.__init__c O s, | j �|� z|||�W �S | j �� X dS )aD
Call C{func(*args, **kw)} such that the contents of C{newContext} will
be available for it to retrieve using L{getContext}.
@param newContext: A C{dict} of data to push onto the context for the
duration of the call to C{func}.
@param func: A callable which will be called.
@param *args: Any additional positional arguments to pass to C{func}.
@param **kw: Any additional keyword arguments to pass to C{func}.
@return: Whatever is returned by C{func}
@raise: Whatever is raised by C{func}.
N)r �append�pop)r Z
newContext�func�args�kwr r r
�callWithContextA s zContextTracker.callWithContextNc C s8 t | j�D ](}z|| W S tk
r0 Y q
X q
|S )a(
Retrieve the value for a key from the context.
@param key: The key to look up in the context.
@param default: The value to return if C{key} is not found in the
context.
@return: The value most recently remembered in the context for C{key}.
)�reversedr �KeyError)r �key�default�ctxr r r
�
getContextZ s zContextTracker.getContext)N)�__name__�
__module__�__qualname__�__doc__r r r r r r r
r s !r c @ s. e Zd Zdd� Zdd� Zdd� Zd
dd �ZdS )�ThreadedContextTrackerc C s t � | _d S r )r �storager
r r r
r o s zThreadedContextTracker.__init__c C s6 z
| j jW S tk
r0 t� }| j _| Y S X d S r )r �ct�AttributeErrorr )r r! r r r
�currentContextr s
z%ThreadedContextTracker.currentContextc O s | � � j||f|�|�S r )r# r )r r r r r r r r
r y s z&ThreadedContextTracker.callWithContextNc C s | � � �||�S r )r# r )r r r r r r
r | s z!ThreadedContextTracker.getContext)N)r r r r r# r r r r r r
r n s r c C s | a t jat jad S r )ZtheContextTrackerr �callr �get)Zctrr r r
�installContextTracker� s r&