Ejemplo n.º 1
0
    def on_any_event(self, event):
        """
        Run the functions on any event
        """
        logger.info("Got {} event for path {}. Event Type: {}".format(event.event_type,
            event.src_path, str(event.event_type)))

        try:
            self.last_run = datetime.datetime.now()

            run_function_list(self.fns)
            if self.callback:
                self.callback(event, self.fns)
        except:
            logger.exception(
                "An error occurred in watch. Event {} for {}".format(event.event_type,
                                                                     event.src_path),
                )
Ejemplo n.º 2
0
def watch(fns=None, path=None, files=None, run_immediately=True,
          recursive=True, throttle=1, keep_alive=True, poll_timeout=0, callback=None, predicate=None, handler=None):
    """

    Runs the given function list when items at path change

    :param fns: function list that will be run when path changes. Can be either a
                dict with 3 keys: fn, args, kwargs
                OR can be a partial function (using functools.partial)
                OR can be a list of either/both of those things.
    :param path: The path for the watchdog to observe,
                 defaults to '.'
    :param files: The files to watch. File list will be retrieved using util.get_files,
                  If this is a dict and no path is specified, path will be passed in to files.
    :param run_immediately: True to run all functions immediately
    :param recursive: true to watch files in path recursively
    :param throttle: number of seconds to throttle each run
    :param keep_alive: true to keep build script alive for this watch
    :param poll_timeout: In certain setups, such as vagrant, FS notifications don't work.
                         Set this to a positive integer to force polling the FS.
                         This will also set the default blocking timeout.
    :param callback: Callback to run after function list
    :param predicate: If you want to use a custom predicate
                      to determine if you want to run the
                      function list, you can pass a function
                      that takes an event and a watcher,
                      returning a boolean to run function
                      list or callbacks
    :param handler: the watch handler to use
    """

    logger.debug("Registering watch function")

    path = path or "."

    if run_immediately:
        run_function_list(fns)

    if files:
        normalize_files_dict(files, path)

    def predikate(event, watcher):
        if predicate and not predicate(event, watcher):
            return False

        if not watcher.files:
            return True
        files = get_files(watcher.files)

        found = getattr(event, 'dest_path', None) in files
        return found or event.src_path in files

    handler = handler or WatchHandler(fns,
                                      throttle=throttle,
                                      files=files,
                                      callback=callback,
                                      predicate=predikate)

    observer = PollingObserver(timeout=poll_timeout) if poll_timeout else Observer()
    observer.schedule(handler, path=path, recursive=recursive)
    observer.start()

    if keep_alive:
        register_keep_alive()