Skip to content

Commit

Permalink
remov try/catch around import from sh (#267)
Browse files Browse the repository at this point in the history
* remov try/catch around import from sh

* Fix error in type checking.

* Test can only run when sys_utils imports cleanly. Do not run test on architectures where it does not
  • Loading branch information
jscheidtmann authored Jan 29, 2025
1 parent e3394fe commit 215a162
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 57 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ coverage.xml
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
Expand Down
24 changes: 11 additions & 13 deletions python/PiFinder/sys_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
import re
from typing import Dict, Any

try:
import sh
from sh import wpa_cli, unzip, su, passwd

REAL_SYS_UTILS = True
except ImportError:
REAL_SYS_UTILS = False
import sh
from sh import wpa_cli, unzip, su, passwd

import socket
from PiFinder import utils
Expand Down Expand Up @@ -152,19 +147,22 @@ def set_ap_name(self, ap_name):
def get_host_name(self):
return socket.gethostname()

def get_connected_ssid(self):
def get_connected_ssid(self) -> str:
"""
Returns the SSID of the connected wifi network or
None if not connected or in AP mode
"""
if self.wifi_mode() == "AP":
return None
return ""
# get output from iwgetid
iwgetid = sh.Command("iwgetid")
_t = iwgetid(_ok_code=(0, 255)).strip()
return _t.split(":")[-1].strip('"')
try:
iwgetid = sh.Command("iwgetid")
_t = iwgetid(_ok_code=(0, 255)).strip()
return _t.split(":")[-1].strip('"')
except sh.CommandNotFound:
return "ssid_not_found"

def set_host_name(self, hostname):
def set_host_name(self, hostname) -> None:
if hostname == self.get_host_name():
return
_result = sh.sudo("hostnamectl", "set-hostname", hostname)
Expand Down
91 changes: 48 additions & 43 deletions python/tests/test_sys_utils.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import pytest
from PiFinder import sys_utils

try:
from PiFinder import sys_utils

@pytest.mark.unit
def test_wpa_supplicant_parsing():
# This could be read from a file or passed from another function
wpa_supplicant_example = """
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
@pytest.mark.unit
def test_wpa_supplicant_parsing():
# This could be read from a file or passed from another function
wpa_supplicant_example = """
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid="My Home Network"
psk="password123"
key_mgmt=WPA-PSK
}
network={
ssid="My Home Network"
psk="password123"
key_mgmt=WPA-PSK
}
network={
ssid="Work Network"
psk="compl3x=p@ssw0rd!"
key_mgmt=WPA-PSK
}
"""
wpa_list = [
line.strip()
for line in wpa_supplicant_example.strip().split("\n")
if line.strip()
]
result = sys_utils.Network._parse_wpa_supplicant(wpa_list)
assert result[1]["psk"] == "compl3x=p@ssw0rd!"
network={
ssid="Work Network"
psk="compl3x=p@ssw0rd!"
key_mgmt=WPA-PSK
}
"""
wpa_list = [
line.strip()
for line in wpa_supplicant_example.strip().split("\n")
if line.strip()
]
result = sys_utils.Network._parse_wpa_supplicant(wpa_list)
assert result[1]["psk"] == "compl3x=p@ssw0rd!"

example2 = """
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
example2 = """
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
Expand All @@ -50,18 +51,22 @@ def test_wpa_supplicant_parsing():
network={
ssid="testytest"
psk="oesrucoeahu1234"
key_mgmt=WPA-PSK
}
network={
ssid="testytest"
psk="oesrucoeahu1234"
key_mgmt=WPA-PSK
}
network={
ssid="00xx33"
psk="1234@===!!!"
key_mgmt=WPA-PSK
}
"""
wpa_list = [line for line in example2.split("\n") if line.strip()]
result = sys_utils.Network._parse_wpa_supplicant(wpa_list)
assert result[1]["psk"] == "1234@===!!!"
network={
ssid="00xx33"
psk="1234@===!!!"
key_mgmt=WPA-PSK
}
"""
wpa_list = [line for line in example2.split("\n") if line.strip()]
result = sys_utils.Network._parse_wpa_supplicant(wpa_list)
assert result[1]["psk"] == "1234@===!!!"


except ImportError:
pass

0 comments on commit 215a162

Please sign in to comment.