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 for pathlib.Path filepath arguments for adapter IO functions #1704

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 14 additions & 3 deletions src/py-opentimelineio/opentimelineio/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os
import itertools
import pathlib

from .. import (
exceptions,
Expand Down Expand Up @@ -132,10 +133,15 @@ def read_from_file(
timeline = read_from_file("file_with_no_extension", "cmx_3600")
"""

adapter = _from_filepath_or_name(filepath, adapter_name)
# convert pathlib Path objects to simple string
string_filepath = filepath
if isinstance(string_filepath, pathlib.PurePath):
string_filepath = os.fspath(filepath)

adapter = _from_filepath_or_name(string_filepath, adapter_name)

return adapter.read_from_file(
filepath=filepath,
filepath=string_filepath,
media_linker_name=media_linker_name,
media_linker_argument_map=media_linker_argument_map,
**adapter_argument_map
Expand Down Expand Up @@ -189,9 +195,14 @@ def write_to_file(

adapter = _from_filepath_or_name(filepath, adapter_name)

# convert pathlib Path objects to simple string
string_filepath = filepath
if isinstance(string_filepath, pathlib.PurePath):
string_filepath = os.fspath(filepath)

return adapter.write_to_file(
input_otio=input_otio,
filepath=filepath,
filepath=string_filepath,
**adapter_argument_map
)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_builtin_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
otio_json,
)

import pathlib
import tempfile


Expand Down Expand Up @@ -90,6 +91,14 @@ def test_otio_json_default(self):
test_str = otio.adapters.write_to_string(tl)
self.assertJsonEqual(tl, otio.adapters.read_from_string(test_str))

def test_otio_pathlib_filepath(self):
"""Tests reading / writing with a filepath that's a Path object."""
tl = otio.adapters.read_from_file(pathlib.Path(SCREENING_EXAMPLE_PATH))
with tempfile.TemporaryDirectory() as temp_dir:
tmp_path = pathlib.Path(temp_dir) / "tmp_pathlib.otio"
otio.adapters.write_to_file(input_otio=tl, filepath=tmp_path)
self.assertJsonEqual(tl, otio.adapters.read_from_file(filepath=tmp_path))


if __name__ == '__main__':
unittest.main()
Loading