Skip to content

Commit

Permalink
refactor: make unschedule_toast raise new ToastNotFoundError exceptio…
Browse files Browse the repository at this point in the history
…n instead of UserWarning
  • Loading branch information
DatGuy1 committed Sep 11, 2023
1 parent 2d9eacd commit 49a33b3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/windows_toasts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ._version import __author__, __description__, __license__, __title__, __url__, __version__ # noqa: F401
from .events import ToastActivatedEventArgs, ToastDismissalReason, ToastDismissedEventArgs, ToastFailedEventArgs
from .exceptions import InvalidImageException
from .exceptions import InvalidImageException, ToastNotFoundError
from .toast import Toast
from .toast_audio import AudioSource, ToastAudio
from .toasters import InteractableWindowsToaster, WindowsToaster
Expand Down Expand Up @@ -35,6 +35,7 @@
"ToastFailedEventArgs",
# exceptions.py
"InvalidImageException",
"ToastNotFoundError",
# toast_audio.py
"AudioSource",
"ToastAudio",
Expand Down
4 changes: 4 additions & 0 deletions src/windows_toasts/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class InvalidImageException(Exception):
"""The image provided was invalid"""


class ToastNotFoundError(Exception):
"""The toast could not be found"""
8 changes: 4 additions & 4 deletions src/windows_toasts/toasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)

from .events import ToastActivatedEventArgs
from .exceptions import ToastNotFoundError
from .toast import Toast
from .toast_document import ToastDocument
from .wrappers import ToastDuration, ToastImagePosition, ToastScenario
Expand Down Expand Up @@ -174,22 +175,21 @@ def schedule_toast(self, toast: Toast, displayTime: datetime) -> None:

self.toastNotifier.add_to_schedule(scheduledNotificationToSend)

def unschedule_toast(self, toast: Toast) -> bool:
def unschedule_toast(self, toast: Toast) -> None:
"""
Unschedule the passed notification toast
:return: Whether the unscheduling failed or succeeded
:raises: ToastNotFoundError: If the toast could not be found
"""
scheduledToasts = self.toastNotifier.get_scheduled_toast_notifications()
targetNotification = next(
(scheduledToast for scheduledToast in scheduledToasts if scheduledToast.tag == toast.tag), None
)
if targetNotification is None:
warnings.warn(f"Toast unscheduling failed. Toast {toast} not found")
return False
raise ToastNotFoundError(f"Toast unscheduling failed. Toast {toast} not found")

self.toastNotifier.remove_from_schedule(targetNotification)
return True

def clear_toasts(self) -> None:
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_toasts.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_progress_bar():
def test_scheduled_toast(pytestconfig):
from datetime import datetime, timedelta

from src.windows_toasts import ToastProgressBar
from src.windows_toasts import ToastNotFoundError, ToastProgressBar

progressBar = ToastProgressBar(
"Preparing...", "Python 4 release", progress=0.5, progress_override="? millenniums remaining"
Expand All @@ -257,9 +257,9 @@ def test_scheduled_toast(pytestconfig):
toaster.schedule_toast(clonedToast, datetime.now() + timedelta(seconds=10))

if pytestconfig.getoption("real_run"):
assert toaster.unschedule_toast(clonedToast)
toaster.unschedule_toast(clonedToast)
else:
with warns(UserWarning, match="Toast unscheduling failed."):
with raises(ToastNotFoundError, match="Toast unscheduling failed."):
toaster.unschedule_toast(clonedToast)


Expand Down

0 comments on commit 49a33b3

Please sign in to comment.