Move get_localhost_auth_uri() to ui.common

This commit is contained in:
Andrew Resch 2008-12-11 09:13:14 +00:00
commit 2b34f64304
4 changed files with 31 additions and 64 deletions

View file

@ -25,8 +25,11 @@
import os import os
from sha import sha from sha import sha
import urlparse
from deluge import bencode from deluge import bencode
from deluge.log import LOG as log from deluge.log import LOG as log
import deluge.configmanager
class TorrentInfo(object): class TorrentInfo(object):
def __init__(self, filename): def __init__(self, filename):
@ -116,3 +119,21 @@ def get_torrent_info(filename):
"files": files, "files": files,
"info_hash": info_hash "info_hash": info_hash
} }
def get_localhost_auth_uri(uri):
"""
Grabs the localclient auth line from the 'auth' file and creates a localhost uri
:param uri: the uri to add the authentication info to
:returns: a localhost uri containing authentication information or None if the information is not available
"""
auth_file = deluge.configmanager.get_config_dir("auth")
if os.path.exists(auth_file):
u = urlparse.urlsplit(uri)
for line in open(auth_file):
username, password = line.split(":")
if username == "localclient":
# We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly
hostname = u.hostname.replace("localhost", "127.0.0.1")
return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port)
return None

View file

@ -6,6 +6,7 @@ import optparse
from deluge.ui.console import UI_PATH from deluge.ui.console import UI_PATH
from deluge.ui.console.colors import Template, make_style, templates, default_style as style from deluge.ui.console.colors import Template, make_style, templates, default_style as style
from deluge.ui.client import aclient as client from deluge.ui.client import aclient as client
from deluge.ui.common import get_localhost_auth_uri
import shlex import shlex
@ -90,26 +91,6 @@ def load_commands(command_dir, exclude=[]):
except OSError, e: except OSError, e:
return {} return {}
def get_localhost_auth_uri(uri):
"""
Grabs the localclient auth line from the 'auth' file and creates a localhost uri
:param uri: the uri to add the authentication info to
:returns: a localhost uri containing authentication information or None if the information is not available
"""
import deluge.configmanager
auth_file = deluge.configmanager.get_config_dir("auth")
if os.path.exists(auth_file):
import urlparse
u = urlparse.urlsplit(uri)
for line in open(auth_file):
username, password = line.split(":")
if username == "localclient":
# We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly
hostname = u.hostname.replace("localhost", "127.0.0.1")
return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port)
return None
class ConsoleUI(object): class ConsoleUI(object):
prompt = '>>> ' prompt = '>>> '

View file

@ -37,6 +37,7 @@ import deluge.component as component
import deluge.xmlrpclib as xmlrpclib import deluge.xmlrpclib as xmlrpclib
import deluge.common import deluge.common
import deluge.ui.gtkui.common as common import deluge.ui.gtkui.common as common
from deluge.ui.common import get_localhost_auth_uri
from deluge.ui.client import aclient as client from deluge.ui.client import aclient as client
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
from deluge.log import LOG as log from deluge.log import LOG as log
@ -151,7 +152,7 @@ class ConnectionManager(component.Component):
if self.gtkui_config["classic_mode"]: if self.gtkui_config["classic_mode"]:
self.start_localhost(DEFAULT_PORT) self.start_localhost(DEFAULT_PORT)
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
uri = self.get_localhost_auth_uri(DEFAULT_URI) uri = get_localhost_auth_uri(DEFAULT_URI)
while not self.test_online_status(uri): while not self.test_online_status(uri):
time.sleep(0.01) time.sleep(0.01)
client.set_core_uri(uri) client.set_core_uri(uri)
@ -183,7 +184,7 @@ class ConnectionManager(component.Component):
while not auth_uri: while not auth_uri:
# We need to keep trying because the daemon may not have been started yet # We need to keep trying because the daemon may not have been started yet
# and the 'auth' file may not have been created # and the 'auth' file may not have been created
auth_uri = self.get_localhost_auth_uri(uri) auth_uri = get_localhost_auth_uri(uri)
time.sleep(0.01) time.sleep(0.01)
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
@ -192,24 +193,6 @@ class ConnectionManager(component.Component):
client.set_core_uri(auth_uri) client.set_core_uri(auth_uri)
self.hide() self.hide()
def get_localhost_auth_uri(self, uri):
"""
Grabs the localclient auth line from the 'auth' file and creates a localhost uri
:param uri: the uri to add the authentication info to
:returns: a localhost uri containing authentication information or None if the information is not available
"""
auth_file = deluge.configmanager.get_config_dir("auth")
if os.path.exists(auth_file):
u = urlparse.urlsplit(uri)
for line in open(auth_file):
username, password = line.split(":")
if username == "localclient":
# We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly
hostname = u.hostname.replace("localhost", "127.0.0.1")
return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port)
return None
def start(self): def start(self):
if self.gtkui_config["autoconnect"]: if self.gtkui_config["autoconnect"]:
# We need to update the autoconnect_host_uri on connection to host # We need to update the autoconnect_host_uri on connection to host
@ -258,7 +241,7 @@ class ConnectionManager(component.Component):
online = HOSTLIST_STATUS.index("Offline") online = HOSTLIST_STATUS.index("Offline")
if urlparse.urlsplit(uri).hostname == "localhost" or urlparse.urlsplit(uri).hostname == "127.0.0.1": if urlparse.urlsplit(uri).hostname == "localhost" or urlparse.urlsplit(uri).hostname == "127.0.0.1":
uri = self.get_localhost_auth_uri(uri) uri = get_localhost_auth_uri(uri)
if uri == current_uri: if uri == current_uri:
online = HOSTLIST_STATUS.index("Connected") online = HOSTLIST_STATUS.index("Connected")
@ -366,7 +349,7 @@ class ConnectionManager(component.Component):
try: try:
u = urlparse.urlsplit(uri) u = urlparse.urlsplit(uri)
if u.hostname == "localhost" or u.hostname == "127.0.0.1": if u.hostname == "localhost" or u.hostname == "127.0.0.1":
host = xmlrpclib.ServerProxy(self.get_localhost_auth_uri(uri)) host = xmlrpclib.ServerProxy(get_localhost_auth_uri(uri))
else: else:
host = xmlrpclib.ServerProxy(uri) host = xmlrpclib.ServerProxy(uri)
host.ping() host.ping()
@ -474,7 +457,7 @@ class ConnectionManager(component.Component):
# We need to stop this daemon # We need to stop this daemon
# Call the shutdown method on the daemon # Call the shutdown method on the daemon
if u.hostname == "127.0.0.1" or u.hostname == "localhost": if u.hostname == "127.0.0.1" or u.hostname == "localhost":
uri = self.get_localhost_auth_uri(uri) uri = get_localhost_auth_uri(uri)
core = xmlrpclib.ServerProxy(uri) core = xmlrpclib.ServerProxy(uri)
core.shutdown() core.shutdown()
# Update display to show change # Update display to show change
@ -529,7 +512,7 @@ class ConnectionManager(component.Component):
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
auth_uri = None auth_uri = None
while not auth_uri: while not auth_uri:
auth_uri = self.get_localhost_auth_uri(uri) auth_uri = get_localhost_auth_uri(uri)
time.sleep(0.01) time.sleep(0.01)
while not self.test_online_status(auth_uri): while not self.test_online_status(auth_uri):
@ -545,7 +528,7 @@ class ConnectionManager(component.Component):
# Status is OK, so lets change to this host # Status is OK, so lets change to this host
if localhost: if localhost:
client.set_core_uri(self.get_localhost_auth_uri(uri)) client.set_core_uri(get_localhost_auth_uri(uri))
else: else:
client.set_core_uri(uri) client.set_core_uri(uri)

View file

@ -38,6 +38,7 @@ from web import cookies, setcookie as w_setcookie
from web import seeother as w_seeother from web import seeother as w_seeother
from deluge.common import fsize, fspeed, ftime from deluge.common import fsize, fspeed, ftime
from deluge.ui.common import get_localhost_auth_uri
from deluge import component from deluge import component
from deluge.log import LOG as log from deluge.log import LOG as log
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
@ -158,25 +159,6 @@ def get_newforms_data(form_class):
#/utils #/utils
#daemon: #daemon:
def get_localhost_auth_uri(uri):
"""
Grabs the localclient auth line from the 'auth' file and creates a localhost uri
:param uri: the uri to add the authentication info to
:returns: a localhost uri containing authentication information or None if the information is not available
"""
import deluge.configmanager
auth_file = deluge.configmanager.get_config_dir("auth")
if os.path.exists(auth_file):
import urlparse
u = urlparse.urlsplit(uri)
for line in open(auth_file):
username, password = line.split(":")
if username == "localclient":
# We use '127.0.0.1' in place of 'localhost' just incase this isn't defined properly
hostname = u.hostname.replace("localhost", "127.0.0.1")
return u.scheme + "://" + username + ":" + password + "@" + hostname + ":" + str(u.port)
return None
def daemon_test_online_status(uri): def daemon_test_online_status(uri):
"""Tests the status of URI.. Returns True or False depending on status. """Tests the status of URI.. Returns True or False depending on status.
""" """