| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # This will attempt to import the actual App Engine modules, and if it fails, | 5 # This will attempt to import the actual App Engine modules, and if it fails, |
| 6 # they will be replaced with fake modules. This is useful during testing. | 6 # they will be replaced with fake modules. This is useful during testing. |
| 7 try: | 7 try: |
| 8 import google.appengine.ext.blobstore as blobstore | 8 import google.appengine.ext.blobstore as blobstore |
| 9 from google.appengine.ext.blobstore.blobstore import BlobReferenceProperty | 9 from google.appengine.ext.blobstore.blobstore import BlobReferenceProperty |
| 10 import google.appengine.ext.db as db | 10 import google.appengine.ext.db as db |
| 11 import google.appengine.ext.webapp as webapp | 11 import google.appengine.ext.webapp as webapp |
| 12 import google.appengine.api.files as files | 12 import google.appengine.api.files as files |
| 13 import google.appengine.api.memcache as memcache | 13 import google.appengine.api.memcache as memcache |
| 14 import google.appengine.api.urlfetch as urlfetch | 14 import google.appengine.api.urlfetch as urlfetch |
| 15 # Default to a 5 minute cache timeout. | |
| 16 CACHE_TIMEOUT = 300 | |
| 17 except ImportError: | 15 except ImportError: |
| 18 # Cache for one second because zero means cache forever. | |
| 19 CACHE_TIMEOUT = 1 | |
| 20 import re | 16 import re |
| 21 from StringIO import StringIO | 17 from StringIO import StringIO |
| 22 | 18 |
| 23 FAKE_URL_FETCHER_CONFIGURATION = None | 19 FAKE_URL_FETCHER_CONFIGURATION = None |
| 24 | 20 |
| 25 def ConfigureFakeUrlFetch(configuration): | 21 def ConfigureFakeUrlFetch(configuration): |
| 26 """|configuration| is a dictionary mapping strings to fake urlfetch classes. | 22 """|configuration| is a dictionary mapping strings to fake urlfetch classes. |
| 27 A fake urlfetch class just needs to have a fetch method. The keys of the | 23 A fake urlfetch class just needs to have a fetch method. The keys of the |
| 28 dictionary are treated as regex, and they are matched with the URL to | 24 dictionary are treated as regex, and they are matched with the URL to |
| 29 determine which fake urlfetch is used. | 25 determine which fake urlfetch is used. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 119 |
| 124 def GetBlobKeys(self): | 120 def GetBlobKeys(self): |
| 125 return _BLOBS.keys() | 121 return _BLOBS.keys() |
| 126 | 122 |
| 127 def finalize(self, filename): | 123 def finalize(self, filename): |
| 128 pass | 124 pass |
| 129 | 125 |
| 130 files = FakeFiles() | 126 files = FakeFiles() |
| 131 | 127 |
| 132 class InMemoryMemcache(object): | 128 class InMemoryMemcache(object): |
| 133 """A fake memcache that does nothing. | 129 """An in-memory memcache implementation. |
| 134 """ | 130 """ |
| 131 def __init__(self): |
| 132 self._namespaces = {} |
| 133 |
| 135 class Client(object): | 134 class Client(object): |
| 136 def set_multi_async(self, mapping, namespace='', time=0): | 135 def set_multi_async(self, mapping, namespace='', time=0): |
| 137 return | 136 for k, v in mapping.iteritems(): |
| 137 memcache.set(k, v, namespace=namespace, time=time) |
| 138 | 138 |
| 139 def get_multi_async(self, keys, namespace='', time=0): | 139 def get_multi_async(self, keys, namespace='', time=0): |
| 140 return _RPC(result=dict((k, None) for k in keys)) | 140 return _RPC(result=dict( |
| 141 (k, memcache.get(k, namespace=namespace, time=time)) for k in keys)) |
| 141 | 142 |
| 142 def set(self, key, value, namespace='', time=0): | 143 def set(self, key, value, namespace='', time=0): |
| 143 return | 144 self._GetNamespace(namespace)[key] = value |
| 144 | 145 |
| 145 def get(self, key, namespace='', time=0): | 146 def get(self, key, namespace='', time=0): |
| 146 return None | 147 return self._GetNamespace(namespace).get(key) |
| 147 | 148 |
| 148 def delete(self, key, namespace): | 149 def delete(self, key, namespace=''): |
| 149 return | 150 del self._GetNamespace(namespace)[key] |
| 151 |
| 152 def delete_multi(self, keys, namespace=''): |
| 153 for k in keys: |
| 154 self.delete(k, namespace=namespace) |
| 155 |
| 156 def _GetNamespace(self, namespace): |
| 157 if namespace not in self._namespaces: |
| 158 self._namespaces[namespace] = {} |
| 159 return self._namespaces[namespace] |
| 150 | 160 |
| 151 memcache = InMemoryMemcache() | 161 memcache = InMemoryMemcache() |
| 152 | 162 |
| 153 class webapp(object): | 163 class webapp(object): |
| 154 class RequestHandler(object): | 164 class RequestHandler(object): |
| 155 """A fake webapp.RequestHandler class for Handler to extend. | 165 """A fake webapp.RequestHandler class for Handler to extend. |
| 156 """ | 166 """ |
| 157 def __init__(self, request, response): | 167 def __init__(self, request, response): |
| 158 self.request = request | 168 self.request = request |
| 159 self.response = response | 169 self.response = response |
| (...skipping 26 matching lines...) Expand all Loading... |
| 186 | 196 |
| 187 @staticmethod | 197 @staticmethod |
| 188 def gql(query, key): | 198 def gql(query, key): |
| 189 return _Db_Result(db._store.get(key, None)) | 199 return _Db_Result(db._store.get(key, None)) |
| 190 | 200 |
| 191 def put(self): | 201 def put(self): |
| 192 db._store[self._key] = self._value | 202 db._store[self._key] = self._value |
| 193 | 203 |
| 194 class BlobReferenceProperty(object): | 204 class BlobReferenceProperty(object): |
| 195 pass | 205 pass |
| OLD | NEW |