-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist_test_content.py
124 lines (107 loc) · 3.68 KB
/
list_test_content.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
from pathlib import Path
import json
import sys
def scene_metadata(fp):
return fp.parent / 'metadata' / fp.with_suffix('.json').name
def write_scene_metadata(gltf, credits):
data = {
"version" : 2,
"legal" : credits,
"tags" : ["testing"],
"screenshot" : f"metadata/{gltf.stem}.jpg",
"name" : f"{gltf.stem}",
"path" : f"{gltf.name}",
"summary" : f"{gltf.stem}"
}
meta = scene_metadata(gltf)
with open(meta, 'w') as meta_fp:
json.dump(data, meta_fp, indent=4)
def gen_directory_metadata(dp):
c = None
cfp = dp / 'metadata' / 'credits.json'
if cfp.exists():
with open( cfp, 'r') as fo:
c = json.load(fo)
for scene_fp in dp.glob('*.gltf'):
write_scene_metadata(scene_fp, c)
for scene_fp in dp.glob('*.glb'):
write_scene_metadata(scene_fp, c)
def read_scene_data(gltf_fp):
fp = scene_metadata(gltf_fp)
data = None
gltf_data = None
with open(fp, 'r') as fo:
data = json.load(fo)
with open(gltf_fp, 'r') as gltf_fo:
gltf_data = json.load(gltf_fo)
data["extensions"] = [ext for ext in gltf_data["extensionsUsed"] if ext.startswith("MPEG_")]
return data
def copyright(c):
artist = c.get('artist', None)
lic = f"<a alt=\"license\" href=\"{c['licenseUrl']}\">{c['license']}</a>"
res = [
f"© {c['year']}, {c['owner']}, {lic}\n"
]
if artist is None:
res.append( f" - {c['what']}\n" )
else:
res.append( f" - {artist} for {c['what']}\n" )
return res
def as_table_row(scene, dfp):
name = scene['name']
res = [
"<tr>","<td>",
f"<a href=\"{dfp.stem}\">{name}</a><br>",
f"<img src=\"{dfp.stem}/{scene['screenshot']}\" alt=\"{name}\"/>",
"</td>",
"<td>",
f"{scene['summary']}<br>"
]
# res.append("Credit:")
# for c in scene["legal"]:
# res += copyright(c)
res + ["</td>", "</tr>"]
return res
def process_root_readme(root):
listing = ["<table>","<tr>","<th>Model</th>","<th>Description</th>","</tr>"]
for p in root.iterdir():
if p.is_dir():
scenes = [read_scene_data(sfp) for sfp in p.glob('*.gltf')]
for s in scenes:
listing += as_table_row(s, p)
listing += ["</table>"]
with open('listing.md', 'w') as ofp:
ofp.write("\n".join(listing))
def process_directory_readme(dp):
readme = []
for sfp in dp.glob('*.gltf'):
scene = read_scene_data(sfp)
readme.append(f"\n## {scene['name']}")
readme.append(f"\n### Summary")
readme.append(scene["summary"]+"\n")
readme.append(f"\n### Extensions used\n")
for ext in scene["extensions"]:
readme.append(f"- {ext}")
readme.append(f"\n### Screeshot")
readme.append(f"![screenshot]({scene['screenshot']})")
readme.append(f"\n### Legal\n")
for c in scene["legal"]:
readme += copyright(c)
with open( dp / 'README.md', 'w') as ofp:
ofp.write("\n".join(readme))
if __name__ == "__main__":
"""
1. python ./list_test_content.py
create listing.md with a table listing all models for use in the main README.md
2. python ./list_test_content.py DIR
generates a README.md draft for a DIR from using the available metadata
"""
if len(sys.argv) == 1:
root = Path(__file__).parent
# for p in root.iterdir():
# if p.is_dir():
# gen_directory_metadata(p)
process_root_readme(root)
else:
p = Path(sys.argv[1])
process_directory_readme(p)