Commits

Waldemar Kornewald  committed 7341a1f

simplified integration of webapp-based handlers, added support for profiling the deferred handler, and added an appstats handler

  • Participants
  • Parent commits b95ed54

Comments (0)

Files changed (4)

File appstats/__init__.py

Empty file added.

File appstats/ui.py

+# Initialize Django
+from djangoappengine.main import main
+
+from google.appengine.ext.appstats.ui import main
+
+if __name__ == '__main__':
+    main()

File deferred/handler.py

-import os, sys
+# Initialize Django
+from djangoappengine.main.main import make_profileable
 
-parent_dir = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
-if parent_dir not in sys.path:
-    sys.path.insert(0, parent_dir)
+from google.appengine.ext.deferred.handler import main
 
-# Initialize Django
-from djangoappengine.main import main as gaemain
+main = make_profileable(main)
 
-# Import and run the actual handler
-from google.appengine.ext.deferred.handler import main
 if __name__ == '__main__':
     main()

File main/main.py

     # Run the WSGI CGI handler with that application.
     run_wsgi_app(application)
 
-def profile_main():
+def profile_main(func):
     import logging, cProfile, pstats, random, StringIO
     only_forced_profile = getattr(settings, 'ONLY_FORCED_PROFILE', False)
     profile_percentage = getattr(settings, 'PROFILE_PERCENTAGE', None)
                 'profile=forced' not in os.environ.get('QUERY_STRING')) or \
             (not only_forced_profile and profile_percentage and
                 float(profile_percentage) / 100.0 <= random.random()):
-        return real_main()
+        return func()
 
     prof = cProfile.Profile()
-    prof = prof.runctx('real_main()', globals(), locals())
+    prof = prof.runctx('func()', globals(), locals())
     stream = StringIO.StringIO()
     stats = pstats.Stats(prof, stream=stream)
     sort_by = getattr(settings, 'SORT_PROFILE_RESULTS_BY', 'time')
         stats.print_callers()
     logging.info('Profile data:\n%s', stream.getvalue())
 
-main = getattr(settings, 'ENABLE_PROFILER', False) and profile_main or real_main
+def make_profileable(func):
+    if getattr(settings, 'ENABLE_PROFILER', False):
+        return lambda: profile_main(func)
+    return func
+
+main = make_profileable(real_main)
 
 if __name__ == '__main__':
     main()