From 2c419a1cd960137ef54212f454e84a3902ca2920 Mon Sep 17 00:00:00 2001 From: epheo Date: Thu, 15 Feb 2024 12:48:26 +0100 Subject: [PATCH] Allow password arg from None type in driver class As password isn't a mandatory requirement for the follwing classes also accept None type arg for password. - Nmcli0990Wireless - NmcliWireless - WpasupplicantWireless Replace assert with if not raise RunTime error. --- .../open_gopro/wifi/adapters/wireless.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/demos/python/sdk_wireless_camera_control/open_gopro/wifi/adapters/wireless.py b/demos/python/sdk_wireless_camera_control/open_gopro/wifi/adapters/wireless.py index 46f073cc..f2ea919c 100644 --- a/demos/python/sdk_wireless_camera_control/open_gopro/wifi/adapters/wireless.py +++ b/demos/python/sdk_wireless_camera_control/open_gopro/wifi/adapters/wireless.py @@ -143,13 +143,20 @@ def _detect_driver(self) -> WifiController: def _sudo_from_stdin(self) -> str: """Ask for sudo password input from stdin + This method prompts the user to enter the sudo password from the command line. + It validates the password by running a command with sudo and checking if the password is valid. + Returns: - str: password + str: The entered sudo password. + + Raises: + RuntimeError: If the password is empty or invalid. """ # Need password for sudo if not self._password: self._password = getpass("Need to run as sudo. Enter password: ") - assert self._password, 'Password is required' + if not self._password: + raise RuntimeError("Can't use sudo with empty password.") # Validate password if "VALID PASSWORD" not in cmd(f'echo "{self._password}" | sudo -S echo "VALID PASSWORD"'): raise RuntimeError("Invalid password") @@ -248,7 +255,7 @@ def is_on(self) -> bool: class NmcliWireless(WifiController): """Linux nmcli Driver < 0.9.9.0.""" - def __init__(self, password: str, interface: Optional[str] = None) -> None: + def __init__(self, password: str | None, interface: Optional[str] = None) -> None: WifiController.__init__(self, interface=interface, password=password) def _clean(self, partial: str) -> None: @@ -409,7 +416,7 @@ def power(self, power: bool) -> bool: class Nmcli0990Wireless(WifiController): """Linux nmcli Driver >= 0.9.9.0.""" - def __init__(self, password: str, interface: Optional[str] = None) -> None: + def __init__(self, password: str | None, interface: Optional[str] = None) -> None: WifiController.__init__(self, interface=interface, password=password) def _clean(self, partial: str) -> None: @@ -568,7 +575,7 @@ class WpasupplicantWireless(WifiController): _file = "/tmp/wpa_supplicant.conf" - def __init__(self, password: str, interface: Optional[str] = None) -> None: + def __init__(self, password: str | None, interface: Optional[str] = None) -> None: WifiController.__init__(self, interface=interface, password=password) def connect(self, ssid: str, password: str, timeout: float = 15) -> bool: