Fix up docstrings in AlertManager and do some minor optimizations

This commit is contained in:
Andrew Resch 2009-05-28 19:15:32 +00:00
parent 8c587f7330
commit 4dc2f7d9d0
1 changed files with 32 additions and 18 deletions

View File

@ -1,7 +1,7 @@
# #
# alertmanager.py # alertmanager.py
# #
# Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com> # Copyright (C) 2007-2009 Andrew Resch <andrewresch@gmail.com>
# #
# Deluge is free software. # Deluge is free software.
# #
@ -31,10 +31,15 @@
# this exception statement from your version. If you delete this exception # this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# #
#
"""
"""The AlertManager handles all the libtorrent alerts.""" The AlertManager handles all the libtorrent alerts.
This should typically only be used by the Core. Plugins should utilize the
`:mod:EventManager` for similar functionality.
"""
from twisted.internet import reactor from twisted.internet import reactor
@ -49,10 +54,10 @@ except ImportError:
from deluge.log import LOG as log from deluge.log import LOG as log
class AlertManager(component.Component): class AlertManager(component.Component):
def __init__(self, session): def __init__(self):
log.debug("AlertManager initialized..") log.debug("AlertManager initialized..")
component.Component.__init__(self, "AlertManager", interval=0.05) component.Component.__init__(self, "AlertManager", interval=0.05)
self.session = session self.session = component.get("Core").session
self.session.set_alert_mask( self.session.set_alert_mask(
lt.alert.category_t.error_notification | lt.alert.category_t.error_notification |
@ -69,12 +74,15 @@ class AlertManager(component.Component):
self.handle_alerts() self.handle_alerts()
def register_handler(self, alert_type, handler): def register_handler(self, alert_type, handler):
"""Registers a function that will be called when 'alert_type' is pop'd
in handle_alerts. The handler function should look like:
handler(alert)
Where 'alert' is the actual alert object from libtorrent
""" """
if alert_type not in self.handlers.keys(): Registers a function that will be called when 'alert_type' is pop'd
in handle_alerts. The handler function should look like: handler(alert)
Where 'alert' is the actual alert object from libtorrent.
:param alert_type: str, this is string representation of the alert name
:param handler: func(alert), the function to be called when the alert is raised
"""
if alert_type not in self.handlers:
# There is no entry for this alert type yet, so lets make it with an # There is no entry for this alert type yet, so lets make it with an
# empty list. # empty list.
self.handlers[alert_type] = [] self.handlers[alert_type] = []
@ -84,7 +92,11 @@ class AlertManager(component.Component):
log.debug("Registered handler for alert %s", alert_type) log.debug("Registered handler for alert %s", alert_type)
def deregister_handler(self, handler): def deregister_handler(self, handler):
"""De-registers the 'handler' function from all alert types.""" """
De-registers the `:param:handler` function from all alert types.
:param handler: func, the handler function to deregister
"""
# Iterate through all handlers and remove 'handler' where found # Iterate through all handlers and remove 'handler' where found
for (key, value) in self.handlers: for (key, value) in self.handlers:
if handler in value: if handler in value:
@ -92,16 +104,21 @@ class AlertManager(component.Component):
value.remove(handler) value.remove(handler)
def handle_alerts(self, wait=False): def handle_alerts(self, wait=False):
"""Pops all libtorrent alerts in the session queue and handles them """
appropriately.""" Pops all libtorrent alerts in the session queue and handles them
appropriately.
:param wait: bool, if True then the handler functions will be run right
away and waited to return before processing the next alert
"""
alert = self.session.pop_alert() alert = self.session.pop_alert()
# Loop through all alerts in the queue
while alert is not None: while alert is not None:
# Loop through all alerts in the queue
alert_type = type(alert).__name__ alert_type = type(alert).__name__
# Display the alert message # Display the alert message
log.debug("%s: %s", alert_type, alert.message()) log.debug("%s: %s", alert_type, alert.message())
# Call any handlers for this alert type # Call any handlers for this alert type
if alert_type in self.handlers.keys(): if alert_type in self.handlers:
for handler in self.handlers[alert_type]: for handler in self.handlers[alert_type]:
if not wait: if not wait:
reactor.callLater(0, handler, alert) reactor.callLater(0, handler, alert)
@ -109,6 +126,3 @@ class AlertManager(component.Component):
handler(alert) handler(alert)
alert = self.session.pop_alert() alert = self.session.pop_alert()
# Return True so that the timer continues
return True