Add 'paused' to TorrentState.

Move some alert handlers around.
This commit is contained in:
Andrew Resch 2007-10-01 22:29:05 +00:00
commit 9a66035f0d
4 changed files with 67 additions and 30 deletions

View file

@ -174,3 +174,19 @@ def fetch_url(url):
else: else:
log.debug("URL doesn't appear to be a valid torrent file: %s", url) log.debug("URL doesn't appear to be a valid torrent file: %s", url)
return None return None
def pythonize(var):
"""Translates DBUS types back to basic Python types."""
if isinstance(var, list):
return [pythonize(value) for value in var]
if isinstance(var, tuple):
return tuple([pythonize(value) for value in var])
if isinstance(var, dict):
return dict(
[(pythonize(key), pythonize(value)) for key, value in var.iteritems()]
)
for klass in [unicode, str, bool, int, float, long]:
if isinstance(var,klass):
return klass(var)
return var

View file

@ -127,18 +127,16 @@ class Core(dbus.service.Object):
self.config.register_set_function("max_upload_slots_global", self.config.register_set_function("max_upload_slots_global",
self.on_set_max_upload_slots_global) self.on_set_max_upload_slots_global)
# Start the AlertManager
self.alerts = AlertManager(self.session)
# Start the TorrentManager # Start the TorrentManager
self.torrents = TorrentManager(self.session) self.torrents = TorrentManager(self.session, self.alerts)
# Load plugins # Load plugins
self.plugins = PluginManager() self.plugins = PluginManager()
# Start the AlertManager # Register alert handlers
self.alerts = AlertManager(self.session)
# Register alert functions
self.alerts.register_handler("torrent_finished_alert",
self.on_alert_torrent_finished)
self.alerts.register_handler("torrent_paused_alert", self.alerts.register_handler("torrent_paused_alert",
self.on_alert_torrent_paused) self.on_alert_torrent_paused)
@ -472,19 +470,9 @@ class Core(dbus.service.Object):
self.session.set_max_uploads(value) self.session.set_max_uploads(value)
## Alert handlers ## ## Alert handlers ##
def on_alert_torrent_finished(self, alert):
log.debug("on_alert_torrent_finished")
# Get the torrent_id
torrent_id = str(alert.handle.info_hash())
log.debug("%s is finished..", torrent_id)
# Write the fastresume file
self.torrents.write_fastresume(torrent_id)
def on_alert_torrent_paused(self, alert): def on_alert_torrent_paused(self, alert):
log.debug("on_alert_torrent_paused") log.debug("on_alert_torrent_paused")
# Get the torrent_id # Get the torrent_id
torrent_id = str(alert.handle.info_hash()) torrent_id = str(alert.handle.info_hash())
# Write the fastresume file
self.torrents.write_fastresume(torrent_id)
# Emit torrent_paused signal # Emit torrent_paused signal
self.torrent_paused(torrent_id) self.torrent_paused(torrent_id)

View file

@ -49,10 +49,17 @@ class Torrent:
self.total_uploaded = 0 self.total_uploaded = 0
# Set the allocation mode # Set the allocation mode
self.compact = compact self.compact = compact
# The reply from the tracker
self.tracker_reply = ""
def set_tracker_reply(self, reply):
"""Sets the tracker reply message"""
self.tracker_reply = reply
def get_state(self): def get_state(self):
"""Returns the state of this torrent for saving to the session state""" """Returns the state of this torrent for saving to the session state"""
return (self.torrent_id, self.filename, self.compact) status = self.handle.status()
return (self.torrent_id, self.filename, self.compact, status.paused)
def get_eta(self): def get_eta(self):
"""Returns the ETA in seconds for this torrent""" """Returns the ETA in seconds for this torrent"""

View file

@ -45,10 +45,11 @@ from deluge.core.torrent import Torrent
from deluge.log import LOG as log from deluge.log import LOG as log
class TorrentState: class TorrentState:
def __init__(self, torrent_id, filename, compact): def __init__(self, torrent_id, filename, compact, paused):
self.torrent_id = torrent_id self.torrent_id = torrent_id
self.filename = filename self.filename = filename
self.compact = compact self.compact = compact
self.paused = paused
class TorrentManagerState: class TorrentManagerState:
def __init__(self): def __init__(self):
@ -59,10 +60,12 @@ class TorrentManager:
session. This object is also responsible for saving the state of the session. This object is also responsible for saving the state of the
session for use on restart.""" session for use on restart."""
def __init__(self, session): def __init__(self, session, alerts):
log.debug("TorrentManager init..") log.debug("TorrentManager init..")
# Set the libtorrent session # Set the libtorrent session
self.session = session self.session = session
# Set the alertmanager
self.alerts = alerts
# Get the core config # Get the core config
self.config = ConfigManager("core.conf") self.config = ConfigManager("core.conf")
# Per torrent connection limit and upload slot limit # Per torrent connection limit and upload slot limit
@ -79,6 +82,12 @@ class TorrentManager:
self.config.register_set_function("max_upload_slots_per_torrent", self.config.register_set_function("max_upload_slots_per_torrent",
self.on_set_max_upload_slots_per_torrent) self.on_set_max_upload_slots_per_torrent)
# Register alert functions
self.alerts.register_handler("torrent_finished_alert",
self.on_alert_torrent_finished)
self.alerts.register_handler("torrent_paused_alert",
self.on_alert_torrent_paused)
def shutdown(self): def shutdown(self):
log.debug("TorrentManager shutting down..") log.debug("TorrentManager shutting down..")
# Save state on shutdown # Save state on shutdown
@ -96,7 +105,7 @@ class TorrentManager:
"""Returns a list of torrent_ids""" """Returns a list of torrent_ids"""
return self.torrents.keys() return self.torrents.keys()
def add(self, filename, filedump=None, compact=None): def add(self, filename, filedump=None, compact=None, paused=False):
"""Add a torrent to the manager and returns it's torrent_id""" """Add a torrent to the manager and returns it's torrent_id"""
log.info("Adding torrent: %s", filename) log.info("Adding torrent: %s", filename)
@ -148,7 +157,8 @@ class TorrentManager:
lt.torrent_info(torrent_filedump), lt.torrent_info(torrent_filedump),
self.config["download_location"], self.config["download_location"],
resume_data=fastresume, resume_data=fastresume,
compact_mode=compact) compact_mode=compact,
paused=paused)
except RuntimeError: except RuntimeError:
log.warning("Error adding torrent") log.warning("Error adding torrent")
@ -278,7 +288,8 @@ class TorrentManager:
# Try to add the torrents in the state to the session # Try to add the torrents in the state to the session
for torrent_state in state.torrents: for torrent_state in state.torrents:
self.add(torrent_state.filename, compact=torrent_state.compact) self.add(torrent_state.filename, compact=torrent_state.compact,
paused=torrent_state.paused)
def save_state(self): def save_state(self):
"""Save the state of the TorrentManager to the torrents.state file""" """Save the state of the TorrentManager to the torrents.state file"""
@ -339,3 +350,18 @@ class TorrentManager:
for key in self.torrents.keys(): for key in self.torrents.keys():
self.torrents[key].handle.set_max_uploads(value) self.torrents[key].handle.set_max_uploads(value)
## Alert handlers ##
def on_alert_torrent_finished(self, alert):
log.debug("on_alert_torrent_finished")
# Get the torrent_id
torrent_id = str(alert.handle.info_hash())
log.debug("%s is finished..", torrent_id)
# Write the fastresume file
self.write_fastresume(torrent_id)
def on_alert_torrent_paused(self, alert):
log.debug("on_alert_torrent_paused")
# Get the torrent_id
torrent_id = str(alert.handle.info_hash())
# Write the fastresume file
self.write_fastresume(torrent_id)