Fix LP#1004793 : Console: Enable use of connect in non-interactive mode

This commit is contained in:
Calum Lind 2013-05-16 02:41:59 +01:00
parent e1b09f2694
commit cf669f3cfa
2 changed files with 33 additions and 24 deletions

View File

@ -34,17 +34,17 @@
# statement from all source files in the program, then also delete it here. # statement from all source files in the program, then also delete it here.
# #
# #
import sys
from twisted.internet import defer, reactor from twisted.internet import defer
import deluge.component as component import deluge.component as component
from deluge.error import DelugeError from deluge.error import DelugeError
from deluge.ui.client import client from deluge.ui.client import client
from deluge.ui.console import UI_PATH
from colors import strip_colors from colors import strip_colors
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class Commander: class Commander:
def __init__(self, cmds, interactive=False): def __init__(self, cmds, interactive=False):
self._commands = cmds self._commands = cmds
@ -73,6 +73,7 @@ class Commander:
# Do a little hack here to print 'command --help' properly # Do a little hack here to print 'command --help' properly
parser._print_help = parser.print_help parser._print_help = parser.print_help
def print_help(f=None): def print_help(f=None):
if self.interactive: if self.interactive:
self.write(parser.format_help()) self.write(parser.format_help())
@ -110,20 +111,21 @@ class Commander:
return ret return ret
def exec_args(self, args, host, port, username, password): def exec_args(self, args, host, port, username, password):
commands = []
if args:
# Multiple commands split by ";"
commands = [arg.strip() for arg in args.split(';')]
def on_connect(result): def on_connect(result):
def on_started(result): def on_started(result):
def on_started(result): def on_started(result):
def do_command(result, cmd): def do_command(result, cmd):
return self.do_command(cmd) return self.do_command(cmd)
d = defer.succeed(None) d = defer.succeed(None)
# If we have args, lets process them and quit
# allow multiple commands split by ";"
commands = [arg.strip() for arg in args.split(';')]
for command in commands: for command in commands:
if command in ("quit", "exit"):
break
d.addCallback(do_command, command) d.addCallback(do_command, command)
if "quit" not in commands and "exit" not in commands:
d.addCallback(do_command, "quit") d.addCallback(do_command, "quit")
# We need to wait for the rpcs in start() to finish before processing # We need to wait for the rpcs in start() to finish before processing
@ -136,17 +138,21 @@ class Commander:
rm = reason.value.message rm = reason.value.message
else: else:
rm = reason.getErrorMessage() rm = reason.getErrorMessage()
print "Could not connect to: %s:%d\n %s"%(host,port,rm) if host:
print "Could not connect to daemon: %s:%s\n %s" % (host, port, rm)
else:
print "Could not connect to localhost daemon\n %s" % rm
self.do_command("quit") self.do_command("quit")
if not username and host in ("127.0.0.1", "localhost"):
# No username was provided and it's the localhost, so we can try
# to grab the credentials from the auth file.
from deluge.ui.common import get_localhost_auth
username, password = get_localhost_auth()
if host: if host:
d = client.connect(host, port, username, password) d = client.connect(host, port, username, password)
else: else:
d = client.connect() d = client.connect()
if not self.interactive:
if commands[0].startswith("connect"):
d = self.do_command(commands.pop(0))
elif 'help' in commands:
self.do_command('help')
sys.exit(0)
d.addCallback(on_connect) d.addCallback(on_connect)
d.addErrback(on_connect_fail) d.addErrback(on_connect_fail)

View File

@ -55,8 +55,9 @@ class Command(BaseCommand):
def do_connect(): def do_connect():
d = client.connect(host, port, username, password) d = client.connect(host, port, username, password)
def on_connect(result): def on_connect(result):
if self.console.interactive:
self.console.write("{!success!}Connected to %s:%s!" % (host, port)) self.console.write("{!success!}Connected to %s:%s!" % (host, port))
component.start() return component.start()
def on_connect_fail(result): def on_connect_fail(result):
try: try:
@ -64,6 +65,7 @@ class Command(BaseCommand):
except: except:
msg = result.value.args[0] msg = result.value.args[0]
self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, msg)) self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, msg))
return result
d.addCallback(on_connect) d.addCallback(on_connect)
d.addErrback(on_connect_fail) d.addErrback(on_connect_fail)
@ -71,7 +73,8 @@ class Command(BaseCommand):
if client.connected(): if client.connected():
def on_disconnect(result): def on_disconnect(result):
do_connect() self.console.statusbars.update_statusbars()
client.disconnect().addCallback(on_disconnect) return do_connect()
return client.disconnect().addCallback(on_disconnect)
else: else:
do_connect() return do_connect()