/
manifest.py
249 lines (203 loc) · 7.87 KB
/
manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#
# Copyright 2017 Pixar Animation Studios
#
# Licensed under the Apache License, Version 2.0 (the "Apache License")
# with the following modification; you may not use this file except in
# compliance with the Apache License and the following modification to it:
# Section 6. Trademarks. is deleted and replaced with:
#
# 6. Trademarks. This License does not grant permission to use the trade
# names, trademarks, service marks, or product names of the Licensor
# and its affiliates, except as required to comply with Section 4(c) of
# the License and to reproduce the content of the NOTICE file.
#
# You may obtain a copy of the Apache License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License with the above modification is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
"""Implementation of an adapter registry system for OTIO."""
import inspect
import logging
import os
# on some python interpreters, pkg_resources is not available
try:
import pkg_resources
except ImportError:
pkg_resources = None
from .. import (
core,
exceptions,
)
def manifest_from_file(filepath):
"""Read the .json file at filepath into a Manifest object."""
result = core.deserialize_json_from_file(filepath)
result.source_files.append(filepath)
result._update_plugin_source(filepath)
return result
def manifest_from_string(input_string):
"""Deserialize the json string into a manifest object."""
result = core.deserialize_json_from_string(input_string)
# try and get the caller's name
name = "unknown"
stack = inspect.stack()
if len(stack) > 1 and len(stack[1]) > 3:
# filename function name
name = "{}:{}".format(stack[1][1], stack[1][3])
# set the value in the manifest
src_string = "call to manifest_from_string() in " + name
result.source_files.append(src_string)
result._update_plugin_source(src_string)
return result
@core.register_type
class Manifest(core.SerializableObject):
"""Defines an OTIO plugin Manifest.
This is an internal OTIO implementation detail. A manifest tracks a
collection of adapters and allows finding specific adapters by suffix
For writing your own adapters, consult:
https://opentimelineio.readthedocs.io/en/latest/tutorials/write-an-adapter.html#
"""
_serializable_label = "PluginManifest.1"
def __init__(self):
core.SerializableObject.__init__(self)
self.adapters = []
self.media_linkers = []
self.source_files = []
# hook system stuff
self.hooks = []
self.hook_scripts = []
adapters = core.serializable_field(
"adapters",
type([]),
"Adapters this manifest describes."
)
media_linkers = core.serializable_field(
"media_linkers",
type([]),
"Media Linkers this manifest describes."
)
hooks = core.serializable_field(
"hooks",
type([]),
"Hooks that hooks scripts can be attached to."
)
hook_scripts = core.serializable_field(
"hook_scripts",
type([]),
"Scripts that can be attached to hooks."
)
def _update_plugin_source(self, path):
"""Track the source .json for a given adapter."""
for thing in (self.adapters + self.media_linkers):
thing._json_path = path
def from_filepath(self, suffix):
"""Return the adapter object associated with a given file suffix."""
for adapter in self.adapters:
if suffix.lower() in adapter.suffixes:
return adapter
raise exceptions.NoKnownAdapterForExtensionError(suffix)
def adapter_module_from_suffix(self, suffix):
"""Return the adapter module associated with a given file suffix."""
adp = self.from_filepath(suffix)
return adp.module()
def from_name(self, name, kind_list="adapters"):
"""Return the adapter object associated with a given adapter name."""
for thing in getattr(self, kind_list):
if name == thing.name:
return thing
raise exceptions.NotSupportedError(
"Could not find plugin: '{}' in kind_list: '{}'."
" options: {}".format(
name,
kind_list,
getattr(self, kind_list)
)
)
def adapter_module_from_name(self, name):
"""Return the adapter module associated with a given adapter name."""
adp = self.from_name(name)
return adp.module()
_MANIFEST = None
def load_manifest():
# build the manifest of adapters, starting with builtin adapters
result = manifest_from_file(
os.path.join(
os.path.dirname(os.path.dirname(inspect.getsourcefile(core))),
"adapters",
"builtin_adapters.plugin_manifest.json"
)
)
# layer contrib plugins after built in ones
try:
import opentimelineio_contrib as otio_c
contrib_manifest = manifest_from_file(
os.path.join(
os.path.dirname(inspect.getsourcefile(otio_c)),
"adapters",
"contrib_adapters.plugin_manifest.json"
)
)
result.adapters.extend(contrib_manifest.adapters)
result.media_linkers.extend(contrib_manifest.media_linkers)
except ImportError:
pass
# Discover setuptools-based plugins
if pkg_resources:
for plugin in pkg_resources.iter_entry_points(
"opentimelineio.plugins"
):
plugin_name = plugin.name
try:
plugin_entry_point = plugin.load()
try:
plugin_manifest = plugin_entry_point.plugin_manifest()
except AttributeError:
if not pkg_resources.resource_exists(
plugin.module_name,
'plugin_manifest.json'
):
raise
manifest_stream = pkg_resources.resource_stream(
plugin.module_name,
'plugin_manifest.json'
)
plugin_manifest = core.deserialize_json_from_string(
manifest_stream.read().decode('utf-8')
)
manifest_stream.close()
except Exception:
logging.exception(
"could not load plugin: {}".format(plugin_name)
)
continue
result.adapters.extend(plugin_manifest.adapters)
result.media_linkers.extend(plugin_manifest.media_linkers)
else:
# XXX: Should we print some kind of warning that pkg_resources isn't
# available?
pass
# read local adapter manifests, if they exist
_local_manifest_path = os.environ.get("OTIO_PLUGIN_MANIFEST_PATH", None)
if _local_manifest_path is not None:
for json_path in _local_manifest_path.split(":"):
if not os.path.exists(json_path):
# XXX: In case error reporting is requested
# print(
# "Warning: OpenTimelineIO cannot access path '{}' from "
# "$OTIO_PLUGIN_MANIFEST_PATH".format(json_path)
# )
continue
LOCAL_MANIFEST = manifest_from_file(json_path)
result.adapters.extend(LOCAL_MANIFEST.adapters)
result.media_linkers.extend(LOCAL_MANIFEST.media_linkers)
return result
def ActiveManifest(force_reload=False):
global _MANIFEST
if not _MANIFEST or force_reload:
_MANIFEST = load_manifest()
return _MANIFEST