From 1173f1c714f63ea03e25461e1ee6da7dc98a126f Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 21 Feb 2011 17:44:12 +0100 Subject: [PATCH] support globbing for multi-file add and have better fail reports --- deluge/ui/console/modes/add_util.py | 50 ++++++++++++++++---------- deluge/ui/console/modes/alltorrents.py | 36 +++++++++++++++---- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/deluge/ui/console/modes/add_util.py b/deluge/ui/console/modes/add_util.py index 28372631d..49720df97 100644 --- a/deluge/ui/console/modes/add_util.py +++ b/deluge/ui/console/modes/add_util.py @@ -42,31 +42,43 @@ from deluge.ui.client import client import deluge.component as component from optparse import make_option -import os -import base64 +import os,base64,glob -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 = {} if options["path"]: t_options["download_location"] = os.path.expanduser(options["path"]) t_options["add_paused"] = options["add_paused"] - # Keep a list of deferreds to make a DeferredList - if not os.path.exists(t_file): - fail_cb("{!error!}%s doesn't exist!" % t_file) - return - if not os.path.isfile(t_file): - fail_cb("{!error!}%s is a directory!" % t_file) - return - + files = glob.glob(t_file) + num_files = len(files) + ress["total"] = num_files - filename = os.path.split(t_file)[-1] - filedump = base64.encodestring(open(t_file).read()) + if num_files <= 0: + fail_cb("Doesn't exist",t_file,ress) - def on_success(result): - success_cb("{!success!}Torrent added!") - def on_fail(result): - fail_cb("{!error!}Torrent was not added! %s" % result) + for f in files: + 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 + + try: + add_get_info(f) + except Exception as e: + fail_cb(e.message,f,ress) + continue + + 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) - client.core.add_torrent_file(filename, filedump, t_options).addCallback(on_success).addErrback(on_fail) - diff --git a/deluge/ui/console/modes/alltorrents.py b/deluge/ui/console/modes/alltorrents.py index ee57a59e5..578cdbf33 100644 --- a/deluge/ui/console/modes/alltorrents.py +++ b/deluge/ui/console/modes/alltorrents.py @@ -412,13 +412,37 @@ class AllTorrents(BaseMode): self.popup.add_line("_Checking",data=FILTER.CHECKING,foreground="blue") 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): - log.debug("Adding Torrent: %s (dl path: %s) (paused: %d)",result["file"],result["path"],result["add_paused"]) - def suc_cb(msg): - self.report_message("Torrent Added",msg) - def fail_cb(msg): - self.report_message("Failed To Add Torrent",msg) - add_torrent(result["file"],result,suc_cb,fail_cb) + log.debug("Adding Torrent(s): %s (dl path: %s) (paused: %d)",result["file"],result["path"],result["add_paused"]) + ress = {"succ":0, + "fail":0, + "fmsg":[]} + + 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): dl = ""