Skip to content
This repository
Browse code

Starting to re-work test vs. live

  • Loading branch information...
commit e87f0809c6d4b5173d12bf5c2199be1c5271864a 1 parent ace37c1
Harry Fuecks authored

Showing 1 changed file with 36 additions and 22 deletions. Show diff stats Hide diff stats

  1. +36 22 urldammit.py
58 urldammit.py
@@ -18,17 +18,33 @@
18 18 '/([0-9a-f]{40})', 'urldammit',
19 19 '/find/(.*)', 'find',
20 20 )
21   -application = web.application(urls, globals() )
22   -
23   -manager = URIManager(config.get_db())
24   -
25   -# cache URIs we know about
26   -known = cachemanager.new_instance('known')
27   -
28   -# cache queries we know nothing to avoid
29   -# backend requests
30   -unknown = cachemanager.new_instance('unknown')
31 21
  22 +instances = {'manager': None, 'known': None, 'unknown': None}
  23 +def make_instance_getter(key, fn):
  24 + def get_instance():
  25 + if instances[key] == None:
  26 + instances[key] = fn()
  27 + return instances[key]
  28 + return get_instance
  29 +
  30 +"""
  31 +import sys
  32 +if len(sys.argv) > 1 and sys.argv[1] == "webtest":
  33 + print "Running in test mode"
  34 + from dammit.nullcache import NullCache
  35 + get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db_mock()))
  36 + get_known = make_instance_getter('known', lambda: NullCache())
  37 + get_unknown = make_instance_getter('unknown', lambda: NullCache())
  38 + del sys.argv[1]
  39 +else:
  40 + get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db()))
  41 + get_known = make_instance_getter('known', lambda: cachemanager.new_instance('known'))
  42 + get_unknown = make_instance_getter('unknown', lambda: cachemanager.new_instance('unknown'))
  43 +"""
  44 +from dammit.nullcache import NullCache
  45 +get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db_mock()))
  46 +get_known = make_instance_getter('known', lambda: NullCache())
  47 +get_unknown = make_instance_getter('unknown', lambda: NullCache())
32 48
33 49 class urldammit(object):
34 50 """
@@ -129,6 +145,9 @@ def ithas(key, cache):
129 145 return cache[key]
130 146 return None
131 147
  148 + unknown = get_unknown()
  149 + known = get_known()
  150 +
132 151 u = ithas(id, unknown)
133 152 if u:
134 153 web.notfound()
@@ -136,7 +155,7 @@ def ithas(key, cache):
136 155
137 156 u = ithas(id, known)
138 157 if not u:
139   - u = manager.load(id)
  158 + u = get_manager().load(id)
140 159
141 160
142 161 if not u:
@@ -163,9 +182,11 @@ def _store(self, uri, i):
163 182 tags = unpack_tags(getattr(i, 'tags', []))
164 183 pairs = unpack_pairs(getattr(i, 'pairs', {}))
165 184 location = getattr(i, 'location', None)
  185 + known = get_known()
  186 + unknown = get_unknown()
166 187
167 188 try:
168   - u = manager.register(
  189 + u = get_manager().register(
169 190 uri,
170 191 int(status),
171 192 tags = tags,
@@ -185,9 +206,10 @@ def _store(self, uri, i):
185 206 return False
186 207
187 208 def _delete(self, id):
  209 + known = get_known()
188 210 if id in known:
189 211 del known[id]
190   - manager.delete(id)
  212 + get_manager().delete(id)
191 213
192 214 def _ok(self, u):
193 215 """
@@ -288,14 +310,6 @@ def required(input, key):
288 310
289 311
290 312 if __name__ == '__main__':
291   - import sys
292   -
293   - if len(sys.argv) > 1 and sys.argv[1] == "webtest":
294   - print "Running in test mode"
295   - manager = URIManager(config.get_db_mock())
296   - known = {}
297   - unknown = {}
298   - del sys.argv[1]
299   -
  313 + application = web.application(urls, globals())
300 314 application.run()
301 315

0 comments on commit e87f080

Please sign in to comment.
Something went wrong with that request. Please try again.