Skip to content

Commit

Permalink
Allow password arg from None type in driver class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epheo committed Feb 15, 2024
1 parent d084cfd commit 2c419a1
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2c419a1

Please sign in to comment.