mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-11 12:04:10 +00:00
Remove dependency on libtorrent for add torrent dialog
This commit is contained in:
parent
cc128029f9
commit
4bd88df4de
@ -7,3 +7,4 @@ Deluge 1.1.0 - "" (In Development)
|
||||
GtkUI:
|
||||
* Add peer progress to the peers tab
|
||||
* Sorting # column will place downloaders above seeds
|
||||
* Remove dependency on libtorrent for add torrent dialog
|
||||
|
128
deluge/bencode.py
Normal file
128
deluge/bencode.py
Normal file
@ -0,0 +1,128 @@
|
||||
# The contents of this file are subject to the BitTorrent Open Source License
|
||||
# Version 1.1 (the License). You may not copy or use this file, in either
|
||||
# source code or executable form, except in compliance with the License. You
|
||||
# may obtain a copy of the License at http://www.bittorrent.com/license/.
|
||||
#
|
||||
# Software distributed under the License is distributed on an AS IS basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
# for the specific language governing rights and limitations under the
|
||||
# License.
|
||||
|
||||
# Written by Petru Paler
|
||||
|
||||
def decode_int(x, f):
|
||||
f += 1
|
||||
newf = x.index('e', f)
|
||||
n = int(x[f:newf])
|
||||
if x[f] == '-':
|
||||
if x[f + 1] == '0':
|
||||
raise ValueError
|
||||
elif x[f] == '0' and newf != f+1:
|
||||
raise ValueError
|
||||
return (n, newf+1)
|
||||
|
||||
def decode_string(x, f):
|
||||
colon = x.index(':', f)
|
||||
n = int(x[f:colon])
|
||||
if x[f] == '0' and colon != f+1:
|
||||
raise ValueError
|
||||
colon += 1
|
||||
return (x[colon:colon+n], colon+n)
|
||||
|
||||
def decode_list(x, f):
|
||||
r, f = [], f+1
|
||||
while x[f] != 'e':
|
||||
v, f = decode_func[x[f]](x, f)
|
||||
r.append(v)
|
||||
return (r, f + 1)
|
||||
|
||||
def decode_dict(x, f):
|
||||
r, f = {}, f+1
|
||||
while x[f] != 'e':
|
||||
k, f = decode_string(x, f)
|
||||
r[k], f = decode_func[x[f]](x, f)
|
||||
return (r, f + 1)
|
||||
|
||||
decode_func = {}
|
||||
decode_func['l'] = decode_list
|
||||
decode_func['d'] = decode_dict
|
||||
decode_func['i'] = decode_int
|
||||
decode_func['0'] = decode_string
|
||||
decode_func['1'] = decode_string
|
||||
decode_func['2'] = decode_string
|
||||
decode_func['3'] = decode_string
|
||||
decode_func['4'] = decode_string
|
||||
decode_func['5'] = decode_string
|
||||
decode_func['6'] = decode_string
|
||||
decode_func['7'] = decode_string
|
||||
decode_func['8'] = decode_string
|
||||
decode_func['9'] = decode_string
|
||||
|
||||
def bdecode(x):
|
||||
try:
|
||||
r, l = decode_func[x[0]](x, 0)
|
||||
except (IndexError, KeyError, ValueError):
|
||||
raise Exception("not a valid bencoded string")
|
||||
if l != len(x):
|
||||
raise Exception("invalid bencoded value (data after valid prefix)")
|
||||
return r
|
||||
|
||||
from types import StringType, IntType, LongType, DictType, ListType, TupleType
|
||||
|
||||
|
||||
class Bencached(object):
|
||||
|
||||
__slots__ = ['bencoded']
|
||||
|
||||
def __init__(self, s):
|
||||
self.bencoded = s
|
||||
|
||||
def encode_bencached(x,r):
|
||||
r.append(x.bencoded)
|
||||
|
||||
def encode_int(x, r):
|
||||
r.extend(('i', str(x), 'e'))
|
||||
|
||||
def encode_bool(x, r):
|
||||
if x:
|
||||
encode_int(1, r)
|
||||
else:
|
||||
encode_int(0, r)
|
||||
|
||||
def encode_string(x, r):
|
||||
r.extend((str(len(x)), ':', x))
|
||||
|
||||
def encode_list(x, r):
|
||||
r.append('l')
|
||||
for i in x:
|
||||
encode_func[type(i)](i, r)
|
||||
r.append('e')
|
||||
|
||||
def encode_dict(x,r):
|
||||
r.append('d')
|
||||
ilist = x.items()
|
||||
ilist.sort()
|
||||
for k, v in ilist:
|
||||
r.extend((str(len(k)), ':', k))
|
||||
encode_func[type(v)](v, r)
|
||||
r.append('e')
|
||||
|
||||
encode_func = {}
|
||||
encode_func[Bencached] = encode_bencached
|
||||
encode_func[IntType] = encode_int
|
||||
encode_func[LongType] = encode_int
|
||||
encode_func[StringType] = encode_string
|
||||
encode_func[ListType] = encode_list
|
||||
encode_func[TupleType] = encode_list
|
||||
encode_func[DictType] = encode_dict
|
||||
|
||||
try:
|
||||
from types import BooleanType
|
||||
encode_func[BooleanType] = encode_bool
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
def bencode(x):
|
||||
r = []
|
||||
encode_func[type(x)](x, r)
|
||||
return ''.join(r)
|
@ -179,7 +179,7 @@ class AddTorrentDialog(component.Component):
|
||||
break
|
||||
|
||||
def add_from_files(self, filenames):
|
||||
import deluge.libtorrent as lt
|
||||
import deluge.bencode
|
||||
import os.path
|
||||
new_row = None
|
||||
|
||||
@ -187,41 +187,36 @@ class AddTorrentDialog(component.Component):
|
||||
# Get the torrent data from the torrent file
|
||||
try:
|
||||
log.debug("Attempting to open %s for add.", filename)
|
||||
_file = open(filename, "rb")
|
||||
filedump = _file.read()
|
||||
if not filedump:
|
||||
log.warning("Torrent appears to be corrupt!")
|
||||
continue
|
||||
filedump = lt.bdecode(filedump)
|
||||
_file.close()
|
||||
metadata = deluge.bencode.bdecode(open(filename, "rb").read())
|
||||
except Exception, e:
|
||||
log.warning("Unable to open %s: %s", filename, e)
|
||||
continue
|
||||
|
||||
try:
|
||||
info = lt.torrent_info(filedump)
|
||||
except RuntimeError, e:
|
||||
log.warning("Torrent appears to be corrupt!")
|
||||
continue
|
||||
from sha import sha
|
||||
info_hash = sha(deluge.bencode.bencode(metadata["info"])).hexdigest()
|
||||
|
||||
if str(info.info_hash()) in self.infos:
|
||||
if info_hash in self.infos:
|
||||
log.debug("Torrent already in list!")
|
||||
continue
|
||||
|
||||
# Get list of files from torrent info
|
||||
files = []
|
||||
for f in info.files():
|
||||
prefix = ""
|
||||
if len(metadata["info"]["files"]) > 1:
|
||||
prefix = metadata["info"]["name"]
|
||||
|
||||
for f in metadata["info"]["files"]:
|
||||
files.append({
|
||||
'path': f.path,
|
||||
'size': f.size,
|
||||
'path': os.path.join(prefix, *f["path"]),
|
||||
'size': f["length"],
|
||||
'download': True
|
||||
})
|
||||
|
||||
name = "%s (%s)" % (info.name(), os.path.split(filename)[-1])
|
||||
name = "%s (%s)" % (metadata["info"]["name"], os.path.split(filename)[-1])
|
||||
new_row = self.torrent_liststore.append(
|
||||
[str(info.info_hash()), name, filename])
|
||||
self.files[str(info.info_hash())] = files
|
||||
self.infos[str(info.info_hash())] = info
|
||||
[info_hash, name, filename])
|
||||
self.files[info_hash] = files
|
||||
self.infos[info_hash] = metadata
|
||||
|
||||
(model, row) = self.listview_torrents.get_selection().get_selected()
|
||||
if not row and new_row:
|
||||
@ -258,6 +253,7 @@ class AddTorrentDialog(component.Component):
|
||||
self.add_files(None, split_files)
|
||||
|
||||
def prepare_file(self, file, file_name, file_num, files_storage):
|
||||
log.debug("file_name: %s", file_name)
|
||||
first_slash_index = file_name.find("/")
|
||||
if first_slash_index == -1:
|
||||
files_storage[file_name] = (file_num, file)
|
||||
|
Loading…
x
Reference in New Issue
Block a user