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

Adds support to filepath_from_url to support non-encoded URLs #1664

15 changes: 14 additions & 1 deletion src/py-opentimelineio/opentimelineio/url_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ def filepath_from_url(urlstr):
""" Take a url and return a filepath """

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

# If on Windows, remove the first leading slash left by urlparse
if os.name == 'nt' and filepath.startswith('/'):
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 @@ -10,6 +10,7 @@

SAMPLE_DATA_DIR = os.path.join(os.path.dirname(__file__), "sample_data")
SCREENING_EXAMPLE_PATH = os.path.join(SAMPLE_DATA_DIR, "screening_example.edl")
PREMIERE_XML_EXAMPLE_PATH = os.path.join(SAMPLE_DATA_DIR, "premiere_example.xml")
MEDIA_EXAMPLE_PATH_REL = os.path.relpath(
os.path.join(
os.path.dirname(__file__),
Expand Down Expand Up @@ -51,6 +52,17 @@ def test_roundtrip_rel(self):
# should have reconstructed it by this point
self.assertEqual(os.path.normpath(result), MEDIA_EXAMPLE_PATH_REL)

def test_premiere_xml_urls(self):
timeline = otio.adapters.read_from_file(PREMIERE_XML_EXAMPLE_PATH)
for clip in timeline.find_clips():
media_ref = clip.media_reference

if hasattr(media_ref, 'target_url') and media_ref.target_url is not None:
url = media_ref.target_url
self.assertTrue(url.startswith("file://"))
processed_url = otio.url_utils.filepath_from_url(url)
self.assertNotEquals(url, processed_url)


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