Michael Merickel
2018-10-15 81576ee51564c49d5ff3c1c07f214f22a8438231
commit | author | age
a1a9fb 1 import threading
17b8bc 2
b60bdb 3 from pyramid.registry import global_registry
a1a9fb 4
CM 5 class ThreadLocalManager(threading.local):
67ed63 6     def __init__(self, default=None):
CM 7         # http://code.google.com/p/google-app-engine-django/issues/detail?id=119
815471 8         # we *must* use a keyword argument for ``default`` here instead
67ed63 9         # of a positional argument to work around a bug in the
CM 10         # implementation of _threading_local.local in Python, which is
11         # used by GAE instead of _thread.local
a1a9fb 12         self.stack = []
CM 13         self.default = default
815471 14
a1a9fb 15     def push(self, info):
CM 16         self.stack.append(info)
17
18     set = push # b/c
19
20     def pop(self):
21         if self.stack:
22             return self.stack.pop()
23
24     def get(self):
25         try:
26             return self.stack[-1]
27         except IndexError:
28             return self.default()
29
30     def clear(self):
31         self.stack[:] = []
32
33 def defaults():
dda9fa 34     return {'request': None, 'registry': global_registry}
a1a9fb 35
67ed63 36 manager = ThreadLocalManager(default=defaults)
947b8b 37
CM 38 def get_current_request():
822653 39     """
MM 40     Return the currently active request or ``None`` if no request
925580 41     is currently active.
CM 42
43     This function should be used *extremely sparingly*, usually only
67bfa0 44     in unit testing code.  It's almost always usually a mistake to use
925580 45     ``get_current_request`` outside a testing context because its
CM 46     usage makes it possible to write code that can be neither easily
47     tested nor scripted.
822653 48
947b8b 49     """
CM 50     return manager.get()['request']
51
52 def get_current_registry(context=None): # context required by getSiteManager API
822653 53     """
MM 54     Return the currently active :term:`application registry` or the
2cfb02 55     global application registry if no request is currently active.
925580 56
CM 57     This function should be used *extremely sparingly*, usually only
859ff0 58     in unit testing code.  It's almost always usually a mistake to use
925580 59     ``get_current_registry`` outside a testing context because its
CM 60     usage makes it possible to write code that can be neither easily
61     tested nor scripted.
822653 62
947b8b 63     """
CM 64     return manager.get()['registry']
822653 65
MM 66 class RequestContext(object):
67     def __init__(self, request):
68         self.request = request
69
70     def begin(self):
71         request = self.request
72         registry = request.registry
73         manager.push({'registry': registry, 'request': request})
74         return request
75
76     def end(self):
77         manager.pop()
78
79     def __enter__(self):
80         return self.begin()
81
82     def __exit__(self, *args):
83         self.end()