Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/py-opentimelineio/opentimelineio/url_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Utilities for conversion between urls and file paths"""

import os
import re

from urllib import (
parse as urlparse,
Expand Down Expand Up @@ -37,7 +38,33 @@ def url_from_filepath(fpath):


def filepath_from_url(urlstr):
""" Take a url and return a filepath """
"""
Take an url and return a filepath.

URLs can either be encoded according to the `RFC 3986`_ standard or not.
Additionally, Windows mapped paths need to be accounted for when processing a
URL; however, there are `ongoing discussions`_ about how to best handle this within
Python. This function is meant to cover all of 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
"""

parsed_result = urlparse.urlparse(urlstr)
return request.url2pathname(parsed_result.path)

# Check if original urlstr is URL encoded
if urlparse.unquote(urlstr) != urlstr:
filepath = request.url2pathname(parsed_result.path)

# Otherwise, combine the netloc and path
else:
filepath = parsed_result.netloc + parsed_result.path

filepath = filepath.replace("\\", "/")

# If on Windows and using a drive letter,
# remove the first leading slash left by urlparse
if re.match(r"/[a-zA-Z]:/.*", filepath):
filepath = filepath[1:]

return filepath
12 changes: 12 additions & 0 deletions tests/test_url_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
MEDIA_EXAMPLE_PATH_ABS
)

ENCODED_WINDOWS_URL = "file://localhost/S%3a/path/file.ext"
WINDOWS_URL = "file://S:/path/file.ext"
CORRECTED_WINDOWS_PATH = "S:/path/file.ext"


class TestConversions(unittest.TestCase):
def test_roundtrip_abs(self):
Expand All @@ -51,6 +55,14 @@ def test_roundtrip_rel(self):
# should have reconstructed it by this point
self.assertEqual(os.path.normpath(result), MEDIA_EXAMPLE_PATH_REL)

def test_windows_urls(self):
for url in (ENCODED_WINDOWS_URL, WINDOWS_URL):
print(f"Original URL: {url}")
self.assertTrue(url.startswith("file://"))
processed_url = otio.url_utils.filepath_from_url(url)
print(f"Processed URL Path: {processed_url}")
self.assertEqual(processed_url, CORRECTED_WINDOWS_PATH)


if __name__ == "__main__":
unittest.main()