ImplicitMount¶
- class pyremotedata.implicit_mount.ImplicitMount(user: str | None = None, remote: str | None = None, verbose: bool = False)
Bases:
object
This is a low-level wrapper of LFTP, which provides a pythonic interface for executing LFTP commands and reading the output. It provides a robust and efficient backend for communicating with a remote storage server using the SFTP protocol, using a persistent LFTP shell handled in the background by a subprocess. It is designed to be used as a base class for higher-level wrappers, such as the IOHandler class, or as a standalone class for users familiar with LFTP.
OBS: The attributes of this method should not be used unless for development or advanced use cases, all responsibility in this case is on the user.
TODO: This class relies on a proper SSH setup on your machine (and the remote server) for passwordless SFTP. Thoroughly test this on a fresh install, and add instructions, and the possibility for automatic setup, for setting up passwordless SFTP and SSH keys, as well as proper error handling for when this is not set up correctly.
TODO: Add further arguments such as port, password, etc. to the constructor.
- Parameters:
user (str) – The username to use for connecting to the remote directory.
remote (str) – The remote server to connect to.
verbose (bool) – If True, print the commands executed by the class.
- execute_command(command: str, output: bool = True, blocking: bool = True, execute: bool = True, default_args: dict | None = None, **kwargs) str | List[str] | None
Executes a command on the LFTP shell.
- Parameters:
command (str) – The command to execute.
output (bool) – If True, the function will return the output of the command.
blocking (bool) – If True, the function will block until the command is complete. If output is True, blocking must also be True.
execute (bool) – If True, the function will execute the command, otherwise it will return the command as a string.
default_args (Union[dict, None]) – A dictionary of default arguments to use for the command. If None, no default arguments will be used.
**kwargs – Keyword arguments to pass to the command.
- Returns:
If execute is False, the function will return the command as a string. If output is True, returns a string, list or None depending of the number of output lines, otherwise returns None.
- Return type:
Union[str, List[str], None]
- static format_options(**kwargs) str
Takes a dictionary of options and formats them into a string of command line arguments suitable for LFTP.
- Parameters:
**kwargs – Keyword arguments to format.
- Returns:
The formatted arguments.
- Return type:
str
- lcd(local_path: str) str
Changes the current local directory using the LFTP command lcd.
- Parameters:
local_path (str) – The local directory to change to.
- lls(local_path: str, **kwargs) List[str]
Find all files in the given local directory using the LFTP command !ls or !find.
OBS: This function should probably not be used, just use the standard OS commands instead.
- lpwd() str
Get the current local directory using the LFTP command lpwd.
- Returns:
The current local directory.
- Return type:
str
- ls(path: str = '.', recursive: bool = False, use_cache: bool = True) List[str]
Find all files in the given remote directory using the LFTP command cls. Can be used recursively, even though LFTP does not support recursive listing with the cls command.
- Parameters:
path (str) – The remote directory to search in.
recursive (bool) – If True, the function will search recursively.
use_cache (bool) – If True, the function will use cls, otherwise it will use recls. recls forces a refresh of the cache.
- Returns:
If the directory is empty, the function will return None, if the directory contains one file, the function will return a string, otherwise it will return a list of strings.
- Return type:
Union[None, str, List[str]]
- mirror(remote_path: str, local_destination: str, blocking: bool = True, execute: bool = True, do_return: bool = True, **kwargs) None | List[str]
Download a directory from the remote directory to the given local destination using the LFTP mirror command.
- Parameters:
remote_path (str) – The remote directory to download.
local_destination (str) – The local destination to download the directory to.
blocking (bool) – If True, the function will block until the download is complete.
execute (bool) – If True, the function will execute the mirror command, otherwise it will return the command as a string.
do_return (bool) – If True, the function will return a list of the newly downloaded files.
**kwargs – Keyword arguments to pass to the mirror command.
- Returns:
If do_return is True, the function will return a list of the newly downloaded files, otherwise it will return None.
- Return type:
Union[None, List[str]]
- mount(lftp_settings: dict | None = None) None
Mount the remote directory.
- Parameters:
lftp_settings (Union[dict, None]) – A dictionary of LFTP settings to use for mounting the remote directory. If None, the default settings will be used.
- Raises:
Exception – If the subprocess fails to start.
RuntimeError – If the connection to the remote directory fails.
- pget(remote_path: str, local_destination: str, blocking: bool = True, execute: bool = True, output: bool | None = None, **kwargs)
Download a single file from the remote directory using the LFTP command pget.
- Parameters:
remote_path (str) – The remote file to download.
local_destination (str) – The local destination to download the file to.
blocking (bool) – If True, the function will block until the download is complete.
execute (bool) – If True, the function will execute the pget command, otherwise it will return the command as a string.
output (Union[bool, None]) – If True, the function will return the absolute local path of the downloaded file, otherwise it will return None.
- Returns:
If output is True, the function will return the absolute local path of the downloaded file, otherwise it will return None.
- Return type:
Union[None, str]
- put(local_path: str, remote_destination: str | None = None, blocking: bool = True, execute: bool = True, output: bool | None = None, **kwargs)
Upload a single file to the remote directory using the LFTP command put.
TODO: Is it really just a single file?
- Parameters:
local_path (str) – The local file to upload.
remote_destination (str, optional) – The remote destination to upload the file to. If None, the file will be uploaded to the current remote directory.
blocking (bool) – If True, the function will block until the upload is complete.
execute (bool) – If True, the function will execute the put command, otherwise it will return the command as a string.
output (Union[bool, None]) – If True, the function will return the absolute remote path of the uploaded file, otherwise it will return None.
**kwargs – Keyword arguments to pass to the put command.
- pwd() str
Get the current remote directory using the LFTP command pwd.
- Returns:
The current remote directory.
- Return type:
str
- unmount(timeout: float = 1) None
Unmount the remote directory.
- Parameters:
timeout (float) – The maximum time to wait for the lftp shell to terminate. If the timeout is exceeded, the lftp shell will be forcefully terminated.