Skip to content

Commit a84ddfa

Browse files
committed
refactor: helper/model/emojis: Use TypedDict for emoji data.
1 parent 1bf7f16 commit a84ddfa

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

tools/convert-unicode-emoji-data

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ for emoji_code, emoji_names in emoji_dict.items():
2323
ordered_emojis = OrderedDict(sorted(emoji_map.items()))
2424

2525
with open(OUTPUT_FILE, "w") as f:
26-
f.write('from collections import OrderedDict\n\n\n')
26+
f.write('from collections import OrderedDict\n\n')
27+
f.write('from helper import NamedEmojiData\n\n\n')
2728
f.write('# Generated automatically by '
2829
'tools/convert-unicode-emoji-data\n')
2930
f.write('# Do not modify.\n\n')
@@ -32,7 +33,7 @@ with open(OUTPUT_FILE, "w") as f:
3233
# {'smile': {'code': '263a', 'type': 'unicode_emoji'}}
3334
f.write(" ('%s', {'code': '%s',\n"
3435
" 'type': '%s'}),\n" % (emoji_code, emoji_name, 'unicode_emoji'))
35-
f.write('])\n')
36+
f.write(']) # type: NamedEmojiData\n')
3637

3738
print("Emoji list saved in {}".format(OUTPUT_FILE))
3839
if os.path.exists(INPUT_FILE):

zulipterminal/helper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414

1515
import lxml.html
1616
from mypy_extensions import TypedDict
17+
from typing_extensions import Literal
1718

1819

1920
MACOS = platform.system() == "Darwin"
2021
LINUX = platform.system() == "Linux"
2122
WSL = 'microsoft' in platform.release().lower()
2223

24+
EmojiData = TypedDict('EmojiData', {
25+
'code': str,
26+
'type': Literal['realm_emoji', 'unicode_emoji'],
27+
})
28+
29+
NamedEmojiData = Dict[str, EmojiData]
30+
2331
Message = TypedDict('Message', {
2432
'id': int,
2533
'sender_id': int,

zulipterminal/model.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from zulipterminal import unicode_emojis
1515
from zulipterminal.config.keys import keys_for_command
1616
from zulipterminal.helper import (
17-
Message, asynch, canonicalize_color, classify_unread_counts,
18-
display_error_if_present, index_messages, initial_index, notify, set_count,
17+
Message, NamedEmojiData, asynch, canonicalize_color,
18+
classify_unread_counts, display_error_if_present, index_messages,
19+
initial_index, notify, set_count,
1920
)
2021
from zulipterminal.ui_tools.utils import create_msg_box_list
2122

@@ -127,8 +128,8 @@ def __init__(self, controller: Any) -> None:
127128
self.unread_counts = classify_unread_counts(self)
128129

129130
custom_emojis = self.fetch_custom_emojis()
130-
all_emojis = (list(unicode_emojis.EMOJI_DATA.items())
131-
+ list(custom_emojis.items()))
131+
all_emojis = list(unicode_emojis.EMOJI_DATA.items()
132+
+ list(custom_emojis.items()))
132133
self.active_emoji_data = OrderedDict(sorted(all_emojis,
133134
key=lambda e: e[0]))
134135

@@ -348,12 +349,12 @@ def update_stream_message(self, topic: str, msg_id: int,
348349
display_error_if_present(response, self.controller)
349350
return response['result'] == 'success'
350351

351-
def fetch_custom_emojis(self) -> Dict[str, Dict[str, str]]:
352+
def fetch_custom_emojis(self) -> NamedEmojiData:
352353
response = self.client.get_realm_emoji()
353354
custom_emojis = {emoji['name']: {'code': emoji_code,
354355
'type': 'realm_emoji'}
355356
for emoji_code, emoji in response['emoji'].items()
356-
if not emoji['deactivated']}
357+
if not emoji['deactivated']} # type: NamedEmojiData
357358
return custom_emojis
358359

359360
def get_messages(self, *,

zulipterminal/unicode_emojis.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from collections import OrderedDict
22

3+
from helper import NamedEmojiData
4+
35

46
# Generated automatically by tools/convert-unicode-emoji-data
57
# Do not modify.
@@ -3111,4 +3113,4 @@
31113113
'type': 'unicode_emoji'}),
31123114
('zzz', {'code': '1f4a4',
31133115
'type': 'unicode_emoji'}),
3114-
])
3116+
]) # type: NamedEmojiData

0 commit comments

Comments
 (0)