Permalink
Browse files

Change app.client to be a proxy so changes to config at runtime are t…

…aken into effect
  • Loading branch information...
dcramer authored and gandalfar committed Jun 25, 2011
1 parent b226a99 commit e6e504dd65af9a77908ead372f6eeb3c139f672b
View
@@ -105,7 +105,7 @@
from flaskext.babel import Babel
from sentry.core.cleaner import Cleaner
-from sentry.client import get_client
+from sentry.client import ClientProxy
from sentry.db import get_backend
# Build configuration
@@ -116,7 +116,7 @@
app.db = get_backend(app)
# Load configured client
-app.client = get_client(app)
+app.client = ClientProxy(app)
# Flask-Babel (internationalization)
app.babel = Babel(app)
View
@@ -1,5 +1,30 @@
-def get_client(app):
- setting = app.config['CLIENT']
- module, class_name = setting.rsplit('.', 1)
+class ClientProxy(object):
+ def __init__(self, app):
+ self.__app = app
+ self.__client = (None, None)
+
+ def __getattr__(self, attr):
+ return getattr(self.__get_client(), attr)
+
+ def __call__(self, *args, **kwargs):
+ return self.__get_client()(*args, **kwargs)
+
+ def __eq__(self, other):
+ return self.__get_client() == other
+
+ def __get_client(self):
+ setting, client = self.__client
+ if setting != self.__app.config['CLIENT']:
+ setting = self.__app.config['CLIENT']
+ client = get_client(setting)
+ self.__client = (setting, client)
+ return client
+
+ def capture(self, *args, **kwargs):
+ print self.__get_client().capture
+ return self.__get_client().capture(*args, **kwargs)
+
+def get_client(path):
+ module, class_name = path.rsplit('.', 1)
return getattr(__import__(module, {}, {}, class_name), class_name)()
View
@@ -12,6 +12,7 @@ def setUp(self):
'db': 9
}
}
+ app.config['CLIENT'] = 'sentry.client.base.SentryClient'
app.db = get_backend(app)
# Flush the Redis instance
No changes.
@@ -0,0 +1,19 @@
+from .. import BaseTest
+
+from sentry import app
+from sentry.client import ClientProxy
+from sentry.client.logging import LoggingSentryClient
+
+class ClientTest(BaseTest):
+ def test_client_proxy(self):
+ proxy = ClientProxy(app)
+
+ app.config['CLIENT'] = 'sentry.client.logging.LoggingSentryClient'
+
+ self.assertTrue(isinstance(proxy._ClientProxy__get_client(), LoggingSentryClient))
+ self.assertEquals(proxy._ClientProxy__get_client(), proxy._ClientProxy__get_client())
+
+ app.config['CLIENT'] = 'sentry.client.base.SentryClient'
+
+ self.assertFalse(isinstance(proxy._ClientProxy__get_client(), LoggingSentryClient))
+ self.assertEquals(proxy._ClientProxy__get_client(), proxy._ClientProxy__get_client())

0 comments on commit e6e504d

Please sign in to comment.