Skip to content
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

Download file with a directory specified by local_path parameter #3016

Merged
merged 7 commits into from
Jun 11, 2024

Conversation

snbianco
Copy link
Contributor

@snbianco snbianco commented May 30, 2024

Fixes #2501

Appends filename to a local_path argument to avoid IsADirectoryError. Also updated documentation to be a bit more clear about the function and the parameter.

@pep8speaks
Copy link

pep8speaks commented May 30, 2024

Hello @snbianco! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-06-11 16:15:11 UTC

@snbianco snbianco marked this pull request as ready for review June 3, 2024 02:07
@snbianco
Copy link
Contributor Author

snbianco commented Jun 3, 2024

@bsipocz

Hi! I'm Sam, a new developer at STScI working with @astrojimig. This is a PR to fix a bug in one of our methods.

filename = os.path.basename(uri)
if not local_path: # local file path is not defined
local_path = filename
elif os.path.isdir(local_path): # local file path is directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we want to add some logic here that if the local_path directory doesn't exist, the directory is created? Or is that already covered elsewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was kind of wondering about this too... right now, a directory that doesn't exist gets created as a file in the pwd since the check on line 539 returns false. I considered checking if the string contains a . character to tell if it's a file, but we would then run into issues with paths like this: ./tmp/

Would it make more sense to check that the string contains a ., but that it isn't the leading character?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's some logic with pathlib that you could use for this!

Something like:

local_path = pathlib.Path(local_path)
if not local_path.exists():
        local_path.mkdir(parents=True, exist_ok=True)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Most recent version creates a Path object and differentiates between a file and a directory by checking path.suffix. If a file extension isn't found, then it's assumed that local_path is meant to point to a directory. If the directory does not exist, it's then created.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great!

filename = os.path.basename(uri)
if not local_path: # local file path is not defined
local_path = filename
elif os.path.isdir(local_path): # local file path is directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also might need a second elif to finish this off: in the case that local_path is supplied as a filename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the most recent changes, I differentiate between a filename and a directory by checking if the pathlib.Path object has an extension. I don't think we should need a branch to check that local_path is a filename since the parameter shouldn't need any further processing and can be passed directly into the download_file function.

@bsipocz bsipocz added the mast label Jun 11, 2024
@bsipocz bsipocz added this to the v0.4.8 milestone Jun 11, 2024
Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Content-wise it looks good, but can I ask you to rebase the PR to: 1) remove the merge commit 2) to consolidate the commits to a few logical one by e.g. squashing the style fixing iterations, changelog iterations, etc. No need to squash down to one, but eliminate the incremental iterations.

astroquery/mast/tests/test_mast_remote.py Show resolved Hide resolved
@bsipocz
Copy link
Member

bsipocz commented Jun 11, 2024

minor note: unfortunately "address" is not one of the verbs that links an issue to a PR, pick one of close/fix/resolve instead :) https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#about-linked-issues-and-pull-requests

@snbianco snbianco force-pushed the download-file-directory branch 2 times, most recently from 9196d0a to 584a1ec Compare June 11, 2024 15:41
Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this all looks good now, and no new test failure is present.

@bsipocz bsipocz merged commit ab7cf03 into astropy:main Jun 11, 2024
8 checks passed
@snbianco snbianco deleted the download-file-directory branch July 3, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: mast.Observations.download_file local_path should accept directory
4 participants