Skip to content

Download file with a directory specified by local_path parameter#3016

Merged
bsipocz merged 7 commits into
astropy:mainfrom
snbianco:download-file-directory
Jun 11, 2024
Merged

Download file with a directory specified by local_path parameter#3016
bsipocz merged 7 commits into
astropy:mainfrom
snbianco:download-file-directory

Conversation

@snbianco

@snbianco snbianco commented May 30, 2024

Copy link
Copy Markdown
Contributor

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

pep8speaks commented May 30, 2024

Copy link
Copy Markdown

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

snbianco commented Jun 3, 2024

Copy link
Copy Markdown
Contributor Author

@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.

Comment thread astroquery/mast/observations.py Outdated
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

Copy link
Copy Markdown

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
Copy Markdown
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?

Copy link
Copy Markdown

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
Copy Markdown
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks great!

Comment thread astroquery/mast/observations.py Outdated
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

Copy link
Copy Markdown

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
Copy Markdown
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

@bsipocz bsipocz left a comment

Copy link
Copy Markdown
Member

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.

Comment thread astroquery/mast/tests/test_mast_remote.py
@bsipocz

bsipocz commented Jun 11, 2024

Copy link
Copy Markdown
Member

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
@snbianco
snbianco force-pushed the download-file-directory branch from 584a1ec to 1ffbdee Compare June 11, 2024 16:00
@snbianco
snbianco force-pushed the download-file-directory branch from bc2864b to 1a69e07 Compare June 11, 2024 16:15

@bsipocz bsipocz left a comment

Copy link
Copy Markdown
Member

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
@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