Skip to content

Commit

Permalink
replace App.add_background_task with asyncio.create_task
Browse files Browse the repository at this point in the history
  • Loading branch information
samschott committed Sep 19, 2024
1 parent 392eb8f commit 8ab53fd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/maestral_cocoa/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def on_row_clicked(self, sender: Any, row: SyncEventRow) -> None:
message="The file or folder no longer exists.",
)

async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
async def periodic_refresh_gui(self) -> None:
while self._refresh:
await self.refresh_gui()
await asyncio.sleep(self._refresh_interval)
Expand Down Expand Up @@ -173,5 +173,5 @@ def show(self) -> None:
self._initial_load = True

self._refresh = True
self.app.add_background_task(self.periodic_refresh_gui)
asyncio.create_task(self.periodic_refresh_gui())
super().show()
60 changes: 28 additions & 32 deletions src/maestral_cocoa/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

# system imports
import asyncio
import os
import sys
import gc
Expand Down Expand Up @@ -38,7 +39,6 @@
KeyringAccessError,
MaestralApiError,
)
from mypy.dmypy.client import action

# local imports
from . import __version__ as __gui_version__
Expand All @@ -62,7 +62,7 @@

def _build_snooze_command(snooze_time: float, maestral_proxy: MaestralProxy, **kwargs) -> toga.Command:

def snooze(interface, *args, **kwargs) -> None:
def snooze(**kwargs) -> None:
maestral_proxy.notification_snooze = snooze_time

return toga.Command(action=snooze, **kwargs)
Expand Down Expand Up @@ -116,7 +116,7 @@ def startup(self) -> None:
try:
pending_link = self.mdbx.pending_link
except KeyringAccessError:
self.add_background_task(self.update_error)
asyncio.create_task(self.update_error())
return

pending_folder = self.mdbx.pending_dropbox_folder
Expand All @@ -132,7 +132,7 @@ def startup(self) -> None:
setup_dialog.goto_page(2)

else:
self.add_background_task(self.on_setup_completed)
asyncio.create_task(self.on_setup_completed())

def set_icon(self, status: str) -> None:
if status != self._cached_status:
Expand All @@ -141,11 +141,7 @@ def set_icon(self, status: str) -> None:
)
self._cached_status = status

async def on_menu_open(self, interface, *args, **kwargs) -> None:
await self.update_snoozed(self)
await self.update_status(self)

async def on_setup_completed(self, interface, *args, **kwargs) -> None:
async def on_setup_completed(self) -> None:
self.mdbx.start_sync()
self.updater.start_updater()
self._linked_ui = True
Expand Down Expand Up @@ -173,7 +169,7 @@ def get_or_start_maestral_daemon(self) -> MaestralProxy:

return MaestralProxy(self.config_name)

def on_open_clicked(self, interface, *args, **kwargs):
def on_open_clicked(self, **kwargs):
click.launch(self.mdbx.dropbox_path)

def set_up_commands(self) -> None:
Expand Down Expand Up @@ -327,16 +323,16 @@ def set_up_commands(self) -> None:
# ==== callbacks menu items ========================================================

@staticmethod
def on_website_clicked(interface, *args, **kwargs) -> None:
def on_website_clicked(**kwargs) -> None:
"""Open the Dropbox website."""
click.launch("https://www.dropbox.com/")

@staticmethod
def on_help_clicked(interface, *args, **kwargs) -> None:
def on_help_clicked(**kwargs) -> None:
"""Open the Dropbox help website."""
click.launch(f"{__url__}/docs")

def on_start_stop_clicked(self, interface, *args, **kwargs) -> None:
def on_start_stop_clicked(self, **kwargs) -> None:
"""Pause / resume syncing on menu item clicked."""
try:
if self.item_pause.text == self.PAUSE_TEXT:
Expand All @@ -349,9 +345,9 @@ def on_start_stop_clicked(self, interface, *args, **kwargs) -> None:
self.mdbx.start_sync()
self.item_pause.text = self.PAUSE_TEXT
except NoDropboxDirError:
self.add_background_task(self._exec_dbx_location_dialog)
asyncio.create_task(self._exec_dbx_location_dialog())
except NotLinkedError:
self.add_background_task(self.restart)
asyncio.create_task(self.restart())

def _get_or_create_window(self, clazz: Type[MaestralWindow]) -> MaestralWindow:
for window in self.windows:
Expand All @@ -360,16 +356,16 @@ def _get_or_create_window(self, clazz: Type[MaestralWindow]) -> MaestralWindow:

return clazz(mdbx=self.mdbx)

def on_settings_clicked(self, interface, *args, **kwargs) -> None:
def on_settings_clicked(self, **kwargs) -> None:
self._get_or_create_window(SettingsWindow).show()

def on_sync_issues_clicked(self, interface, *args, **kwargs) -> None:
def on_sync_issues_clicked(self, **kwargs) -> None:
self._get_or_create_window(SyncIssuesWindow).show()

def on_activity_clicked(self, interface, *args, **kwargs) -> None:
def on_activity_clicked(self, **kwargs) -> None:
self._get_or_create_window(ActivityWindow).show()

def on_rebuild_clicked(self, interface, *args, **kwargs) -> None:
def on_rebuild_clicked(self, **kwargs) -> None:
choice = self.alert(
title="Rebuilt Maestral's sync index?",
message=(
Expand All @@ -386,7 +382,7 @@ def on_rebuild_clicked(self, interface, *args, **kwargs) -> None:

# ==== periodic refresh of gui =====================================================

async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
async def periodic_refresh_gui(self, **kwargs) -> None:
while True:
try:
await self.update_status(self)
Expand All @@ -396,7 +392,7 @@ async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
except CommunicationError:
super().exit()

async def update_status(self, interface, *args, **kwargs) -> None:
async def update_status(self, **kwargs) -> None:
"""Change icon according to status."""
n_sync_errors = len(self.mdbx.sync_errors)
has_sync_issues = n_sync_errors > 0
Expand Down Expand Up @@ -425,7 +421,7 @@ async def update_status(self, interface, *args, **kwargs) -> None:

self.item_status.text = status

async def update_snoozed(self, interface, *args, **kwargs) -> None:
async def update_snoozed(self, **kwargs) -> None:
minutes = self.mdbx.notification_snooze

if minutes > 0:
Expand All @@ -441,7 +437,7 @@ async def update_snoozed(self, interface, *args, **kwargs) -> None:
self.menu_snooze.remove(self.item_resume_notifications)
self.menu_snooze.remove(self.item_snooze_separator)

async def update_error(self, interface, *args, **kwargs) -> None:
async def update_error(self) -> None:
errs = self.mdbx.fatal_errors

if not errs:
Expand All @@ -458,7 +454,7 @@ async def update_error(self, interface, *args, **kwargs) -> None:
err = errs[-1]

if isinstance(err, NoDropboxDirError):
await self._exec_dbx_location_dialog(self)
await self._exec_dbx_location_dialog()
elif isinstance(err, TokenRevokedError):
await self._exec_relink_dialog(RelinkDialog.REVOKED)
elif isinstance(err, TokenExpiredError):
Expand All @@ -474,9 +470,9 @@ async def update_error(self, interface, *args, **kwargs) -> None:
# We don't know this error yet. Show a full stacktrace dialog.
await self._exec_error_dialog(err)

async def _exec_dbx_location_dialog(self, interface, *args, **kwargs) -> None:
async def _exec_dbx_location_dialog(self) -> None:

def start_sync_callback(a, *aa, **kwa):
def start_sync_callback(**kwargs):
self.mdbx.start_sync()

location_dialog = DbxLocationDialog(mdbx=self.mdbx)
Expand Down Expand Up @@ -511,23 +507,23 @@ async def _exec_error_dialog(self, err: Exception) -> None:

# ==== quit functions ==============================================================

async def exit_and_stop_daemon(self, interface, *args, **kwargs) -> None:
async def exit_and_stop_daemon(self, **kwargs) -> None:
"""Stops the sync daemon and quits Maestral."""
await call_async(stop_maestral_daemon_process, self.config_name)
super().exit()

def exit(self, interface, *args, **kwargs) -> None:
def exit(self, **kwargs) -> None:
"""Quits Maestral. Stops the sync daemon only if we started it ourselves."""
# Note: Keep this method synchrounous for compatibility with the parent class.
# Note: Keep this method synchronous for compatibility with the parent class.

async def async_exit(interface, *args, **kwargs) -> None:
async def async_exit() -> None:
if self._daemon_started:
stop_maestral_daemon_process(self.config_name)
super(MaestralGui, self).exit()

self.add_background_task(async_exit)
asyncio.create_task(async_exit())

async def restart(self, interface, *args, **kwargs) -> None:
async def restart(self) -> None:
"""Restarts the Maestral GUI and sync daemon."""
# Schedule restart after current process has quit
pid = os.getpid() # get ID of current process
Expand Down
8 changes: 4 additions & 4 deletions src/maestral_cocoa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, mdbx: MaestralProxy) -> None:

self.combobox_dbx_location.dialog_message = path_selection_message
self.refresh_gui()
self.app.add_background_task(self.refresh_profile_pic)
asyncio.create_task(self.refresh_profile_pic())

# ==== callback implementations ====================================================

Expand Down Expand Up @@ -241,14 +241,14 @@ def refresh_gui(self) -> None:
if FROZEN:
self._update_cli_tool_button()

async def refresh_profile_pic(self, interface, *args, **kwargs) -> None:
async def refresh_profile_pic(self) -> None:
try:
path = await call_async_maestral(self.mdbx.config_name, "get_profile_pic")
self.set_profile_pic(path)
except (ConnectionError, MaestralApiError, NotLinkedError):
pass

async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
async def periodic_refresh_gui(self) -> None:
while self._refresh:
self.refresh_gui()
await asyncio.sleep(self._refresh_interval)
Expand Down Expand Up @@ -280,5 +280,5 @@ def on_close_pressed(self, sender: Any = None) -> bool:

def show(self) -> None:
self._refresh = True
self.app.add_background_task(self.periodic_refresh_gui)
asyncio.create_task(self.periodic_refresh_gui())
super().show()
4 changes: 2 additions & 2 deletions src/maestral_cocoa/syncissues.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(self, mdbx: MaestralProxy) -> None:

self.refresh_gui()

async def periodic_refresh_gui(self, interface, *args, **kwargs) -> None:
async def periodic_refresh_gui(self) -> None:
while self._refresh:
self.refresh_gui()
await asyncio.sleep(self._refresh_interval)
Expand Down Expand Up @@ -176,5 +176,5 @@ def on_close_pressed(self, sender: Any = None) -> bool:

def show(self) -> None:
self._refresh = True
self.app.add_background_task(self.periodic_refresh_gui)
asyncio.create_task(self.periodic_refresh_gui())
super().show()
4 changes: 2 additions & 2 deletions src/maestral_cocoa/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(self, mdbx: MaestralProxy, app: SystemTrayApp) -> None:
self.config_name = self.mdbx.config_name

def _start_updater(self) -> None:
self.app.add_background_task(self._periodic_check_for_updates)
asyncio.create_task(self._periodic_check_for_updates())

def set_update_check_interval(self, value: int) -> None:
pass
Expand Down Expand Up @@ -194,7 +194,7 @@ async def check_for_updates_in_background(self, interface, *args, **kwargs) -> N
self.mdbx.set_state("app", "update_notification_last", time.time())
self._show_update_dialog(res.latest_release, res.release_notes)

async def _periodic_check_for_updates(self, interface, *args, **kwargs) -> None:
async def _periodic_check_for_updates(self) -> None:
while True:
await asyncio.sleep(30 * 60)
await self.check_for_updates_in_background(self)
Expand Down

0 comments on commit 8ab53fd

Please sign in to comment.