~nchohan/appscale/zk3.3.4

« back to all changes in this revision

Viewing changes to AppServer/google/appengine/runtime/request_environment.py

  • Committer: Chris Bunch
  • Date: 2012-02-17 08:19:21 UTC
  • mfrom: (787.2.3 appscale-raj-merge)
  • Revision ID: cgb@cs.ucsb.edu-20120217081921-pakidyksaenlpzur
merged with main branch, gaining rabbitmq and upgrades for hbase, cassandra, and hypertable, as well as upgrading to gae 1.6.1 for python and go

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2007 Google Inc.
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License");
 
6
# you may not use this file except in compliance with the License.
 
7
# You may obtain a copy of the License at
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS,
 
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
# See the License for the specific language governing permissions and
 
15
# limitations under the License.
 
16
#
 
17
 
 
18
 
 
19
 
 
20
 
 
21
"""A request-local environment and logging stream."""
 
22
 
 
23
 
 
24
 
 
25
 
 
26
 
 
27
 
 
28
 
 
29
import collections
 
30
import os
 
31
import sys
 
32
import threading
 
33
 
 
34
_sys_stderr = sys.stderr
 
35
 
 
36
 
 
37
class RequestEnvironment(threading.local):
 
38
  """A thread local request environment.
 
39
 
 
40
  A thread local request environment that provides an error stream errors and a
 
41
  dict of the request environment environ as would be found in os.environ. A
 
42
  single error stream is shared between threads of a single request, but each
 
43
  thread accesses an independent copy of environ created when
 
44
  CloneRequestEnvironment is called. A request environment of one thread can be
 
45
  installed in another thread as follows:
 
46
    1. Call CloneRequestEnvironment in the first thread.
 
47
    2. Call the returned callable from the other thread.
 
48
  """
 
49
 
 
50
  def __init__(self):
 
51
    super(RequestEnvironment, self).__init__()
 
52
    self.Reset()
 
53
 
 
54
  def Reset(self):
 
55
    """Resets the error stream and environment for this request."""
 
56
    self.errors = _sys_stderr
 
57
    self.environ = {}
 
58
 
 
59
  def Init(self, errors, environ):
 
60
    self.errors = errors
 
61
    self.environ = environ
 
62
 
 
63
  def CloneRequestEnvironment(self):
 
64
    """Returns a callable that will install the environment in another thread.
 
65
 
 
66
    Returns:
 
67
      A callable that will duplicate the request environment of this thread in
 
68
      another thread that calls it.
 
69
    """
 
70
    errors = self.errors
 
71
    environ = dict(self.environ)
 
72
    return lambda: self.Init(errors, environ)
 
73
 
 
74
  def Clear(self):
 
75
    """Clears the thread locals."""
 
76
    self.__dict__.clear()
 
77
    self.Reset()
 
78
 
 
79
 
 
80
class RequestLocalStream(object):
 
81
  """A stream that delegates to a RequestEnvironment stream."""
 
82
 
 
83
  def __init__(self, request):
 
84
    self._request = request
 
85
 
 
86
  def close(self):
 
87
    pass
 
88
 
 
89
  def flush(self):
 
90
    self._request.errors.flush()
 
91
 
 
92
  def write(self, data):
 
93
    self._request.errors.write(data)
 
94
 
 
95
  def writelines(self, data):
 
96
    self._request.errors.writelines(data)
 
97
 
 
98
 
 
99
class RequestLocalEnviron(collections.MutableMapping):
 
100
  """A MutableMapping that delegates to a RequestEnvironment environ."""
 
101
 
 
102
  def __init__(self, request):
 
103
    self._request = request
 
104
 
 
105
  def __len__(self):
 
106
    return len(self._request.environ)
 
107
 
 
108
  def __iter__(self):
 
109
    return iter(self._request.environ)
 
110
 
 
111
  def __getitem__(self, key):
 
112
    return self._request.environ[key]
 
113
 
 
114
  def __setitem__(self, key, value):
 
115
    self._request.environ[key] = value
 
116
 
 
117
  def __delitem__(self, key):
 
118
    del self._request.environ[key]
 
119
 
 
120
  def __repr__(self):
 
121
    return repr(self._request.environ)
 
122
 
 
123
  def has_key(self, key):
 
124
    return key in self._request.environ
 
125
 
 
126
  def copy(self):
 
127
    return dict(self._request.environ)
 
128
 
 
129
 
 
130
 
 
131
  def viewitems(self):
 
132
    return collections.ItemsView(self)
 
133
 
 
134
  def viewkeys(self):
 
135
    return collections.KeysView(self)
 
136
 
 
137
  def viewvalues(self):
 
138
    return collections.ValuesView(self)
 
139
 
 
140
 
 
141
current_request = RequestEnvironment()
 
142
 
 
143
 
 
144
 
 
145
 
 
146
def PatchOsEnviron():
 
147
  """Replace os.environ by a RequestLocalEnviron instance.
 
148
 
 
149
  This is called from init.py when it modifies the execution
 
150
  environment (in the wider sense of the word).
 
151
  """
 
152
  os.environ = RequestLocalEnviron(current_request)