Example #1
0
def test_unschedule_self(observer):
    """
    Tests that unscheduling a watch from within an event handler correctly
    correctly unregisters emitter and handler without deadlocking.
    """
    class EventHandler(FileSystemEventHandler):
        def on_modified(self, event):
            observer.unschedule(watch)
            unschedule_finished.set()

    unschedule_finished = Event()
    watch = observer.schedule(EventHandler(), '')
    observer.start()

    (emitter, ) = observer.emitters
    emitter.queue_event(FileModifiedEvent(''))

    assert unschedule_finished.wait()
    assert len(observer.emitters) == 0
Example #2
0
def test_unschedule_self(observer):
    """
    Tests that unscheduling a watch from within an event handler correctly
    correctly unregisters emitter and handler without deadlocking.
    """
    class EventHandler(FileSystemEventHandler):
        def on_modified(self, event):
            observer.unschedule(watch)
            unschedule_finished.set()

    unschedule_finished = Event()
    watch = observer.schedule(EventHandler(), '')
    observer.start()

    (emitter,) = observer.emitters
    emitter.queue_event(FileModifiedEvent(''))

    assert unschedule_finished.wait()
    assert len(observer.emitters) == 0
Example #3
0
class XBMCIF(threading.Thread):
    """Wrapper around the builtins to make sure no two commands are executed at
    the same time (xbmc will cancel previous if not finished)
    """
    def __init__(self):
        threading.Thread.__init__(self)
        self._stop_event = Event()
        self._cmd_queue = utils.OrderedSetQueue()

    def stop(self):
        self._stop_event.set()
        self._cmd_queue.put("stop")  # unblock wait

    def queue_scan(self, library, path=None):
        path = escape_param(path) if path else ""
        cmd = "UpdateLibrary(%s,%s,%s)" % (library, path,
                                           settings.SHOW_PROGRESS_DIALOG)
        self._cmd_queue.put(cmd)

    def queue_clean(self, library):
        self._cmd_queue.put("CleanLibrary(%s,%s)" %
                            (library, settings.SHOW_PROGRESS_DIALOG))

    def queue_remove(self, library, path=None):
        if settings.REMOVAL_ENABLED:
            if settings.PER_FILE_REMOVE and path and library == 'video':
                videolibrary.remove_video(path)
            else:
                self._cmd_queue.put("CleanLibrary(%s,%s)" %
                                    (library, settings.SHOW_PROGRESS_DIALOG))

    def run(self):
        player = xbmc.Player()
        while True:
            self._cmd_queue.wait()
            if self._stop_event.wait(settings.SCAN_DELAY):
                return
            while player.isPlaying():
                self._stop_event.wait(1)
            if self._stop_event.is_set():
                return

            # Remove item right before it's executed so that any duplicates of
            # this commands gets flushed from the queue.
            cmd = self._cmd_queue.get_nowait()
            log("[xbmcif] executing builtin: '%s'" % cmd)
            xbmc.executebuiltin(cmd)

            # wait for scan to start. we need a timeout or else we a screwed
            # if we missed it.
            # TODO: replace this crap with Monitor callbacks in Helix
            log("[xbmcif] waiting for scan/clean start..")
            timeout = 3000
            while not xbmc.getCondVisibility('Library.IsScanning') \
                    and not self._stop_event.is_set():
                xbmc.sleep(100)
                timeout -= 100
                if timeout <= 0:
                    log("[xbmcif] wait for scan/clean timed out.")
                    break
            log("[xbmcif] scan/clean started.")
            log("[xbmcif] waiting for scan/clean end..")

            # wait for scan to end
            while xbmc.getCondVisibility('Library.IsScanning') \
                    and not self._stop_event.is_set():
                xbmc.sleep(100)
            log("[xbmcif] scan/clean ended.")
Example #4
0
class XBMCIF(threading.Thread):
    """Wrapper around the builtins to make sure no two commands a executed at
    the same time (xbmc will cancel previous if not finished)
    """

    def __init__(self):
        threading.Thread.__init__(self)
        self._stop_event = Event()
        self._cmd_queue = utils.OrderedSetQueue()

    def stop(self):
        self._stop_event.set()
        self._cmd_queue.put("stop")  # unblock wait

    def queue_scan(self, library, path=None):
        path = escape_param(path) if path else ""
        cmd = "UpdateLibrary(%s,%s,%s)" % (library, path,
                                           settings.SHOW_PROGRESS_DIALOG)
        self._cmd_queue.put(cmd)

    def queue_clean(self, library):
        self._cmd_queue.put("CleanLibrary(%s,%s)"
                            % (library, settings.SHOW_PROGRESS_DIALOG))

    def queue_remove(self, library, path=None):
        if settings.REMOVAL_ENABLED:
            if settings.PER_FILE_REMOVE and path and library == 'video':
                videolibrary.remove_video(path)
            else:
                self._cmd_queue.put("CleanLibrary(%s,%s)"
                                    % (library, settings.SHOW_PROGRESS_DIALOG))

    def run(self):
        player = xbmc.Player()
        while True:
            self._cmd_queue.wait()
            if self._stop_event.wait(settings.SCAN_DELAY):
                return
            while player.isPlaying():
                self._stop_event.wait(1)
            if self._stop_event.is_set():
                return

            # Remove item right before it's executed so that any duplicates of
            # this commands gets flushed from the queue.
            cmd = self._cmd_queue.get_nowait()
            log("[xbmcif] executing builtin: '%s'" % cmd)
            xbmc.executebuiltin(cmd.encode('utf-8'))

            # wait for scan to start. we need a timeout or else we a screwed
            # if we missed it.
            # TODO: replace this crap with Monitor callbacks in Helix
            log("[xbmcif] waiting for scan/clean start..")
            timeout = 3000
            while not xbmc.getCondVisibility('Library.IsScanning') \
                    and not self._stop_event.is_set():
                xbmc.sleep(100)
                timeout -= 100
                if timeout <= 0:
                    log("[xbmcif] wait for scan/clean timed out.")
                    break
            log("[xbmcif] scan/clean started.")
            log("[xbmcif] waiting for scan/clean end..")

            # wait for scan to end
            while xbmc.getCondVisibility('Library.IsScanning') \
                    and not self._stop_event.is_set():
                xbmc.sleep(100)
            log("[xbmcif] scan/clean ended.")