-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathotio_json.py
153 lines (120 loc) · 4.42 KB
/
otio_json.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
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project
"""Adapter for reading and writing native .otio json files."""
from .. import (
core,
versioning,
exceptions
)
import os
_DEFAULT_VERSION_ENVVAR = "OTIO_DEFAULT_TARGET_VERSION_FAMILY_LABEL"
def read_from_file(filepath):
"""
De-serializes an OpenTimelineIO object from a file
Args:
filepath (str): The path to an otio file to read from
Returns:
OpenTimeline: An OpenTimeline object
"""
return core.deserialize_json_from_file(filepath)
def read_from_string(input_str):
"""
De-serializes an OpenTimelineIO object from a json string
Args:
input_str (str): A string containing json serialized otio contents
Returns:
OpenTimeline: An OpenTimeline object
"""
return core.deserialize_json_from_string(input_str)
def _fetch_downgrade_map_from_env():
version_envvar = os.environ[_DEFAULT_VERSION_ENVVAR]
try:
family, label = version_envvar.split(":")
except ValueError:
raise exceptions.InvalidEnvironmentVariableError(
"Environment variable '{}' is incorrectly formatted with '{}'."
"Variable must be formatted as 'FAMILY:LABEL'".format(
_DEFAULT_VERSION_ENVVAR,
version_envvar,
)
)
try:
# technically fetch_map returns an AnyDictionary, but the pybind11
# code wrapping the call to the serializer expects a python
# dictionary. This turns it back into a normal dictionary.
return dict(versioning.fetch_map(family, label))
except KeyError:
raise exceptions.InvalidEnvironmentVariableError(
"Environment variable '{}' is requesting family '{}' and label"
" '{}', however this combination does not exist in the "
"currently loaded manifests. Full version map: {}".format(
_DEFAULT_VERSION_ENVVAR,
family,
label,
versioning.full_map()
)
)
def write_to_string(input_otio, target_schema_versions=None, indent=4):
"""
Serializes an OpenTimelineIO object into a string
Args:
input_otio (OpenTimeline): An OpenTimeline object
indent (int): number of spaces for each json indentation level. Use\
-1 for no indentation or newlines.
If target_schema_versions is None and the environment variable
"OTIO_DEFAULT_TARGET_VERSION_FAMILY_LABEL" is set, will read a map out of
that for downgrade target. The variable should be of the form
FAMILY:LABEL, for example "MYSTUDIO:JUNE2022".
Returns:
str: A json serialized string representation
Raises:
otio.exceptions.InvalidEnvironmentVariableError: if there is a problem
with the default environment variable
"OTIO_DEFAULT_TARGET_VERSION_FAMILY_LABEL".
"""
if (
target_schema_versions is None
and _DEFAULT_VERSION_ENVVAR in os.environ
):
target_schema_versions = _fetch_downgrade_map_from_env()
return core.serialize_json_to_string(
input_otio,
target_schema_versions,
indent
)
def write_to_file(
input_otio,
filepath,
target_schema_versions=None,
indent=4
):
"""
Serializes an OpenTimelineIO object into a file
Args:
input_otio (OpenTimeline): An OpenTimeline object
filepath (str): The name of an otio file to write to
indent (int): number of spaces for each json indentation level.\
Use -1 for no indentation or newlines.
If target_schema_versions is None and the environment variable
"OTIO_DEFAULT_TARGET_VERSION_FAMILY_LABEL" is set, will read a map out of
that for downgrade target. The variable should be of the form
FAMILY:LABEL, for example "MYSTUDIO:JUNE2022".
Returns:
bool: Write success
Raises:
ValueError: on write error
otio.exceptions.InvalidEnvironmentVariableError: if there is a problem
with the default environment variable
"OTIO_DEFAULT_TARGET_VERSION_FAMILY_LABEL".
"""
if (
target_schema_versions is None
and _DEFAULT_VERSION_ENVVAR in os.environ
):
target_schema_versions = _fetch_downgrade_map_from_env()
return core.serialize_json_to_file(
input_otio,
filepath,
target_schema_versions,
indent
)