2020-08-30 06:28:48 +00:00
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import List, NamedTuple, Optional
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkStatusMonitor:
|
|
|
|
@classmethod
|
|
|
|
def get_network_status_monitor(cls) -> 'NetworkStatusMonitor':
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
from .darwin import DarwinNetworkStatus
|
2022-08-15 05:21:14 +00:00
|
|
|
|
2020-08-30 06:28:48 +00:00
|
|
|
return DarwinNetworkStatus()
|
|
|
|
else:
|
2023-05-01 08:28:11 +00:00
|
|
|
from .network_manager import (
|
|
|
|
DBusException,
|
|
|
|
NetworkManagerMonitor,
|
|
|
|
UnsupportedException,
|
|
|
|
)
|
2022-08-15 05:21:14 +00:00
|
|
|
|
2020-08-30 06:28:48 +00:00
|
|
|
try:
|
|
|
|
return NetworkManagerMonitor()
|
2020-09-26 08:59:16 +00:00
|
|
|
except (UnsupportedException, DBusException):
|
2020-08-30 06:28:48 +00:00
|
|
|
return NullNetworkStatusMonitor()
|
|
|
|
|
|
|
|
def is_network_status_available(self):
|
|
|
|
"""Is the network status really available, and not just a dummy implementation?"""
|
|
|
|
return type(self) != NetworkStatusMonitor
|
|
|
|
|
|
|
|
def is_network_metered(self) -> bool:
|
|
|
|
"""Is the currently connected network a metered connection?"""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_current_wifi(self) -> Optional[str]:
|
|
|
|
"""Get current SSID or None if Wifi is off."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-02-18 05:50:58 +00:00
|
|
|
def get_known_wifis(self) -> List['SystemWifiInfo']:
|
2020-08-30 06:28:48 +00:00
|
|
|
"""Get WiFi networks known to system."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class SystemWifiInfo(NamedTuple):
|
|
|
|
ssid: str
|
|
|
|
last_connected: Optional[datetime]
|
|
|
|
|
|
|
|
|
|
|
|
class NullNetworkStatusMonitor(NetworkStatusMonitor):
|
|
|
|
"""Dummy implementation, in case we don't have one for current platform."""
|
|
|
|
|
|
|
|
def is_network_status_available(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_network_metered(self) -> bool:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_current_wifi(self) -> Optional[str]:
|
|
|
|
pass
|
|
|
|
|
2021-02-18 05:50:58 +00:00
|
|
|
def get_known_wifis(self) -> List['SystemWifiInfo']:
|
|
|
|
return []
|