get_torrent_status() and get_torrent_info() now use pickled
dictionaries thus removing the need for the templates.
This commit is contained in:
parent
2eb455179b
commit
ac1bffb65f
|
@ -33,6 +33,7 @@
|
|||
|
||||
import logging
|
||||
import os.path
|
||||
import pickle
|
||||
|
||||
try:
|
||||
import dbus, dbus.service
|
||||
|
@ -128,31 +129,21 @@ class Core(dbus.service.Object):
|
|||
self.torrent_paused(torrent_id)
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
in_signature="s", out_signature="(sxi)")
|
||||
in_signature="s", out_signature="ay")
|
||||
def get_torrent_info(self, torrent_id):
|
||||
# Get the info tuple from the torrent and return it
|
||||
return self.torrents[torrent_id].get_info()
|
||||
# Pickle the info dictionary from the torrent and return it
|
||||
info = self.torrents[torrent_id].get_info()
|
||||
info = pickle.dumps(info)
|
||||
return info
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
in_signature="s",
|
||||
out_signature="(ibdixxddiixii)")
|
||||
out_signature="ay")
|
||||
def get_torrent_status(self, torrent_id):
|
||||
# Get the status tuple from the torrent and return it
|
||||
return self.torrents[torrent_id].get_status()
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
in_signature="",
|
||||
out_signature="as")
|
||||
def get_torrent_status_template(self):
|
||||
# A list of strings the correspond to the status tuple
|
||||
return self.torrents.get_status_template()
|
||||
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
in_signature="",
|
||||
out_signature="as")
|
||||
def get_torrent_info_template(self):
|
||||
# A list of strings the correspond to the info tuple
|
||||
return self.torrents.get_info_template()
|
||||
# Pickle the status dictionary from the torrent and return it
|
||||
status = self.torrents[torrent_id].get_status()
|
||||
status = pickle.dumps(status)
|
||||
return status
|
||||
|
||||
## Queueing functions ######
|
||||
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
|
||||
|
|
|
@ -75,29 +75,28 @@ class Torrent:
|
|||
"""Returns the torrents info.. stuff that remains constant, such as
|
||||
name."""
|
||||
|
||||
return (
|
||||
self.handle.torrent_info().name(),
|
||||
self.handle.torrent_info().total_size(),
|
||||
self.handle.status().num_pieces
|
||||
)
|
||||
return {
|
||||
"name": self.handle.torrent_info().name(),
|
||||
"total_size": self.handle.torrent_info().total_size(),
|
||||
"num_pieces": self.handle.status().num_pieces
|
||||
}
|
||||
|
||||
def get_status(self):
|
||||
"""Returns the torrent status"""
|
||||
status = self.handle.status()
|
||||
|
||||
return (
|
||||
status.state,
|
||||
status.paused,
|
||||
status.progress,
|
||||
status.next_announce.seconds,
|
||||
status.total_payload_download,
|
||||
status.total_payload_upload,
|
||||
status.download_payload_rate,
|
||||
status.upload_payload_rate,
|
||||
status.num_peers,
|
||||
status.num_seeds,
|
||||
status.total_wanted,
|
||||
self.get_eta(),
|
||||
self.queue[self.torrent_id]
|
||||
)
|
||||
|
||||
return {
|
||||
"state": int(status.state),
|
||||
"paused": status.paused,
|
||||
"progress": status.progress,
|
||||
"next_announce": status.next_announce.seconds,
|
||||
"total_payload_download": status.total_payload_download,
|
||||
"total_payload_upload": status.total_payload_upload,
|
||||
"download_payload_rate": status.download_payload_rate,
|
||||
"upload_payload_rate": status.upload_payload_rate,
|
||||
"num_peers": status.num_peers,
|
||||
"num_seeds": status.num_seeds,
|
||||
"total_wanted": status.total_wanted,
|
||||
"eta": self.get_eta(),
|
||||
"queue": self.queue[self.torrent_id]
|
||||
}
|
||||
|
|
|
@ -59,15 +59,24 @@ class TorrentManager:
|
|||
"""Return the Torrent with torrent_id"""
|
||||
return self.torrents[torrent_id]
|
||||
|
||||
def add(self, filename, filedump):
|
||||
def add(self, filename, filedump=None):
|
||||
"""Add a torrent to the manager and returns it's torrent_id"""
|
||||
# Get the core config
|
||||
config = Config("core.conf")
|
||||
|
||||
# Convert the filedump data array into a string of bytes
|
||||
if filedump is not None:
|
||||
filedump = "".join(chr(b) for b in filedump)
|
||||
else:
|
||||
# Get the data from the file
|
||||
try:
|
||||
filedump = open(os.path.join(config["torrentfiles_location"],
|
||||
filename, "rb")).read()
|
||||
except IOError:
|
||||
log.warning("Unable to open %s", filename)
|
||||
return None
|
||||
|
||||
# Bdecode the filedata sent from the UI
|
||||
# Bdecode the filedata
|
||||
torrent_filedump = lt.bdecode(filedump)
|
||||
handle = None
|
||||
|
||||
|
@ -144,29 +153,3 @@ class TorrentManager:
|
|||
except IOError:
|
||||
log.warning("Unable to save state file.")
|
||||
|
||||
|
||||
def get_info_template(self):
|
||||
"""Returns a list of strings that correspond to the info tuple"""
|
||||
return [
|
||||
"name",
|
||||
"total_size",
|
||||
"num_pieces"
|
||||
]
|
||||
|
||||
def get_status_template(self):
|
||||
"""Returns a list of strings that correspond to the status tuple"""
|
||||
return [
|
||||
"state",
|
||||
"paused",
|
||||
"progress",
|
||||
"next_announce",
|
||||
"total_payload_download",
|
||||
"total_payload_upload",
|
||||
"download_payload_rate",
|
||||
"upload_payload_rate",
|
||||
"num_peers",
|
||||
"num_seeds",
|
||||
"total_wanted",
|
||||
"eta",
|
||||
"position"
|
||||
]
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
|
||||
import logging
|
||||
import os.path
|
||||
import pickle
|
||||
|
||||
try:
|
||||
import dbus, dbus.service
|
||||
|
@ -127,20 +128,20 @@ def queue_bottom(torrent_ids):
|
|||
for torrent_id in torrent_ids:
|
||||
core.queue_bottom(torrent_id)
|
||||
|
||||
def get_torrent_status_dict(core, torrent_id):
|
||||
"""Builds and returns a status dictionary using the status template"""
|
||||
status = core.get_torrent_status(torrent_id)
|
||||
template = core.get_torrent_status_template()
|
||||
status_dict = {}
|
||||
for string in template:
|
||||
status_dict[string] = status[template.index(string)]
|
||||
return status_dict
|
||||
|
||||
def get_torrent_info_dict(core, torrent_id):
|
||||
"""Builds and returns an info dictionary using the info template"""
|
||||
def get_torrent_info(core, torrent_id):
|
||||
"""Builds the info dictionary and returns it"""
|
||||
info = core.get_torrent_info(torrent_id)
|
||||
template = core.get_torrent_info_template()
|
||||
info_dict = {}
|
||||
for string in template:
|
||||
info_dict[string] = info[template.index(string)]
|
||||
return info_dict
|
||||
# Join the array of bytes into a string for pickle to read
|
||||
info = "".join(chr(b) for b in info)
|
||||
# De-serialize the object
|
||||
info = pickle.loads(info)
|
||||
return info
|
||||
|
||||
def get_torrent_status(core, torrent_id):
|
||||
"""Builds the status dictionary and returns it"""
|
||||
status = core.get_torrent_status(torrent_id)
|
||||
# Join the array of bytes into a string for pickle to read
|
||||
status = "".join(chr(b) for b in status)
|
||||
# De-serialize the object
|
||||
status = pickle.loads(status)
|
||||
return status
|
||||
|
|
|
@ -157,10 +157,10 @@ class TorrentView:
|
|||
# This function is used for the foreach method of the treemodel
|
||||
def update_row(model, path, row, user_data):
|
||||
torrent_id = self.torrent_model.get_value(row, 0)
|
||||
status = functions.get_torrent_status_dict(self.core, torrent_id)
|
||||
status = functions.get_torrent_status(self.core, torrent_id)
|
||||
# Set values for each column in the row
|
||||
self.torrent_model.set_value(row, TORRENT_VIEW_COL_QUEUE,
|
||||
status["position"]+1)
|
||||
status["queue"]+1)
|
||||
self.torrent_model.set_value(row, TORRENT_VIEW_COL_PROGRESS,
|
||||
status["progress"]*100)
|
||||
self.torrent_model.set_value(row, TORRENT_VIEW_COL_STATUS,
|
||||
|
@ -186,13 +186,13 @@ class TorrentView:
|
|||
def add_row(self, torrent_id):
|
||||
"""Adds a new torrent row to the treeview"""
|
||||
# Get the status and info dictionaries
|
||||
status = functions.get_torrent_status_dict(self.core, torrent_id)
|
||||
info = functions.get_torrent_info_dict(self.core, torrent_id)
|
||||
status = functions.get_torrent_status(self.core, torrent_id)
|
||||
info = functions.get_torrent_info(self.core, torrent_id)
|
||||
|
||||
# Insert the row with info provided from core
|
||||
self.torrent_model.insert(status["position"], [
|
||||
self.torrent_model.insert(status["queue"], [
|
||||
torrent_id,
|
||||
status["position"]+1,
|
||||
status["queue"]+1,
|
||||
None,
|
||||
info["name"],
|
||||
info["total_size"],
|
||||
|
|
Loading…
Reference in New Issue