summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/rm.py
blob: bf1f9a02a9c190b6723520cc302ac988ea61f0ad (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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com>
# Copyright (C) 2009 Andrew Resch <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.
#

import logging

import deluge.component as component
from deluge.ui.client import client

from . import BaseCommand

log = logging.getLogger(__name__)


class Command(BaseCommand):
    """Remove a torrent"""

    aliases = ['del']

    def add_arguments(self, parser):
        parser.add_argument(
            '--remove_data',
            action='store_true',
            default=False,
            help=_('Also removes the torrent data'),
        )
        parser.add_argument(
            '-c',
            '--confirm',
            action='store_true',
            default=False,
            help=_('List the matching torrents without removing.'),
        )
        parser.add_argument(
            'torrent_ids',
            metavar='<torrent-id>',
            nargs='+',
            help=_('One or more torrent ids'),
        )

    def handle(self, options):
        self.console = component.get('ConsoleUI')
        torrent_ids = self.console.match_torrents(options.torrent_ids)

        if not options.confirm:
            self.console.write(
                '{!info!}%d %s %s{!info!}'
                % (
                    len(torrent_ids),
                    _n('torrent', 'torrents', len(torrent_ids)),
                    _n('match', 'matches', len(torrent_ids)),
                )
            )
            for t_id in torrent_ids:
                name = self.console.get_torrent_name(t_id)
                self.console.write('* %-50s (%s)' % (name, t_id))
            self.console.write(
                _('Confirm with -c to remove the listed torrents (Count: %d)')
                % len(torrent_ids)
            )
            return

        def on_removed_finished(errors):
            if errors:
                self.console.write(
                    'Error(s) occurred when trying to delete torrent(s).'
                )
                for t_id, e_msg in errors:
                    self.console.write('Error removing torrent %s : %s' % (t_id, e_msg))

        log.info('Removing %d torrents', len(torrent_ids))
        d = client.core.remove_torrents(torrent_ids, options.remove_data)
        d.addCallback(on_removed_finished)

    def complete(self, line):
        # We use the ConsoleUI torrent tab complete method
        return component.get('ConsoleUI').tab_complete_torrent(line)