forked from zulip/zulip-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-unicode-emoji-data
executable file
·75 lines (63 loc) · 2.06 KB
/
convert-unicode-emoji-data
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
#!/usr/bin/env python3
# Convert emoji file downloaded using tools/fetch-unicode-emoji-data
# into what is required by ZT.
from pathlib import Path, PurePath
try:
# Ignored for type-checking, as it is a temporary file, deleted at the end of file
from zulipterminal.unicode_emoji_dict import ( # type: ignore [import-not-found,import-untyped,unused-ignore]
EMOJI_NAME_MAPS,
)
except ModuleNotFoundError:
print(
"ERROR: Could not find downloaded unicode emoji\n"
"Try fetching the unicode first using tools/fetch-unicode-emojis-data"
)
exit(1)
DOCSTRING = "Unicode emoji data, synchronized semi-regularly with the server source"
WORKING_FOLDER = Path(__file__).resolve().parent.parent / "zulipterminal"
INPUT_FILE = WORKING_FOLDER / "unicode_emoji_dict.py"
OUTPUT_FILE = WORKING_FOLDER / "unicode_emojis.py"
SCRIPT_NAME = PurePath(__file__).name
emoji_dict = EMOJI_NAME_MAPS
emoji_map = {}
for emoji_code, emoji_data in emoji_dict.items():
emoji_map[emoji_data["canonical_name"]] = {
"code": emoji_code,
"aliases": emoji_data["aliases"],
}
ordered_emojis = dict(sorted(emoji_map.items()))
with OUTPUT_FILE.open("w") as f:
f.write(
"\n".join(
[
'"""',
DOCSTRING,
'"""',
"# Ignore long lines",
"# ruff: noqa: E501",
"",
"# fmt: off",
f"# Generated automatically by tools/{SCRIPT_NAME}",
"# Do not modify.\n",
"EMOJI_DATA = {\n",
]
)
)
for emoji_name, emoji in ordered_emojis.items():
# {'smile': {'code': '263a', 'aliases': []}}
f.write(f' "{emoji_name}": {emoji},\n')
f.write(
"\n".join(
[
"}",
"# fmt: on",
"",
]
)
)
print(f"Emoji list saved in {OUTPUT_FILE}")
try:
INPUT_FILE.unlink()
print(f"{INPUT_FILE} deleted.")
except Exception as exc:
print(f"Warning: Could not delete file:\n{exc}")