-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tweak/enhance auto data install #50
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import sys | ||
import warnings | ||
from collections import OrderedDict | ||
from pathlib import Path | ||
|
||
import astropy.io.fits as fits | ||
import astropy.units as u | ||
|
@@ -179,16 +180,18 @@ def setup_logging(level='INFO', filename=None): | |
**************************************************************************** | ||
""" | ||
|
||
def get_default_data_path(): | ||
return Path.home() / "data" / "stpsf-data" | ||
|
||
|
||
def auto_download_stpsf_data(): | ||
import os | ||
import tarfile | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from urllib.request import urlretrieve | ||
|
||
# Create a default directory for the data files | ||
default_path = Path.home() / "data" / "stpsf-data" | ||
default_path = get_default_data_path() | ||
default_path.mkdir(parents=True, exist_ok=True) | ||
|
||
os.environ["STPSF_PATH"] = str(default_path) | ||
|
@@ -206,6 +209,7 @@ def auto_download_stpsf_data(): | |
# Extract the tarball | ||
with tarfile.open(filename, "r:gz") as tar: | ||
tar.extractall(default_path.parent, filter="fully_trusted") | ||
warnings.warn(f"STPSF data files downloaded and installed to {default_path}.") | ||
|
||
if not any(default_path.iterdir()): | ||
raise IOError(f"Failed to get and extract STPSF data files to {default_path}") | ||
|
@@ -216,30 +220,32 @@ def auto_download_stpsf_data(): | |
def get_stpsf_data_path(data_version_min=None, return_version=False): | ||
"""Get the STPSF data path | ||
|
||
Simply checking an environment variable is not always enough, since | ||
for packaging this code as a Mac .app bundle, environment variables are | ||
not available since .apps run outside the Terminal or X11 environments. | ||
|
||
Therefore, check first the environment variable STPSF_PATH, and secondly | ||
Check first the environment variable STPSF_PATH, and secondly | ||
check the configuration file in the user's home directory. | ||
|
||
If data_version_min is supplied (as a 3-tuple of integers), it will be | ||
compared with the version number from version.txt in the STPSF data | ||
package. | ||
""" | ||
import os | ||
from pathlib import Path | ||
|
||
path_from_config = conf.STPSF_PATH # read from astropy configuration | ||
if path_from_config == 'from_environment_variable': | ||
path = os.getenv('STPSF_PATH') | ||
if path is None: | ||
message = ( | ||
'Environment variable $STPSF_PATH is not set!\n' | ||
f'{MISSING_STPSF_DATA_MESSAGE} searching default location..s' | ||
) | ||
warnings.warn(message) | ||
path = auto_download_stpsf_data() | ||
|
||
# Check if the data were already downloaded to the default path | ||
default_path = get_default_data_path() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change isn't necessary as this logic already exists within
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My point here was intentionally refactoring to allow checking for a prior successful call to auto_download_stpsf_data, prior to displaying the big giant warning message about trying to download the data. If the data has already been downloaded successfully, it’s incorrect to display a warning that says “stpsf will try to download the data”. The current code will display the warning text every time the package is imported in a new Python session, even if the data is already indeed present and fine in the default directory. That’s excess warnings for no benefit to the user. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your concern (deleted my previous reply because I misunderstood what download message you were referring to) I think its important that the May just make more sense to move the |
||
if os.path.exists(default_path): | ||
# If so, let's look there | ||
path = default_path | ||
else: | ||
# If not, let's try to download the data | ||
message = ( | ||
'Environment variable $STPSF_PATH is not set!\n' | ||
f'{MISSING_STPSF_DATA_MESSAGE} searching default location.' | ||
) | ||
warnings.warn(message) | ||
path = auto_download_stpsf_data() | ||
else: | ||
path = path_from_config | ||
|
||
|
@@ -1069,4 +1075,4 @@ def get_target_phase_map_filename(apername): | |
raise ValueError("File wss_target_phase_{}.fits, \ | ||
not found under {}.".format(apername.split('_')[1].lower(),path)) | ||
|
||
return was_targ_file | ||
return was_targ_file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good addition! Thanks for cleaning up the docs!