<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def commit(self, **params):
 """ Rebuild kevent operations by removing open files that no longer need to be watched, and adding new files if they are not currently being watched. This is done by comparing self.paths to self.paths_open. """

        log = self._getparam('log', self._discard, **params)

        #  Find all the modules that no longer need watching
        #
        removed = 0
        added = 0
        for path in list(self.paths_open):
            if path not in self.paths:
                fd = self.paths_open[path]
                if self._mode == WF_KQUEUE:
                    #  kevent automatically deletes the event when the fd is closed
                    try:
                        os.close(fd)
                    except Exception as e:
                        log.warning("close failed on watched file %r -- %s", path, e)
                elif self._mode == WF_INOTIFYX:
                    try:
                        pynotifyx.rm_watch(self._inx_fd, fd)
                    except Exception as e:
                        log.warning("remove failed on watched file %r -- %s", path, e)
                    if path in self._inx_inode:
                        del self._inx_inode[path]
                elif self._mode == WF_POLLING:
                    if fd in self._poll_stat:
                        del self._poll_stat[fd]
                    else:
                        log.warning("fd watched path %r missing from _poll_stat map", path)
                    try:
                        os.close(fd)
                    except Exception as e:
                        log.warning("close failed on watched file %r -- %s", path, e)
                if fd in self.fds_open:
                    del self.fds_open[fd]
                else:
                    log.warning("fd watched path %r missing from fd map", path)
                del self.paths_open[path]
                log.debug("Removed watch for path %r", path)
                removed += 1

        #  Find all the paths that are new and should be watched
        #
        fdlist = []
        failed = []
        last_exc = None
        log.debug("%d watched path%s", len(self.paths), ses(len(self.paths)))
        for path in list(self.paths):
            if path not in self.paths_open:
                try:
                    if not self._add_file(path, **params):
                        continue
                except Exception as e:
                    last_exc = e
                    failed.append(path)
                    continue
                fdlist.append(self.paths_open[path])

                if path in self.paths_pending:
                    log.debug("pending path %r has now appeared", path)
                    del self.paths_pending[path]
                    self._trigger(self.paths_open[path], **params)

                added += 1
                log.debug("Added watch for path %r with ident %d", path, self.paths_open[path])
        if failed:
            self._clean_failed_fds(fdlist)
            raise Exception("Failed to set watch on %s -- %s" % (str(failed), str(last_exc)))
        log.debug("%d added, %d removed", added, removed)