refactor(modules/notification): streamline imports and simplify cache utilities

- Enhancements: Refactored import section to remove unused modules, added explicit parentheses in hash generation for clarity, and updated debug logging messages for cache cleanup.
- Bug Fixes: Ensured consistent encoding of hash inputs and improved error handling in image dimension hashing.
- Style: Applied consistent formatting and spacing throughout the notification module.
This commit is contained in:
sankalp committed 2026-02-26 05:54:51 +05:45
1 parent a5b8a3f7bd
commit ffa9188cd1
1 file changed
+36 -27
+36 -27
View File
@@ -1,38 +1,33 @@
import os
import hashlib import hashlib
import os
import time import time
import uuid
from fabric.utils import get_relative_path
from gi.repository import Gdk, GdkPixbuf, GLib, Gtk # type: ignore
from loguru import logger
import config.data as data
from .unified_cache import (
get_unified_cache_key,
save_to_cache,
get_from_cache,
cleanup_cache,
get_fallback_icon,
ensure_cache_dir
)
from fabric.notifications import ( from fabric.notifications import (
Notification, Notification,
NotificationAction, NotificationAction,
NotificationCloseReason, NotificationCloseReason,
) )
from fabric.utils import get_relative_path
from fabric.widgets.box import Box from fabric.widgets.box import Box
from fabric.widgets.button import Button from fabric.widgets.button import Button
from fabric.widgets.centerbox import CenterBox from fabric.widgets.centerbox import CenterBox
from fabric.widgets.eventbox import EventBox from fabric.widgets.eventbox import EventBox
from fabric.widgets.image import Image from fabric.widgets.image import Image
from fabric.widgets.label import Label from fabric.widgets.label import Label
from utils.roam import modus_service from gi.repository import Gdk, GdkPixbuf, GLib # type: ignore
from loguru import logger
import config.data as data
from services.modus import notification_service
from utils.functions import escape_markup_text from utils.functions import escape_markup_text
from utils.roam import modus_service
from widgets.custom_image import CustomImage from widgets.custom_image import CustomImage
from widgets.customrevealer import SlideRevealer from widgets.customrevealer import SlideRevealer
from widgets.wayland import WaylandWindow as Window from widgets.wayland import WaylandWindow as Window
from services.modus import notification_service
from .unified_cache import (
get_unified_cache_key,
)
NOTIFICATION_WIDTH = 360 NOTIFICATION_WIDTH = 360
NOTIFICATION_IMAGE_SIZE = 48 NOTIFICATION_IMAGE_SIZE = 48
@@ -256,7 +251,9 @@ def get_notification_image_cache_key(notification_id, image_pixbuf):
try: try:
width = image_pixbuf.get_width() width = image_pixbuf.get_width()
height = image_pixbuf.get_height() height = image_pixbuf.get_height()
dimension_hash = hashlib.md5(f"{width}x{height}".encode()).hexdigest()[:8] dimension_hash = hashlib.md5(
f"{width}x{height}".encode()
).hexdigest()[:8]
return dimension_hash return dimension_hash
except Exception: except Exception:
pass pass
@@ -291,7 +288,9 @@ def cache_notification_image(notification_id, image_pixbuf, size=(64, 64)):
logger.debug(f"Generated and cached notification image: {cache_key}") logger.debug(f"Generated and cached notification image: {cache_key}")
return cache_path, cache_key return cache_path, cache_key
except Exception as scale_error: except Exception as scale_error:
logger.debug(f"Failed to cache image (temp file likely gone): {scale_error}") logger.debug(
f"Failed to cache image (temp file likely gone): {scale_error}"
)
return None, None return None, None
except Exception as e: except Exception as e:
@@ -328,7 +327,9 @@ def cleanup_notification_image_cache(cache_key=None):
filepath = os.path.join(NOTIFICATION_IMAGE_CACHE_DIR, filename) filepath = os.path.join(NOTIFICATION_IMAGE_CACHE_DIR, filename)
try: try:
os.unlink(filepath) os.unlink(filepath)
logger.debug(f"Cleaned up cached notification image: {filename}") logger.debug(
f"Cleaned up cached notification image: {filename}"
)
except Exception as e: except Exception as e:
logger.warning(f"Failed to cleanup cache file {filename}: {e}") logger.warning(f"Failed to cleanup cache file {filename}: {e}")
except Exception as e: except Exception as e:
@@ -502,7 +503,9 @@ def preload_notification_assets(notification):
# Cache notification image if available # Cache notification image if available
if hasattr(notification, "image_pixbuf") and notification.image_pixbuf: if hasattr(notification, "image_pixbuf") and notification.image_pixbuf:
cache_notification_image(notification.id, notification.image_pixbuf, (35, 35)) cache_notification_image(
notification.id, notification.image_pixbuf, (35, 35)
)
except Exception as e: except Exception as e:
logger.warning(f"Failed to preload notification assets: {e}") logger.warning(f"Failed to preload notification assets: {e}")
@@ -660,7 +663,9 @@ class NotificationWidget(Box):
children=[ children=[
Label( Label(
name="notification-summary", name="notification-summary",
markup=escape_markup_text(notification.summary.replace("\n", " ")), markup=escape_markup_text(
notification.summary.replace("\n", " ")
),
h_align="start", h_align="start",
max_chars_width=40, max_chars_width=40,
ellipsization="end", ellipsization="end",
@@ -675,7 +680,9 @@ class NotificationWidget(Box):
), ),
( (
Label( Label(
markup=escape_markup_text(notification.body.replace("\n", " ")), markup=escape_markup_text(
notification.body.replace("\n", " ")
),
h_align="start", h_align="start",
max_chars_width=45, max_chars_width=45,
ellipsization="end", ellipsization="end",
@@ -755,7 +762,9 @@ class NotificationWidget(Box):
# Try to get cached notification image first # Try to get cached notification image first
if hasattr(notification, "image_pixbuf") and notification.image_pixbuf: if hasattr(notification, "image_pixbuf") and notification.image_pixbuf:
try: try:
cache_key = get_notification_image_cache_key(notification_id, notification.image_pixbuf) cache_key = get_notification_image_cache_key(
notification_id, notification.image_pixbuf
)
cached_image = get_cached_notification_image(cache_key) cached_image = get_cached_notification_image(cache_key)
if cached_image: if cached_image:
return cached_image return cached_image
@@ -825,9 +834,9 @@ class NotificationWidget(Box):
self, "notification_image_cache_key", None self, "notification_image_cache_key", None
), ),
) )
logger.debug(f"Cleaned up caches for manually dismissed notification") logger.debug("Cleaned up caches for manually dismissed notification")
else: else:
logger.debug(f"Preserved caches for timeout/auto-dismissed notification") logger.debug("Preserved caches for timeout/auto-dismissed notification")
super().destroy() super().destroy()
# @staticmethod # @staticmethod
@@ -1174,7 +1183,7 @@ class ModusNoti(Window):
notification = self.notification_queue.pop(0) notification = self.notification_queue.pop(0)
# Check if notification is still valid (might have been removed) # Check if notification is still valid (might have been removed)
if not notification or not hasattr(notification, 'app_icon'): if not notification or not hasattr(notification, "app_icon"):
# Skip invalid notifications and try next one # Skip invalid notifications and try next one
if self.notification_queue: if self.notification_queue:
self._show_next_notification() self._show_next_notification()