Skip to content

Commit

Permalink
fixes WLAN-Pi#14
Browse files Browse the repository at this point in the history
and some other cleanups and fixes
  • Loading branch information
Michael Ketchel committed Nov 5, 2024
1 parent 1b80d9b commit a91f081
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion wlanpi_core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def init() -> None:
)

if __name__ == "__main__":
sys.exit(main())
return sys.exit(main())


init()
19 changes: 17 additions & 2 deletions wlanpi_core/models/network/wlan/wlan_dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
20 changes: 10 additions & 10 deletions wlanpi_core/utils/g_lib_loop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Callable, Optional

from gi.repository import GLib

Expand All @@ -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,
):
Expand All @@ -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
)
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions wlanpi_core/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a91f081

Please sign in to comment.