File: signals.py

package info (click to toggle)
python-django-debug-toolbar 1%3A1.2.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 1,628 kB
  • sloc: python: 2,789; makefile: 180; sh: 1
file content (90 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import absolute_import, unicode_literals

from django.core.signals import (
    request_started, request_finished, got_request_exception)
from django.db.backends.signals import connection_created
from django.db.models.signals import (
    class_prepared, pre_init, post_init, pre_save, post_save,
    pre_delete, post_delete, post_syncdb)
try:
    from django.dispatch.dispatcher import WEAKREF_TYPES
except ImportError:
    import weakref
    WEAKREF_TYPES = weakref.ReferenceType,
from django.utils.translation import ugettext_lazy as _, ungettext
from django.utils.importlib import import_module

from debug_toolbar.panels import Panel


class SignalsPanel(Panel):
    template = 'debug_toolbar/panels/signals.html'

    SIGNALS = {
        'request_started': request_started,
        'request_finished': request_finished,
        'got_request_exception': got_request_exception,
        'connection_created': connection_created,
        'class_prepared': class_prepared,
        'pre_init': pre_init,
        'post_init': post_init,
        'pre_save': pre_save,
        'post_save': post_save,
        'pre_delete': pre_delete,
        'post_delete': post_delete,
        'post_syncdb': post_syncdb,
    }

    def nav_subtitle(self):
        signals = self.get_stats()['signals']
        num_receivers = sum(len(s[2]) for s in signals)
        num_signals = len(signals)
        # here we have to handle a double count translation, hence the
        # hard coding of one signal
        if num_signals == 1:
            return ungettext("%(num_receivers)d receiver of 1 signal",
                             "%(num_receivers)d receivers of 1 signal",
                             num_receivers) % {'num_receivers': num_receivers}
        return ungettext("%(num_receivers)d receiver of %(num_signals)d signals",
                         "%(num_receivers)d receivers of %(num_signals)d signals",
                         num_receivers) % {'num_receivers': num_receivers,
                                           'num_signals': num_signals}

    title = _("Signals")

    @property
    def signals(self):
        signals = self.SIGNALS.copy()
        for signal in self.toolbar.config['EXTRA_SIGNALS']:
            mod_path, signal_name = signal.rsplit('.', 1)
            signals_mod = import_module(mod_path)
            signals[signal_name] = getattr(signals_mod, signal_name)
        return signals

    def process_response(self, request, response):
        signals = []
        for name, signal in sorted(self.signals.items(), key=lambda x: x[0]):
            if signal is None:
                continue
            receivers = []
            for receiver in signal.receivers:
                receiver = receiver[1]
                if isinstance(receiver, WEAKREF_TYPES):
                    receiver = receiver()
                if receiver is None:
                    continue

                receiver = getattr(receiver, '__wraps__', receiver)
                receiver_name = getattr(receiver, '__name__', str(receiver))
                if getattr(receiver, '__self__', None) is not None:
                    receiver_class_name = getattr(receiver.__self__, '__class__', type).__name__
                    text = "%s.%s" % (receiver_class_name, receiver_name)
                elif getattr(receiver, 'im_class', None) is not None:   # Python 2 only
                    receiver_class_name = receiver.im_class.__name__
                    text = "%s.%s" % (receiver_class_name, receiver_name)
                else:
                    text = "%s" % receiver_name
                receivers.append(text)
            signals.append((name, signal, receivers))

        self.record_stats({'signals': signals})