Current Path : /usr/lib/python2.7/dist-packages/twisted/python/ |
Current File : //usr/lib/python2.7/dist-packages/twisted/python/context.pyc |
Mc @ sh d Z d d l m Z i Z e j Z d d d YZ d e f d YZ d Z e e d S( s 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. i( t localt ContextTrackerc B s, e Z d Z d Z d Z d d Z RS( s 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( t defaultContextDictt contexts( t self( ( s: /usr/lib/python2.7/dist-packages/twisted/python/context.pyt __init__9 s c O s6 | j j | z | | | SWd | j j Xd S( sD 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 t appendt pop( R t newContextt funct argst kw( ( s: /usr/lib/python2.7/dist-packages/twisted/python/context.pyt callWithContext= s c C s>