From 5c936a68da74d7f246cb4351ac5fe698143a684b Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Fri, 31 Jul 2026 00:24:23 +0545 Subject: [PATCH] refactor: replace hasattr(self, ...) with direct None-safe access --- src/window/controlcenter/expanded_player.py | 12 ++++--- src/window/controlcenter/main.py | 36 ++++++++++----------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/window/controlcenter/expanded_player.py b/src/window/controlcenter/expanded_player.py index 44afb56..381a225 100644 --- a/src/window/controlcenter/expanded_player.py +++ b/src/window/controlcenter/expanded_player.py @@ -50,7 +50,7 @@ class EmbeddedExpandedPlayer(Box): self._check_and_update_playing_state() def destroy(self): - if hasattr(self, "player_content") and hasattr(self.player_content, "destroy"): + if self.player_content and hasattr(self.player_content, "destroy"): self.player_content.destroy() super().destroy() @@ -284,6 +284,8 @@ class PlayerBox(Box): self.cover_path = self.fallback_cover_path self.exit = False self._user_seeking = False + self._seek_timeout = None + self._debounce_seek = None self._signal_connections = [] self._property_bindings = [] self._seekbar_signal_ids = [] @@ -654,7 +656,7 @@ class PlayerBox(Box): icon_file = ( "player/play.svg" if current_status == "paused" else "player/pause.svg" ) - if hasattr(self, "play_pause_icon"): + if self.play_pause_icon: self.play_pause_icon.dynamic_file(icon_file) else: # Fallback if play_pause_icon isn't available @@ -717,7 +719,7 @@ class PlayerBox(Box): logger.warning( f"[expanded_player] self.player.seek_position(value) failed: {e}" ) - if hasattr(self, "_seek_timeout") and self._seek_timeout: + if self._seek_timeout: GLib.source_remove(self._seek_timeout) self._seek_timeout = GLib.timeout_add(1000, self._clear_seeking) @@ -730,7 +732,7 @@ class PlayerBox(Box): value = max(0.0, value) self.position_label.set_label(self.length_str(int(value * 1_000_000))) - if hasattr(self, "_debounce_seek") and self._debounce_seek: + if self._debounce_seek: GLib.source_remove(self._debounce_seek) self._debounce_seek = GLib.timeout_add(100, self._do_seek, value) @@ -790,7 +792,7 @@ class ExpandedPlayer(Window): self._check_and_update_playing_state() def destroy(self): - if hasattr(self, "child") and hasattr(self.child, "destroy"): + if self.child and hasattr(self.child, "destroy"): self.child.destroy() super().destroy() diff --git a/src/window/controlcenter/main.py b/src/window/controlcenter/main.py index 0e8134b..bf525ec 100644 --- a/src/window/controlcenter/main.py +++ b/src/window/controlcenter/main.py @@ -544,9 +544,9 @@ class ModusControlCenter(AppletWindow): """Handle visibility changes for resource management""" if not self.get_visible(): # Suspend player updates - if hasattr(self, "music_widget") and hasattr(self.music_widget, "suspend"): + if self.music_widget and hasattr(self.music_widget, "suspend"): self.music_widget.suspend() - if hasattr(self, "_expanded_player_widget") and hasattr( + if self._expanded_player_widget and hasattr( self._expanded_player_widget, "suspend" ): self._expanded_player_widget.suspend() @@ -557,9 +557,9 @@ class ModusControlCenter(AppletWindow): self._initialize_resources() # Resume player updates - if hasattr(self, "music_widget") and hasattr(self.music_widget, "resume"): + if self.music_widget and hasattr(self.music_widget, "resume"): self.music_widget.resume() - if hasattr(self, "_expanded_player_widget") and hasattr( + if self._expanded_player_widget and hasattr( self._expanded_player_widget, "resume" ): self._expanded_player_widget.resume() @@ -647,13 +647,13 @@ class ModusControlCenter(AppletWindow): self._signal_connections.clear() # Disconnect direct scale signals (connected in _initialize_resources) - if hasattr(self, "volume_scale"): + if self.volume_scale: try: self.volume_scale.disconnect_by_func(self.set_volume) self.volume_scale.disconnect_by_func(self.on_volume_scroll) except Exception: pass - if hasattr(self, "brightness_scale"): + if self.brightness_scale: try: self.brightness_scale.disconnect_by_func(self.set_brightness) self.brightness_scale.disconnect_by_func(self.on_brightness_scroll) @@ -878,7 +878,7 @@ class ModusControlCenter(AppletWindow): if not self._resources_initialized: return try: - if hasattr(self, "bluetooth_man") and hasattr(self.bluetooth_man, "client"): + if self.bluetooth_man and hasattr(self.bluetooth_man, "client"): client = self.bluetooth_man.client target = not client.enabled set_bluetooth_enabled_with_fallback(client, target) @@ -909,7 +909,7 @@ class ModusControlCenter(AppletWindow): def update_wifi_icon(self, *_): try: - if self.wifi_service and hasattr(self, "wifi_svg"): + if self.wifi_service and self.wifi_svg: is_enabled = self.wifi_service.enabled self.wifi_svg.dynamic_file( "applets/wifi.svg" if is_enabled else "applets/wifi-off.svg" @@ -1113,7 +1113,7 @@ class ModusControlCenter(AppletWindow): self._signal_connections.clear() # Disconnect scale widget signals (connected directly, not tracked) - if hasattr(self, "volume_scale") and self.volume_scale: + if self.volume_scale: try: self.volume_scale.disconnect_by_func(self.set_volume) except Exception as e: @@ -1123,7 +1123,7 @@ class ModusControlCenter(AppletWindow): except Exception as e: logger.error(f"An error occurred: {e}") - if hasattr(self, "brightness_scale") and self.brightness_scale: + if self.brightness_scale: try: self.brightness_scale.disconnect_by_func(self.set_brightness) except Exception as e: @@ -1148,28 +1148,28 @@ class ModusControlCenter(AppletWindow): def _cleanup_managers(self): """Clean up all manager instances""" try: - if hasattr(self, "wifi_man") and self.wifi_man: + if self.wifi_man: try: self.wifi_man.destroy() except Exception as e: logger.warning(f"Failed to destroy WiFi manager: {e}") self.wifi_man = None - if hasattr(self, "bluetooth_man") and self.bluetooth_man: + if self.bluetooth_man: try: self.bluetooth_man.destroy() except Exception as e: logger.warning(f"Failed to destroy Bluetooth manager: {e}") self.bluetooth_man = None - if hasattr(self, "network_service") and self.network_service: + if self.network_service: try: self.network_service.destroy() except Exception as e: logger.warning(f"Failed to destroy network service: {e}") self.network_service = None - if hasattr(self, "wifi_service") and self.wifi_service: + if self.wifi_service: try: self.wifi_service.disconnect_by_func(self.update_wifi_icon) except Exception as e: @@ -1186,7 +1186,7 @@ class ModusControlCenter(AppletWindow): def _cleanup_widgets(self): try: # Clean up main widgets - if hasattr(self, "widgets") and self.widgets: + if self.widgets: try: self.widgets.destroy() except Exception as e: @@ -1194,7 +1194,7 @@ class ModusControlCenter(AppletWindow): self.widgets = None # Clean up center box - if hasattr(self, "center_box") and self.center_box: + if self.center_box: try: self.center_box.destroy() except Exception as e: @@ -1215,7 +1215,7 @@ class ModusControlCenter(AppletWindow): "bluetooth_label", "focus_status_label", "caffeine_status_label", - "wlan_svg", + "wifi_svg", "bluetooth_svg", "focus_icon", "flight_icon", @@ -1223,7 +1223,7 @@ class ModusControlCenter(AppletWindow): ] for attr in widget_attrs: - if hasattr(self, attr) and getattr(self, attr): + if getattr(self, attr, None): try: widget = getattr(self, attr) if hasattr(widget, "destroy"):