From a91f0810a9c26e7aa0bc07e3c8077074e856bba6 Mon Sep 17 00:00:00 2001 From: Michael Ketchel Date: Mon, 4 Nov 2024 22:21:18 -0500 Subject: [PATCH] fixes https://github.com/WLAN-Pi/wlanpi-core/issues/14 and some other cleanups and fixes --- wlanpi_core/__main__.py | 2 +- wlanpi_core/models/network/wlan/wlan_dbus.py | 19 +++++++++++++++++-- wlanpi_core/utils/g_lib_loop.py | 20 ++++++++++---------- wlanpi_core/utils/general.py | 12 ++++++++++++ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/wlanpi_core/__main__.py b/wlanpi_core/__main__.py index 5dbc987..3e16003 100644 --- a/wlanpi_core/__main__.py +++ b/wlanpi_core/__main__.py @@ -111,7 +111,7 @@ def init() -> None: ) if __name__ == "__main__": - sys.exit(main()) + return sys.exit(main()) init() diff --git a/wlanpi_core/models/network/wlan/wlan_dbus.py b/wlanpi_core/models/network/wlan/wlan_dbus.py index 9ef0098..f63f5e0 100644 --- a/wlanpi_core/models/network/wlan/wlan_dbus.py +++ b/wlanpi_core/models/network/wlan/wlan_dbus.py @@ -9,7 +9,9 @@ WPAS_DBUS_OPATH, WPAS_DBUS_SERVICE, ) +from wlanpi_core.models.network.wlan.exceptions import WlanDBUSInterfaceException from wlanpi_core.models.network.wlan.wlan_dbus_interface import WlanDBUSInterface +from wlanpi_core.utils.general import run_command class WlanDBUS: @@ -46,8 +48,21 @@ def get_interface(self, interface) -> WlanDBUSInterface: self.interfaces[interface] = new_interface return self.interfaces[interface] + @staticmethod + def _fetch_system_interfaces() -> list[str]: + return run_command( + "ls /sys/class/ieee80211/*/device/net/", shell=True + ).grep_stdout_for_string("/", negate=True, split=True) + def fetch_interfaces(self, wpas_obj): available_interfaces = [] + for system_interface in self._fetch_system_interfaces(): + try: + self.get_interface(system_interface) + except WlanDBUSInterfaceException as e: + self.logger.warning( + f"Error trying to optimistically register interface {system_interface}: {e}" + ) ifaces = wpas_obj.Get( WPAS_DBUS_INTERFACE, "Interfaces", dbus_interface=self.DBUS_IFACE @@ -71,7 +86,7 @@ def get_systemd_network_interfaces(self, timeout: int = DEFAULT_TIMEOUT): """ wpas_obj = self.bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH) - self.logger.debug("Checking available interfaces", 3) + self.logger.debug("Checking available interfaces") available_interfaces = self.fetch_interfaces(wpas_obj) - self.logger.debug(f"Available interfaces: {available_interfaces}", 3) + self.logger.debug(f"Available interfaces: {available_interfaces}") return available_interfaces diff --git a/wlanpi_core/utils/g_lib_loop.py b/wlanpi_core/utils/g_lib_loop.py index 73ad398..2c59951 100644 --- a/wlanpi_core/utils/g_lib_loop.py +++ b/wlanpi_core/utils/g_lib_loop.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Callable, Optional from gi.repository import GLib @@ -10,7 +10,7 @@ def __init__( self, loop: Optional[GLib.MainLoop] = None, timeout_seconds: Optional[int] = None, - timeout_callback: Optional[callable] = None, + timeout_callback: Optional[Callable] = None, timeout_callback_args: Optional[list] = None, timeout_callback_kwargs: Optional[dict] = None, ): @@ -25,10 +25,10 @@ def __init__( def start_timeout( self, seconds: Optional[int] = None, - callback: Optional[callable] = None, - *args, - **kwargs - ): + callback: Optional[Callable] = None, + *args: tuple[any], + **kwargs: dict[str, any] + ) -> None: self.timeout_source = GLib.timeout_source_new_seconds( seconds if seconds else self.timeout_seconds ) @@ -41,16 +41,16 @@ def start_timeout( self.loop.get_context() ) - def stop_timeout(self): + def stop_timeout(self) -> None: if self.timeout_source and self.timeout_source_attachment: self.timeout_source.remove(self.timeout_source_attachment) - def finish(self): + def finish(self) -> None: self.stop_timeout() self.loop.quit() - def run(self, *args, **kwargs): - self.loop.run(*args, **kwargs) + def run(self, *args: tuple[any], **kwargs: [dict[str, any]]) -> None: + return self.loop.run(*args, **kwargs) def __enter__(self): return self diff --git a/wlanpi_core/utils/general.py b/wlanpi_core/utils/general.py index 9629b73..9f01ff1 100644 --- a/wlanpi_core/utils/general.py +++ b/wlanpi_core/utils/general.py @@ -223,3 +223,15 @@ def get_current_unix_timestamp() -> float: """ ms = datetime.datetime.now() return time.mktime(ms.timetuple()) * 1000 + + +def byte_array_to_string(s) -> str: + """Converts a byte array to string, replacing non-printable characters with spaces.""" + r = "" + for c in s: + if 32 <= c < 127: + r += "%c" % c + else: + r += " " + # r += urllib.quote(chr(c)) + return r