Skip to content

Commit

Permalink
added support for pathlib.Path filepath arguments for adapter IO func…
Browse files Browse the repository at this point in the history
…tions (#1704)

Signed-off-by: Tim Lehr <[email protected]>
  • Loading branch information
timlehr authored and reinecke committed Apr 12, 2024
1 parent feac606 commit fbe098e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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()

0 comments on commit fbe098e

Please sign in to comment.