summaryrefslogtreecommitdiffstats
path: root/deluge/ui/common.py
blob: 64d5ca216226957415fc6b88f743516c1540f627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#
# Copyright (C) Damien Churchill 2008-2009 <damoxc@gmail.com>
# Copyright (C) Andrew Resch 2009 <andrewresch@gmail.com>
#
# 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.
#

"""
The ui common module contains methods and classes that are deemed useful for all the interfaces.
"""
import logging
import os
from hashlib import sha1 as sha
from typing import Tuple

from deluge import bencode
from deluge.common import decode_bytes

log = logging.getLogger(__name__)


# Dummy translation dicts so the text is available for Translators.
#
# All entries in deluge.common.TORRENT_STATE should be added here.
#
# No need to import these, just simply use the `_()` function around a status variable.
def _(message):
    return message


STATE_TRANSLATION = {
    'All': _('All'),
    'Active': _('Active'),
    'Allocating': _('Allocating'),
    'Checking': _('Checking'),
    'Downloading': _('Downloading'),
    'Seeding': _('Seeding'),
    'Paused': _('Paused'),
    'Queued': _('Queued'),
    'Error': _('Error'),
}

TORRENT_DATA_FIELD = {
    'queue': {'name': '#', 'status': ['queue']},
    'name': {'name': _('Name'), 'status': ['state', 'name']},
    'progress_state': {'name': _('Progress'), 'status': ['progress', 'state']},
    'state': {'name': _('State'), 'status': ['state']},
    'progress': {'name': _('Progress'), 'status': ['progress']},
    'size': {'name': _('Size'), 'status': ['total_wanted']},
    'downloaded': {'name': _('Downloaded'), 'status': ['all_time_download']},
    'uploaded': {'name': _('Uploaded'), 'status': ['total_uploaded']},
    'remaining': {'name': _('Remaining'), 'status': ['total_remaining']},
    'ratio': {'name': _('Ratio'), 'status': ['ratio']},
    'download_speed': {'name': _('Down Speed'), 'status': ['download_payload_rate']},
    'upload_speed': {'name': _('Up Speed'), 'status': ['upload_payload_rate']},
    'max_download_speed': {'name': _('Down Limit'), 'status': ['max_download_speed']},
    'max_upload_speed': {'name': _('Up Limit'), 'status': ['max_upload_speed']},
    'max_connections': {'name': _('Max Connections'), 'status': ['max_connections']},
    'max_upload_slots': {'name': _('Max Upload Slots'), 'status': ['max_upload_slots']},
    'peers': {'name': _('Peers'), 'status': ['num_peers', 'total_peers']},
    'seeds': {'name': _('Seeds'), 'status': ['num_seeds', 'total_seeds']},
    'avail': {'name': _('Avail'), 'status': ['distributed_copies']},
    'seeds_peers_ratio': {'name': _('Seeds:Peers'), 'status': ['seeds_peers_ratio']},
    'time_added': {'name': _('Added'), 'status': ['time_added']},
    'tracker': {'name': _('Tracker'), 'status': ['tracker_host']},
    'download_location': {
        'name': _('Download Folder'),
        'status': ['download_location'],
    },
    'seeding_time': {'name': _('Seeding Time'), 'status': ['seeding_time']},
    'active_time': {'name': _('Active Time'), 'status': ['active_time']},
    'time_since_transfer': {
        'name': _('Last Activity'),
        'status': ['time_since_transfer'],
    },
    'finished_time': {'name': _('Finished Time'), 'status': ['finished_time']},
    'last_seen_complete': {
        'name': _('Complete Seen'),
        'status': ['last_seen_complete'],
    },
    'completed_time': {'name': _('Completed'), 'status': ['completed_time']},
    'eta': {'name': _('ETA'), 'status': ['eta']},
    'shared': {'name': _('Shared'), 'status': ['shared']},
    'prioritize_first_last': {
        'name': _('Prioritize First/Last'),
        'status': ['prioritize_first_last'],
    },
    'sequential_download': {
        'name': _('Sequential Download'),
        'status': ['sequential_download'],
    },
    'is_auto_managed': {'name': _('Auto Managed'), 'status': ['is_auto_managed']},
    'auto_managed': {'name': _('Auto Managed'), 'status': ['auto_managed']},
    'stop_at_ratio': {'name': _('Stop At Ratio'), 'status': ['stop_at_ratio']},
    'stop_ratio': {'name': _('Stop Ratio'), 'status': ['stop_ratio']},
    'remove_at_ratio': {'name': _('Remove At Ratio'), 'status': ['remove_at_ratio']},
    'move_completed': {'name': _('Move On Completed'), 'status': ['move_completed']},
    'move_completed_path': {
        'name': _('Move Completed Path'),
        'status': ['move_completed_path'],
    },
    'move_on_completed': {
        'name': _('Move On Completed'),
        'status': ['move_on_completed'],
    },
    'move_on_completed_path': {
        'name': _('Move On Completed Path'),
        'status': ['move_on_completed_path'],
    },
    'owner': {'name': _('Owner'), 'status': ['owner']},
    'pieces': {'name': _('Pieces'), 'status': ['num_pieces', 'piece_length']},
    'seed_rank': {'name': _('Seed Rank'), 'status': ['seed_rank']},
    'super_seeding': {'name': _('Super Seeding'), 'status': ['super_seeding']},
}

TRACKER_STATUS_TRANSLATION = [
    _('Error'),
    _('Warning'),
    _('Announce OK'),
    _('Announce Sent'),
]

PREFS_CATOG_TRANS = {
    'interface': _('Interface'),
    'downloads': _('Downloads'),
    'bandwidth': _('Bandwidth'),
    'queue': _('Queue'),
    'network': _('Network'),
    'proxy': _('Proxy'),
    'cache': _('Cache'),
    'other': _('Other'),
    'daemon': _('Daemon'),
    'plugins': _('Plugins'),
}

FILE_PRIORITY = {
    0: 'Skip',
    1: 'Low',
    2: 'Low',
    3: 'Low',
    4: 'Normal',
    5: 'High',
    6: 'High',
    7: 'High',
    _('Skip'): 0,
    _('Low'): 1,
    _('Normal'): 4,
    _('High'): 7,
}

del _

# The keys from session statistics for cache status.
DISK_CACHE_KEYS = [
    'disk.num_blocks_read',
    'disk.num_blocks_written',
    'disk.num_read_ops',
    'disk.num_write_ops',
    'read_hit_ratio',
    'write_hit_ratio',
    'disk.disk_blocks_in_use',
    'disk.read_cache_blocks',
]


class TorrentInfo:
    """Collects information about a torrent file.

    Args:
        filename (str, optional): The path to the .torrent file.
        filetree (int, optional): The version of filetree to create (defaults to 1).
        torrent_file (dict, optional): A bdecoded .torrent file contents.
        force_bt_version (int, optional): The BitTorrent spec to use for parsing (defaults to 1).

    """

    def __init__(self, filename='', filetree=1, torrent_file=None, force_bt_version=1):
        self._filedata = None
        if torrent_file:
            self._metainfo = torrent_file
        elif filename:
            log.debug('Attempting to open %s.', filename)
            try:
                with open(filename, 'rb') as _file:
                    self._filedata = _file.read()
            except OSError as ex:
                log.warning('Unable to open %s: %s', filename, ex)
                return

            try:
                self._metainfo = bencode.bdecode(self._filedata)
            except bencode.BTFailure as ex:
                log.warning('Failed to decode %s: %s', filename, ex)
                return
        else:
            log.warning('Requires valid arguments.')
            return

        # info_dict with keys decoded to unicode.
        info_dict = {k.decode(): v for k, v in self._metainfo[b'info'].items()}
        self._info_hash = sha(bencode.bencode(info_dict)).hexdigest()

        # Get encoding from torrent file if available
        encoding = info_dict.get(
            'encoding', info_dict.get('codepage', b'UTF-8')
        ).decode()

        # 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'])
        else:
            self._name = decode_bytes(info_dict['name'], encoding)

        meta_version = info_dict['meta version'] if 'meta version' in info_dict else -1
        is_hybrid = 'files' in info_dict and meta_version == 2

        parse_v1 = False
        parse_v2 = False
        if is_hybrid:
            if force_bt_version == 1:
                parse_v1 = True
            elif force_bt_version == 2:
                parse_v2 = True
        elif 'files' in info_dict:
            parse_v1 = True
        elif meta_version == 2 and 'file tree' in info_dict:
            parse_v2 = True

        # Get list of files from torrent info
        self._files = []
        if parse_v1:
            paths = {}
            dirs = {}
            prefix = self._name

            for index, f in enumerate(info_dict['files']):
                f = {k.decode(): v for k, v in f.items()}

                if 'path.utf-8' in f:
                    path = decode_bytes(os.path.join(*f['path.utf-8']))
                else:
                    path = decode_bytes(os.path.join(*f['path']), encoding)

                if prefix:
                    path = os.path.join(prefix, path)

                # Ensure agnostic path separator
                path = path.replace('\\', '/')

                self._files.append(
                    {'path': path, 'size': f['length'], 'download': True}
                )
                paths[path] = {'path': path, 'index': index, 'length': f['length']}

                dirname = os.path.dirname(path)
                while dirname:
                    dirinfo = dirs.setdefault(dirname, {})
                    dirinfo['length'] = dirinfo.get('length', 0) + f['length']
                    dirname = os.path.dirname(dirname)

            if filetree == 2:

                def walk(full_path, item):
                    if item['type'] == 'dir':
                        item.update(dirs[full_path])
                    else:
                        item.update(paths[full_path])
                    item['download'] = True

                file_tree = FileTree2(list(paths))
                file_tree.walk(walk)
            else:

                def walk(full_path, item):
                    if isinstance(item, dict):
                        return item
                    return [paths[full_path]['index'], paths[full_path]['length'], True]

                file_tree = FileTree(paths)
                file_tree.walk(walk)
            self._files_tree = file_tree.get_tree()
        elif parse_v2:

            def single_file_torrent(inner_info_dict):
                if len(inner_info_dict['file tree']) > 1:
                    return False

                file_name = [key for key in inner_info_dict['file tree']][0]
                return inner_info_dict['name'] == file_name

            if not single_file_torrent(info_dict):
                info_dict['file tree'] = {info_dict['name']: info_dict['file tree']}

            if filetree == 2:

                def walk(full_path, item):
                    if item['type'] == 'file':
                        item['path'] = full_path
                        self._files.append(
                            {
                                'path': full_path,
                                'size': item['length'],
                                'download': True,
                            }
                        )
                    item['download'] = True

                file_tree = FileTree2BTv2(info_dict['file tree'])
                file_tree.walk(walk)
            else:

                def walk(full_path, item):
                    if isinstance(item, dict):
                        return item
                    self._files.append(
                        {'path': full_path, 'size': item[1], 'download': True}
                    )
                    return [item[0], item[1], True]

                file_tree = FiletreeBTv2(info_dict['file tree'])
                file_tree.walk(walk)

            self._files_tree = file_tree.get_tree()
        else:
            self._files.append(
                {'path': self._name, 'size': info_dict['length'], 'download': True}
            )
            if filetree == 2:
                self._files_tree = {
                    'contents': {
                        self._name: {
                            'type': 'file',
                            'index': 0,
                            'length': info_dict['length'],
                            'download': True,
                        }
                    }
                }
            else:
                self._files_tree = {self._name: (0, info_dict['length'], True)}

    @classmethod
    def from_metadata(cls, metadata, trackers=None):
        """Create a TorrentInfo from metadata and trackers

        Args:
            metadata (dict): A bdecoded info section of torrent file.
            trackers (list of lists, optional): The trackers to include.

        """
        if not isinstance(metadata, dict):
            return

        metainfo = {b'info': metadata}
        if trackers:
            metainfo[b'announce'] = trackers[0][0].encode('utf-8')
            trackers_utf8 = [[t.encode('utf-8') for t in tier] for tier in trackers]
            metainfo[b'announce-list'] = trackers_utf8
        return cls(torrent_file=metainfo)

    def as_dict(self, *keys):
        """The torrent info as a dictionary, filtered by keys.

        Args:
            keys (str): A space-separated string of keys.

        Returns:
            dict: The torrent info dict with specified keys.
        """
        return {key: getattr(self, key) for key in keys}

    @property
    def name(self):
        """The name of the torrent.

        Returns:
            str: The torrent name.

        """
        return self._name

    @property
    def info_hash(self):
        """The calculated torrent info_hash.

        Returns:
            str: The torrent info_hash.
        """
        return self._info_hash

    @property
    def files(self):
        """The files that the torrent contains.

        Returns:
            list: The list of torrent files.

        """
        return self._files

    @property
    def files_tree(self):
        """A tree of the files the torrent contains.

        ::

            {
                "some_directory": {
                    "some_file": (index, size, download)
                }
            }

        Returns:
            dict: The tree of files.

        """
        return self._files_tree

    @property
    def metainfo(self):
        """Returns the torrent metainfo dictionary.

        This is the bdecoded torrent file contents.

        Returns:
            dict: The metainfo dictionary.

        """
        return self._metainfo

    @property
    def filedata(self):
        """The contents of the .torrent file.

        Returns:
            bytes: The bencoded metainfo.

        """
        if not self._filedata:
            self._filedata = bencode.bencode(self._metainfo)
        return self._filedata


class FileTree2:
    """
    Converts a list of paths, from a V1 torrent, into a file tree.

    Each file will have the dictionary structure of:
        { file_name: {type, path, index, length, download} }
    where:
        type (str): will always be "file"
        path (str): the absolute file path from the root the torrent
        index (int): the index of file in the torrent
        length (int): the size of the file, in bytes
        download (bool): marks the file to download

    Folder will be dictionaries of files:
        { dir1: type, contents: {file_name1: {...}, file_name2: {...}}, dir2: ... }
    where:
        type (str): will always be "dir"
        contents (dict): a dictionary of inner files and folders

    The entire tree will start with a root dictionary:
        { contents: {dirs...}, type: "dir" }

    Args:
        paths (list): The paths to be converted.
    """

    def __init__(self, paths: list):
        self.tree = {'contents': {}, 'type': 'dir'}

        def get_parent(path):
            parent = self.tree
            while '/' in path:
                directory, path = path.split('/', 1)
                child = parent['contents'].get(directory)
                if child is None:
                    parent['contents'][directory] = {'type': 'dir', 'contents': {}}
                parent = parent['contents'][directory]
            return parent, path

        for path in paths:
            if path[-1] == '/':
                path = path[:-1]
                parent, path = get_parent(path)
                parent['contents'][path] = {'type': 'dir', 'contents': {}}
            else:
                parent, path = get_parent(path)
                parent['contents'][path] = {'type': 'file'}

    def get_tree(self):
        """
        Return the tree.

        :returns: the file tree.
        :rtype: dictionary
        """
        return self.tree

    def walk(self, callback):
        """
        Walk through the file tree calling the callback function on each item
        contained.

        :param callback: The function to be used as a callback, it should have
            the signature func(item, path) where item is a `tuple` for a file
            and `dict` for a directory.
        :type callback: function
        """

        def walk(directory, parent_path):
            for path in list(directory['contents']):
                full_path = os.path.join(parent_path, path).replace('\\', '/')
                if directory['contents'][path]['type'] == 'dir':
                    directory['contents'][path] = (
                        callback(full_path, directory['contents'][path])
                        or directory['contents'][path]
                    )
                    walk(directory['contents'][path], full_path)
                else:
                    directory['contents'][path] = (
                        callback(full_path, directory['contents'][path])
                        or directory['contents'][path]
                    )

        walk(self.tree, '')

    def __str__(self):
        lines = []

        def write(path, item):
            depth = path.count('/')
            path = os.path.basename(path)
            path = path + '/' if item['type'] == 'dir' else path
            lines.append('  ' * depth + path)

        self.walk(write)
        return '\n'.join(lines)


class FileTree:
    """
    Converts a dict of paths, from a V1 torrent, into a file tree.

    Each file will have the dictionary structure of:
        { file_name: [index, length, download] }
    Where:
        index (int): the index of file in the torrent
        length (int): the size of the file, in bytes
        download (bool): marks the file to download

    Folder will be dictionaries of files:
        { dir1: {file_name1: [...], file_name2: [...]}, dir2: ... }

    Args:
        paths (dict): The paths to be converted.
    """

    def __init__(self, paths: dict):
        self.tree = {}

        def get_parent(path):
            parent = self.tree
            while '/' in path:
                directory, path = path.split('/', 1)
                child = parent.get(directory)
                if child is None:
                    parent[directory] = {}
                parent = parent[directory]
            return parent, path

        for path in paths:
            if path[-1] == '/':
                path = path[:-1]
                parent, path = get_parent(path)
                parent[path] = {}
            else:
                parent, path = get_parent(path)
                parent[path] = []

    def get_tree(self):
        """
        Return the tree, after first converting all file lists to a tuple.

        Returns:
            dict: the file tree.
        """

        def to_tuple(path, item):
            if isinstance(item, dict):
                return item
            return tuple(item)

        self.walk(to_tuple)
        return self.tree

    def walk(self, callback):
        """
        Walk through the file tree calling the callback function on each item
        contained.

        Args:
            callback (function): The function to be used as a callback, it should have
                the signature func(item, path) where item is a `tuple` for a file
                and `dict` for a directory.
        """

        def walk(directory, parent_path):
            for path in list(directory):
                full_path = os.path.join(parent_path, path).replace('\\', '/')
                if isinstance(directory[path], dict):
                    directory[path] = (
                        callback(full_path, directory[path]) or directory[path]
                    )
                    walk(directory[path], full_path)
                else:
                    directory[path] = (
                        callback(full_path, directory[path]) or directory[path]
                    )

        walk(self.tree, '')

    def __str__(self):
        lines = []

        def write(path, item):
            depth = path.count('/')
            path = os.path.basename(path)
            path = isinstance(item, dict) and path + '/' or path
            lines.append('  ' * depth + path)

        self.walk(write)
        return '\n'.join(lines)


class FiletreeBTv2(FileTree):
    """
    Converts a dict of paths, from a V2 torrent, into a file tree.

    Each file will have the dictionary structure of:
        { file_name: [index, length, download] }
    Where:
        index (int): the index of file in the torrent
        length (int): the size of the file, in bytes
        download (bool): marks the file to download

    Folder will be dictionaries of files:
        { dir1: {file_name1: [...], file_name2: [...]}, dir2: ... }

    Args:
        file_tree (dict): The paths to be converted.
    """

    def __init__(self, file_tree):
        self.tree = {}

        def get_parent(curr_tree_dict, index, parent) -> int:
            for key, item in curr_tree_dict.items():
                key = decode_bytes(key)
                if b'' in item:
                    parent[key] = [index, item[b''][b'length']]
                    index += 1
                else:
                    parent[key] = {}
                    index = get_parent(item, index, parent[key])
            return index

        get_parent(file_tree, 0, self.tree)


class FileTree2BTv2(FileTree2):
    """
    Converts a dict of paths, from a V2 torrent, into a file tree.

    Each file will have the dictionary structure of:
        { file_name: {type, path, index, length, download} }
    where:
        type (str): will always be "file"
        path (str): the absolute file path from the root the torrent
        index (int): the index of file in the torrent
        length (int): the size of the file, in bytes
        download (bool): marks the file to download

    Folder will be dictionaries of files:
        { dir1: type, contents: {file_name1: {...}, file_name2: {...}}, dir2: ... }
    where:
        type (str): will always be "dir"
        contents (dict): a dictionary of inner files and folders

    The entire tree will start with a root dictionary:
        { contents: {dirs...}, type: "dir" }

    Args:
        file_tree (dict): The paths to be converted.
    """

    def __init__(self, file_tree):
        self.tree = {'contents': {}, 'type': 'dir'}

        def get_parent(curr_tree_dict, index, parent) -> Tuple[int, int]:
            total_length = 0
            for key, item in curr_tree_dict.items():
                key = decode_bytes(key)
                if b'' in item:
                    length = item[b''][b'length']
                    total_length += length
                    parent['contents'][key] = {
                        'index': index,
                        'length': length,
                        'type': 'file',
                    }
                    index += 1
                else:
                    parent['contents'][key] = {
                        'contents': {},
                        'type': 'dir',
                        'length': 0,
                    }
                    index, length = get_parent(item, index, parent['contents'][key])
                    parent['contents'][key]['length'] = length
                    total_length += length
            return index, total_length

        get_parent(file_tree, 0, self.tree)