<s> from __future__ import absolute_import , unicode_literals <EOL> import weakref <EOL> from importlib import import_module <EOL> from django . core . signals import ( <EOL> got_request_exception , request_finished , request_started , <EOL> ) <EOL> from django . db . backends . signals import connection_created <EOL> from django . db . models . signals import ( <EOL> class_prepared , post_delete , post_init , post_migrate , post_save , <EOL> pre_delete , pre_init , pre_save , <EOL> ) <EOL> from django . utils . translation import ugettext_lazy as _ , ungettext <EOL> from debug_toolbar . panels import Panel <EOL> class SignalsPanel ( Panel ) : <EOL> template = '<STR_LIT>' <EOL> SIGNALS = { <EOL> '<STR_LIT>' : request_started , <EOL> '<STR_LIT>' : request_finished , <EOL> '<STR_LIT>' : got_request_exception , <EOL> '<STR_LIT>' : connection_created , <EOL> '<STR_LIT>' : class_prepared , <EOL> '<STR_LIT>' : pre_init , <EOL> '<STR_LIT>' : post_init , <EOL> '<STR_LIT>' : pre_save , <EOL> '<STR_LIT>' : post_save , <EOL> '<STR_LIT>' : pre_delete , <EOL> '<STR_LIT>' : post_delete , <EOL> '<STR_LIT>' : post_migrate , <EOL> } <EOL> def nav_subtitle ( self ) : <EOL> signals = self . get_stats ( ) [ '<STR_LIT>' ] <EOL> num_receivers = sum ( len ( s [ <NUM_LIT:2> ] ) for s in signals ) <EOL> num_signals = len ( signals ) <EOL> if num_signals == <NUM_LIT:1> : <EOL> return ungettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> num_receivers ) % { '<STR_LIT>' : num_receivers } <EOL> return ungettext ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> num_receivers ) % { '<STR_LIT>' : num_receivers , <EOL> '<STR_LIT>' : num_signals } <EOL> title = _ ( "<STR_LIT>" ) <EOL> @ property <EOL> def signals ( self ) : <EOL> signals = self . SIGNALS . copy ( ) <EOL> for signal in self . toolbar . config [ '<STR_LIT>' ] : <EOL> mod_path , signal_name = signal . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> signals_mod = import_module ( mod_path ) <EOL> signals [ signal_name ] = getattr ( signals_mod , signal_name ) <EOL> return signals <EOL> def generate_stats ( self , request , response ) : <EOL> signals = [ ] <EOL> for name , signal in sorted ( self . signals . items ( ) , key = lambda x : x [ <NUM_LIT:0> ] ) : <EOL> if signal is None : <EOL> continue <EOL> receivers = [ ] <EOL> for receiver in signal . receivers : <EOL> receiver = receiver [ <NUM_LIT:1> ] <EOL> if isinstance ( receiver , weakref . ReferenceType ) : <EOL> receiver = receiver ( ) <EOL> if receiver is None : <EOL> continue <EOL> receiver = getattr ( receiver , '<STR_LIT>' , receiver ) <EOL> receiver_name = getattr ( receiver , '<STR_LIT>' , str ( receiver ) ) <EOL> if getattr ( receiver , '<STR_LIT>' , None ) is not None : <EOL> receiver_class_name = getattr ( receiver . __self__ , '<STR_LIT>' , type ) . __name__ <EOL> text = "<STR_LIT>" % ( receiver_class_name , receiver_name ) <EOL> elif getattr ( receiver , '<STR_LIT>' , None ) is not None : <EOL> receiver_class_name = receiver . im_class . __name__ <EOL> text = "<STR_LIT>" % ( receiver_class_name , receiver_name ) <EOL> else : <EOL> text = "<STR_LIT:%s>" % receiver_name <EOL> receivers . append ( text ) <EOL> signals . append ( ( name , signal , receivers ) ) <EOL> self . record_stats ( { '<STR_LIT>' : signals } ) </s>
