Dont make code that still uses the old "TorrentAddedEvent" fail, instead log a warning and make it work.

This commit is contained in:
Pedro Algarvio 2010-12-10 04:59:05 +00:00
parent 1c7676bfe5
commit e52018bfcd
2 changed files with 38 additions and 13 deletions

View File

@ -53,7 +53,19 @@ class EventManager(component.Component):
if event.name in self.handlers: if event.name in self.handlers:
for handler in self.handlers[event.name]: for handler in self.handlers[event.name]:
#log.debug("Running handler %s for event %s with args: %s", event.name, handler, event.args) #log.debug("Running handler %s for event %s with args: %s", event.name, handler, event.args)
handler(*event.args) try:
handler(*event.args)
except TypeError:
if event.name != "TorrentAddedEvent":
raise
else:
log.warning("TorrentAddedEvent recently got an extra "
"argument, \"from_state\" and the handler "
"\"%s\" is not accepting that extra "
"argument. Correcting for now but this code "
"should be changed.", handler)
handler(event.args[0])
def register_event_handler(self, event, handler): def register_event_handler(self, event, handler):
""" """

View File

@ -140,7 +140,7 @@ class DelugeRPCProtocol(Protocol):
while data: while data:
# Increase the byte counter # Increase the byte counter
self.factory.bytes_recv += len(data) self.factory.bytes_recv += len(data)
dobj = zlib.decompressobj() dobj = zlib.decompressobj()
try: try:
request = rencode.loads(dobj.decompress(data)) request = rencode.loads(dobj.decompress(data))
@ -167,9 +167,22 @@ class DelugeRPCProtocol(Protocol):
#log.debug("Received RPCEvent: %s", event) #log.debug("Received RPCEvent: %s", event)
# A RPCEvent was received from the daemon so run any handlers # A RPCEvent was received from the daemon so run any handlers
# associated with it. # associated with it.
def call_handler(event, handler, args):
try:
handler(*args)
except TypeError:
if event != "TorrentAddedEvent":
raise
else:
log.warning("TorrentAddedEvent recently got an extra "
"argument, \"from_state\" and the handler "
"\"%s\" is not accepting that extra "
"argument. Correcting for now but this code "
"should be changed.", handler)
handler(args[0])
if event in self.factory.event_handlers: if event in self.factory.event_handlers:
for handler in self.factory.event_handlers[event]: for handler in self.factory.event_handlers[event]:
reactor.callLater(0, handler, *request[2]) reactor.callLater(0, call_handler, event, handler, request[2])
continue continue
request_id = request[1] request_id = request[1]
@ -213,7 +226,7 @@ class DelugeRPCClientFactory(ClientFactory):
def __init__(self, daemon, event_handlers): def __init__(self, daemon, event_handlers):
self.daemon = daemon self.daemon = daemon
self.event_handlers = event_handlers self.event_handlers = event_handlers
self.bytes_recv = 0 self.bytes_recv = 0
self.bytes_sent = 0 self.bytes_sent = 0
@ -329,7 +342,7 @@ class DaemonSSLProxy(DaemonProxy):
:param request_id: the request_id of the Deferred to pop :param request_id: the request_id of the Deferred to pop
:type request_id: int :type request_id: int
""" """
return self.__deferred.pop(request_id) return self.__deferred.pop(request_id)
@ -343,7 +356,7 @@ class DaemonSSLProxy(DaemonProxy):
:param handler: the function to be called when `:param:event` :param handler: the function to be called when `:param:event`
is emitted from the daemon is emitted from the daemon
:type handler: function :type handler: function
""" """
if event not in self.__factory.event_handlers: if event not in self.__factory.event_handlers:
# This is a new event to handle, so we need to tell the daemon # This is a new event to handle, so we need to tell the daemon
@ -422,10 +435,10 @@ class DaemonSSLProxy(DaemonProxy):
def get_bytes_recv(self): def get_bytes_recv(self):
return self.__factory.bytes_recv return self.__factory.bytes_recv
def get_bytes_sent(self): def get_bytes_sent(self):
return self.__factory.bytes_sent return self.__factory.bytes_sent
class DaemonClassicProxy(DaemonProxy): class DaemonClassicProxy(DaemonProxy):
def __init__(self, event_handlers={}): def __init__(self, event_handlers={}):
import deluge.core.daemon import deluge.core.daemon
@ -466,7 +479,7 @@ class DaemonClassicProxy(DaemonProxy):
:param handler: the function to be called when `:param:event` :param handler: the function to be called when `:param:event`
is emitted from the daemon is emitted from the daemon
:type handler: function :type handler: function
""" """
self.__daemon.core.eventmanager.register_event_handler(event, handler) self.__daemon.core.eventmanager.register_event_handler(event, handler)
@ -571,7 +584,7 @@ class Client(object):
:rtype: bool :rtype: bool
:raises OSError: received from subprocess.call() :raises OSError: received from subprocess.call()
""" """
try: try:
if deluge.common.windows_check(): if deluge.common.windows_check():
@ -679,7 +692,7 @@ class Client(object):
def get_bytes_recv(self): def get_bytes_recv(self):
""" """
Returns the number of bytes received from the daemon. Returns the number of bytes received from the daemon.
:returns: the number of bytes received :returns: the number of bytes received
:rtype: int :rtype: int
""" """
@ -688,11 +701,11 @@ class Client(object):
def get_bytes_sent(self): def get_bytes_sent(self):
""" """
Returns the number of bytes sent to the daemon. Returns the number of bytes sent to the daemon.
:returns: the number of bytes sent :returns: the number of bytes sent
:rtype: int :rtype: int
""" """
return self._daemon_proxy.get_bytes_sent() return self._daemon_proxy.get_bytes_sent()
# This is the object clients will use # This is the object clients will use
client = Client() client = Client()