2020
2121class DjangoDebugToolbarStats (Stats ):
2222 __root = None
23-
23+
2424 def get_root_func (self ):
2525 if self .__root is None :
2626 for func , (cc , nc , tt , ct , callers ) in self .stats .iteritems ():
@@ -44,14 +44,14 @@ def __init__(self, statobj, func, depth=0, stats=None,
4444 self .parent_ids = parent_ids
4545 self .hsv = hsv
4646 self ._line_stats_text = None
47-
47+
4848 def parent_classes (self ):
4949 return self .parent_classes
50-
50+
5151 def background (self ):
5252 r ,g ,b = hsv_to_rgb (* self .hsv )
5353 return 'rgb(%f%%,%f%%,%f%%)' % (r * 100 , g * 100 , b * 100 )
54-
54+
5555 def func_std_string (self ): # match what old profile produced
5656 func_name = self .func
5757 if func_name [:2 ] == ('~' , 0 ):
@@ -66,16 +66,16 @@ def func_std_string(self): # match what old profile produced
6666 idx = file_name .find ('/site-packages/' )
6767 if idx > - 1 :
6868 file_name = file_name [idx + 14 :]
69-
69+
7070 file_path , file_name = file_name .rsplit (os .sep , 1 )
71-
71+
7272 return mark_safe ('<span class="path">{0}/</span><span class="file">{1}</span> in <span class="func">{3}</span>(<span class="lineno">{2}</span>)' .format (
7373 file_path ,
7474 file_name ,
7575 line_num ,
7676 method ,
7777 ))
78-
78+
7979 def subfuncs (self ):
8080 i = 0
8181 h , s , v = self .hsv
@@ -94,36 +94,36 @@ def subfuncs(self):
9494 id = str (self .id ) + '_' + str (i ),
9595 parent_ids = self .parent_ids + [self .id ],
9696 hsv = (h1 ,s1 ,1 ))
97-
97+
9898 def count (self ):
9999 return self .stats [1 ]
100-
100+
101101 def tottime (self ):
102102 return self .stats [2 ]
103-
103+
104104 def cumtime (self ):
105105 cc , nc , tt , ct = self .stats
106106 return self .stats [3 ]
107-
107+
108108 def tottime_per_call (self ):
109109 cc , nc , tt , ct = self .stats
110-
110+
111111 if nc == 0 :
112112 return 0
113-
113+
114114 return tt / nc
115-
115+
116116 def cumtime_per_call (self ):
117117 cc , nc , tt , ct = self .stats
118-
118+
119119 if cc == 0 :
120120 return 0
121-
121+
122122 return ct / cc
123-
123+
124124 def indent (self ):
125125 return 16 * self .depth
126-
126+
127127 def line_stats_text (self ):
128128 if self ._line_stats_text is None and DJ_PROFILE_USE_LINE_PROFILER :
129129 lstats = self .statobj .line_stats
@@ -143,16 +143,16 @@ class ProfilingDebugPanel(DebugPanel):
143143 name = 'Profiling'
144144 template = 'debug_toolbar/panels/profiling.html'
145145 has_content = True
146-
146+
147147 def nav_title (self ):
148148 return _ ('Profiling' )
149-
149+
150150 def url (self ):
151151 return ''
152-
152+
153153 def title (self ):
154154 return _ ('Profiling' )
155-
155+
156156 def _unwrap_closure_and_profile (self , func ):
157157 if not hasattr (func , 'func_code' ):
158158 return
@@ -161,7 +161,7 @@ def _unwrap_closure_and_profile(self, func):
161161 for cell in func .func_closure :
162162 if hasattr (cell .cell_contents , 'func_code' ):
163163 self ._unwrap_closure_and_profile (cell .cell_contents )
164-
164+
165165 def process_view (self , request , view_func , view_args , view_kwargs ):
166166 __traceback_hide__ = True
167167 self .profiler = cProfile .Profile ()
@@ -176,7 +176,7 @@ def process_view(self, request, view_func, view_args, view_kwargs):
176176 self .line_profiler = None
177177 out = self .profiler .runcall (view_func , * args , ** view_kwargs )
178178 return out
179-
179+
180180 def add_node (self , func_list , func , max_depth , cum_time = 0.1 ):
181181 func_list .append (func )
182182 func .has_subfuncs = False
@@ -187,17 +187,20 @@ def add_node(self, func_list, func, max_depth, cum_time=0.1):
187187 (subfunc .func in self .stats .line_stats .timings ))):
188188 func .has_subfuncs = True
189189 self .add_node (func_list , subfunc , max_depth , cum_time = cum_time )
190-
190+
191191 def process_response (self , request , response ):
192+ __traceback_hide__ = True
193+ if not hasattr (self , 'profiler' ):
194+ return None
192195 self .profiler .create_stats ()
193196 self .stats = DjangoDebugToolbarStats (self .profiler )
194197 if DJ_PROFILE_USE_LINE_PROFILER :
195198 self .stats .line_stats = self .line_profiler .get_stats ()
196199 self .stats .calc_callees ()
197-
200+
198201 root = FunctionCall (self .stats , self .stats .get_root_func (), depth = 0 )
199-
202+
200203 func_list = []
201204 self .add_node (func_list , root , 10 , root .stats [3 ]/ 8 )
202-
203- self .stats_record ({'func_list' : func_list })
205+
206+ self .record_stats ({'func_list' : func_list })
0 commit comments