From ad20ec62f2373e41612f74c93dcd1d5f59cb7a95 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sat, 28 Jul 2018 17:19:33 +0100 Subject: [PATCH] [Py2to3] Fix TorrentInfo metainfo dict key lookups --- deluge/common.py | 2 +- deluge/ui/common.py | 67 +++++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 04588cf24..f2cef2f45 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -930,7 +930,7 @@ def decode_bytes(byte_str, encoding='utf8'): lambda: (encoding, 'ignore'), ] - if encoding is not 'utf8': + if encoding.lower() not in ['utf8', 'utf-8']: encodings.insert(0, lambda: (encoding, 'strict')) for l in encodings: diff --git a/deluge/ui/common.py b/deluge/ui/common.py index 79c847cce..02eed51f5 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -195,7 +195,8 @@ class TorrentInfo(object): def __init__(self, filename='', filetree=1, metainfo=None, metadata=None): # Get the torrent metainfo from the torrent file if metadata: - self._metainfo_dict = {'info': bencode.bdecode(metadata)} + self._metainfo_dict = {b'info': bencode.bdecode(metadata)} + self._metainfo = bencode.bencode(self._metainfo_dict) else: self._metainfo = metainfo @@ -214,50 +215,52 @@ class TorrentInfo(object): log.warning('Failed to decode %s: %s', filename, ex) return - info_dict = self._metainfo_dict['info'] + info_dict = self._metainfo_dict[b'info'] self._info_hash = sha(bencode.bencode(info_dict)).hexdigest() # Get encoding from torrent file if available - encoding = self._metainfo_dict.get('encoding', None) - codepage = self._metainfo_dict.get('codepage', None) + encoding = self._metainfo_dict.get(b'encoding', None) + codepage = self._metainfo_dict.get(b'codepage', None) if not encoding: encoding = codepage if codepage else 'UTF-8' # Decode 'name' with encoding unless 'name.utf-8' found. - if 'name.utf-8' in info_dict: - self._name = decode_bytes(info_dict['name.utf-8']) + if b'name.utf-8' in info_dict: + self._name = decode_bytes(info_dict[b'name.utf-8']) else: - self._name = decode_bytes(info_dict['name'], encoding) + if encoding: + encoding = encoding.decode() + self._name = decode_bytes(info_dict[b'name'], encoding) # Get list of files from torrent info - if 'files' in info_dict: + if b'files' in info_dict: paths = {} dirs = {} - prefix = self._name if len(info_dict['files']) > 1 else '' + prefix = self._name if len(info_dict[b'files']) > 1 else '' - for index, f in enumerate(info_dict['files']): - if 'path.utf-8' in f: - path = decode_bytes(os.path.join(*f['path.utf-8'])) - del f['path.utf-8'] + for index, f in enumerate(info_dict[b'files']): + if b'path.utf-8' in f: + path = decode_bytes(os.path.join(*f[b'path.utf-8'])) + del f[b'path.utf-8'] else: - path = decode_bytes(os.path.join(*f['path']), encoding) + path = decode_bytes(os.path.join(*f[b'path']), encoding) if prefix: path = os.path.join(prefix, path) - f['path'] = path - f['index'] = index - if 'sha1' in f and len(f['sha1']) == 20: - f['sha1'] = f['sha1'].encode('hex') - if 'ed2k' in f and len(f['ed2k']) == 16: - f['ed2k'] = f['ed2k'].encode('hex') - if 'filehash' in f and len(f['filehash']) == 20: - f['filehash'] = f['filehash'].encode('hex') + f[b'path'] = path + f[b'index'] = index + if b'sha1' in f and len(f[b'sha1']) == 20: + f[b'sha1'] = f[b'sha1'].encode(b'hex') + if b'ed2k' in f and len(f[b'ed2k']) == 16: + f[b'ed2k'] = f['ed2k'].encode(b'hex') + if b'filehash' in f and len(f[b'filehash']) == 20: + f[b'filehash'] = f[b'filehash'].encode(b'hex') paths[path] = f dirname = os.path.dirname(path) while dirname: dirinfo = dirs.setdefault(dirname, {}) - dirinfo['length'] = dirinfo.get('length', 0) + f['length'] + dirinfo[b'length'] = dirinfo.get(b'length', 0) + f[b'length'] dirname = os.path.dirname(dirname) if filetree == 2: @@ -274,7 +277,7 @@ class TorrentInfo(object): def walk(path, item): if isinstance(item, dict): return item - return [paths[path]['index'], paths[path]['length'], True] + return [paths[path][b'index'], paths[path][b'length'], True] file_tree = FileTree(paths) file_tree.walk(walk) @@ -286,32 +289,32 @@ class TorrentInfo(object): self._name: { 'type': 'file', 'index': 0, - 'length': info_dict['length'], + 'length': info_dict[b'length'], 'download': True, }, }, } else: self._files_tree = { - self._name: (0, info_dict['length'], True), + self._name: (0, info_dict[b'length'], True), } self._files = [] - if 'files' in info_dict: + if b'files' in info_dict: prefix = '' - if len(info_dict['files']) > 1: + if len(info_dict[b'files']) > 1: prefix = self._name - for f in info_dict['files']: + for f in info_dict[b'files']: self._files.append({ - 'path': f['path'], - 'size': f['length'], + 'path': f[b'path'], + 'size': f[b'length'], 'download': True, }) else: self._files.append({ 'path': self._name, - 'size': info_dict['length'], + 'size': info_dict[b'length'], 'download': True, })