mirror of
https://github.com/borgbase/vorta
synced 2025-01-03 13:45:49 +00:00
Handle DBusException in NetworkManagerMonitor (#636)
* Catch DBus permission errors * Better DBusException handling in NetworkManagerMonitor
This commit is contained in:
parent
23374876d6
commit
262c91a180
2 changed files with 59 additions and 20 deletions
|
@ -16,11 +16,16 @@ def __init__(self, nm_adapter: 'NetworkManagerDBusAdapter' = None):
|
||||||
self._nm = nm_adapter or NetworkManagerDBusAdapter.get_system_nm_adapter()
|
self._nm = nm_adapter or NetworkManagerDBusAdapter.get_system_nm_adapter()
|
||||||
|
|
||||||
def is_network_metered(self) -> bool:
|
def is_network_metered(self) -> bool:
|
||||||
|
try:
|
||||||
return self._nm.get_global_metered_status() in (NMMetered.YES, NMMetered.GUESS_YES)
|
return self._nm.get_global_metered_status() in (NMMetered.YES, NMMetered.GUESS_YES)
|
||||||
|
except DBusException:
|
||||||
|
logger.exception("Failed to check if network is metered, assuming it isn't")
|
||||||
|
return False
|
||||||
|
|
||||||
def get_current_wifi(self) -> Optional[str]:
|
def get_current_wifi(self) -> Optional[str]:
|
||||||
# Only check the primary connection. VPN over WiFi will still show the WiFi as Primary Connection.
|
# Only check the primary connection. VPN over WiFi will still show the WiFi as Primary Connection.
|
||||||
# We don't check all active connections, as NM won't disable WiFi when connecting a cable.
|
# We don't check all active connections, as NM won't disable WiFi when connecting a cable.
|
||||||
|
try:
|
||||||
active_connection_path = self._nm.get_primary_connection_path()
|
active_connection_path = self._nm.get_primary_connection_path()
|
||||||
if not active_connection_path:
|
if not active_connection_path:
|
||||||
return
|
return
|
||||||
|
@ -30,11 +35,24 @@ def get_current_wifi(self) -> Optional[str]:
|
||||||
ssid = self._get_ssid_from_settings(settings)
|
ssid = self._get_ssid_from_settings(settings)
|
||||||
if ssid:
|
if ssid:
|
||||||
return ssid
|
return ssid
|
||||||
|
except DBusException:
|
||||||
|
logger.exception("Failed to get currently connected WiFi network, assuming none")
|
||||||
|
return None
|
||||||
|
|
||||||
def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]:
|
def get_known_wifis(self) -> Optional[List[SystemWifiInfo]]:
|
||||||
|
try:
|
||||||
|
connections_paths = self._nm.get_connections_paths()
|
||||||
|
except DBusException:
|
||||||
|
logger.exception("Failed to list connections")
|
||||||
|
return None
|
||||||
|
|
||||||
wifis = []
|
wifis = []
|
||||||
for connection_path in self._nm.get_connections_paths():
|
for connection_path in connections_paths:
|
||||||
|
try:
|
||||||
settings = self._nm.get_settings(connection_path)
|
settings = self._nm.get_settings(connection_path)
|
||||||
|
except DBusException:
|
||||||
|
logger.warning("Couldn't load settings for %s", connection_path, exc_info=True)
|
||||||
|
else:
|
||||||
ssid = self._get_ssid_from_settings(settings)
|
ssid = self._get_ssid_from_settings(settings)
|
||||||
if ssid:
|
if ssid:
|
||||||
timestamp = settings['connection'].get('timestamp')
|
timestamp = settings['connection'].get('timestamp')
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
from vorta.network_status.abc import SystemWifiInfo
|
from vorta.network_status.abc import SystemWifiInfo
|
||||||
from vorta.network_status.network_manager import NetworkManagerMonitor, NMMetered, NetworkManagerDBusAdapter, \
|
from vorta.network_status.network_manager import NetworkManagerMonitor, NMMetered, NetworkManagerDBusAdapter, \
|
||||||
ActiveConnectionInfo, decode_ssid
|
ActiveConnectionInfo, decode_ssid, DBusException
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -95,6 +95,27 @@ def test_get_known_wifis_with_never_used_connection(nm_monitor):
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_known_wifis_partial_failure(nm_monitor):
|
||||||
|
nm_monitor._nm.get_connections_paths.return_value = [
|
||||||
|
'/org/freedesktop/NetworkManager/Settings/12',
|
||||||
|
'/org/freedesktop/NetworkManager/Settings/42',
|
||||||
|
]
|
||||||
|
nm_monitor._nm.get_settings.side_effect = [
|
||||||
|
DBusException("Test"),
|
||||||
|
{
|
||||||
|
'connection': {},
|
||||||
|
'802-11-wireless': {'ssid': [84, 69, 83, 84]},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
result = nm_monitor.get_known_wifis()
|
||||||
|
|
||||||
|
assert result == [SystemWifiInfo(
|
||||||
|
ssid='TEST',
|
||||||
|
last_connected=None,
|
||||||
|
)]
|
||||||
|
|
||||||
|
|
||||||
def test_get_known_wifis_with_no_wifi_connections(nm_monitor):
|
def test_get_known_wifis_with_no_wifi_connections(nm_monitor):
|
||||||
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
|
nm_monitor._nm.get_connections_paths.return_value = ['/org/freedesktop/NetworkManager/Settings/12']
|
||||||
nm_monitor._nm.get_settings.return_value = {
|
nm_monitor._nm.get_settings.return_value = {
|
||||||
|
|
Loading…
Reference in a new issue