-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
230 lines (177 loc) · 6.86 KB
/
Copy pathpreprocess.py
File metadata and controls
230 lines (177 loc) · 6.86 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
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
#!/usr/bin/env python3
"""
Scans downloaded icon repositories and extracts metadata into JSON files
for the icon browser frontend.
Handles two different icon theme structures:
- Adwaita: Adwaita/<size>/<category>/<icon>.svg
- Breeze: icons/<category>/<size>/<icon>.svg
"""
import json
import os
import sys
from collections import defaultdict
from pathlib import Path
from typing import TypedDict
class IconData(TypedDict):
category: str
sizes: dict[str, dict[str, str]]
paths: list[str]
SCRIPT_DIR = Path(__file__).parent.resolve()
ICONS_DIR = SCRIPT_DIR / "icons"
DATA_DIR = SCRIPT_DIR / "data"
ADWAITA_ROOT = ICONS_DIR / "adwaita-icon-theme" / "Adwaita"
BREEZE_ROOT = ICONS_DIR / "breeze-icons" / "icons"
def normalize_size(size: str) -> str:
"""Normalize size names: '16x16' -> '16', keep 'scalable', 'symbolic', etc."""
if "x" in size and size.replace("x", "").isdigit():
return size.split("x")[0]
return size
def scan_adwaita(root: Path) -> dict[str, IconData]:
"""
Scan Adwaita icons.
Structure: <root>/<size>/<category>/<icon>.<ext>
"""
icons: defaultdict[str, IconData] = defaultdict(lambda: {"category": "", "sizes": {}, "paths": []})
for dirpath, _dn, filenames in os.walk(root):
rel = Path(dirpath).relative_to(root)
parts = rel.parts
if any(p.startswith(".") for p in parts):
continue
# Need at least size/category
if len(parts) < 2:
continue
size = normalize_size(parts[0]) # e.g. "scalable", "16", "symbolic"
category = parts[1] # e.g. "places", "devices", "emblems"
for fname in filenames:
if fname.startswith(".") or not fname.endswith((".svg", ".png")):
continue
fpath = Path(dirpath) / fname
ext = fpath.suffix.lower()
icon_name = fpath.stem
entry = icons[icon_name]
entry["category"] = category
entry["paths"].append(str(fpath.relative_to(root)))
if size not in entry["sizes"]:
entry["sizes"][size] = {}
entry["sizes"][size][ext[1:]] = str(fpath.relative_to(root))
return dict(icons)
def scan_breeze(root: Path) -> dict[str, IconData]:
"""
Scan Breeze icons.
Structure: <root>/<category>/<size>/<icon>.<ext>
"""
icons: defaultdict[str, IconData] = defaultdict(lambda: {"category": "", "sizes": {}, "paths": []})
# Breeze size directories contain subdirectories for formats
# e.g. icons/actions/16/svg/ or icons/actions/16/ (with files directly)
# Also: icons/actions/16@2x/, icons/actions/16@3x/ for HiDPI
for dirpath, _dn, filenames in os.walk(root):
rel = Path(dirpath).relative_to(root)
parts = rel.parts
if any(p.startswith(".") for p in parts):
continue
# Need at least category/size
if len(parts) < 2:
continue
category = parts[0] # e.g. "actions", "apps", "places"
size_dir = parts[1] # e.g. "16", "16@2x", "22", "scalable"
# Skip CMakeLists, .cpp, .h, .theme.in files in category root
# These are in the category directory itself (len(parts) == 1)
# The size_dir might be a direct size or might have a format subdir
# Check if there's a format subdirectory (svg/, png/)
if len(parts) >= 3:
# e.g. icons/actions/16/svg/icon.svg
fmt_dir = parts[2]
if fmt_dir in ("svg", "png"):
size = size_dir
else:
# Some other subdirectory, skip
continue
else:
# Files directly in size dir: icons/actions/16/icon.svg
size = size_dir
for fname in filenames:
if fname.startswith(".") or not fname.endswith((".svg", ".png")):
continue
fpath = Path(dirpath) / fname
ext = fpath.suffix.lower()
icon_name = fpath.stem
entry = icons[icon_name]
entry["category"] = category
entry["paths"].append(str(fpath.relative_to(root)))
if size not in entry["sizes"]:
entry["sizes"][size] = {}
entry["sizes"][size][ext[1:]] = str(fpath.relative_to(root))
return dict(icons)
def build_common_index(adwaita: dict, breeze: dict) -> list[dict]:
"""Find icons that exist in both themes."""
common_names = set(adwaita.keys()) & set(breeze.keys())
result = []
for name in sorted(common_names):
result.append(
{
"name": name,
"adwaita": adwaita[name],
"breeze": breeze[name],
}
)
return result
def main():
if not ADWAITA_ROOT.exists():
print(f"Error: Adwaita directory not found: {ADWAITA_ROOT}")
print("Run download-icons.sh first.")
sys.exit(1)
if not BREEZE_ROOT.exists():
print(f"Error: Breeze directory not found: {BREEZE_ROOT}")
print("Run download-icons.sh first.")
sys.exit(1)
DATA_DIR.mkdir(exist_ok=True)
print("Scanning Adwaita icons...")
adwaita_icons = scan_adwaita(ADWAITA_ROOT)
print(f" Found {len(adwaita_icons)} icons")
print("Scanning Breeze icons...")
breeze_icons = scan_breeze(BREEZE_ROOT)
print(f" Found {len(breeze_icons)} icons")
print("Finding common icons...")
common = build_common_index(adwaita_icons, breeze_icons)
print(f" Found {len(common)} icons in both themes")
# Collect all categories from both themes
categories = sorted(
set(info["category"] for icons in [adwaita_icons, breeze_icons] for info in icons.values() if info["category"])
)
# Collect all unique sizes
all_sizes = set()
for icons in [adwaita_icons, breeze_icons]:
for info in icons.values():
all_sizes.update(info["sizes"].keys())
# Write output
adwaita_out = DATA_DIR / "adwaita-icons.json"
breeze_out = DATA_DIR / "breeze-icons.json"
common_out = DATA_DIR / "common-icons.json"
meta_out = DATA_DIR / "meta.json"
with open(adwaita_out, "w") as f:
json.dump(adwaita_icons, f, indent=2)
print(f"Wrote {adwaita_out}")
with open(breeze_out, "w") as f:
json.dump(breeze_icons, f, indent=2)
print(f"Wrote {breeze_out}")
with open(common_out, "w") as f:
json.dump(common, f, indent=2)
print(f"Wrote {common_out}")
with open(meta_out, "w") as f:
json.dump(
{
"categories": categories,
"sizes": sorted(all_sizes),
"counts": {
"adwaita": len(adwaita_icons),
"breeze": len(breeze_icons),
"common": len(common),
},
},
f,
indent=2,
)
print(f"Wrote {meta_out}")
print("\nDone! Open index.html in a browser.")
if __name__ == "__main__":
main()