How to update PyQt elements in a script wrapper without using emits in original script
<p>I have a script which I have written a gui wrapper (In PyQt4) to make it easier to use for external users. I'd like to monitor the status of certain stages of the script (eg. Running, Failed, Waiting, etc.) while updating PyQt elements to reflect this. Ideally I don't want to modify the original script to include emits or signals as many of the places I run it on don't have PyQt4 installed. Is this possible? </p>

<p>It's easy enough to write your own system for emitting signals:</p>

<pre><code>from collections import defaultdict

slots = defaultdict(list)

def connect(signal, slot):
    slots[signal].append(slot)

def disconnect(signal, slot=None):
    if slot is not None:
        slots[signal] = [x for x in slots[signal] if x is not slot]
    else:
        del slots[signal][:]

def emit(signal, *args):
    for slot in slots[signal]:
        slot(*args)

...

emit('statusChanged', 'Failed', 'error message')
</code></pre>

<p>Now your PyQt4 wrapper can connect to these signals and either handle them directly, or re-emit an appropriate PyQt signal as necessary:</p>

<pre><code>import myscript

class MainWindow(QtGui.QMainWindow):
    statusChanged = QtCore.pyqtSignal(str, str)

    def __init__(self):
        super(MainWindow, self).__init__()
        myscript.connect('statusChanged', self.handleStatusChanged)

    def handleStatusChanged(self, status, message):
        print(status, message)
        self.statusChanged.emit(status, message)
</code></pre>

python python-3.x pyqt4