[UI][Common] Add daemon version check

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.

Closes: https://github.com/deluge-torrent/deluge/pull/427
This commit is contained in:
DjLegolas 2023-06-03 10:16:18 +03:00 committed by Calum Lind
parent 1751d62df9
commit 1989d0de73
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 45 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import pytest_twisted
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
@ -170,3 +170,23 @@ class TestClient:
d.addCallbacks(self.fail, on_failure)
return d
@pytest_twisted.inlineCallbacks
def test_daemon_version(self):
username, password = get_localhost_auth()
yield client.connect(
'localhost', self.listen_port, username=username, password=password
)
assert client.daemon_version == get_version()
@pytest_twisted.inlineCallbacks
def test_daemon_version_check_min(self):
username, password = get_localhost_auth()
yield client.connect(
'localhost', self.listen_port, username=username, password=password
)
assert client.daemon_version_check_min(get_version())
assert not client.daemon_version_check_min(f'{get_version()}1')
assert client.daemon_version_check_min('0.1.0')

View File

@ -15,7 +15,7 @@ from twisted.internet import defer, reactor, ssl
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
@ -227,6 +227,7 @@ class DelugeRPCClientFactory(ClientFactory):
self.daemon.host = None
self.daemon.port = None
self.daemon.username = None
self.daemon.daemon_version = None
self.daemon.connected = False
if (
@ -260,6 +261,7 @@ class DaemonSSLProxy(DaemonProxy):
self.host = None
self.port = None
self.username = None
self.daemon_version = None
self.authentication_level = 0
self.connected = False
@ -389,7 +391,7 @@ class DaemonSSLProxy(DaemonProxy):
log.debug('__on_connect called')
def on_info(daemon_info):
self.daemon_info = daemon_info
self.daemon_version = daemon_info
log.debug('Got info from daemon: %s', daemon_info)
self.daemon_info_deferred.callback(daemon_info)
@ -741,6 +743,26 @@ class Client:
return None
@property
def daemon_version(self) -> str:
"""Get the connected daemon version
Returns:
The daemon version
"""
return self._daemon_proxy.daemon_version if self.connected() else ''
def daemon_version_check_min(self, min_version=get_version()) -> bool:
"""Check connected daemon against a minimum version.
Returns:
If connected daemon meets minimum version requirement.
"""
if not (self.daemon_version and min_version):
return False
return VersionSplit(self.daemon_version) >= VersionSplit(min_version)
def register_event_handler(self, event, handler):
"""
Registers a handler that will be called when an event is received from the daemon.