ftp_download.timings
1from .prefs import Conf, LocalLogger 2 3import os 4import ftplib 5import asyncio 6import types 7 8 9log = LocalLogger() 10 11 12def blocking_download_task( 13 ftp: ftplib.FTP, 14 remote_file_path: str, 15 local_path: str 16 ) -> None: 17 18 """ 19 Sychronous version of `download_task()`. 20 21 ### Args: 22 23 - **ftp** (`ftplib.FTP`): `ftplib.FTP` object logged in to the server that houses the file to be downloaded 24 - **remote_file_path** (`str`): path to the file on the remote server 25 - **local_path** (`str`): path to local folder where the file will be downloaded 26 """ # noqa 27 28 filename = os.path.split(remote_file_path)[1] 29 local_file_path = os.path.join(local_path, filename) 30 31 if Conf.verbose: 32 print(f"Retrieving {filename}...") 33 log.debug( 34 f"Trying to download {ftp.__dict__['host']}" + 35 f"{remote_file_path} with blocking task" 36 ) 37 38 with open(local_file_path, "wb") as local_file: 39 40 try: 41 ftp.retrbinary(f"RETR {remote_file_path}", local_file.write) 42 43 if Conf.verbose: 44 print(f"File saved: {local_file_path}") 45 log.info(f"File saved: {local_file_path}") 46 47 except Exception as UnexpectedError: 48 log.error(f"Could not download {filename}:\n{UnexpectedError}") 49 print(f"Could not download {filename}:\n{UnexpectedError}") 50 51 52async def download_task( 53 ftp: ftplib.FTP, 54 remote_file_path: str, 55 local_path: str 56 ) -> None: 57 58 """ 59 Asynchronous task that downloads a single file. Create a local file with the same filename found in `remote_file_path` inside `local_path`. 60 61 ### Args: 62 63 - **ftp** (`ftplib.FTP`): `ftplib.FTP` object logged in to the server that houses the file to be downloaded 64 - **remote_file_path** (`str`): path to the file on the remote server 65 - **local_path** (`str`): path to local folder where the file will be downloaded 66 """ # noqa 67 68 filename = os.path.split(remote_file_path)[1] 69 local_file_path = os.path.join(local_path, filename) 70 71 if Conf.verbose: 72 print(f"Retrieving {filename}...") 73 log.debug( 74 f"Trying to download {ftp.__dict__['host']}" + 75 f"{remote_file_path} with async task" 76 ) 77 78 with open(local_file_path, "wb") as local_file: 79 80 try: 81 ftp.retrbinary(f"RETR {remote_file_path}", local_file.write) 82 83 if Conf.verbose: 84 print(f"File saved: {local_file_path}") 85 log.info(f"File saved: {local_file_path}") 86 87 except Exception as UnexpectedError: 88 log.error(f"Could not download {filename}:\n{UnexpectedError}") 89 print(f"Could not download {filename}:\n{UnexpectedError}") 90 91 92async def run_multiple(tasks: list) -> None: 93 for task in tasks: 94 await task 95 96 97def run(coro): 98 99 """ 100 Emulate asyncio.run() on older versions 101 102 Adapted from: 103 https://stackoverflow.com/questions/55590343/asyncio-run-or-run-until-complete 104 """ # noqa 105 106 if not isinstance(coro, types.CoroutineType): 107 raise TypeError("run() requires a coroutine object") 108 109 loop = asyncio.new_event_loop() 110 asyncio.set_event_loop(loop) 111 try: 112 return loop.run_until_complete(coro) 113 finally: 114 loop.close() 115 asyncio.set_event_loop(None)
log =
<ftp_download.prefs.LocalLogger object>
def
blocking_download_task(ftp: ftplib.FTP, remote_file_path: str, local_path: str) -> None:
13def blocking_download_task( 14 ftp: ftplib.FTP, 15 remote_file_path: str, 16 local_path: str 17 ) -> None: 18 19 """ 20 Sychronous version of `download_task()`. 21 22 ### Args: 23 24 - **ftp** (`ftplib.FTP`): `ftplib.FTP` object logged in to the server that houses the file to be downloaded 25 - **remote_file_path** (`str`): path to the file on the remote server 26 - **local_path** (`str`): path to local folder where the file will be downloaded 27 """ # noqa 28 29 filename = os.path.split(remote_file_path)[1] 30 local_file_path = os.path.join(local_path, filename) 31 32 if Conf.verbose: 33 print(f"Retrieving {filename}...") 34 log.debug( 35 f"Trying to download {ftp.__dict__['host']}" + 36 f"{remote_file_path} with blocking task" 37 ) 38 39 with open(local_file_path, "wb") as local_file: 40 41 try: 42 ftp.retrbinary(f"RETR {remote_file_path}", local_file.write) 43 44 if Conf.verbose: 45 print(f"File saved: {local_file_path}") 46 log.info(f"File saved: {local_file_path}") 47 48 except Exception as UnexpectedError: 49 log.error(f"Could not download {filename}:\n{UnexpectedError}") 50 print(f"Could not download {filename}:\n{UnexpectedError}")
Sychronous version of download_task()
.
Args:
- ftp (
ftplib.FTP
):ftplib.FTP
object logged in to the server that houses the file to be downloaded - remote_file_path (
str
): path to the file on the remote server - local_path (
str
): path to local folder where the file will be downloaded
async def
download_task(ftp: ftplib.FTP, remote_file_path: str, local_path: str) -> None:
53async def download_task( 54 ftp: ftplib.FTP, 55 remote_file_path: str, 56 local_path: str 57 ) -> None: 58 59 """ 60 Asynchronous task that downloads a single file. Create a local file with the same filename found in `remote_file_path` inside `local_path`. 61 62 ### Args: 63 64 - **ftp** (`ftplib.FTP`): `ftplib.FTP` object logged in to the server that houses the file to be downloaded 65 - **remote_file_path** (`str`): path to the file on the remote server 66 - **local_path** (`str`): path to local folder where the file will be downloaded 67 """ # noqa 68 69 filename = os.path.split(remote_file_path)[1] 70 local_file_path = os.path.join(local_path, filename) 71 72 if Conf.verbose: 73 print(f"Retrieving {filename}...") 74 log.debug( 75 f"Trying to download {ftp.__dict__['host']}" + 76 f"{remote_file_path} with async task" 77 ) 78 79 with open(local_file_path, "wb") as local_file: 80 81 try: 82 ftp.retrbinary(f"RETR {remote_file_path}", local_file.write) 83 84 if Conf.verbose: 85 print(f"File saved: {local_file_path}") 86 log.info(f"File saved: {local_file_path}") 87 88 except Exception as UnexpectedError: 89 log.error(f"Could not download {filename}:\n{UnexpectedError}") 90 print(f"Could not download {filename}:\n{UnexpectedError}")
Asynchronous task that downloads a single file. Create a local file with the same filename found in remote_file_path
inside local_path
.
Args:
- ftp (
ftplib.FTP
):ftplib.FTP
object logged in to the server that houses the file to be downloaded - remote_file_path (
str
): path to the file on the remote server - local_path (
str
): path to local folder where the file will be downloaded
async def
run_multiple(tasks: list) -> None:
def
run(coro):
98def run(coro): 99 100 """ 101 Emulate asyncio.run() on older versions 102 103 Adapted from: 104 https://stackoverflow.com/questions/55590343/asyncio-run-or-run-until-complete 105 """ # noqa 106 107 if not isinstance(coro, types.CoroutineType): 108 raise TypeError("run() requires a coroutine object") 109 110 loop = asyncio.new_event_loop() 111 asyncio.set_event_loop(loop) 112 try: 113 return loop.run_until_complete(coro) 114 finally: 115 loop.close() 116 asyncio.set_event_loop(None)
Emulate asyncio.run() on older versions
Adapted from: https://stackoverflow.com/questions/55590343/asyncio-run-or-run-until-complete