FileClient#

class atomate2.utils.file_client.FileClient(key_filename: str | Path = '~/.ssh/id_rsa', config_filename: str | Path = '~/.ssh/config')[source]#

Bases: object

Tool for performing operations on files.

The client is agnostic of whether file operations are happening locally or via SSH. All operations have a host parameter that specifies which remote host the operation should be performed on.

Note

To use abbreviated host names without user information, the FileClient requires the appropriate configuration to be defined in the ssh config file.

Parameters
key_filenamestr or Path

Path to private key file (for remote connections only).

config_filenamestr or Path

Path to OpenSSH config file defining host connection settings.

connect(host: str)[source]#

Connect to a remote host.

Parameters
hoststr

A remote host filesystem. Supports using hosts defined in the ssh config file. The host can be specified as either “username@remote_host” or just “remote_host” in which case the username will be inferred from the current user.

get_ssh(host: str) paramiko.client.SSHClient[source]#

Get an SSH connection to a host.

Parameters
hoststr

A remote host filesystem. Supports using hosts defined in the ssh config file. The host can be specified as either “username@remote_host” or just “remote_host” in which case the username will be inferred from the current user.

Returns
SSHClient

An ssh client to the host.

get_sftp(host: str) paramiko.sftp_client.SFTPClient[source]#

Get an SFTP connection to a host.

Parameters
hoststr

A remote host filesystem. Supports using hosts defined in the ssh config file. The host can be specified as either “username@remote_host” or just “remote_host” in which case the username will be inferred from the current user.

Returns
SFTPClient

An sftp client to the host.

exists(path: str | Path, host: Optional[str] = None) bool[source]#

Check whether a file exists.

Parameters
pathstr or Path

A path to check existence of.

hoststr or None

A remote file system host on which to perform file operations.

Returns
bool

Whether the file exists.

is_file(path: str | Path, host: Optional[str] = None) bool[source]#

Whether a path is a file.

Parameters
pathstr or Path

A path.

hoststr or None

A remote file system host on which to perform file operations.

Returns
bool

Whether the path is a file.

is_dir(path: str | Path, host: Optional[str] = None) bool[source]#

Whether a path is a directory.

Parameters
pathstr or Path

A path.

hoststr or None

A remote file system host on which to perform file operations.

Returns
bool

Whether the path is a directory.

listdir(path: str | Path, host: Optional[str] = None) list[Path][source]#

Get the directory listing.

Parameters
pathstr or Path

Full path to the directory.

hoststr or None

A remote file system host on which to perform file operations.

Returns
list of Path

List of filenames and directories.

copy(src_filename: str | Path, dest_filename: str | Path, src_host: Optional[str] = None, dest_host: Optional[str] = None)[source]#

Copy a file from source to destination.

Parameters
src_filenamestr or Path

Full path to source file.

dest_filenamestr or Path

Full path to destination file.

src_hoststr or None

A remote file system host for the source file.

dest_hoststr or None

A remote file system host for the destination file.

remove(path: str | Path, host: Optional[str] = None)[source]#

Remove a file (does not work on directories).

Parameters
pathstr or Path

Path to a file.

hoststr or None

A remote file system host on which to perform file operations.

rename(old_path: str | Path, new_path: str | Path, host: Optional[str] = None)[source]#

Rename (move) a file.

Parameters
old_pathstr or Path

Path to an existing file.

new_pathstr or Path

Requested path to new file.

hoststr or None

A remote file system host on which to perform file operations.

abspath(path: str | Path, host: Optional[str] = None) Path[source]#

Get the absolute path.

Parameters
pathstr or Path

A path to a file or directory.

hoststr or None

A remote file system host on which to perform file operations.

Returns
Path

The absolute file path.

glob(path: str | Path, host: Optional[str] = None) list[Path][source]#

Glob files and folders.

Parameters
pathstr or Path

A path to glob.

hoststr or None

A remote file system host on which to perform file operations.

Returns
list[Path]

A list of globs files and directories.

gzip(path: str | Path, host: Optional[str] = None, compresslevel: int = 6, force: bool = False)[source]#

Gzip a file.

Parameters
pathstr or Path

Path to a file to gzip.

hoststr or None

A remote file system host on which to perform file operations.

compresslevelbool

Level of compression, 1-9. 9 is default for GzipFile, 6 is default for gzip.

forcebool

Overwrite gzipped file if it already exists.

gunzip(path: str | Path, host: Optional[str] = None, force: bool = False)[source]#

Ungzip a file.

Parameters
pathstr or Path

Path to a file to gzip.

hoststr or None

A remote file system host on which to perform file operations.

forcebool

Overwrite non-gzipped file if it already exists.

close()[source]#

Close all connections.