Skip to content

Commit 7c7f323

Browse files
Add timeout parameter and adjust directory handling
1 parent b0c379e commit 7c7f323

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

tasks/ftp/retrieve_from_ftp.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(
1616
username: str,
1717
password: str,
1818
local_dir: str = "./",
19+
timeout: int = 30,
1920
*args,
2021
**kwargs,
2122
):
@@ -28,6 +29,7 @@ def __init__(
2829
username (str): Username for FTP authentication.
2930
password (str): Password for FTP authentication.
3031
local_dir (str, optional): Local directory to save the retrieved file. Defaults to "./".
32+
timeout (int, optional): Timeout value for FTP connection measued in seconds. Defaults to 30 seconds.
3133
*args: Variable length argument list.
3234
**kwargs: Arbitrary keyword arguments.
3335
"""
@@ -37,6 +39,7 @@ def __init__(
3739
self.username = username
3840
self.password = password
3941
self.local_dir = local_dir
42+
self.timeout = timeout
4043

4144
def run(self) -> Result:
4245
"""
@@ -55,16 +58,16 @@ def run(self) -> Result:
5558
5659
"""
5760
try:
58-
ftp = FTP(self.ftp_url, timeout=30) # Modify timeout as needed
61+
if not os.path.isdir(self.local_dir):
62+
os.makedirs(self.local_dir, exist_ok=True)
63+
64+
ftp = FTP(self.ftp_url, timeout=self.timeout) # Modify timeout as needed
5965
ftp.login(user=self.username, passwd=self.password)
6066

6167
remote_dir, remote_filename = os.path.split(self.ftp_remote_filepath)
6268
if remote_dir:
6369
ftp.cwd(remote_dir)
6470

65-
if not os.path.isdir(self.local_dir):
66-
return Failure(f"Local directory does not exist: {self.local_dir}")
67-
6871
local_filepath = os.path.join(self.local_dir, remote_filename)
6972
with open(local_filepath, "wb") as f:
7073
ftp.retrbinary(f"RETR {remote_filename}", f.write)

tasks/ftp/upload_to_ftp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(
1616
username: str,
1717
password: str,
1818
destination_dir: str = "/",
19+
timeout: int = 30,
1920
*args,
2021
**kwargs,
2122
):
@@ -28,13 +29,15 @@ def __init__(
2829
username (str): Username credential for FTP auth.
2930
password (str): Password credential for FTP auth.
3031
destination_dir (str): Destination directory on the FTP server where the file will be uploaded to. Defaults to "/".
32+
timeout (int, optional): Timeout value for FTP connection measued in seconds. Defaults to 30 seconds.
3133
"""
3234
super().__init__(*args, **kwargs)
3335
self.local_filepath = local_filepath
3436
self.ftp_url = ftp_url
3537
self.username = username
3638
self.password = password
3739
self.destination_dir = destination_dir
40+
self.timeout = timeout
3841

3942
def run(self) -> Result:
4043
"""
@@ -57,7 +60,7 @@ def run(self) -> Result:
5760
if not os.path.isfile(self.local_filepath):
5861
return Failure(f"Local file does not exist: {self.local_filepath}")
5962

60-
ftp = FTP(self.ftp_url, timeout=30) # Modify timeout as needed
63+
ftp = FTP(self.ftp_url, timeout=self.timeout) # Modify timeout as needed
6164
ftp.login(user=self.username, passwd=self.password)
6265

6366
if self.destination_dir:

0 commit comments

Comments
 (0)