From 0745c0eff85ea4e4a3646c454098503ed08c56ff Mon Sep 17 00:00:00 2001 From: DjLegolas Date: Sun, 5 Mar 2023 15:40:50 +0000 Subject: [PATCH] [Component] Refactor to remove hasattr usage Functions are defined in the class now https://github.com/deluge-torrent/deluge/pull/221#discussion_r228275050 --- deluge/component.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/deluge/component.py b/deluge/component.py index 5646e8bd2..585cd04bb 100644 --- a/deluge/component.py +++ b/deluge/component.py @@ -59,7 +59,7 @@ class Component: Deluge core. **update()** - This method is called every 1 second by default while the - Componented is in a *Started* state. The interval can be + Component is in a *Started* state. The interval can be specified during instantiation. The update() timer can be paused by instructing the :class:`ComponentRegistry` to pause this Component. @@ -83,7 +83,7 @@ class Component: **Stopping** - The Component has had it's stop method called, but it hasn't fully stopped yet. - **Paused** - The Component has had it's update timer stopped, but will + **Paused** - The Component has had its update timer stopped, but will still be considered in a Started state. """ @@ -111,9 +111,8 @@ class Component: _ComponentRegistry.deregister(self) def _component_start_timer(self): - if hasattr(self, 'update'): - self._component_timer = LoopingCall(self.update) - self._component_timer.start(self._component_interval) + self._component_timer = LoopingCall(self.update) + self._component_timer.start(self._component_interval) def _component_start(self): def on_start(result): @@ -129,13 +128,10 @@ class Component: return fail(result) if self._component_state == 'Stopped': - if hasattr(self, 'start'): - self._component_state = 'Starting' - d = deferLater(reactor, 0, self.start) - d.addCallbacks(on_start, on_start_fail) - self._component_starting_deferred = d - else: - d = maybeDeferred(on_start, None) + self._component_state = 'Starting' + d = deferLater(reactor, 0, self.start) + d.addCallbacks(on_start, on_start_fail) + self._component_starting_deferred = d elif self._component_state == 'Starting': return self._component_starting_deferred elif self._component_state == 'Started': @@ -165,14 +161,11 @@ class Component: return result if self._component_state != 'Stopped' and self._component_state != 'Stopping': - if hasattr(self, 'stop'): - self._component_state = 'Stopping' - d = maybeDeferred(self.stop) - d.addCallback(on_stop) - d.addErrback(on_stop_fail) - self._component_stopping_deferred = d - else: - d = maybeDeferred(on_stop, None) + self._component_state = 'Stopping' + d = maybeDeferred(self.stop) + d.addCallback(on_stop) + d.addErrback(on_stop_fail) + self._component_stopping_deferred = d if self._component_state == 'Stopping': return self._component_stopping_deferred @@ -222,9 +215,7 @@ class Component: def _component_shutdown(self): def on_stop(result): - if hasattr(self, 'shutdown'): - return maybeDeferred(self.shutdown) - return succeed(None) + return maybeDeferred(self.shutdown) d = self._component_stop() d.addCallback(on_stop)