summaryrefslogtreecommitdiffstats
path: root/deluge/ui/console/cmdline/commands/connect.py
blob: 6588f7a04f4b36abbb8b49de19ddda2b12a0a04c (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
# -*- 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.
#

from __future__ import unicode_literals

import logging

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

from . import BaseCommand

log = logging.getLogger(__name__)


class Command(BaseCommand):
    """Connect to a new deluge server"""

    usage = _('Usage: connect <host[:port]> [<username>] [<password>]')

    def add_arguments(self, parser):
        parser.add_argument(
            'host', help=_('Daemon host and port'), metavar='<host[:port]>'
        )
        parser.add_argument(
            'username', help=_('Username'), metavar='<username>', nargs='?', default=''
        )
        parser.add_argument(
            'password', help=_('Password'), metavar='<password>', nargs='?', default=''
        )

    def add_parser(self, subparsers):
        parser = subparsers.add_parser(
            self.name, help=self.__doc__, description=self.__doc__, prog='connect'
        )
        self.add_arguments(parser)

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

        host = options.host
        try:
            host, port = host.split(':')
            port = int(port)
        except ValueError:
            port = 58846

        def do_connect():
            d = client.connect(host, port, options.username, options.password)

            def on_connect(result):
                if self.console.interactive:
                    self.console.write('{!success!}Connected to %s:%s!' % (host, port))
                return component.start()

            def on_connect_fail(result):
                try:
                    msg = result.value.exception_msg
                except AttributeError:
                    msg = result.value.message
                self.console.write(
                    '{!error!}Failed to connect to %s:%s with reason: %s'
                    % (host, port, msg)
                )
                return result

            d.addCallbacks(on_connect, on_connect_fail)
            return d

        if client.connected():

            def on_disconnect(result):
                if self.console.statusbars:
                    self.console.statusbars.update_statusbars()
                return do_connect()

            return client.disconnect().addCallback(on_disconnect)
        else:
            return do_connect()