99import io
1010from time import sleep
1111
12- NMCLI = shutil .which ("nmcli" )
13- if not NMCLI :
12+ EXE = shutil .which ("nmcli" )
13+ if not EXE :
1414 raise ImportError ('Could not find NetworkManager "nmcli"' )
1515
16- NMCMD = [NMCLI , "-g" , "SSID,BSSID,FREQ,SIGNAL" , "device" , "wifi" ] # Debian stretch, Ubuntu 18.04
17- NMLEG = [NMCLI , "-t" , "-f" , "SSID,BSSID,FREQ,SIGNAL" , "device" , "wifi" ] # ubuntu 16.04
18- NMSCAN = [NMCLI , "device" , "wifi" , "rescan" ]
16+ NMCMD = [EXE , "-g" , "SSID,BSSID,FREQ,SIGNAL" , "device" , "wifi" ] # Debian stretch, Ubuntu 18.04
17+ NMLEG = [EXE , "-t" , "-f" , "SSID,BSSID,FREQ,SIGNAL" , "device" , "wifi" ] # ubuntu 16.04
18+ NMSCAN = [EXE , "device" , "wifi" , "rescan" ]
1919
2020
2121def cli_config_check () -> bool :
2222 # %% check that NetworkManager CLI is available and WiFi is active
23- ret = subprocess .run ([NMCLI , "-t" , "radio" , "wifi" ], stdout = subprocess .PIPE , text = True , timeout = 2 )
2423
25- if ret .returncode != 0 :
24+ assert isinstance (EXE , str )
25+ try :
26+ ret = subprocess .check_output ([EXE , "-t" , "radio" , "wifi" ], text = True , timeout = 2 )
27+ except subprocess .CalledProcessError as err :
28+ logging .error (err )
2629 return False
2730
28- stdout = ret .stdout . strip ().split (":" )
31+ stdout = ret .strip ().split (":" )
2932 if "enabled" in stdout :
3033 return True
3134
@@ -42,16 +45,19 @@ def cli_config_check() -> bool:
4245
4346def get_signal () -> str :
4447
45- ret = subprocess .run (NMCMD , timeout = 1.0 )
46- if ret .returncode != 0 :
47- raise ConnectionError ("could not connect with NetworkManager for WiFi" )
48+ try :
49+ subprocess .check_call (NMCMD , timeout = 1.0 )
50+ except subprocess .CalledProcessError as err :
51+ raise ConnectionError (f"could not connect with NetworkManager for WiFi { err } " )
52+
4853 sleep (0.5 ) # nmcli errored for less than about 0.2 sec.
4954 # takes several seconds to update, so do it now.
50- ret = subprocess .run (NMSCAN , timeout = 1.0 , stdout = subprocess .PIPE , text = True )
51- if ret .returncode != 0 :
52- logging .error ("consider slowing scan cadence." )
55+ try :
56+ ret = subprocess .check_output (NMSCAN , timeout = 1.0 , text = True )
57+ except subprocess .CalledProcessError as err :
58+ logging .error (f"consider slowing scan cadence. { err } " )
5359
54- return ret . stdout
60+ return ret
5561
5662
5763def parse_signal (raw : str ) -> list [dict [str , T .Any ]]:
0 commit comments