# Path: deluge/ui/console/main.py
# class BaseCommand(object):
# 
#     usage = 'usage'
#     option_list = tuple()
#     aliases = []
# 
#     def complete(self, text, *args):
#         return []
#     def handle(self, *args, **options):
#         pass
# 
#     @property
#     def name(self):
#         return 'base'
# 
#     @property
#     def epilog(self):
#         return self.__doc__
# 
#     def split(self, text):
#         if deluge.common.windows_check():
#             text = text.replace('\\', '\\\\')
#         return shlex.split(text)
# 
#     def create_parser(self):
#         return OptionParser(prog = self.name,
#                             usage = self.usage,
#                             epilog = self.epilog,
#                             option_list = self.option_list)
# 
# Path: deluge/ui/client.py
# RPC_RESPONSE = 1
# RPC_ERROR = 2
# RPC_EVENT = 3
# def format_kwargs(kwargs):
#     def __init__(self, method, args, kwargs, exception_type, exception_msg, traceback):
#     def __repr__(self):
#     def format_message(self):
#     def connectionMade(self):
#     def dataReceived(self, data):
#     def send_request(self, request):
#     def __init__(self, daemon, event_handlers):
#     def startedConnecting(self, connector):
#     def clientConnectionFailed(self, connector, reason):
#     def clientConnectionLost(self, connector, reason):
#     def __init__(self, event_handlers={}):
#     def connect(self, host, port, username, password):
#     def disconnect(self):
#     def call(self, method, *args, **kwargs):
#     def pop_deferred(self, request_id):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def __rpcError(self, error_data):
#     def __on_connect(self, result, username, password):
#     def __on_connect_fail(self, reason):
#     def __on_login(self, result, username):
#     def __on_login_fail(self, result):
#     def set_disconnect_callback(self, cb):
#     def get_bytes_recv(self):
#     def get_bytes_sent(self):
#     def __init__(self, event_handlers={}):
#     def disconnect(self):
#     def call(self, method, *args, **kwargs):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def __init__(self, daemon, method):
#     def __call__(self, *args, **kwargs):
#     def __getattr__(self, name):
#     def __call__(self, *args, **kwargs):
#     def __init__(self):
#     def connect(self, host="127.0.0.1", port=58846, username="", password=""):
#         def on_connect_fail(result):
#     def disconnect(self):
#     def start_classic_mode(self):
#     def start_daemon(self, port, config):
#     def is_localhost(self):
#     def is_classicmode(self):
#     def connected(self):
#     def connection_info(self):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def force_call(self, block=False):
#     def __getattr__(self, method):
#     def set_disconnect_callback(self, cb):
#     def __on_disconnect(self):
#     def get_bytes_recv(self):
#     def get_bytes_sent(self):
# class DelugeRPCError(object):
# class DelugeRPCRequest(object):
# class DelugeRPCProtocol(Protocol):
# class DelugeRPCClientFactory(ClientFactory):
# class DaemonProxy(object):
# class DaemonSSLProxy(DaemonProxy):
# class DaemonClassicProxy(DaemonProxy):
# class DottedObject(object):
# class RemoteMethod(DottedObject):
# class Client(object):


from deluge.ui.console.main import BaseCommand
from deluge.ui.client import client
import deluge.ui.console.colors as colors
import deluge.component as component
class Command(BaseCommand):
    """Connect to a new deluge server."""

    usage = "Usage: connect <host[:port]> <username> <password>"

    def handle(self, host="127.0.0.1:58846", username="", password="", **options):
        self.console = component.get("ConsoleUI")
        try:
            host, port = host.split(":")
        except ValueError:
            port = 58846
        else:
            port = int(port)

        def do_connect():
            d = client.connect(host, port, username, password)
            def on_connect(result):
                self.console.write("{!success!}Connected to %s:%s!" % (host, port))
                component.start()

            def on_connect_fail(result):
                self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, result.value.args[0]))

            d.addCallback(on_connect)
            d.addErrback(on_connect_fail)
            return d

        if client.connected():
            def on_disconnect(result):
                do_connect()

# Path: deluge/ui/console/main.py
# class BaseCommand(object):
# 
#     usage = 'usage'
#     option_list = tuple()
#     aliases = []
# 
#     def complete(self, text, *args):
#         return []
#     def handle(self, *args, **options):
#         pass
# 
#     @property
#     def name(self):
#         return 'base'
# 
#     @property
#     def epilog(self):
#         return self.__doc__
# 
#     def split(self, text):
#         if deluge.common.windows_check():
#             text = text.replace('\\', '\\\\')
#         return shlex.split(text)
# 
#     def create_parser(self):
#         return OptionParser(prog = self.name,
#                             usage = self.usage,
#                             epilog = self.epilog,
#                             option_list = self.option_list)
# 
# Path: deluge/ui/client.py
# RPC_RESPONSE = 1
# RPC_ERROR = 2
# RPC_EVENT = 3
# def format_kwargs(kwargs):
#     def __init__(self, method, args, kwargs, exception_type, exception_msg, traceback):
#     def __repr__(self):
#     def format_message(self):
#     def connectionMade(self):
#     def dataReceived(self, data):
#     def send_request(self, request):
#     def __init__(self, daemon, event_handlers):
#     def startedConnecting(self, connector):
#     def clientConnectionFailed(self, connector, reason):
#     def clientConnectionLost(self, connector, reason):
#     def __init__(self, event_handlers={}):
#     def connect(self, host, port, username, password):
#     def disconnect(self):
#     def call(self, method, *args, **kwargs):
#     def pop_deferred(self, request_id):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def __rpcError(self, error_data):
#     def __on_connect(self, result, username, password):
#     def __on_connect_fail(self, reason):
#     def __on_login(self, result, username):
#     def __on_login_fail(self, result):
#     def set_disconnect_callback(self, cb):
#     def get_bytes_recv(self):
#     def get_bytes_sent(self):
#     def __init__(self, event_handlers={}):
#     def disconnect(self):
#     def call(self, method, *args, **kwargs):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def __init__(self, daemon, method):
#     def __call__(self, *args, **kwargs):
#     def __getattr__(self, name):
#     def __call__(self, *args, **kwargs):
#     def __init__(self):
#     def connect(self, host="127.0.0.1", port=58846, username="", password=""):
#         def on_connect_fail(result):
#     def disconnect(self):
#     def start_classic_mode(self):
#     def start_daemon(self, port, config):
#     def is_localhost(self):
#     def is_classicmode(self):
#     def connected(self):
#     def connection_info(self):
#     def register_event_handler(self, event, handler):
#     def deregister_event_handler(self, event, handler):
#     def force_call(self, block=False):
#     def __getattr__(self, method):
#     def set_disconnect_callback(self, cb):
#     def __on_disconnect(self):
#     def get_bytes_recv(self):
#     def get_bytes_sent(self):
# class DelugeRPCError(object):
# class DelugeRPCRequest(object):
# class DelugeRPCProtocol(Protocol):
# class DelugeRPCClientFactory(ClientFactory):
# class DaemonProxy(object):
# class DaemonSSLProxy(DaemonProxy):
# class DaemonClassicProxy(DaemonProxy):
# class DottedObject(object):
# class RemoteMethod(DottedObject):
# class Client(object):


# Path: deluge/ui/console/commands/connect.py
from deluge.ui.console.main import BaseCommand
from deluge.ui.client import client
import deluge.ui.console.colors as colors
import deluge.component as component

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

    usage = "Usage: connect <host[:port]> <username> <password>"

    def handle(self, host="127.0.0.1:58846", username="", password="", **options):
        self.console = component.get("ConsoleUI")
        try:
            host, port = host.split(":")
        except ValueError:
            port = 58846
        else:
            port = int(port)

        def do_connect():
            d = client.connect(host, port, username, password)
            def on_connect(result):
                self.console.write("{!success!}Connected to %s:%s!" % (host, port))
                component.start()

            def on_connect_fail(result):
                self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, result.value.args[0]))

            d.addCallback(on_connect)
            d.addErrback(on_connect_fail)
            return d

        if client.connected():
            def on_disconnect(result):
                do_connect()

            client.disconnect().addCallback(on_disconnect)