File: test_hooks_plugins.py

package info (click to toggle)
opentimelineio 0.18.1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 23,480 kB
  • sloc: cpp: 163,182; python: 50,821; ansic: 6,470; makefile: 1,091; sh: 892; xml: 182; javascript: 2
file content (252 lines) | stat: -rw-r--r-- 8,074 bytes parent folder | download
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
250
251
252
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project

import unittest
import os
from copy import deepcopy

import opentimelineio as otio

import opentimelineio.test_utils as otio_test_utils

from tests import (
    baseline_reader,
    utils,
)

import tempfile


HOOKSCRIPT_PATH = "hookscript_example"
POST_WRITE_HOOKSCRIPT_PATH = "post_write_hookscript_example"
CUSTOM_ADAPTER_HOOKSCRIPT_PATH = "custom_adapter_hookscript_example"

POST_RUN_NAME = "hook ran and did stuff"
TEST_METADATA = {'extra_data': True}


class HookScriptTest(unittest.TestCase, otio_test_utils.OTIOAssertions):
    """Tests for the hook function plugins."""
    def setUp(self):

        self.jsn = baseline_reader.json_baseline_as_string(HOOKSCRIPT_PATH)
        self.hook_script = otio.adapters.read_from_string(
            self.jsn,
            'otio_json'
        )
        self.hook_script._json_path = os.path.join(
            baseline_reader.MODPATH,
            "baselines",
            HOOKSCRIPT_PATH
        )

    def test_plugin_hook(self):
        self.assertEqual(self.hook_script.name, "example hook")
        self.assertEqual(self.hook_script.filepath, "example.py")

    def test_plugin_hook_runs(self):
        tl = otio.schema.Timeline()
        tl = self.hook_script.run(tl)
        self.assertEqual(tl.name, POST_RUN_NAME)


class TestPluginHookSystem(unittest.TestCase):
    """ Test the hook point definition system """
    def setUp(self):
        self.man = utils.create_manifest()
        self.jsn = baseline_reader.json_baseline_as_string(HOOKSCRIPT_PATH)
        self.hsf = otio.adapters.otio_json.read_from_string(self.jsn)
        self.hsf._json_path = os.path.join(
            baseline_reader.MODPATH,
            "baselines",
            HOOKSCRIPT_PATH
        )
        self.post_jsn = baseline_reader.json_baseline_as_string(
            POST_WRITE_HOOKSCRIPT_PATH
        )
        self.post_hsf = otio.adapters.otio_json.read_from_string(
            self.post_jsn
        )
        self.post_hsf._json_path = os.path.join(
            baseline_reader.MODPATH,
            "baselines",
            POST_WRITE_HOOKSCRIPT_PATH
        )
        self.adapter_hook_jsn = baseline_reader.json_baseline_as_string(
            CUSTOM_ADAPTER_HOOKSCRIPT_PATH
        )
        self.adapter_hookscript = otio.adapters.otio_json.read_from_string(
            self.adapter_hook_jsn)
        self.adapter_hookscript._json_path = os.path.join(
            baseline_reader.MODPATH,
            "baselines",
            HOOKSCRIPT_PATH
        )
        self.man.hook_scripts = [self.hsf, self.post_hsf, self.adapter_hookscript]
        self.orig_manifest = otio.plugins.manifest._MANIFEST
        otio.plugins.manifest._MANIFEST = self.man

    def tearDown(self):
        utils.remove_manifest(self.man)
        otio.plugins.manifest._MANIFEST = self.orig_manifest

    def test_plugin_adapter(self):
        self.assertEqual(self.hsf.name, "example hook")
        self.assertEqual(self.hsf.filepath, "example.py")
        self.assertEqual(otio.adapters.from_name("example").adapter_hook_names(),
                         ["custom_adapter_hook"])

    def test_load_adapter_module(self):
        target = os.path.join(
            baseline_reader.MODPATH,
            "baselines",
            "example.py"
        )

        self.assertEqual(self.hsf.module_abs_path(), target)
        self.assertTrue(hasattr(self.hsf.module(), "hook_function"))

    def test_run_hook_function(self):
        tl = otio.schema.Timeline()

        result = self.hsf.run(tl, TEST_METADATA)
        self.assertEqual(result.name, POST_RUN_NAME)
        self.assertEqual(result.metadata.get("extra_data"), True)

    def test_run_custom_hook_function(self):
        tl = otio.schema.Timeline()
        result = otio.hooks.run(hook="custom_adapter_hook", tl=tl,
                                extra_args=TEST_METADATA)
        self.assertEqual(result.metadata["custom_hook"], TEST_METADATA)

    def test_run_hook_through_adapters(self):
        hook_map = dict(TEST_METADATA)
        hook_map["run_custom_hook"] = True

        result = otio.adapters.read_from_string(
            'foo', adapter_name='example',
            media_linker_name='example',
            hook_function_argument_map=hook_map
        )

        self.assertEqual(result.name, POST_RUN_NAME)
        self.assertEqual(result.metadata.get("extra_data"), True)
        self.assertEqual(result.metadata["custom_hook"]["extra_data"], True)

    def test_post_write_hook(self):
        self.man.adapters.extend(self.orig_manifest.adapters)

        tl = otio.schema.Timeline()
        tl_copy = deepcopy(tl)

        with tempfile.TemporaryDirectory() as temp_dir:
            filename = os.path.join(temp_dir, "post_hook_unittest.otio")
            arg_map = dict()
            otio.adapters.write_to_file(
                tl,
                filename,
                adapter_name='otio_json',
                hook_function_argument_map=arg_map
            )

            self.assertTrue(os.path.exists(filename))
            self.assertEqual(
                os.path.getsize(filename),
                tl.metadata.get('filesize')
            )

            # In this case the post hook actually changed the metadata.
            # Users must take care to catch changes they do in their hooks.
            self.assertNotEqual(tl, tl_copy)

    def test_serialize(self):

        self.assertEqual(
            str(self.hsf),
            "HookScript({}, {})".format(
                repr(self.hsf.name),
                repr(self.hsf.filepath)
            )
        )
        self.assertEqual(
            repr(self.hsf),
            "otio.hooks.HookScript("
            "name={}, "
            "filepath={}"
            ")".format(
                repr(self.hsf.name),
                repr(self.hsf.filepath)
            )
        )

    def test_available_hookscript_names(self):
        # for not just assert that it returns a non-empty list
        self.assertEqual(
            list(otio.hooks.available_hookscripts()),
            [self.hsf, self.post_hsf, self.adapter_hookscript]
        )
        self.assertEqual(
            otio.hooks.available_hookscript_names(),
            [self.hsf.name, self.post_hsf.name, self.adapter_hookscript.name]
        )

    def test_manifest_hooks(self):
        self.assertEqual(
            sorted(list(otio.hooks.names())),
            sorted(
                ["post_adapter_read", "post_media_linker",
                 "pre_adapter_write", "post_adapter_write",
                 "custom_adapter_hook"]
            )
        )

        self.assertEqual(
            list(otio.hooks.scripts_attached_to("pre_adapter_write")),
            [
                self.hsf.name,
                self.hsf.name
            ]
        )

        self.assertEqual(
            list(otio.hooks.scripts_attached_to("post_adapter_read")),
            []
        )

        self.assertEqual(
            list(otio.hooks.scripts_attached_to("post_media_linker")),
            [
                self.hsf.name
            ]
        )

        self.assertEqual(
            list(otio.hooks.scripts_attached_to("post_adapter_write")),
            [
                self.post_hsf.name
            ]
        )

        self.assertEqual(
            list(otio.hooks.scripts_attached_to("custom_adapter_hook")),
            [
                self.adapter_hookscript.name
            ]
        )

        tl = otio.schema.Timeline()
        result = otio.hooks.run("pre_adapter_write", tl, TEST_METADATA)
        self.assertEqual(result.name, POST_RUN_NAME)
        self.assertEqual(result.metadata, TEST_METADATA)

        # test deleting all the functions
        del otio.hooks.scripts_attached_to("pre_adapter_write")[:]

        tl = otio.schema.Timeline()
        tl.name = "ORIGINAL"
        result = otio.hooks.run("pre_adapter_write", tl, TEST_METADATA)
        self.assertEqual(result.name, "ORIGINAL")


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