/usr/lib/python3/dist-packages/twisted/python
NameSizeModeActions
test/-0755rm
_pydoctortemplates/-0755rm
__pycache__/-0755rm
compat.py169310644editdlrm
components.py141990644editdlrm
constants.py5130644editdlrm
context.py40540644editdlrm
deprecate.py276680644editdlrm
failure.py270070644editdlrm
fakepwd.py67320644editdlrm
filepath.py540230644editdlrm
formmethod.py121070644editdlrm
htmlizer.py36260644editdlrm
lockfile.py80260644editdlrm
log.py223020644editdlrm
logfile.py101190644editdlrm
modules.py267150644editdlrm
monkey.py21640644editdlrm
procutils.py13710644editdlrm
randbytes.py34590644editdlrm
rebuild.py71220644editdlrm
reflect.py204800644editdlrm
release.py11040644editdlrm
roots.py71780644editdlrm
runtime.py59240644editdlrm
sendmsg.py26820644editdlrm
shortcut.py23020644editdlrm
syslog.py36520644editdlrm
systemd.py29870644editdlrm
text.py54170644editdlrm
threadable.py33270644editdlrm
threadpool.py102030644editdlrm
twisted-completion.zsh13710644editdlrm
url.py2440644editdlrm
urlpath.py84470644editdlrm
usage.py345770644editdlrm
util.py274320644editdlrm
versions.py2730644editdlrm
win32.py47940644editdlrm
zippath.py90280644editdlrm
zipstream.py96800644editdlrm
_appdirs.py8200644editdlrm
_inotify.py34960644editdlrm
_pydoctor.py67350644editdlrm
_release.py188790644editdlrm
_shellcomp.py252790644editdlrm
_textattributes.py90970644editdlrm
_tzhelper.py31280644editdlrm
_url.py2280644editdlrm
__init__.py5980644editdlrm
Edit: /usr/lib/python3/dist-packages/twisted/python/context.py (4054B)
# -*- test-case-name: twisted.test.test_context -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ 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. """ from threading import local from typing import Dict, Type defaultContextDict: Dict[Type[object], Dict[str, str]] = {} setDefault = defaultContextDict.__setitem__ class ContextTracker: """ 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.. """ def __init__(self): self.contexts = [defaultContextDict] def callWithContext(self, newContext, func, *args, **kw): """ 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 Exception: Whatever is raised by C{func}. """ self.contexts.append(newContext) try: return func(*args, **kw) finally: self.contexts.pop() def getContext(self, key, default=None): """ 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}. """ for ctx in reversed(self.contexts): try: return ctx[key] except KeyError: pass return default class ThreadedContextTracker: def __init__(self): self.storage = local() def currentContext(self): try: return self.storage.ct except AttributeError: ct = self.storage.ct = ContextTracker() return ct def callWithContext(self, ctx, func, *args, **kw): return self.currentContext().callWithContext(ctx, func, *args, **kw) def getContext(self, key, default=None): return self.currentContext().getContext(key, default) theContextTracker = ThreadedContextTracker() call = theContextTracker.callWithContext get = theContextTracker.getContext def installContextTracker(ctr): global theContextTracker global call global get theContextTracker = ctr call = theContextTracker.callWithContext get = theContextTracker.getContext