Skip to content

Commit 22f20fd

Browse files
ssteinbachjminor
authored andcommitted
Adapter argument map (#183)
* Add support for keyword arguments that get passed to adapters.
1 parent 5f42fb0 commit 22f20fd

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-34
lines changed

opentimelineio/adapters/__init__.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ def from_name(name):
117117

118118

119119
def read_from_file(
120-
filepath,
121-
adapter_name=None,
122-
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
123-
media_linker_argument_map=None
124-
):
120+
filepath,
121+
adapter_name=None,
122+
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
123+
media_linker_argument_map=None,
124+
**adapter_argument_map
125+
):
125126
"""Read filepath using adapter_name.
126127
127128
If adapter_name is None, try and infer the adapter name from the filepath.
@@ -134,18 +135,20 @@ def read_from_file(
134135
adapter = _from_filepath_or_name(filepath, adapter_name)
135136

136137
return adapter.read_from_file(
137-
filepath,
138-
media_linker_name,
139-
media_linker_argument_map
138+
filepath=filepath,
139+
media_linker_name=media_linker_name,
140+
media_linker_argument_map=media_linker_argument_map,
141+
**adapter_argument_map
140142
)
141143

142144

143145
def read_from_string(
144-
input_str,
145-
adapter_name,
146-
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
147-
media_linker_argument_map=None
148-
):
146+
input_str,
147+
adapter_name,
148+
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
149+
media_linker_argument_map=None,
150+
**adapter_argument_map
151+
):
149152
"""Read a timeline from input_str using adapter_name.
150153
151154
This is useful if you obtain a timeline from someplace other than the
@@ -158,13 +161,19 @@ def read_from_string(
158161

159162
adapter = plugins.ActiveManifest().from_name(adapter_name)
160163
return adapter.read_from_string(
161-
input_str,
162-
media_linker_name,
163-
media_linker_argument_map
164+
input_str=input_str,
165+
media_linker_name=media_linker_name,
166+
media_linker_argument_map=media_linker_argument_map,
167+
**adapter_argument_map
164168
)
165169

166170

167-
def write_to_file(input_otio, filepath, adapter_name=None):
171+
def write_to_file(
172+
input_otio,
173+
filepath,
174+
adapter_name=None,
175+
**adapter_argument_map
176+
):
168177
"""Write input_otio to filepath using adapter_name.
169178
170179
If adapter_name is None, infer the adapter_name to use based on the
@@ -176,15 +185,26 @@ def write_to_file(input_otio, filepath, adapter_name=None):
176185

177186
adapter = _from_filepath_or_name(filepath, adapter_name)
178187

179-
return adapter.write_to_file(input_otio, filepath)
188+
return adapter.write_to_file(
189+
input_otio=input_otio,
190+
filepath=filepath,
191+
**adapter_argument_map
192+
)
180193

181194

182-
def write_to_string(input_otio, adapter_name):
195+
def write_to_string(
196+
input_otio,
197+
adapter_name,
198+
**adapter_argument_map
199+
):
183200
"""Return input_otio written to a string using adapter_name.
184201
185202
Example:
186203
raw_text = otio.adapters.write_to_string(my_timeline, "otio_json")
187204
"""
188205

189206
adapter = plugins.ActiveManifest().from_name(adapter_name)
190-
return adapter.write_to_string(input_otio)
207+
return adapter.write_to_string(
208+
input_otio=input_otio,
209+
**adapter_argument_map
210+
)

opentimelineio/adapters/adapter.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def read_from_file(
103103
self,
104104
filepath,
105105
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
106-
media_linker_argument_map=None
106+
media_linker_argument_map=None,
107+
**adapter_argument_map
107108
):
108109
"""Execute the read_from_file function on this adapter.
109110
@@ -124,12 +125,14 @@ def read_from_file(
124125
contents = fo.read()
125126
result = self._execute_function(
126127
"read_from_string",
127-
input_str=contents
128+
input_str=contents,
129+
**adapter_argument_map
128130
)
129131
else:
130132
result = self._execute_function(
131133
"read_from_file",
132-
filepath=filepath
134+
filepath=filepath,
135+
**adapter_argument_map
133136
)
134137

135138
if media_linker_name and (
@@ -143,7 +146,7 @@ def read_from_file(
143146

144147
return result
145148

146-
def write_to_file(self, input_otio, filepath):
149+
def write_to_file(self, input_otio, filepath, **adapter_argument_map):
147150
"""Execute the write_to_file function on this adapter.
148151
149152
If write_to_string exists, but not write_to_file, execute that with
@@ -154,28 +157,31 @@ def write_to_file(self, input_otio, filepath):
154157
not self.has_feature("write_to_file") and
155158
self.has_feature("write_to_string")
156159
):
157-
result = self.write_to_string(input_otio)
160+
result = self.write_to_string(input_otio, **adapter_argument_map)
158161
with open(filepath, 'w') as fo:
159162
fo.write(result)
160163
return filepath
161164

162165
return self._execute_function(
163166
"write_to_file",
164167
input_otio=input_otio,
165-
filepath=filepath
168+
filepath=filepath,
169+
**adapter_argument_map
166170
)
167171

168172
def read_from_string(
169173
self,
170174
input_str,
171175
media_linker_name=media_linker.MediaLinkingPolicy.ForceDefaultLinker,
172-
media_linker_argument_map=None
176+
media_linker_argument_map=None,
177+
**adapter_argument_map
173178
):
174179
"""Call the read_from_string function on this adapter."""
175180

176181
result = self._execute_function(
177182
"read_from_string",
178-
input_str=input_str
183+
input_str=input_str,
184+
**adapter_argument_map
179185
)
180186

181187
if media_linker_name and (
@@ -189,10 +195,14 @@ def read_from_string(
189195

190196
return result
191197

192-
def write_to_string(self, input_otio):
198+
def write_to_string(self, input_otio, **adapter_argument_map):
193199
"""Call the write_to_string function on this adapter."""
194200

195-
return self._execute_function("write_to_string", input_otio=input_otio)
201+
return self._execute_function(
202+
"write_to_string",
203+
input_otio=input_otio,
204+
**adapter_argument_map
205+
)
196206

197207
def __str__(self):
198208
return (

tests/baselines/example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
import opentimelineio as otio
3131

3232

33-
def read_from_file(filepath):
34-
fake_tl = otio.schema.Timeline(name=filepath)
33+
def read_from_file(filepath, suffix=""):
34+
fake_tl = otio.schema.Timeline(name=filepath+str(suffix))
3535
fake_tl.tracks.append(otio.schema.Track())
3636
fake_tl.tracks[0].append(otio.schema.Clip(name=filepath + "_clip"))
3737
return fake_tl
3838

3939

40-
def read_from_string(input_str):
41-
return read_from_file(input_str)
40+
def read_from_string(input_str, suffix=""):
41+
return read_from_file(input_str, suffix)
4242

4343

4444
# in practice, these will be in separate plugins, but for simplicity in the

tests/test_adapter_plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def test_has_feature(self):
111111
self.assertTrue(self.adp.has_feature("read_from_file"))
112112
self.assertFalse(self.adp.has_feature("write"))
113113

114+
def test_pass_arguments_to_adapter(self):
115+
self.assertEqual(self.adp.read_from_file("foo", suffix=3).name, "foo3")
116+
114117
def test_run_media_linker_during_adapter(self):
115118
mfest = otio.plugins.ActiveManifest()
116119

0 commit comments

Comments
 (0)