-
Notifications
You must be signed in to change notification settings - Fork 293
/
autogen_plugin_documentation.py
420 lines (303 loc) · 10.1 KB
/
autogen_plugin_documentation.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python
#
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the OpenTimelineIO project
"""Generates documentation of all the built in plugins for OpenTimelineIO"""
# @TODO: HookScripts and Hooks don't have formatters. Because OTIO doesn't
# ship with any, I've left places for them but haven't fleshed them out.
import argparse
import tempfile
import textwrap
import io
import opentimelineio as otio
# force using the same path separator regardless of what the OS says
# this ensures same behavior on windows and linux
PATH_SEP = "/"
ALL_PLUGINS_TEXT = """This documents all the plugins that otio could find."""
PUBLIC_ONLY_TEXT = """This documents all the plugins that ship with in the open
source OpenTimelineIO distribution.
"""
DOCUMENT_HEADER = """# Plugin Documentation
{plugin_header_text}
This document is automatically generated by running the
`autogen_plugin_documentation` command, or by running `make plugin-model`. It
is part of the unit tests suite and should be updated whenever the schema
changes. If it needs to be updated, run: `make doc-plugins-update` and this
file should be regenerated.
# Manifests
The manifests describe plugins that are visible to OpenTimelineIO. The core and
contrib manifests are listed first, then any user-defined local plugins.
{manifests}
# Core Plugins
Manifest path: `{manifest_path}`
{manifest_contents}
# Contrib Plugins
Plugins in Contrib are supported by the community and provided as-is.
Manifest path: `{contrib_manifest_path}`
{contrib_manifest_contents}
{local_manifest_text}
"""
MANIFEST_CONTENT_TEMPLATE = """
## Adapter Plugins
Adapter plugins convert to and from OpenTimelineIO.
[Adapters documentation page for more information](./adapters).
[Tutorial on how to write an adapter](write-an-adapter).
{adapters}
## Media Linkers
Media Linkers run after the adapter has read in the file and convert the media
references into valid references where appropriate.
[Tutorial on how to write a Media Linker](write-a-media-linker).
{media_linkers}
## SchemaDefs
SchemaDef plugins define new external schema.
[Tutorial on how to write a schemadef](write-a-schemadef).
{schemadefs}
## HookScripts
HookScripts are extra plugins that run on _hooks_.
[Tutorial on how to write a hookscript](write-a-hookscript).
{hook_scripts}
## Hooks
Hooks are the points at which hookscripts will run.
{hooks}
"""
LOCAL_MANIFEST_TEMPLATE = """
# Local Manifests
Local manifests found:
{manifest_paths}
{local_manifest_body}
"""
PLUGIN_TEMPLATE = """
### {name}
```
{doc}
```
*source*: `{path}`
{other}
"""
ADAPTER_TEMPLATE = """
*Supported Features (with arguments)*:
{}
"""
SCHEMADEF_TEMPLATE = """
*Serializable Classes*:
{}
"""
def _parsed_args():
""" parse commandline arguments with argparse """
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-d",
"--dryrun",
action="store_true",
default=False,
help="Dryrun mode - print out instead of perform actions"
)
group.add_argument(
"-o",
"--output",
type=str,
default=None,
help="Update the baseline with the current version"
)
parser.add_argument(
"-p",
"--public-only",
default=False,
action="store_true",
help=(
"Only include plugins defined in the public open source manifests."
" Used by unit test."
)
)
parser.add_argument(
"-s",
"--sanitized-paths",
default=False,
action="store_true",
help="Sanitize paths to only show last three directories in a path."
)
return parser.parse_args()
def _format_plugin(plugin_map, extra_stuff, sanitized_paths):
# XXX: always force unix path separator so that the output is consistent
# between every platform.
PATH_SEP = "/"
path = plugin_map['path']
# force using PATH_SEP in place of os.path.sep
path = path.replace("\\", PATH_SEP)
if sanitized_paths:
path = PATH_SEP.join(path.split(PATH_SEP)[-3:])
return PLUGIN_TEMPLATE.format(
name=plugin_map['name'],
doc=plugin_map['doc'],
path=path,
other=extra_stuff,
)
def _format_doc(docstring, prefix):
"""Use textwrap to format a docstring for markdown."""
initial_indent = prefix
# subsequent_indent = " " * len(prefix)
subsequent_indent = " " * 2
block = docstring.split("\n")
fmt_block = []
for line in block:
line = textwrap.fill(
line,
initial_indent=initial_indent,
subsequent_indent=subsequent_indent,
width=len(subsequent_indent) + 80,
)
initial_indent = subsequent_indent
fmt_block.append(line)
return "\n".join(fmt_block)
def _format_adapters(plugin_map):
feature_lines = []
for feature, feature_data in plugin_map['supported features'].items():
doc = feature_data['doc']
if doc:
feature_lines.append(
_format_doc(doc, f"- {feature}: \n```\n") + "\n```"
)
else:
feature_lines.append(
f"- {feature}:"
)
for arg in feature_data["args"]:
feature_lines.append(f" - {arg}")
return ADAPTER_TEMPLATE.format("\n".join(feature_lines))
def _format_schemadefs(plugin_map):
feature_lines = []
for sd in plugin_map['SchemaDefs'].keys():
doc = plugin_map['SchemaDefs'][sd]['doc']
if doc:
feature_lines.append(
_format_doc(doc, f"- {sd}: \n```\n") + "\n```"
)
else:
feature_lines.append(f"- {sd}:")
return SCHEMADEF_TEMPLATE.format("\n".join(feature_lines))
_PLUGIN_FORMAT_MAP = {
"adapters": _format_adapters,
"schemadefs": _format_schemadefs,
}
def _manifest_formatted(
plugin_info_map,
manifest_paths=None,
sanitized_paths=False
):
display_map = {}
for pt in otio.plugins.manifest.OTIO_PLUGIN_TYPES:
pt_lines = []
sorted_plugins = [
plugin_info_map[pt][name]
for name in sorted(plugin_info_map[pt].keys())
]
for plug in sorted_plugins:
if "ERROR" in plug or not plug:
continue
# filter out plugins from other manifests
if manifest_paths and plug['from manifest'] not in manifest_paths:
continue
plugin_stuff = ""
try:
plugin_stuff = _PLUGIN_FORMAT_MAP[pt](plug)
except KeyError:
pass
plug_lines = _format_plugin(plug, plugin_stuff, sanitized_paths)
pt_lines.append(plug_lines)
display_map[pt] = "\n".join(str(line) for line in pt_lines)
return MANIFEST_CONTENT_TEMPLATE.format(
adapters=display_map['adapters'],
media_linkers=display_map['media_linkers'],
schemadefs=display_map['schemadefs'],
hook_scripts=display_map['hook_scripts'],
hooks=display_map['hooks'],
)
def generate_and_write_documentation_plugins(
public_only=False,
sanitized_paths=False
):
md_out = io.StringIO()
plugin_info_map = otio.plugins.plugin_info_map()
# start with the manifest list
manifest_path_list = plugin_info_map['manifests'][:]
if public_only:
manifest_path_list = manifest_path_list[:2]
sanitized_paths = manifest_path_list[:]
if sanitized_paths:
# conform all paths to unix-style path separators and leave relative
# paths (relative to root of OTIO directory)
sanitized_paths = [
PATH_SEP.join(p.replace("\\", PATH_SEP).split(PATH_SEP)[-3:])
for p in manifest_path_list
]
manifest_list = "\n".join(f"- `{mp}`" for mp in sanitized_paths)
core_manifest_path = manifest_path_list[0]
core_manifest_path_sanitized = sanitized_paths[0]
core_manifest_text = _manifest_formatted(
plugin_info_map,
[core_manifest_path],
sanitized_paths
)
contrib_manifest_path = manifest_path_list[1]
contrib_manifest_path_sanitized = sanitized_paths[1]
contrib_manifest_text = _manifest_formatted(
plugin_info_map,
[contrib_manifest_path],
sanitized_paths
)
local_manifest_text = ""
if len(plugin_info_map) > 2 and not public_only:
local_manifest_paths = manifest_path_list[2:]
local_manifest_paths_sanitized = sanitized_paths[2:]
local_manifest_list = "\n".join(
f"- `{mp}`" for mp in local_manifest_paths_sanitized
)
local_manifest_body = _manifest_formatted(
plugin_info_map,
local_manifest_paths,
sanitized_paths
)
local_manifest_text = LOCAL_MANIFEST_TEMPLATE.format(
manifest_paths=local_manifest_list,
local_manifest_body=local_manifest_body,
)
header = PUBLIC_ONLY_TEXT if public_only else ALL_PLUGINS_TEXT
md_out.write(
DOCUMENT_HEADER.format(
plugin_header_text=header,
manifests=manifest_list,
manifest_path=core_manifest_path_sanitized,
manifest_contents=core_manifest_text,
contrib_manifest_path=contrib_manifest_path_sanitized,
contrib_manifest_contents=contrib_manifest_text,
local_manifest_text=local_manifest_text,
)
)
return md_out.getvalue()
def main():
""" main entry point """
args = _parsed_args()
docs = generate_and_write_documentation_plugins(
args.public_only,
args.sanitized_paths
)
# print it out somewhere
if args.dryrun:
print(docs)
return
output = args.output
if not output:
output = tempfile.NamedTemporaryFile(
'w',
suffix="otio_serialized_schema.md",
delete=False
).name
with open(output, 'w') as fo:
fo.write(docs)
print(f"wrote documentation to {output}.")
if __name__ == '__main__':
main()