Green border.
[wolnelektury.git] / src / wolnelektury / middleware.py
index 60b382c..956f979 100644 (file)
@@ -1,25 +1,26 @@
-# Orignal version taken from http://www.djangosnippets.org/snippets/186/
+# Original version taken from http://www.djangosnippets.org/snippets/186/
 # Original author: udfalkso
 # Modified by: Shwagroo Team
 
 import sys
 import os
 import re
 # Original author: udfalkso
 # Modified by: Shwagroo Team
 
 import sys
 import os
 import re
-import hotshot, hotshot.stats
+import hotshot
+import hotshot.stats
 import tempfile
 import tempfile
-import StringIO
+import io
 import pprint
 
 from django.conf import settings
 from django.db import connection
 
 
 import pprint
 
 from django.conf import settings
 from django.db import connection
 
 
-words_re = re.compile( r'\s+' )
+words_re = re.compile(r'\s+')
 
 group_prefix_re = [
 
 group_prefix_re = [
-    re.compile( "^.*/django/[^/]+" ),
-    re.compile( "^(.*)/[^/]+$" ), # extract module path
-    re.compile( ".*" ),           # catch strange entries
+    re.compile("^.*/django/[^/]+"),
+    re.compile("^(.*)/[^/]+$"),  # extract module path
+    re.compile(".*"),            # catch strange entries
 ]
 
 
 ]
 
 
@@ -36,24 +37,24 @@ class ProfileMiddleware(object):
     WARNING: It uses hotshot profiler which is not thread safe.
     """
     def process_request(self, request):
     WARNING: It uses hotshot profiler which is not thread safe.
     """
     def process_request(self, request):
-        if (settings.DEBUG or request.user.is_superuser) and request.GET.has_key('prof'):
+        if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
             connection.queries = []
             self.tmpfile = tempfile.mktemp()
             self.prof = hotshot.Profile(self.tmpfile)
 
     def process_view(self, request, callback, callback_args, callback_kwargs):
             connection.queries = []
             self.tmpfile = tempfile.mktemp()
             self.prof = hotshot.Profile(self.tmpfile)
 
     def process_view(self, request, callback, callback_args, callback_kwargs):
-        if (settings.DEBUG or request.user.is_superuser) and request.GET.has_key('prof'):
+        if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
             return self.prof.runcall(callback, request, *callback_args, **callback_kwargs)
 
     def get_group(self, file):
         for g in group_prefix_re:
             return self.prof.runcall(callback, request, *callback_args, **callback_kwargs)
 
     def get_group(self, file):
         for g in group_prefix_re:
-            name = g.findall( file )
+            name = g.findall(file)
             if name:
                 return name[0]
 
     def get_summary(self, results_dict, sum):
             if name:
                 return name[0]
 
     def get_summary(self, results_dict, sum):
-        list = [ (item[1], item[0]) for item in results_dict.items() ]
-        list.sort( reverse = True )
+        list = [(item[1], item[0]) for item in results_dict.items()]
+        list.sort(reverse=True)
         list = list[:40]
 
         res = "      tottime\n"
         list = list[:40]
 
         res = "      tottime\n"
@@ -62,7 +63,7 @@ class ProfileMiddleware(object):
                 foo = 0
             else:
                 foo = 100*item[0]/sum
                 foo = 0
             else:
                 foo = 100*item[0]/sum
-            res += "%4.1f%% %7.3f %s\n" % (foo, item[0], item[1] )
+            res += "%4.1f%% %7.3f %s\n" % (foo, item[0], item[1])
 
         return res
 
 
         return res
 
@@ -75,31 +76,31 @@ class ProfileMiddleware(object):
         sum = 0
 
         for s in stats_str:
         sum = 0
 
         for s in stats_str:
-            fields = words_re.split(s);
+            fields = words_re.split(s)
             if len(fields) == 7:
                 time = float(fields[2])
                 sum += time
                 file = fields[6].split(":")[0]
 
             if len(fields) == 7:
                 time = float(fields[2])
                 sum += time
                 file = fields[6].split(":")[0]
 
-                if not file in mystats:
+                if file not in mystats:
                     mystats[file] = 0
                 mystats[file] += time
 
                 group = self.get_group(file)
                     mystats[file] = 0
                 mystats[file] += time
 
                 group = self.get_group(file)
-                if not group in mygroups:
-                    mygroups[ group ] = 0
-                mygroups[ group ] += time
+                if group not in mygroups:
+                    mygroups[group] = 0
+                mygroups[group] += time
 
         return "<pre>" + \
 
         return "<pre>" + \
-               " ---- By file ----\n\n" + self.get_summary(mystats,sum) + "\n" + \
-               " ---- By group ---\n\n" + self.get_summary(mygroups,sum) + \
+               " ---- By file ----\n\n" + self.get_summary(mystats, sum) + "\n" + \
+               " ---- By group ---\n\n" + self.get_summary(mygroups, sum) + \
                "</pre>"
 
     def process_response(self, request, response):
                "</pre>"
 
     def process_response(self, request, response):
-        if (settings.DEBUG or request.user.is_superuser) and request.GET.has_key('prof'):
+        if (settings.DEBUG or request.user.is_superuser) and 'prof' in request.GET:
             self.prof.close()
 
             self.prof.close()
 
-            out = StringIO.StringIO()
+            out = io.BytesIO()
             old_stdout = sys.stdout
             sys.stdout = out
 
             old_stdout = sys.stdout
             sys.stdout = out
 
@@ -123,4 +124,3 @@ class ProfileMiddleware(object):
             response.content += pprint.pformat(connection.queries)
 
         return response
             response.content += pprint.pformat(connection.queries)
 
         return response
-