/usr/lib/python3/dist-packages/twisted/python
NameSizeModeActions
test/-0755rm
_pydoctortemplates/-0755rm
__pycache__/-0755rm
compat.py231940644editdlrm
components.py142950644editdlrm
constants.py5440644editdlrm
context.py40240644editdlrm
deprecate.py267750644editdlrm
failure.py266380644editdlrm
fakepwd.py61380644editdlrm
filepath.py588870644editdlrm
formmethod.py114600644editdlrm
htmlizer.py35410644editdlrm
lockfile.py77180644editdlrm
log.py224720644editdlrm
logfile.py100830644editdlrm
modules.py271410644editdlrm
monkey.py22270644editdlrm
procutils.py14190644editdlrm
randbytes.py39590644editdlrm
rebuild.py92680644editdlrm
reflect.py194770644editdlrm
release.py11920644editdlrm
roots.py74040644editdlrm
runtime.py62700644editdlrm
sendmsg.py34200644editdlrm
shortcut.py22530644editdlrm
syslog.py37300644editdlrm
systemd.py28340644editdlrm
text.py54820644editdlrm
threadable.py32970644editdlrm
threadpool.py98400644editdlrm
twisted-completion.zsh13710644editdlrm
url.py2440644editdlrm
urlpath.py90840644editdlrm
usage.py350100644editdlrm
util.py279310644editdlrm
versions.py3220644editdlrm
win32.py43170644editdlrm
zippath.py92370644editdlrm
zipstream.py97570644editdlrm
_appdirs.py7880644editdlrm
_inotify.py34550644editdlrm
_oldstyle.py25930644editdlrm
_release.py185480644editdlrm
_setup.py129530644editdlrm
_shellcomp.py248300644editdlrm
_textattributes.py90790644editdlrm
_tzhelper.py31920644editdlrm
_url.py2530644editdlrm
__init__.py6740644editdlrm
Edit: /usr/lib/python3/dist-packages/twisted/python/context.py (4024B)
# -*- 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 __future__ import division, absolute_import from threading import local from twisted.python._oldstyle import _oldStyle defaultContextDict = {} setDefault = defaultContextDict.__setitem__ @_oldStyle 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: 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(object): 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) def installContextTracker(ctr): global theContextTracker global call global get theContextTracker = ctr call = theContextTracker.callWithContext get = theContextTracker.getContext installContextTracker(ThreadedContextTracker())