flexget.components.ftp.sftp_client module#

exception flexget.components.ftp.sftp_client.SftpError[source]#

Bases: Exception

class flexget.components.ftp.sftp_client.HandlerBuilder(sftp, url_prefix, private_key, private_key_pass, host_key)[source]#

Bases: object

Class for building pysftp.Connection.walktree node handlers.

Parameters:
  • sftp (pysftp.Connection) – A Connection object

  • logger – a logger object

  • url_prefix (str) – SFTP URL prefix

  • private_key (str | None)

  • private_key_pass (str | None)

  • host_key (HostKey | None)

get_dir_handler(get_size, files_only, entry_accumulator)[source]#

Build a file node handler suitable for use with pysftp.Connection.walktree.

Parameters:
  • get_size (bool) – boolean indicating whether to compute the for each file

  • files_only (bool) – Boolean indicating whether to skip directories

  • entry_accumulator (list) – list to add entries to

Return type:

Callable[[str], None]

get_file_handler(get_size, dirs_only, entry_accumulator)[source]#

Build a file node handler suitable for use with pysftp.Connection.walktree.

Parameters:
  • get_size (bool) – boolean indicating whether to compute the for each file

  • dirs_only (bool) – boolean indicating whether to skip files

  • entry_accumulator (list) – list to add entries to

Return type:

Callable[[str], None]

get_null_handler()[source]#

Build a noop node handler suitable for use with pysftp.Connection.walktree.

Return type:

Callable[[str], None]

get_unknown_handler()[source]#

Build an unknown node handler suitable for use with pysftp.Connection.walktree.

Return type:

Callable[[str], None]

class flexget.components.ftp.sftp_client.Handlers[source]#

Bases: object

classmethod _accumulate_file_size(sftp, size_accumulator, path)[source]#
Parameters:
  • sftp (pysftp.Connection)

  • size_accumulator (list[int])

  • path (str)

Return type:

None

classmethod _dir_size(sftp, path)[source]#
Parameters:
  • sftp (pysftp.Connection)

  • path (str)

Return type:

int

classmethod handle_directory(sftp, prefix, get_size, files_only, private_key, private_key_pass, host_key, entry_accumulator, path)[source]#

Directory node handler. Adds a directory entry to entry_accumulator.

Parameters:
  • sftp (pysftp.Connection) – A pysftp.Connection object

  • logger – a logger object

  • prefix (str) – SFTP URL prefix

  • get_size (bool) – boolean indicating whether to compute the size of each directory

  • files_only (bool) – Boolean indicating whether to skip directories

  • entry_accumulator (list[Entry]) – a list in which to store entries

  • private_key (str | None) – private key path

  • private_key_pass (str | None) – private key password

  • host_key (HostKey | None) – Host key for the remote server if not in known_hosts

  • path (str) – path to handle

Return type:

None

classmethod handle_file(sftp, prefix, get_size, dirs_only, private_key, private_key_pass, host_key, entry_accumulator, path)[source]#

File node handler. Adds a file entry to entry_accumulator.

Parameters:
  • sftp (pysftp.Connection) – A pysftp.Connection object

  • logger – a logger object

  • prefix (str) – SFTP URL prefix

  • get_size (bool) – boolean indicating whether to compute the size of each file

  • dirs_only (bool) – boolean indicating whether to skip files

  • private_key (str | None) – private key path

  • private_key_pass (str | None) – private key password

  • host_key (HostKey | None) – Host key for the remote server if not in known_hosts

  • entry_accumulator (list[Entry]) – a list in which to store entries

  • path (str) – path to handle

Return type:

None

static _file_size(sftp, path)[source]#

Get the size of a file node.

Parameters:
  • sftp (pysftp.Connection)

  • path (str)

Return type:

int

static _get_entry(sftp, prefix, size_handler, get_size, path, private_key, private_key_pass, host_key)[source]#
Parameters:
  • sftp (pysftp.Connection)

  • prefix (str)

  • size_handler (Callable[[str], int])

  • path (str)

  • private_key (str | None)

  • private_key_pass (str | None)

  • host_key (HostKey | None)

Return type:

Entry

static handle_unknown(path)[source]#

Handle unknown nodes; log a warning.

Parameters:
  • logger – a logger object

  • path (str) – path to handle

Return type:

None

static null_node_handler(path)[source]#

Handle generic noop node.

Parameters:
  • logger – a logger object

  • path (str) – path to handle

Returns:

Return type:

None

class flexget.components.ftp.sftp_client.HostKey(key_type, public_key)[source]#

Bases: object

Host key used to connect to a SFTP server if not defined in known_hosts.

Parameters:
  • key_type (str)

  • public_key (str)

key_type: str#
public_key: str#
class flexget.components.ftp.sftp_client.SftpClient(host, port, username, password=None, private_key=None, private_key_pass=None, host_key=None, connection_tries=3)[source]#

Bases: object

Parameters:
  • host (str)

  • port (int)

  • username (str)

  • password (str | None)

  • private_key (str | None)

  • private_key_pass (str | None)

  • host_key (HostKey | None)

  • connection_tries (int)

static _get_download_path(path, destination)[source]#
Parameters:
  • path (str)

  • destination (str)

Return type:

str

static _get_upload_path(source, to)[source]#
Parameters:
_connect(connection_tries)[source]#
Parameters:

connection_tries (int)

Return type:

pysftp.Connection

_download_file(destination, delete_origin, source)[source]#
Parameters:
  • destination (str)

  • delete_origin (bool)

  • source (str)

Return type:

None

_get_cnopts()[source]#
Return type:

pysftp.CnOpts | None

_get_prefix()[source]#

Generate SFTP URL prefix.

Return type:

str

_put_file(source, destination)[source]#
Parameters:
Return type:

None

_upload_file(source, to)[source]#
Parameters:
Return type:

None

close()[source]#

Close the sftp connection.

Return type:

None

download(source, to, recursive, delete_origin)[source]#

Download the file specified in “source” to the destination specified in “to”.

Parameters:
  • source (str) – path of the resource to download

  • to (str) – path of the directory to download to

  • recursive (bool) – indicates whether to download the contents of “source” recursively

  • delete_origin (bool) – indicates whether to delete the source resource upon download, is the source is a symlink, only the symlink will be removed rather than it’s target.

Return type:

None

is_dir(path)[source]#

Check if the node at a given path is a directory.

Parameters:

path (str) – path to check

Returns:

boolean indicating if the path is a directory

Return type:

bool

is_file(path)[source]#

Check if the node at a given path is a file.

Parameters:

path (str) – path to check

Returns:

boolean indicating if the path is a file

Return type:

bool

Check if the node at a given path is a directory.

Parameters:

path (str) – path to check

Returns:

boolean indicating if the path is a directory

Return type:

bool

list_directories(directories, recursive, get_size, files_only, dirs_only)[source]#

Build a list of entries from a provided list of directories on an SFTP server.

Parameters:
  • directories (list[str]) – list of directories to generate entries for

  • recursive (bool) – boolean indicating whether to list recursively

  • get_size (bool) – boolean indicating whether to compute size for each node (potentially slow for directories)

  • files_only (bool) – boolean indicating whether to exclude directories

  • dirs_only (bool) – boolean indicating whether to exclude files

Returns:

a list of entries describing the contents of the provided directories

Return type:

list[Entry]

make_dirs(path)[source]#

Build directories.

Parameters:

path (str) – path to build

Return type:

None

path_exists(path)[source]#

Check of a path exists.

Parameters:

path (str) – Path to check

Returns:

boolean indicating if the path exists

Return type:

bool

remove_dir(path)[source]#

Remove a directory if it’s empty.

Parameters:

path (str) – directory to remove

Return type:

None

remove_file(path)[source]#

Remove a file if it’s empty.

Parameters:

path (str) – file to remove

Return type:

None

set_socket_timeout(socket_timeout_sec)[source]#

Set the SFTP client socket timeout.

Parameters:

socket_timeout_sec – Socket timeout in seconds

upload(source, to)[source]#

Upload files or directories to an SFTP server.

Parameters:
  • source (Path) – file or directory to upload

  • to (str) – destination

Return type:

None

_handler_builder: HandlerBuilder#
_sftp: pysftp.Connection#
host: str#
host_key: HostKey | None#
password: str | None#
port: int#
prefix: str#
private_key: str | None#
private_key_pass: str | None#
username: str#
flexget.components.ftp.sftp_client._set_authentication_patch(self, password, private_key, private_key_pass)[source]#

Patch pysftp.Connection._set_authentication to support additional key types.