From a027e0897b85f28b03305695bb58250b88ea9555 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Wed, 20 Mar 2024 15:14:52 -0500 Subject: [PATCH] Wrap ftp download within tqdm to show status. --- kghub_downloader/download_utils.py | 38 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/kghub_downloader/download_utils.py b/kghub_downloader/download_utils.py index d7e5334..c03cbf1 100644 --- a/kghub_downloader/download_utils.py +++ b/kghub_downloader/download_utils.py @@ -321,23 +321,27 @@ def download_via_ftp(ftp_server, current_dir, local_dir, glob_pattern=None): # List items in the current directory items = ftp_server.nlst() - for item in items: - # Check if the item is a directory - if is_directory(ftp_server, item): - # Recursively download from the found directory - download_via_ftp( - ftp_server, item, os.path.join(local_dir, item), glob_pattern - ) - # Go back to the parent directory - ftp_server.cwd("..") - else: - # Check if the file matches the pattern - if is_matching_filename(item, glob_pattern): - # Download the file - local_filepath = os.path.join(local_dir, item) - os.makedirs(os.path.dirname(local_filepath), exist_ok=True) - with open(local_filepath, "wb") as f: - ftp_server.retrbinary(f"RETR {item}", f.write) + # Initialize tqdm progress bar + with tqdm(total=len(items), desc=f"Downloading from {current_dir} via ftp") as pbar: + for item in items: + # Check if the item is a directory + if is_directory(ftp_server, item): + # Recursively download from the found directory + download_via_ftp( + ftp_server, item, os.path.join(local_dir, item), glob_pattern + ) + # Go back to the parent directory + ftp_server.cwd("..") + else: + # Check if the file matches the pattern + if is_matching_filename(item, glob_pattern): + # Download the file + local_filepath = os.path.join(local_dir, item) + os.makedirs(os.path.dirname(local_filepath), exist_ok=True) + with open(local_filepath, "wb") as f: + ftp_server.retrbinary(f"RETR {item}", f.write) + # Update the progress bar after each item is processed + pbar.update(1) except error_perm as e: # Handle permission errors print(f"Permission denied: {e}")