-
Notifications
You must be signed in to change notification settings - Fork 293
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
Added support to filepath_from_url for UNC paths and tests for UNC and posix paths #1674
Added support to filepath_from_url for UNC paths and tests for UNC and posix paths #1674
Conversation
f8737b9
to
90ca9f3
Compare
Just occurred to me, should |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #1674 +/- ##
=======================================
Coverage 79.84% 79.84%
=======================================
Files 197 197
Lines 21796 21820 +24
Branches 4358 4363 +5
=======================================
+ Hits 17403 17423 +20
- Misses 2232 2234 +2
- Partials 2161 2163 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
|
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.
Thanks @douglascomet for working on covering more windows URL cases.
Here is a suggestion for an alternative way of achieving this. It only manipulates the path if a windows drive is found or an URL only contains two leading slashes like so: "file://host/share/path/to/file.ext"
Please note that I've also done some minor adjustments to code untouched by your PR.
def filepath_from_url(urlstr):
"""
Take an url and return a filepath.
URLs can either be encoded according to the `RFC 3986`_ standard or not.
Additionally, Windows mapped drive letter and UNC paths need to be accounted for
when processing URL(s); however, there are `ongoing discussions`_ about how to best
handle this within Python developer community. This function is meant to cover
these scenarios in the interim.
.. _RFC 3986: https://tools.ietf.org/html/rfc3986#section-2.1
.. _ongoing discussions: https://discuss.python.org/t/file-uris-in-python/15600
"""
# Parse provided URL
parsed_result = urlparse.urlparse(urlstr)
# De-encode the parsed path
decoded_parsed_path = urlparse.unquote(parsed_result.path)
# Convert the parsed URL to a path
filepath = Path(request.url2pathname(decoded_parsed_path))
# If the network location is a window drive, reassemble the path
if PureWindowsPath(parsed_result.netloc).drive:
filepath = Path(parsed_result.netloc + decoded_parsed_path)
# Check if the specified index is a windows drive, then offset the path
elif PureWindowsPath(filepath.parts[1]).drive:
# Remove leading "/" if/when `request.url2pathname` yields "/S:/path/file.ext"
filepath = filepath.relative_to(filepath.root)
# Should catch UNC paths, as parsing "file:///some/path/to/file.ext" doesn't provide a netloc
elif parsed_result.netloc and parsed_result.netloc != 'localhost':
# Paths of type: "file://host/share/path/to/file.ext" provide "host" as netloc
filepath = Path('//', parsed_result.netloc + decoded_parsed_path)
# Convert "\" to "/" if needed
return filepath.as_posix()
@douglascomet Does your latest commit resolve either of the conversations? |
@meshula Yes, it will. Apologies for the delay with pushing this PR forward. |
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
…lementations Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
664286a
to
56dc9ed
Compare
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
@douglascomet No need to apologize, I was just checking status, no urgency. |
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
Signed-off-by: Doug Halley <[email protected]>
@douglascomet in response to:
I would say it should not raise - the implication is that that is a relative URL. That does, however, imply that But I think this is out of scope for this PR. |
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.
LGTM!
This PR is a follow up to #1664 to add support to
filepath_from_url
and tests for UNC paths.