Skip to content

Commit f374158

Browse files
timlehrreinecke
authored andcommitted
added support for pathlib.Path filepath arguments for adapter IO functions (#1704)
Signed-off-by: Tim Lehr <tim.lehr@disneyanimation.com> Signed-off-by: Eric Reinecke <reinecke.eric@gmail.com> Signed-off-by: Eric Reinecke <ereinecke@netflix.com>
1 parent 2313459 commit f374158

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/py-opentimelineio/opentimelineio/adapters/__init__.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717
import itertools
18+
import pathlib
1819

1920
from .. import (
2021
exceptions,
@@ -132,10 +133,15 @@ def read_from_file(
132133
timeline = read_from_file("file_with_no_extension", "cmx_3600")
133134
"""
134135

135-
adapter = _from_filepath_or_name(filepath, adapter_name)
136+
# convert pathlib Path objects to simple string
137+
string_filepath = filepath
138+
if isinstance(string_filepath, pathlib.PurePath):
139+
string_filepath = os.fspath(filepath)
140+
141+
adapter = _from_filepath_or_name(string_filepath, adapter_name)
136142

137143
return adapter.read_from_file(
138-
filepath=filepath,
144+
filepath=string_filepath,
139145
media_linker_name=media_linker_name,
140146
media_linker_argument_map=media_linker_argument_map,
141147
**adapter_argument_map
@@ -189,9 +195,14 @@ def write_to_file(
189195

190196
adapter = _from_filepath_or_name(filepath, adapter_name)
191197

198+
# convert pathlib Path objects to simple string
199+
string_filepath = filepath
200+
if isinstance(string_filepath, pathlib.PurePath):
201+
string_filepath = os.fspath(filepath)
202+
192203
return adapter.write_to_file(
193204
input_otio=input_otio,
194-
filepath=filepath,
205+
filepath=string_filepath,
195206
**adapter_argument_map
196207
)
197208

tests/test_builtin_adapters.py

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
otio_json,
1414
)
1515

16+
import pathlib
1617
import tempfile
1718

1819

@@ -90,6 +91,14 @@ def test_otio_json_default(self):
9091
test_str = otio.adapters.write_to_string(tl)
9192
self.assertJsonEqual(tl, otio.adapters.read_from_string(test_str))
9293

94+
def test_otio_pathlib_filepath(self):
95+
"""Tests reading / writing with a filepath that's a Path object."""
96+
tl = otio.adapters.read_from_file(pathlib.Path(SCREENING_EXAMPLE_PATH))
97+
with tempfile.TemporaryDirectory() as temp_dir:
98+
tmp_path = pathlib.Path(temp_dir) / "tmp_pathlib.otio"
99+
otio.adapters.write_to_file(input_otio=tl, filepath=tmp_path)
100+
self.assertJsonEqual(tl, otio.adapters.read_from_file(filepath=tmp_path))
101+
93102

94103
if __name__ == '__main__':
95104
unittest.main()

0 commit comments

Comments
 (0)