support globbing for multi-file add and have better fail reports

This commit is contained in:
Nick 2011-02-21 17:44:12 +01:00
parent 3da5cd9816
commit 1173f1c714
2 changed files with 61 additions and 25 deletions

View File

@ -42,31 +42,43 @@ from deluge.ui.client import client
import deluge.component as component import deluge.component as component
from optparse import make_option from optparse import make_option
import os import os,base64,glob
import base64
def add_torrent(t_file, options, success_cb, fail_cb): try:
import libtorrent
add_get_info = libtorrent.torrent_info
except:
add_get_info = deluge.ui.common.TorrentInfo
def add_torrent(t_file, options, success_cb, fail_cb, ress):
t_options = {} t_options = {}
if options["path"]: if options["path"]:
t_options["download_location"] = os.path.expanduser(options["path"]) t_options["download_location"] = os.path.expanduser(options["path"])
t_options["add_paused"] = options["add_paused"] t_options["add_paused"] = options["add_paused"]
# Keep a list of deferreds to make a DeferredList files = glob.glob(t_file)
if not os.path.exists(t_file): num_files = len(files)
fail_cb("{!error!}%s doesn't exist!" % t_file) ress["total"] = num_files
return
if not os.path.isfile(t_file):
fail_cb("{!error!}%s is a directory!" % t_file)
return
if num_files <= 0:
fail_cb("Doesn't exist",t_file,ress)
filename = os.path.split(t_file)[-1] for f in files:
filedump = base64.encodestring(open(t_file).read()) if not os.path.exists(f):
fail_cb("Doesn't exist",f,ress)
continue
if not os.path.isfile(f):
fail_cb("Is a directory",f,ress)
continue
def on_success(result): try:
success_cb("{!success!}Torrent added!") add_get_info(f)
def on_fail(result): except Exception as e:
fail_cb("{!error!}Torrent was not added! %s" % result) fail_cb(e.message,f,ress)
continue
client.core.add_torrent_file(filename, filedump, t_options).addCallback(on_success).addErrback(on_fail) filename = os.path.split(f)[-1]
filedump = base64.encodestring(open(f).read())
client.core.add_torrent_file(filename, filedump, t_options).addCallback(success_cb,f,ress).addErrback(fail_cb,f,ress)

View File

@ -412,13 +412,37 @@ class AllTorrents(BaseMode):
self.popup.add_line("_Checking",data=FILTER.CHECKING,foreground="blue") self.popup.add_line("_Checking",data=FILTER.CHECKING,foreground="blue")
self.popup.add_line("Q_ueued",data=FILTER.QUEUED,foreground="yellow") self.popup.add_line("Q_ueued",data=FILTER.QUEUED,foreground="yellow")
def __report_add_status(self, succ_cnt, fail_cnt, fail_msgs):
if fail_cnt == 0:
self.report_message("Torrents Added","{!success!}Sucessfully added %d torrent(s)"%succ_cnt)
else:
msg = ("{!error!}Failed to add the following %d torrent(s):\n {!error!}"%fail_cnt)+"\n {!error!}".join(fail_msgs)
if succ_cnt != 0:
msg += "\n \n{!success!}Sucessfully added %d torrent(s)"%succ_cnt
self.report_message("Torrent Add Report",msg)
def _do_add(self, result): def _do_add(self, result):
log.debug("Adding Torrent: %s (dl path: %s) (paused: %d)",result["file"],result["path"],result["add_paused"]) log.debug("Adding Torrent(s): %s (dl path: %s) (paused: %d)",result["file"],result["path"],result["add_paused"])
def suc_cb(msg): ress = {"succ":0,
self.report_message("Torrent Added",msg) "fail":0,
def fail_cb(msg): "fmsg":[]}
self.report_message("Failed To Add Torrent",msg)
add_torrent(result["file"],result,suc_cb,fail_cb) def fail_cb(msg,t_file,ress):
log.debug("failed to add torrent: %s: %s"%(t_file,msg))
ress["fail"]+=1
ress["fmsg"].append("%s: %s"%(t_file,msg))
if (ress["succ"]+ress["fail"]) >= ress["total"]:
self.__report_add_status(ress["succ"],ress["fail"],ress["fmsg"])
def suc_cb(tid,t_file,ress):
if tid:
log.debug("added torrent: %s (%s)"%(t_file,tid))
ress["succ"]+=1
if (ress["succ"]+ress["fail"]) >= ress["total"]:
self.__report_add_status(ress["succ"],ress["fail"],ress["fmsg"])
else:
fail_cb("Already in session (probably)",t_file,ress)
add_torrent(result["file"],result,suc_cb,fail_cb,ress)
def _show_torrent_add_popup(self): def _show_torrent_add_popup(self):
dl = "" dl = ""