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

Added support to filepath_from_url for UNC paths and tests for UNC and posix paths #1674

Conversation

douglascomet
Copy link
Contributor

This PR is a follow up to #1664 to add support to filepath_from_url and tests for UNC paths.

@douglascomet douglascomet force-pushed the add_support_to_filepath-from-url_for_unc_paths branch from f8737b9 to 90ca9f3 Compare October 25, 2023 02:15
@douglascomet
Copy link
Contributor Author

douglascomet commented Oct 25, 2023

Just occurred to me, should filepath_from_url raise or do nothing if a urlstr does not have the file:// prefix? Same question if a url with a different scheme is provided?

@codecov-commenter
Copy link

codecov-commenter commented Oct 25, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (d22935e) 79.84% compared to head (1ad7b70) 79.84%.

Additional details and impacted files

Impacted file tree graph

@@           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     
Flag Coverage Δ
py-unittests 79.84% <87.09%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
tests/test_url_conversions.py 95.34% <100.00%> (+2.49%) ⬆️
src/py-opentimelineio/opentimelineio/url_utils.py 85.18% <63.63%> (-14.82%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d22935e...1ad7b70. Read the comment docs.

Copy link
Contributor

@apetrynet apetrynet left a 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()

src/py-opentimelineio/opentimelineio/url_utils.py Outdated Show resolved Hide resolved
src/py-opentimelineio/opentimelineio/url_utils.py Outdated Show resolved Hide resolved
@meshula
Copy link
Collaborator

meshula commented Feb 15, 2024

@douglascomet Does your latest commit resolve either of the conversations?

@douglascomet
Copy link
Contributor Author

douglascomet commented Feb 15, 2024

@douglascomet Does your latest commit resolve either of the conversations?

@meshula Yes, it will. Apologies for the delay with pushing this PR forward.

@douglascomet douglascomet force-pushed the add_support_to_filepath-from-url_for_unc_paths branch from 664286a to 56dc9ed Compare February 15, 2024 05:08
@meshula
Copy link
Collaborator

meshula commented Feb 15, 2024

@douglascomet No need to apologize, I was just checking status, no urgency.

@reinecke reinecke added this to the Public Beta 16 milestone Mar 28, 2024
@reinecke
Copy link
Collaborator

@douglascomet in response to:

Just occurred to me, should filepath_from_url raise or do nothing if a urlstr does not have the file:// prefix? Same question if a url with a different scheme is provided?

I would say it should not raise - the implication is that that is a relative URL. That does, however, imply that filepath_from_url should maybe accepts something like a base_path with is the filepath that paths can be relative to (usually this would be the path of parent dir the loaded otio file is in).

But I think this is out of scope for this PR.

Copy link
Contributor

@apetrynet apetrynet left a comment

Choose a reason for hiding this comment

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

LGTM!

@reinecke reinecke merged commit 82068e3 into AcademySoftwareFoundation:main Mar 28, 2024
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants