Change Component to use individual timers for the components. This

allows for different update intervals for the components.
This commit is contained in:
Andrew Resch 2008-01-21 01:57:25 +00:00
parent 799037a46b
commit f9d25287ea
2 changed files with 27 additions and 18 deletions

View File

@ -40,9 +40,11 @@ COMPONENT_STATE = [
] ]
class Component: class Component:
def __init__(self, name, depend=None): def __init__(self, name, interval=1000, depend=None):
# Register with the ComponentRegistry # Register with the ComponentRegistry
register(name, self, depend) register(name, self, depend)
self._interval = interval
self._timer = None
self._state = COMPONENT_STATE.index("Stopped") self._state = COMPONENT_STATE.index("Stopped")
def get_state(self): def get_state(self):
@ -53,25 +55,36 @@ class Component:
def _start(self): def _start(self):
self._state = COMPONENT_STATE.index("Started") self._state = COMPONENT_STATE.index("Started")
if self._update():
self._timer = gobject.timeout_add(self._interval, self._update)
def stop(self): def stop(self):
pass pass
def _stop(self): def _stop(self):
try:
gobject.source_remove(self._timer)
except:
pass
self._state = COMPONENT_STATE.index("Stopped") self._state = COMPONENT_STATE.index("Stopped")
def shutdown(self): def shutdown(self):
pass pass
def update(self): def _update(self):
pass try:
self.update()
except AttributeError:
# This will stop the timer since the component doesn't have an
# update method.
return False
return True
class ComponentRegistry: class ComponentRegistry:
def __init__(self): def __init__(self):
self.components = {} self.components = {}
self.depend = {} self.depend = {}
self.update_timer = None
def register(self, name, obj, depend): def register(self, name, obj, depend):
"""Registers a component.. depend must be list or None""" """Registers a component.. depend must be list or None"""
@ -84,16 +97,11 @@ class ComponentRegistry:
"""Returns a reference to the component 'name'""" """Returns a reference to the component 'name'"""
return self.components[name] return self.components[name]
def start(self, update_interval=1000): def start(self):
"""Starts all components""" """Starts all components"""
for component in self.components.keys(): for component in self.components.keys():
self.start_component(component) self.start_component(component)
# Start the update timer
self.update_timer = gobject.timeout_add(update_interval, self.update)
# Do an update right away
self.update()
def start_component(self, name): def start_component(self, name):
"""Starts a component""" """Starts a component"""
# Check to see if this component has any dependencies # Check to see if this component has any dependencies
@ -107,15 +115,14 @@ class ComponentRegistry:
self.components[name].start() self.components[name].start()
self.components[name]._start() self.components[name]._start()
def stop(self): def stop(self):
"""Stops all components""" """Stops all components"""
for component in self.components.keys(): for component in self.components.keys():
if self.components[component].get_state != \
COMPONENT_STATE.index("Stopped"):
log.debug("Stopping component %s..", component) log.debug("Stopping component %s..", component)
self.components[component].stop() self.components[component].stop()
self.components[component]._stop() self.components[component]._stop()
# Stop the update timer
gobject.source_remove(self.update_timer)
def update(self): def update(self):
"""Updates all components""" """Updates all components"""
@ -130,6 +137,8 @@ class ComponentRegistry:
def shutdown(self): def shutdown(self):
"""Shuts down all components. This should be called when the program """Shuts down all components. This should be called when the program
exits so that components can do any necessary clean-up.""" exits so that components can do any necessary clean-up."""
# Stop all components first
self.stop()
for component in self.components.keys(): for component in self.components.keys():
log.debug("Shutting down component %s..", component) log.debug("Shutting down component %s..", component)
try: try:

View File

@ -45,7 +45,7 @@ from deluge.log import LOG as log
class QueuedTorrents(component.Component): class QueuedTorrents(component.Component):
def __init__(self): def __init__(self):
component.Component.__init__(self, "QueuedTorrents", ["StatusBar"]) component.Component.__init__(self, "QueuedTorrents", depend=["StatusBar"])
self.queue = [] self.queue = []
self.status_item = None self.status_item = None