FileClient#

class atomate2.utils.file_client.FileClient(key_filename='~/.ssh/id_rsa', config_filename='~/.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_filename (str or Path) – Path to private key file (for remote connections only).

  • config_filename (str or Path) – Path to OpenSSH config file defining host connection settings.

connect(host)[source]#

Connect to a remote host.

Parameters:

host (str) – 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.

Return type:

None

get_ssh(host)[source]#

Get an SSH connection to a host.

Parameters:

host (str) – 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:

An ssh client to the host.

Return type:

.SSHClient

get_sftp(host)[source]#

Get an SFTP connection to a host.

Parameters:

host (str) – 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:

An sftp client to the host.

Return type:

.SFTPClient

exists(path, host=None)[source]#

Check whether a file exists.

Parameters:
  • path (str or Path) – A path to check existence of.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

Whether the file exists.

Return type:

bool

is_file(path, host=None)[source]#

Whether a path is a file.

Parameters:
  • path (str or Path) – A path.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

Whether the path is a file.

Return type:

bool

is_dir(path, host=None)[source]#

Whether a path is a directory.

Parameters:
  • path (str or Path) – A path.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

Whether the path is a directory.

Return type:

bool

listdir(path, host=None)[source]#

Get the directory listing.

Parameters:
  • path (str or Path) – Full path to the directory.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

List of filenames and directories.

Return type:

list of Path

copy(src_filename, dest_filename, src_host=None, dest_host=None)[source]#

Copy a file from source to destination.

Parameters:
  • src_filename (str or Path) – Full path to source file.

  • dest_filename (str or Path) – Full path to destination file.

  • src_host (str or None) – A remote file system host for the source file.

  • dest_host (str or None) – A remote file system host for the destination file.

Return type:

None

remove(path, host=None)[source]#

Remove a file (does not work on directories).

Parameters:
  • path (str or Path) – Path to a file.

  • host (str or None) – A remote file system host on which to perform file operations.

Return type:

None

rename(old_path, new_path, host=None)[source]#

Rename (move) a file.

Parameters:
  • old_path (str or Path) – Path to an existing file.

  • new_path (str or Path) – Requested path to new file.

  • host (str or None) – A remote file system host on which to perform file operations.

Return type:

None

abspath(path, host=None)[source]#

Get the absolute path.

Parameters:
  • path (str or Path) – A path to a file or directory.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

The absolute file path.

Return type:

Path

glob(path, host=None)[source]#

Glob files and folders.

Parameters:
  • path (str or Path) – A path to glob.

  • host (str or None) – A remote file system host on which to perform file operations.

Returns:

A list of globs files and directories.

Return type:

list[Path]

gzip(path, host=None, compresslevel=6, force=False)[source]#

Gzip a file.

Parameters:
  • path (str or Path) – Path to a file to gzip.

  • host (str or None) – A remote file system host on which to perform file operations.

  • compresslevel (bool) – Level of compression, 1-9. 9 is default for GzipFile, 6 is default for gzip.

  • force (bool) –

    How to handle writing a gzipped file if it already exists. Accepts either a string or bool:

    • ”force” or True: Overwrite gzipped file if it already exists.

    • ”raise” or False: Raise an error if file already exists.

    • ”skip” Skip file if it already exists.

Return type:

None

gunzip(path, host=None, force=False)[source]#

Ungzip a file.

Parameters:
  • path (str or Path) – Path to a file to gzip.

  • host (str or None) – A remote file system host on which to perform file operations.

  • force (bool) –

    How to handle writing a non-gzipped file if it already exists. Accepts either a string or bool:

    • ”force” or True: Overwrite non-gzipped file if it already exists.

    • ”raise” or False: Raise an error if file already exists.

    • ”skip” Skip file if it already exists.

Return type:

None

close()[source]#

Close all connections.

Return type:

None