iblqt.core.PathWatcher

class iblqt.core.PathWatcher[source]

Bases: QObject

Watch paths for changes.

Identical to QFileSystemWatcher but using Path instead of str for arguments and signals.

Call addPath() to watch a particular file or directory. Multiple paths can be added using the addPaths() function. Existing paths can be removed by using the removePath() and removePaths() functions.

PathWatcher examines each path added to it. Files that have been added to the PathWatcher can be accessed using the files() function, and directories using the directories() function.

The fileChanged() signal is emitted when a file has been modified, renamed or removed from disk. Similarly, the directoryChanged() signal is emitted when a directory or its contents is modified or removed. Note that PathWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.

Notes

  • On systems running a Linux kernel without inotify support, file systems that contain watched paths cannot be unmounted.

  • The act of monitoring files and directories for modifications consumes system resources. This implies there is a limit to the number of files and directories your process can monitor simultaneously. On all BSD variants, for example, an open file descriptor is required for each monitored file. Some system limits the number of open file descriptors to 256 by default. This means that addPath() and addPaths() will fail if your process tries to add more than 256 files or directories to the PathWatcher. Also note that your process may have other file descriptors open in addition to the ones for files being monitored, and these other open descriptors also count in the total. macOS uses a different backend and does not suffer from this issue.

__init__(parent, paths)[source]

Initialize the PathWatcher.

Parameters:
  • parent (QObject) – The parent object.

  • paths (list[Path] or list[str]) – Paths or directories to be watched.

addPath(path)[source]

Add path to the PathWatcher.

The path is not added if it does not exist, or if it is already being monitored by the PathWatcher.

If path specifies a directory, the directoryChanged() signal will be emitted when path is modified or removed from disk; otherwise the fileChanged() signal is emitted when path is modified, renamed or removed.

If the watch was successful, true is returned.

Reasons for a watch failure are generally system-dependent, but may include the resource not existing, access failures, or the total watch count limit, if the platform has one.

Note

There may be a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit is been reached, path will not be monitored, and false is returned.

Parameters:

path (Path or str) – Path or directory to be watched.

Returns:

True if the watch was successful, otherwise False.

Return type:

bool

addPaths(paths)[source]

Add each path in paths to the PathWatcher.

Paths are not added if they do not exist, or if they are already being monitored by the PathWatcher.

If a path specifies a directory, the directoryChanged() signal will be emitted when the path is modified or removed from disk; otherwise the fileChanged() signal is emitted when the path is modified, renamed, or removed.

The return value is a list of paths that could not be watched.

Reasons for a watch failure are generally system-dependent, but may include the resource not existing, access failures, or the total watch count limit, if the platform has one.

Note

There may be a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit has been reached, the excess paths will not be monitored, and they will be added to the returned list.

Parameters:

paths (list[Path] or list[str]) – Paths or directories to be watched.

Returns:

List of paths that could not be watched.

Return type:

list[Path]

directories()[source]

Return a list of paths to directories that are being watched.

Returns:

List of paths to directories that are being watched.

Return type:

list[Path]

files()[source]

Return a list of paths to files that are being watched.

Returns:

List of paths to files that are being watched.

Return type:

list[Path]

removePath(path)[source]

Remove the specified path from the PathWatcher.

If the watch is successfully removed, true is returned.

Reasons for watch removal failing are generally system-dependent, but may be due to the path having already been deleted, for example.

Parameters:

path (list[Path] or list[str]) – Path or directory to be removed from the PathWatcher.

Returns:

True if the watch was successful, otherwise False.

Return type:

bool

removePaths(paths)[source]

Remove the specified paths from the PathWatcher.

The return value is a list of paths which were not able to be unwatched successfully.

Reasons for watch removal failing are generally system-dependent, but may be due to the path having already been deleted, for example.

Parameters:

paths (list[Path] or list[str]) – Paths or directories to be unwatched.

Returns:

List of paths which were not able to be unwatched successfully.

Return type:

list[Path]

directoryChanged(Path): Signal

Emitted when a directory or its contents is modified or removed.

fileChanged(Path): Signal

Emitted when a file has been modified, renamed or removed from disk.