-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·139 lines (118 loc) · 4.55 KB
/
main.py
File metadata and controls
executable file
·139 lines (118 loc) · 4.55 KB
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
import os
import re
from pathlib import Path
# USER: below, specify assets_folders, json_folders, and assets_to_check according to your needs
# USER: specify assets folder(s)
# assets_folders is a list containing one or more dictionaries.
# Each dictionary represents a folder with assets:
# the "path" key (str) specifies the directory path
# the "recursive" key (bool) indicates whether to include assets in nested subdirectories
# within the specified folder.
assets_folders = [
{
"path": "assets/",
"recursive": True
}
]
# USER: specify JSON folder(s)
# json_folders is a list containing one or more dictionaries.
# Each dictionary represents a folder with JSON files:
# the "path" key (str) specifies the directory path
# the "recursive" (bool) key indicates whether to include JSON files in nested subdirectories
# within the specified folder.
# If there are no relevant JSON folders, use an empty list: json_folders = []
json_folders = [
{
"path": "json/",
"recursive": True
}
]
# USER: specify assets to check
# assets_to_check is a list containing one or more dictionaries.
# Each dictionary represents an asset:
# the "asset_label" key (str) specifies the label used in the output
# the "asset_extension" key (str) specifies the extension that defines the asset
# NOTE:
# for detecting asset filenames in the JSON, the script requires that your
# assets filenames include an extension that starts with a period,
# for instance ".png" for an image filename
assets_to_check = [
{"asset_label": "images", "asset_extension": "png"},
{"asset_label": "sound files", "asset_extension": "mp3"},
{"asset_label": "videos", "asset_extension": "mp4"}
]
def main():
json_files = []
# Add all JSON files from relevant JSON folders
for folder in json_folders:
if not os.path.isdir(folder["path"]):
print(f"ERROR: unable to find folder '{folder["path"]}'")
return
try:
json_files.extend(
[
str(filename)
for folder in json_folders
for filename in (Path(folder["path"]).rglob("*.json") if folder["recursive"] else Path(folder["path"]).glob("*.json"))
]
)
except NameError:
pass
try:
json_files
except NameError:
print("ERROR: Please specify one or more valid JSON folders")
return
try:
assets_folders
except NameError:
print("ERROR: Please specify one or more existing assets folders")
return
for folder in assets_folders:
if not os.path.isdir(folder["path"]):
print(f"ERROR: unable to find folder '{folder}'")
return
# Check assets
for asset in assets_to_check:
check_assets(json_files, asset["asset_label"], asset["asset_extension"])
def check_assets(json_files, asset_label, asset_extension):
"""Checks to see if all assets included in JSON are present in a folder with assets"""
# arguments:
# asset_label is the output label for the category of asset checked
# asset_extension is the filename extension associated with the asset
asset_extension = asset_extension.lstrip(".")
json_assets = []
# Create list of all relevant asset filenames in JSON files
for json_file in json_files:
with open(json_file, "r") as filename:
json_text = filename.read()
json_assets.extend(re.findall(rf"[^\"]*\.{asset_extension}", json_text))
json_assets = set(json_assets)
# Create list of relevant assets in assets folder(s)
assets_files = []
assets_files.extend(
[
str(path)
for folder in assets_folders
for path in (
Path(folder["path"]).rglob(f"*.{asset_extension}") if folder["recursive"] else Path(folder["path"]).glob(f"*.{asset_extension}")
)
]
)
assets_files = set(map(os.path.basename, assets_files))
# Check to see if any assets are missing
missing_assets = json_assets - assets_files
if missing_assets:
# Report missing assets
print(
f"\n\033[41m{len(missing_assets)} MISSING {asset_label.upper()}\033[0m (.{asset_extension})"
)
for asset in missing_assets:
print(asset)
else:
# Report no assets are missing
print(
f"\n\033[0;42mNO MISSING {asset_label.upper()}\033[0m (.{asset_extension})"
)
if __name__ == "__main__":
main()