Skip to content

Commit

Permalink
Removing deprecated urlretrieve to fix download_file issue (#2234)
Browse files Browse the repository at this point in the history
  • Loading branch information
raph-luc authored Nov 10, 2023
1 parent 32c44a2 commit 5c81a34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"pyyaml>=6.0",
"docker>=6.1.3",
"psutil>=5.9.5",
"requests>=2.31.0",
]

extras_require = {
Expand Down
21 changes: 14 additions & 7 deletions src/ansys/fluent/core/examples/downloads.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
"""Functions to download sample datasets from the Ansys example data repository."""
import logging
import os
from pathlib import Path
import re
import shutil
from typing import Optional
import urllib.request
import zipfile

import requests

import ansys.fluent.core as pyfluent

logger = logging.getLogger("pyfluent.networking")


def delete_downloads():
"""Delete all downloaded examples from the default examples folder to free space or
Expand Down Expand Up @@ -55,30 +59,33 @@ def _retrieve_file(
local_path_no_zip = re.sub(".zip$", "", local_path)
file_name_no_zip = re.sub(".zip$", "", file_name)
# First check if file has already been downloaded
print("Checking if specified file already exists...")
logger.info(f"Checking if {local_path_no_zip} already exists...")
if os.path.isfile(local_path_no_zip) or os.path.isdir(local_path_no_zip):
print(f"File already exists. File path:\n{local_path_no_zip}")
logger.info("File already exists.")
if return_without_path:
return file_name_no_zip
else:
return local_path_no_zip

print("File does not exist. Downloading specified file...")
logger.info("File does not exist. Downloading specified file...")

# Check if save path exists
if not os.path.exists(save_path):
os.makedirs(save_path)

# grab the correct url retriever
urlretrieve = urllib.request.urlretrieve
# Download file
logger.info(f'Downloading URL: "{url}"')
content = requests.get(url).content
with open(local_path, "wb") as f:
f.write(content)

# Perform download
urlretrieve(url, filename=local_path)
if local_path.endswith(".zip"):
_decompress(local_path)
local_path = local_path_no_zip
file_name = file_name_no_zip
print(f"Download successful. File path:\n{local_path}")
logger.info("Download successful.")
if return_without_path:
return file_name
else:
Expand Down

0 comments on commit 5c81a34

Please sign in to comment.