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
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
if num_files <= 0:
fail_cb("Doesn't exist",t_file,ress)
filename = os.path.split(t_file)[-1]
filedump = base64.encodestring(open(t_file).read())
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
def on_success(result):
success_cb("{!success!}Torrent added!")
def on_fail(result):
fail_cb("{!error!}Torrent was not added! %s" % result)
try:
add_get_info(f)
except Exception as e:
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("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 = ""