From c41e185b5909d855004ea81deb39c2a0da19d4e5 Mon Sep 17 00:00:00 2001 From: arjun Date: Sun, 17 Nov 2019 18:13:34 +0530 Subject: [PATCH 1/2] update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5f26f24..96513b9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ sdist/ var/ wheels/ *.egg-info/ +.idea .installed.cfg *.egg MANIFEST From 751c60281aea95e5d2a89bc906532890a35f3672 Mon Sep 17 00:00:00 2001 From: arjun Date: Sun, 17 Nov 2019 19:48:18 +0530 Subject: [PATCH 2/2] refactor with async/await --- app.py | 66 +++++++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/app.py b/app.py index 70afea8..294bb40 100755 --- a/app.py +++ b/app.py @@ -95,7 +95,6 @@ def __init__(self, config): def sidebar_column(self): return self.columns.contents[0] - def start(self): self._loading = True loop.create_task(self.animate_loading()) @@ -132,23 +131,21 @@ def chatbox(self): def chatbox(self, chatbox): self.columns.contents[1][0].original_widget = chatbox - @asyncio.coroutine - def animate_loading(self): + async def animate_loading(self): def update(*args): if self._loading: self.chatbox.circular_loading.next_frame() self.urwid_loop.set_alarm_in(0.2, update) + update() - @asyncio.coroutine - def component_did_mount(self): + async def component_did_mount(self): with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: - yield from self.mount_sidebar(executor) - yield from self.mount_chatbox(executor, self.store.state.channels[0]['id']) + await self.mount_sidebar(executor) + await self.mount_chatbox(executor, self.store.state.channels[0]['id']) - @asyncio.coroutine - def mount_sidebar(self, executor): - yield from asyncio.gather( + async def mount_sidebar(self, executor): + await asyncio.gather( loop.run_in_executor(executor, self.store.load_auth), loop.run_in_executor(executor, self.store.load_channels), loop.run_in_executor(executor, self.store.load_stars), @@ -222,8 +219,7 @@ def mount_sidebar(self, executor): loop.create_task(self.get_presences(executor, self.sidebar.get_all_dms())) loop.create_task(self.get_dms_unread(executor, self.sidebar.get_all_dms())) - @asyncio.coroutine - def get_presences(self, executor, dm_widgets): + async def get_presences(self, executor, dm_widgets): """ Compute and return presence because updating UI from another thread is unsafe :param executor: @@ -233,7 +229,7 @@ def get_presences(self, executor, dm_widgets): def get_presence(dm_widget): presence = self.store.get_presence(dm_widget.user) return [dm_widget, presence] - presences = yield from asyncio.gather(*[ + presences = await asyncio.gather(*[ loop.run_in_executor(executor, get_presence, dm_widget) for dm_widget in dm_widgets ]) @@ -243,8 +239,7 @@ def get_presence(dm_widget): if response['ok']: widget.set_presence(response['presence']) - @asyncio.coroutine - def get_dms_unread(self, executor, dm_widgets): + async def get_dms_unread(self, executor, dm_widgets): """ Compute and return unread_count_display because updating UI from another thread is unsafe :param executor: @@ -255,7 +250,7 @@ def get_presence(dm_widget): profile_response = self.store.get_channel_info(dm_widget.id) return [dm_widget, profile_response] - responses = yield from asyncio.gather(*[ + responses = await asyncio.gather(*[ loop.run_in_executor(executor, get_presence, dm_widget) for dm_widget in dm_widgets ]) @@ -265,12 +260,11 @@ def get_presence(dm_widget): if response is not None: widget.set_unread(response['unread_count_display']) - @asyncio.coroutine - def get_channels_info(self, executor, channels): + async def get_channels_info(self, executor, channels): def get_info(channel): info = self.store.get_channel_info(channel.id) return [channel, info] - channels_info = yield from asyncio.gather(*[ + channels_info = await asyncio.gather(*[ loop.run_in_executor(executor, get_info, channel) for channel in channels ]) @@ -279,8 +273,7 @@ def get_info(channel): [widget, response] = channel_info widget.set_unread(response.get('unread_count_display', 0)) - @asyncio.coroutine - def update_chat(self, event): + async def update_chat(self, event): """ Update channel/DM message count badge :param event: @@ -288,9 +281,8 @@ def update_chat(self, event): """ self.sidebar.update_items(event) - @asyncio.coroutine - def mount_chatbox(self, executor, channel): - yield from asyncio.gather( + async def mount_chatbox(self, executor, channel): + await asyncio.gather( loop.run_in_executor(executor, self.store.load_channel, channel), loop.run_in_executor(executor, self.store.load_messages, channel) ) @@ -602,8 +594,7 @@ def scroll_messages(self, *args): self.mark_read_slack(index) ) - @asyncio.coroutine - def mark_read_slack(self, index): + async def mark_read_slack(self, index): if not self.is_chatbox_rendered: return @@ -623,10 +614,9 @@ def mark_read_slack(self, index): if message.channel_id: self.store.mark_read(message.channel_id, message.ts) - @asyncio.coroutine - def _go_to_channel(self, channel_id): + async def _go_to_channel(self, channel_id): with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: - yield from asyncio.gather( + await asyncio.gather( loop.run_in_executor(executor, self.store.load_channel, channel_id), loop.run_in_executor(executor, self.store.load_messages, channel_id) ) @@ -675,12 +665,10 @@ def handle_close_set_snooze(self): self.urwid_loop.widget = self._body self.set_snooze_widget = None - @asyncio.coroutine - def dispatch_snooze_time(self, snoozed_time): + async def dispatch_snooze_time(self, snoozed_time): self.store.set_snooze(snoozed_time) - @asyncio.coroutine - def load_picture_async(self, url, width, message_widget, auth=True): + async def load_picture_async(self, url, width, message_widget, auth=True): width = min(width, 800) bytes_in_cache = self.store.cache.picture.get(url) if bytes_in_cache: @@ -690,7 +678,7 @@ def load_picture_async(self, url, width, message_widget, auth=True): headers = {} if auth: headers = {'Authorization': 'Bearer {}'.format(self.store.slack_token)} - bytes = yield from loop.run_in_executor( + bytes = await loop.run_in_executor( executor, functools.partial(requests.get, url, headers=headers) ) @@ -700,14 +688,13 @@ def load_picture_async(self, url, width, message_widget, auth=True): picture = Image(file.name, width=(width / 10)) message_widget.file = picture - @asyncio.coroutine - def load_profile_avatar(self, url, profile): + async def load_profile_avatar(self, url, profile): bytes_in_cache = self.store.cache.avatar.get(url) if bytes_in_cache: profile.avatar = bytes_in_cache return with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: - bytes = yield from loop.run_in_executor(executor, requests.get, url) + bytes = await loop.run_in_executor(executor, requests.get, url) file = tempfile.NamedTemporaryFile(delete=False) file.write(bytes.content) file.close() @@ -715,8 +702,7 @@ def load_profile_avatar(self, url, profile): self.store.cache.avatar[url] = avatar profile.avatar = avatar - @asyncio.coroutine - def start_real_time(self): + async def start_real_time(self): self.store.slack.rtm_connect(auto_reconnect=True) def stop_typing(*args): @@ -802,7 +788,7 @@ def stop_typing(*args): else: pass # print(json.dumps(event, indent=2)) - yield from asyncio.sleep(0.5) + await asyncio.sleep(0.5) def set_insert_mode(self): self.columns.focus_position = 1