Skip to content

Commit f115859

Browse files
committed
Code review suggestions
1 parent 571d51d commit f115859

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

changehc/delphi_changehc/download_ftp_files.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,34 @@
1010
# third party
1111
import paramiko
1212

13-
class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
14-
def missing_host_key(self, client, hostname, key):
15-
return
16-
1713

1814
def print_callback(filename, bytes_so_far, bytes_total):
15+
"""Log file transfer progress"""
1916
rough_percent_transferred = int(100 * (bytes_so_far / bytes_total))
2017
if (rough_percent_transferred % 25) == 0:
2118
print(f'{filename} transfer: {rough_percent_transferred}%')
2219

2320

2421
def get_files_from_dir(sftp, out_path):
22+
"""Download files from sftp server that have been uploaded in last day
23+
Args:
24+
sftp: SFTP Session from Paramiko client
25+
out_path: Path to local directory into which to download the files
26+
"""
27+
2528
current_time = datetime.datetime.now()
26-
seconds_in_day = 24 * 60 * 60
2729

2830
# go through files in recieving dir
29-
files_to_download = []
31+
filepaths_to_download = {}
3032
for fileattr in sftp.listdir_attr():
3133
file_time = datetime.datetime.fromtimestamp(fileattr.st_mtime)
32-
time_diff_to_current_time = current_time - file_time
33-
if time_diff_to_current_time.total_seconds() <= seconds_in_day:
34-
files_to_download.append(fileattr.filename)
35-
36-
filepaths_to_download = {}
37-
for file in files_to_download:
38-
full_path = path.join(out_path, file)
39-
if path.exists(full_path):
40-
print(f"{file} exists, skipping")
41-
else:
42-
filepaths_to_download[file] = full_path
34+
filename = fileattr.filename
35+
if current_time - file_time < datetime.timedelta(days=1) and \
36+
not path.exists(filename):
37+
filepaths_to_download[filename] = path.join(out_path, filename)
4338

4439
# make sure we don't download more than 2 files per day
45-
assert len(files_to_download) <= 2, "more files dropped than expected"
40+
assert len(filepaths_to_download) <= 2, "more files dropped than expected"
4641

4742
# download!
4843
for infile, outfile in filepaths_to_download.items():
@@ -51,21 +46,29 @@ def get_files_from_dir(sftp, out_path):
5146

5247

5348
def download(out_path, ftp_conn):
49+
"""Downloads files necessary to create CHC signal from ftp server.
50+
Args:
51+
out_path: Path to local directory into which to download the files
52+
ftp_conn: Dict containing login credentials to ftp server
53+
"""
5454

5555
# open client
56-
client = paramiko.SSHClient()
57-
client.set_missing_host_key_policy(AllowAnythingPolicy())
58-
59-
client.connect(ftp_conn["host"], username=ftp_conn["user"],
60-
password=ftp_conn["pass"][1:] + ftp_conn["pass"][0],
61-
port=ftp_conn["port"],
62-
allow_agent=False, look_for_keys=False)
63-
sftp = client.open_sftp()
64-
65-
sftp.chdir('/dailycounts/All_Outpatients_By_County')
66-
get_files_from_dir(sftp, out_path)
67-
68-
sftp.chdir('/dailycounts/Covid_Outpatients_By_County')
69-
get_files_from_dir(sftp, out_path)
70-
71-
client.close()
56+
try:
57+
client = paramiko.SSHClient()
58+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
59+
60+
client.connect(ftp_conn["host"], username=ftp_conn["user"],
61+
password=ftp_conn["pass"][1:] + ftp_conn["pass"][0],
62+
port=ftp_conn["port"],
63+
allow_agent=False, look_for_keys=False)
64+
sftp = client.open_sftp()
65+
66+
sftp.chdir('/dailycounts/All_Outpatients_By_County')
67+
get_files_from_dir(sftp, out_path)
68+
69+
sftp.chdir('/dailycounts/Covid_Outpatients_By_County')
70+
get_files_from_dir(sftp, out_path)
71+
72+
finally:
73+
if client:
74+
client.close()

0 commit comments

Comments
 (0)