|
1 | 1 | """Functions to download sample datasets from the Ansys example data repository.""" |
| 2 | +import logging |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
4 | 5 | import re |
5 | 6 | import shutil |
6 | 7 | from typing import Optional |
7 | | -import urllib.request |
8 | 8 | import zipfile |
9 | 9 |
|
| 10 | +import requests |
| 11 | + |
10 | 12 | import ansys.fluent.core as pyfluent |
11 | 13 |
|
| 14 | +logger = logging.getLogger("pyfluent.networking") |
| 15 | + |
12 | 16 |
|
13 | 17 | def delete_downloads(): |
14 | 18 | """Delete all downloaded examples from the default examples folder to free space or |
@@ -55,30 +59,33 @@ def _retrieve_file( |
55 | 59 | local_path_no_zip = re.sub(".zip$", "", local_path) |
56 | 60 | filename_no_zip = re.sub(".zip$", "", filename) |
57 | 61 | # First check if file has already been downloaded |
58 | | - print("Checking if specified file already exists...") |
| 62 | + logger.info(f"Checking if {local_path_no_zip} already exists...") |
59 | 63 | if os.path.isfile(local_path_no_zip) or os.path.isdir(local_path_no_zip): |
60 | 64 | print(f"File already exists. File path:\n{local_path_no_zip}") |
| 65 | + logger.info("File already exists.") |
61 | 66 | if return_only_filename: |
62 | 67 | return filename_no_zip |
63 | 68 | else: |
64 | 69 | return local_path_no_zip |
65 | 70 |
|
66 | | - print("File does not exist. Downloading specified file...") |
| 71 | + logger.info("File does not exist. Downloading specified file...") |
67 | 72 |
|
68 | 73 | # Check if save path exists |
69 | 74 | if not os.path.exists(save_path): |
70 | 75 | os.makedirs(save_path) |
71 | 76 |
|
72 | | - # grab the correct url retriever |
73 | | - urlretrieve = urllib.request.urlretrieve |
| 77 | + # Download file |
| 78 | + logger.info(f'Downloading URL: "{url}"') |
| 79 | + content = requests.get(url).content |
| 80 | + with open(local_path, "wb") as f: |
| 81 | + f.write(content) |
74 | 82 |
|
75 | | - # Perform download |
76 | | - urlretrieve(url, filename=local_path) |
77 | 83 | if local_path.endswith(".zip"): |
78 | 84 | _decompress(local_path) |
79 | 85 | local_path = local_path_no_zip |
80 | 86 | filename = filename_no_zip |
81 | 87 | print(f"Download successful. File path:\n{local_path}") |
| 88 | + logger.info("Download successful.") |
82 | 89 | if return_only_filename: |
83 | 90 | return filename |
84 | 91 | else: |
|
0 commit comments