From fa309d0d185ef6b3b163792e11c2d4161fbd3bdf Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sun, 15 May 2016 19:21:21 +0100 Subject: [PATCH] [WebUI] Refactor json_api._get_host --- deluge/tests/test_web_api.py | 6 +++--- deluge/ui/web/json_api.py | 34 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/deluge/tests/test_web_api.py b/deluge/tests/test_web_api.py index f756130fe..5c56970be 100644 --- a/deluge/tests/test_web_api.py +++ b/deluge/tests/test_web_api.py @@ -139,13 +139,13 @@ class WebAPITestCase(BaseTestCase, DaemonBase): self.assertEquals(status, tuple(status)) def test_get_host(self): - self.assertEquals(self.deluge_web.web_api._get_host("invalid_id"), None) + self.assertFalse(self.deluge_web.web_api._get_host("invalid_id")) conn = self.deluge_web.web_api.host_list["hosts"][0] self.assertEquals(self.deluge_web.web_api._get_host(conn[0]), conn) def test_add_host(self): conn = [None, '', 0, '', ''] - self.assertEquals(self.deluge_web.web_api._get_host(conn[0]), None) + self.assertFalse(self.deluge_web.web_api._get_host(conn[0])) # Add valid host ret = self.deluge_web.web_api.add_host(conn[1], conn[2], conn[3], conn[4]) self.assertEquals(ret[0], True) @@ -167,7 +167,7 @@ class WebAPITestCase(BaseTestCase, DaemonBase): self.assertEquals(self.deluge_web.web_api._get_host(conn[0]), conn) # Remove valid host self.assertTrue(self.deluge_web.web_api.remove_host(conn[0])) - self.assertEquals(self.deluge_web.web_api._get_host(conn[0]), None) + self.assertFalse(self.deluge_web.web_api._get_host(conn[0])) # Remove non-existing host self.assertFalse(self.deluge_web.web_api.remove_host(conn[0])) diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index e3d3b1a33..54b07a042 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -396,15 +396,11 @@ class WebApi(JSONComponent): component.get("Web.PluginManager").start() else: client.set_disconnect_callback(self._on_client_disconnect) - if component.get("DelugeWeb").config["default_daemon"]: # Sort out getting the default daemon here - default = component.get("DelugeWeb").config["default_daemon"] - host = component.get("Web")._get_host(default) - if host: - return self._connect_daemon(*host[1:]) - else: - return self._connect_daemon() + default_host_id = component.get("DelugeWeb").config["default_daemon"] + host_info = component.get("Web")._get_host(default_host_id) + return self._connect_daemon(*host_info[1:]) return defer.succeed(True) @@ -413,17 +409,21 @@ class WebApi(JSONComponent): return self.stop() def _get_host(self, host_id): - """ - Return the information about a host + """Information about a host from supplied host id. + + Args: + host_id (str): The id of the host. + + Returns: + list: The host information, empty list if not found. - :param host_id: the id of the host - :type host_id: string - :returns: the host information - :rtype: list """ - for host in self.host_list["hosts"]: - if host[0] == host_id: - return host + host_info = [] + for host_entry in self.host_list["hosts"]: + if host_entry[0] == host_id: + host_info = host_entry + break + return host_info def start(self): self.core_config.start() @@ -866,7 +866,7 @@ class WebApi(JSONComponent): :type host_id: string """ host = self._get_host(connection_id) - if host is None: + if not host: return False self.host_list["hosts"].remove(host)