Skip to content

Commit 97517f9

Browse files
committed
tools: Modernize convert-unicode-emoji-data.
* Use `pathlib` rather than `os` * Use f-strings and double quotes where generated data is unchanged * Use strings over many lines rather than repeated function calls * Cleanly handle the server unicode emoji not yet having been fetched * Use the script name rather than hard-coded text
1 parent e846e39 commit 97517f9

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

tools/convert-unicode-emoji-data

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
# Convert emoji file downloaded using tools/fetch-unicode-emoji-data
44
# into what is required by ZT.
55

6-
import os
76
from collections import OrderedDict
7+
from pathlib import Path, PurePath
88

9-
from zulipterminal.unicode_emoji_dict import EMOJI_NAME_MAPS
109

10+
try:
11+
from zulipterminal.unicode_emoji_dict import EMOJI_NAME_MAPS
12+
except ModuleNotFoundError:
13+
print(
14+
"ERROR: Could not find downloaded unicode emoji\n"
15+
"Try fetching the unicode first using tools/fetch-unicode-emojis-data"
16+
)
17+
exit(1)
1118

12-
INPUT_FILE = "zulipterminal/unicode_emoji_dict.py"
13-
OUTPUT_FILE = "zulipterminal/unicode_emojis.py"
19+
WORKING_FOLDER = Path(__file__).resolve().parent.parent / "zulipterminal"
20+
INPUT_FILE = WORKING_FOLDER / "unicode_emoji_dict.py"
21+
OUTPUT_FILE = WORKING_FOLDER / "unicode_emojis.py"
22+
23+
SCRIPT_NAME = PurePath(__file__).name
1424

1525
emoji_dict = EMOJI_NAME_MAPS
1626

@@ -22,19 +32,22 @@ for emoji_code, emoji_names in emoji_dict.items():
2232

2333
ordered_emojis = OrderedDict(sorted(emoji_map.items()))
2434

25-
with open(OUTPUT_FILE, "w") as f:
26-
f.write('from collections import OrderedDict\n\n\n')
27-
f.write('# Generated automatically by '
28-
'tools/convert-unicode-emoji-data\n')
29-
f.write('# Do not modify.\n\n')
30-
f.write('EMOJI_DATA = OrderedDict([\n')
35+
with OUTPUT_FILE.open("w") as f:
36+
f.write(
37+
"from collections import OrderedDict\n\n\n"
38+
f"# Generated automatically by tools/{SCRIPT_NAME}\n"
39+
"# Do not modify.\n\n"
40+
"EMOJI_DATA = OrderedDict([\n"
41+
)
3142
for emoji_code, emoji_name in ordered_emojis.items():
3243
# {'smile': {'code': '263a'}}
33-
f.write(" ('%s', {'code': '%s'}),\n" % (emoji_code,
34-
emoji_name))
35-
f.write('])\n')
36-
37-
print("Emoji list saved in {}".format(OUTPUT_FILE))
38-
if os.path.exists(INPUT_FILE):
39-
os.remove(INPUT_FILE)
40-
print("{} deleted.".format(INPUT_FILE))
44+
f.write(f" ('{emoji_code}', {{'code': '{emoji_name}'}}),\n")
45+
f.write("])\n")
46+
47+
print(f"Emoji list saved in {OUTPUT_FILE}")
48+
49+
try:
50+
INPUT_FILE.unlink()
51+
print(f"{INPUT_FILE} deleted.")
52+
except Exception as exc:
53+
print("Warning: Could not delete file:\n{exc}")

0 commit comments

Comments
 (0)