deluge/deluge/ui/console/modes/add_util.py
Calum Lind ad3cba929e [Lint] Cleanup code to pass PyLint Convention category
Disabled Conventions messages:
   * missing-docstring: Not likely all methods/funcs will ever have docstrings.
   * invalid-name: Far too many too fix so will simply have to ensure submitted
     or altered code keeps to the convention.
   * old-style-class: Not a priority but would be nice to eventually fix this.
   * bad-continuation: Occasionally conflicts with pep8, not worth enabling if using
     pyflakes and pep8 as these will catch most continuation issues.
2015-10-30 18:39:42 +00:00

82 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com>
# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com>
# Copyright (C) 2011 Nick Lanham <nick@afternight.org>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
import base64
import glob
import logging
import os
import deluge.common
from deluge.ui.client import client
from deluge.ui.common import TorrentInfo
log = logging.getLogger(__name__)
def _bracket_fixup(path):
if path.find("[") == -1 and path.find("]") == -1:
return path
sentinal = 256
while path.find(unichr(sentinal)) != -1:
sentinal += 1
if sentinal > 65535:
log.error("Can't fix brackets in path, path contains all possible sentinal characters")
return path
newpath = path.replace("]", unichr(sentinal))
newpath = newpath.replace("[", "[[]")
newpath = newpath.replace(unichr(sentinal), "[]]")
return newpath
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"]
is_url = (not options["path_type"] == 1) and (deluge.common.is_url(t_file) or options["path_type"] == 2)
is_magnet = not(is_url) and (not options["path_type"] == 1) and deluge.common.is_magnet(t_file)
if is_url or is_magnet:
files = [t_file]
else:
files = glob.glob(_bracket_fixup(t_file))
num_files = len(files)
ress["total"] = num_files
if num_files <= 0:
fail_cb("Doesn't exist", t_file, ress)
for f in files:
if is_url:
client.core.add_torrent_url(f, t_options).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress)
elif is_magnet:
client.core.add_torrent_magnet(f, t_options).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress)
else:
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:
TorrentInfo(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)