public
Created

Attach to all available signals in decoratored class and print debug message each time one of the signals is emitted.

  • Download Gist
debug_signals.py
Python
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
def debug_signals(cls):
"""
Attach to all available signals in decoratored class and print debug
message each time one of the signals is emitted.
"""
 
from PyQt4 import QtCore
 
def stripArgsFromSignature(signature):
"""
Take a signature provided by QMetaMethod.signature() and strip off the
arguments, types, etc. The signature passed in will be suitable for a
C++ declaration, but we are in Python so the types, etc. aren't too
useful. We just want the name of the method.
 
However, the type information could be useful if you're overriding a
slot with multiple versions of the same method (i.e. different types)
with the pyqtSignal decorator. See pyqtSignal decorator docs for more
information.
"""
 
# This returns the full signature with type etc. like C++ would
# want, we just want attribute names so strip everything after
# the '('.
paren_idx = signature.index('(')
return signature[:paren_idx]
 
def getSignalNames(self):
"""
Find all user-defined signals provided by the wrapped class
"""
 
# Must happen within closure so we have access to 'self' b/c it's
# the implicit parameter in QMetaObject calls
 
m = self.metaObject()
first_method = m.methodOffset()
last_method = m.methodCount()
 
signals = []
for ii in xrange(first_method, last_method):
if m.method(ii).methodType() == QtCore.QMetaMethod.Signal:
signature = stripArgsFromSignature(m.method(ii).signature())
signals.append(signature)
 
return signals
 
def attachDebugToSignals(self, signal_names):
"""Attach our debug slot to all given signal names"""
 
for name in signal_names:
# Include signal name when we call it to give a bit more context
# for debugging what signal was emitted.
debug_func = partial(self._signalDebugger, name)
getattr(self, name).connect(debug_func)
 
def getSlotName(self, sender_qobj):
"""
Get name of slot executing from sender
 
sender should be self.sender() passed from within the slot
"""
 
# FIXME: This probably isn't too useful b/c it will always be our slot
# b/c we just attached to each signal, we didn't OVERRIDE and inject
# into the existing slot. However, leaving this code here b/c it might
# be a useful trick if we can ever actually inject into the existing
# slots or override the emit() signal call to call us back.
 
try:
# Added in Qt 4.8
idx = sender_qobj.senderSignalIndex()
except AttributeError:
slot_name = ''
else:
m = self.metaObject()
slot_name = stripArgsFromSignature(m.method(idx).signature())
 
return slot_name
 
 
def signalDebugger(self, signal_name, *args):
"""
This is the debugging 'slot.' It's the method that's called each time
one of the signals we connected to is emitted.
 
Debugging goes here!
"""
 
try:
sender = self.sender()
except Exception:
import traceback
traceback.print_exc()
sender = 'Unknown'
 
slot_name = self._getSlotName(sender)
 
# For bonus points override __str__ in your classes and you'll get to
# see that here..
print 'sender: %s\nsignal_name: %s\nslot_name: %s\nargs: %s\n' % (
sender, signal_name, slot_name, args)
 
def debugSignals(self):
"""
'Public' API to attach our own slot to all signals in wrapped class
"""
 
self._attachDebugToSignals(self._getSignalNames())
 
# Replace init so we can automatically attach our signal debugging AFTER
# the 'real' init runs
orig_init = cls.__init__
 
def new_init(self, *args, **kwargs):
"""
'Extend' wrapped class' __init__ so we can attach to all signals
automatically
"""
 
orig_init(self, *args, **kwargs)
self._debugSignals()
 
# Expose helper methods for what we do here so we people using decorator
# could call them later in their code if needed.
cls._getSignalNames = getSignalNames
cls._attachDebugToSignals = attachDebugToSignals
cls._signalDebugger = signalDebugger
cls._debugSignals = debugSignals
cls._getSlotName = getSlotName
 
cls.__init__ = new_init
 
return cls

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.