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