From 6df47daacce1cbe748f49a23c71d440ed32e7a94 Mon Sep 17 00:00:00 2001 From: Michoel Burger Date: Thu, 19 Nov 2015 22:10:37 +0700 Subject: [PATCH 1/2] Pass should_profile callback environ info --- config.py | 10 +++++----- profiler.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index 95e5524..5847e9f 100644 --- a/config.py +++ b/config.py @@ -18,13 +18,13 @@ # from google.appengine.api import users # return users.is_current_user_admin() -def _should_profile_production_default(): +def _should_profile_production_default(environ): """Default to disabling in production if this function isn't overridden. Can be overridden in appengine_config.py""" return False -def _should_profile_development_default(): +def _should_profile_development_default(environ): """Default to enabling in development if this function isn't overridden. Can be overridden in appengine_config.py""" @@ -34,9 +34,9 @@ def _should_profile_development_default(): "should_profile_production": _should_profile_production_default, "should_profile_development": _should_profile_development_default}) -def should_profile(): +def should_profile(environ): """Returns true if the current request should be profiles.""" if util.dev_server: - return _config.should_profile_development() + return _config.should_profile_development(environ) else: - return _config.should_profile_production() + return _config.should_profile_production(environ) diff --git a/profiler.py b/profiler.py index 3b6ef16..4baffad 100644 --- a/profiler.py +++ b/profiler.py @@ -584,7 +584,7 @@ def __call__(self, environ, start_response): CurrentRequestId.set(None) # Never profile calls to the profiler itself to avoid endless recursion. - if (not config.should_profile() or + if (not config.should_profile(environ) or environ.get("PATH_INFO", "").startswith("/gae_mini_profiler/")): result = self.app(environ, start_response) for value in result: From dcbc06c22b7287a5bafd180be884c1e1ed3bc961 Mon Sep 17 00:00:00 2001 From: Michoel Burger Date: Thu, 10 Dec 2015 12:17:24 +0700 Subject: [PATCH 2/2] Support should_profile() without environ --- config.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index 5847e9f..2663650 100644 --- a/config.py +++ b/config.py @@ -12,24 +12,32 @@ # # These functions will be run once per request, so make sure they are fast. # +# If you need access to the environ use the `with_environ` decorator. +# # Example: # ...in appengine_config.py: # def gae_mini_profiler_should_profile_production(): # from google.appengine.api import users # return users.is_current_user_admin() -def _should_profile_production_default(environ): +def _should_profile_production_default(): """Default to disabling in production if this function isn't overridden. Can be overridden in appengine_config.py""" return False -def _should_profile_development_default(environ): +def _should_profile_development_default(): """Default to enabling in development if this function isn't overridden. Can be overridden in appengine_config.py""" return True +def with_environ(func): + """Decorator to distinguish should_profile_*() methods that need access + to the environ""" + func._accept_environ = True + return func + _config = lib_config.register("gae_mini_profiler", { "should_profile_production": _should_profile_production_default, "should_profile_development": _should_profile_development_default}) @@ -37,6 +45,13 @@ def _should_profile_development_default(environ): def should_profile(environ): """Returns true if the current request should be profiles.""" if util.dev_server: - return _config.should_profile_development(environ) + func = _config.should_profile_development else: - return _config.should_profile_production(environ) + func = _config.should_profile_production + + if getattr(func, '_accept_environ', False): + args = environ, + else: + args = tuple() + + return func(*args)