Permalink
Browse files

Starting to re-work test vs. live

  • Loading branch information...
1 parent ace37c1 commit e87f0809c6d4b5173d12bf5c2199be1c5271864a @harryf committed Feb 13, 2009
Showing with 36 additions and 22 deletions.
  1. +36 −22 urldammit.py
View
58 urldammit.py
@@ -18,17 +18,33 @@
'/([0-9a-f]{40})', 'urldammit',
'/find/(.*)', 'find',
)
-application = web.application(urls, globals() )
-
-manager = URIManager(config.get_db())
-
-# cache URIs we know about
-known = cachemanager.new_instance('known')
-
-# cache queries we know nothing to avoid
-# backend requests
-unknown = cachemanager.new_instance('unknown')
+instances = {'manager': None, 'known': None, 'unknown': None}
+def make_instance_getter(key, fn):
+ def get_instance():
+ if instances[key] == None:
+ instances[key] = fn()
+ return instances[key]
+ return get_instance
+
+"""
+import sys
+if len(sys.argv) > 1 and sys.argv[1] == "webtest":
+ print "Running in test mode"
+ from dammit.nullcache import NullCache
+ get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db_mock()))
+ get_known = make_instance_getter('known', lambda: NullCache())
+ get_unknown = make_instance_getter('unknown', lambda: NullCache())
+ del sys.argv[1]
+else:
+ get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db()))
+ get_known = make_instance_getter('known', lambda: cachemanager.new_instance('known'))
+ get_unknown = make_instance_getter('unknown', lambda: cachemanager.new_instance('unknown'))
+"""
+from dammit.nullcache import NullCache
+get_manager = make_instance_getter('manager', lambda: URIManager(config.get_db_mock()))
+get_known = make_instance_getter('known', lambda: NullCache())
+get_unknown = make_instance_getter('unknown', lambda: NullCache())
class urldammit(object):
"""
@@ -129,14 +145,17 @@ def ithas(key, cache):
return cache[key]
return None
+ unknown = get_unknown()
+ known = get_known()
+
u = ithas(id, unknown)
if u:
web.notfound()
return None
u = ithas(id, known)
if not u:
- u = manager.load(id)
+ u = get_manager().load(id)
if not u:
@@ -163,9 +182,11 @@ def _store(self, uri, i):
tags = unpack_tags(getattr(i, 'tags', []))
pairs = unpack_pairs(getattr(i, 'pairs', {}))
location = getattr(i, 'location', None)
+ known = get_known()
+ unknown = get_unknown()
try:
- u = manager.register(
+ u = get_manager().register(
uri,
int(status),
tags = tags,
@@ -185,9 +206,10 @@ def _store(self, uri, i):
return False
def _delete(self, id):
+ known = get_known()
if id in known:
del known[id]
- manager.delete(id)
+ get_manager().delete(id)
def _ok(self, u):
"""
@@ -288,14 +310,6 @@ def required(input, key):
if __name__ == '__main__':
- import sys
-
- if len(sys.argv) > 1 and sys.argv[1] == "webtest":
- print "Running in test mode"
- manager = URIManager(config.get_db_mock())
- known = {}
- unknown = {}
- del sys.argv[1]
-
+ application = web.application(urls, globals())
application.run()

0 comments on commit e87f080

Please sign in to comment.