Skip to content

Commit

Permalink
[UI][Common] Add daemon version check
Browse files Browse the repository at this point in the history
For new UI features to be added, one should make sure the backend daemon
is supported and add fallback in case it doesn't.
Here we add the ability to get the daemon version from the `Client`
class and also check the version against a desired version.
  • Loading branch information
DjLegolas committed Jun 3, 2023
1 parent 8001110 commit a9044df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
22 changes: 21 additions & 1 deletion deluge/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from twisted.internet import defer

from deluge import error
from deluge.common import AUTH_LEVEL_NORMAL, get_localhost_auth
from deluge.common import AUTH_LEVEL_NORMAL, get_localhost_auth, get_version
from deluge.core.authmanager import AUTH_LEVEL_ADMIN
from deluge.ui.client import Client, DaemonSSLProxy, client

Expand Down Expand Up @@ -170,3 +170,23 @@ def on_failure(failure):

d.addCallbacks(self.fail, on_failure)
return d

@pytest_twisted.inlineCallbacks
def test_connection_version(self):
username, password = get_localhost_auth()
yield client.connect(
'localhost', self.listen_port, username=username, password=password
)

assert client.connection_version() == get_version()

@pytest_twisted.inlineCallbacks
def test_daemon_version_above(self):
username, password = get_localhost_auth()
yield client.connect(
'localhost', self.listen_port, username=username, password=password
)

assert client.is_daemon_version_equal_or_greater(get_version())
assert not client.is_daemon_version_equal_or_greater(f'{get_version()}1')
assert client.is_daemon_version_equal_or_greater('0.1.0')
23 changes: 22 additions & 1 deletion deluge/ui/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from twisted.internet.protocol import ClientFactory

from deluge import error
from deluge.common import get_localhost_auth, get_version
from deluge.common import VersionSplit, get_localhost_auth, get_version
from deluge.decorators import deprecated
from deluge.transfer import DelugeTransferProtocol

Expand Down Expand Up @@ -227,6 +227,7 @@ def clientConnectionLost(self, connector, reason): # NOQA: N802
self.daemon.host = None
self.daemon.port = None
self.daemon.username = None
self.daemon.daemon_info = None
self.daemon.connected = False

if (
Expand Down Expand Up @@ -260,6 +261,7 @@ def __init__(self, event_handlers=None):
self.host = None
self.port = None
self.username = None
self.daemon_info = None
self.authentication_level = 0

self.connected = False
Expand Down Expand Up @@ -741,6 +743,25 @@ def connection_info(self):

return None

def connection_version(self):
"""
Get the connected daemon version
Returns:
str: the daemon version
"""
if self.connected():
return self._daemon_proxy.daemon_info

return ''

def is_daemon_version_equal_or_greater(self, version_check):
if VersionSplit(version_check) and self.connected():
daemon_version = self.connection_version()
return VersionSplit(daemon_version) >= VersionSplit(version_check)

return False

def register_event_handler(self, event, handler):
"""
Registers a handler that will be called when an event is received from the daemon.
Expand Down

0 comments on commit a9044df

Please sign in to comment.