Beispiel #1
0
def installMachInterrupt():
    try:
        import signal
        from PyObjCTools import MachSignals
    except:
        return
    MachSignals.signal(signal.SIGINT, machInterrupt)
def installMachInterrupt():
    try:
        import signal
        from PyObjCTools import MachSignals
    except:
        return
    MachSignals.signal(signal.SIGINT, machInterrupt)
Beispiel #3
0
    def _setNotification(self, timeout=0, notificationStr=None, callbackFn=None, callbackArgs=None, callbackKwargs=None):
        if callable(callbackFn):
            self.callbackFn = callbackFn

        if isinstance(callbackArgs, tuple):
            self.callbackArgs = callbackArgs
        else:
            self.callbackArgs = tuple()

        if isinstance(callbackKwargs, dict):
            self.callbackKwargs = callbackKwargs

        self.observerRes = None

        pid = self._getPid()
        err, observer = AXObserverCreate(pid, observerCallback, None)
        if err != kAXErrorSuccess:
            _setError(err, 'Could not create observer for notification')

        err = AXObserverAddNotification(
            observer, self.ref,
            notificationStr,
            self
        )

        if err != kAXErrorSuccess:
            _setError(err, 'Could not add notification to observer')

        #Add observer source to run loop
        CFRunLoopAddSource(
            CFRunLoopGetCurrent(),
            AXObserverGetRunLoopSource(observer),
            kCFRunLoopDefaultMode
        )

        # Set the signal handlers prior to running the run loop
        oldSigIntHandler = MachSignals.signal(signal.SIGINT, _sigHandler)
        # If an error occurs (return value is SIG_ERR), continue as it's not fatal
        AppHelper.runConsoleEventLoop(
            mode=kCFRunLoopDefaultMode,
            installInterrupt=False,
            maxTimeout=timeout,
        )
        MachSignals.signal(signal.SIGINT, oldSigIntHandler)
        err = AXObserverRemoveNotification(observer, self.ref, notificationStr)
        if err != kAXErrorSuccess:
            _setError(err, 'Could not remove notification from observer')

        return self.observerRes
Beispiel #4
0
    def _setNotification(self,
                         timeout=0,
                         notificationStr=None,
                         callbackFn=None,
                         callbackArgs=None,
                         callbackKwargs=None):
        if callable(callbackFn):
            self.callbackFn = callbackFn

        if isinstance(callbackArgs, tuple):
            self.callbackArgs = callbackArgs
        else:
            self.callbackArgs = tuple()

        if isinstance(callbackKwargs, dict):
            self.callbackKwargs = callbackKwargs

        self.observerRes = None

        pid = self._getPid()
        err, observer = AXObserverCreate(pid, observerCallback, None)
        if err != kAXErrorSuccess:
            _setError(err, 'Could not create observer for notification')

        err = AXObserverAddNotification(observer, self.ref, notificationStr,
                                        self)

        if err != kAXErrorSuccess:
            _setError(err, 'Could not add notification to observer')

        #Add observer source to run loop
        CFRunLoopAddSource(CFRunLoopGetCurrent(),
                           AXObserverGetRunLoopSource(observer),
                           kCFRunLoopDefaultMode)

        # Set the signal handlers prior to running the run loop
        oldSigIntHandler = MachSignals.signal(signal.SIGINT, _sigHandler)
        # If an error occurs (return value is SIG_ERR), continue as it's not fatal
        AppHelper.runConsoleEventLoop(
            mode=kCFRunLoopDefaultMode,
            installInterrupt=False,
            maxTimeout=timeout,
        )
        MachSignals.signal(signal.SIGINT, oldSigIntHandler)
        err = AXObserverRemoveNotification(observer, self.ref, notificationStr)
        if err != kAXErrorSuccess:
            _setError(err, 'Could not remove notification from observer')

        return self.observerRes
Beispiel #5
0
    def wait_for(self, notification=None, filter_=None, timeout=5):
        self.callback_result = None

        @PAXObserverCallback
        def _callback(observer, element, notification, refcon):
            logger.debug("CALLBACK")
            logger.debug("%s, %s, %s, %s" %
                         (observer, element, notification, refcon))
            ret_element = self.ref.__class__(element)
            if filter_(ret_element):
                self.callback_result = ret_element

        observer = PAXObserverCreate(self.ref.pid, _callback)

        PAXObserverAddNotification(observer, self.ref.ref, notification,
                                   id(self.ref.ref))

        # Add observer source to run loop
        CFRunLoopAddSource(
            CFRunLoopGetCurrent(),
            AXObserverGetRunLoopSource(observer),
            NSDefaultRunLoopMode,
        )

        def event_stopper():
            end_time = time.time() + timeout
            while time.time() < end_time:
                if self.callback_result is not None:
                    break
            AppHelper.callAfter(AppHelper.stopEventLoop)

        event_watcher = threading.Thread(target=event_stopper)
        event_watcher.daemon = True
        event_watcher.start()

        # Set the signal handlers prior to running the run loop
        oldSigIntHandler = MachSignals.signal(signal.SIGINT, _sigHandler)
        AppHelper.runConsoleEventLoop()
        MachSignals.signal(signal.SIGINT, oldSigIntHandler)

        PAXObserverRemoveNotification(observer, self.ref.ref, notification)

        return self.callback_result
Beispiel #6
0
def installMachInterrupt():
    import signal
    from PyObjCTools import MachSignals

    MachSignals.signal(signal.SIGINT, machInterrupt)