-
Notifications
You must be signed in to change notification settings - Fork 35
/
generate_js.py
executable file
·62 lines (48 loc) · 1.5 KB
/
generate_js.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
#!/usr/bin/env python3
import json
from glob import glob
from os.path import basename, join, splitext
from string import Template
from xml.dom.minidom import Document, parse
SVG = "svg"
OUTFILE = join("dist", "hass-bha-icons.js")
def get_path(dom: Document) -> str:
"""Get the path of the svg file."""
return dom.getElementsByTagName("path")[0].getAttribute("d")
def get_keywords(dom: Document) -> str:
"""Get the keywords of the svg file."""
desc_tags = dom.getElementsByTagName("desc")
if len(desc_tags) > 0 and desc_tags[0].firstChild is not None:
return desc_tags[0].firstChild.nodeValue.split(" ")
else:
return []
doms = [
(basename(splitext(file)[0]), parse(file)) for file in glob(join(SVG, f"*.{SVG}"))
]
icons = {
file_name: {
"path": get_path(dom),
"keywords": get_keywords(dom),
}
for file_name, dom in doms
}
template = Template(
"""const BHA_ICONS_MAP = $icons;
async function getIcon(name) {
return {path: BHA_ICONS_MAP[name]?.path};
}
async function getIconList() {
return Object.entries(BHA_ICONS_MAP).map(([icon, content]) => ({
name: icon,
keywords: content.keywords,
}));
}
window.customIcons = window.customIcons || {};
window.customIcons["bha"] = { getIcon, getIconList };
window.customIconsets = window.customIconsets || {};
window.customIconsets["bha"] = getIcon;
"""
)
js = template.substitute(icons=json.dumps(icons, sort_keys=True, indent=2))
with open(OUTFILE, "w") as outfile:
outfile.write(js)