Skip to content

Commit 181e12c

Browse files
committed
functionalize, cache, python>=3.9
1 parent cee0c6d commit 181e12c

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
python-version: [ '3.7', '3.11' ]
15+
python-version: [ '3.9', '3.11' ]
1616
name: Lint Python ${{ matrix.python-version }}
1717

1818
steps:

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "mozloc"
7-
version = "1.4.0"
7+
version = "1.5.0"
88
description = "Using Mozilla Location services, log location vs. time using WiFi or convert to KML."
99
keywords = ["wifi", "geolocation"]
1010
classifiers = ["Development Status :: 5 - Production/Stable",
@@ -18,7 +18,7 @@ classifiers = ["Development Status :: 5 - Production/Stable",
1818
"Topic :: System :: Networking",
1919
"Topic :: Utilities"
2020
]
21-
requires-python = ">=3.7"
21+
requires-python = ">=3.9"
2222
dynamic = ["readme"]
2323
dependencies = ["requests", "pandas"]
2424

src/mozloc/airport.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@
22

33
from __future__ import annotations
44
import typing as T
5-
import shutil
65
import logging
76
import subprocess
87
import re
98

10-
EXE = shutil.which(
11-
"airport",
12-
path="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources",
13-
)
14-
if not EXE:
15-
raise ImportError("Could not find Airport")
9+
from .cmd import get_airport
1610

1711

1812
def cli_config_check() -> bool:
1913
# %% check that Airport is available and WiFi is active
2014

21-
assert isinstance(EXE, str)
2215
try:
23-
ret = subprocess.check_output([EXE, "--getinfo"], text=True, timeout=30)
16+
ret = subprocess.check_output([get_airport(), "--getinfo"], text=True, timeout=30)
2417
except subprocess.CalledProcessError as err:
2518
logging.error(err)
2619
return False
@@ -38,9 +31,8 @@ def cli_config_check() -> bool:
3831

3932
def get_signal() -> str:
4033

41-
assert isinstance(EXE, str)
4234
try:
43-
ret = subprocess.check_output([EXE, "-s"], text=True, timeout=30.0)
35+
ret = subprocess.check_output([get_airport(), "-s"], text=True, timeout=30.0)
4436
except subprocess.CalledProcessError as err:
4537
logging.error(f"consider slowing scan cadence. {err}")
4638

src/mozloc/cmd.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
import functools
4+
import shutil
5+
6+
7+
@functools.cache
8+
def get_exe(name: str, path: str | None = None) -> str:
9+
if not (exe := shutil.which(name, path=path)):
10+
raise ImportError(f"{name} not found")
11+
return exe
12+
13+
14+
def get_airport() -> str:
15+
return get_exe(
16+
"airport",
17+
"/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources",
18+
)
19+
20+
21+
def get_nmcli() -> str:
22+
return get_exe("nmcli")
23+
24+
25+
def get_netsh() -> str:
26+
return get_exe("netsh")

src/mozloc/netman.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,18 @@
44
import typing as T
55
import subprocess
66
import logging
7-
import shutil
87
import pandas
98
import io
109
from time import sleep
1110

12-
EXE = shutil.which("nmcli")
13-
if not EXE:
14-
raise ImportError('Could not find NetworkManager "nmcli"')
15-
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"]
11+
from .cmd import get_nmcli
1912

2013

2114
def cli_config_check() -> bool:
2215
# %% check that NetworkManager CLI is available and WiFi is active
2316

24-
assert isinstance(EXE, str)
2517
try:
26-
ret = subprocess.check_output([EXE, "-t", "radio", "wifi"], text=True, timeout=2)
18+
ret = subprocess.check_output([get_nmcli(), "-t", "radio", "wifi"], text=True, timeout=2)
2719
except subprocess.CalledProcessError as err:
2820
logging.error(err)
2921
return False
@@ -45,15 +37,23 @@ def cli_config_check() -> bool:
4537

4638
def get_signal() -> str:
4739

40+
cmd = [get_nmcli(), "-g", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
41+
# Debian stretch, Ubuntu 18.04
42+
# cmd = [EXE, "-t", "-f", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
43+
# ubuntu 16.04
44+
4845
try:
49-
subprocess.check_call(NMCMD, timeout=1.0)
46+
subprocess.check_call(cmd, timeout=1.0)
5047
except subprocess.CalledProcessError as err:
5148
raise ConnectionError(f"could not connect with NetworkManager for WiFi {err}")
5249

5350
sleep(0.5) # nmcli errored for less than about 0.2 sec.
5451
# takes several seconds to update, so do it now.
52+
53+
scan = [get_nmcli(), "device", "wifi", "rescan"]
54+
5555
try:
56-
ret = subprocess.check_output(NMSCAN, timeout=1.0, text=True)
56+
ret = subprocess.check_output(scan, timeout=1.0, text=True)
5757
except subprocess.CalledProcessError as err:
5858
logging.error(f"consider slowing scan cadence. {err}")
5959

src/mozloc/netsh.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55
import subprocess
66
import logging
77
import io
8-
import shutil
98

10-
EXE = shutil.which("netsh")
11-
if not EXE:
12-
raise ImportError('Could not find NetSH "netsh"')
13-
14-
CMD = [EXE, "wlan", "show", "networks", "mode=bssid"]
9+
from .cmd import get_netsh
1510

1611

1712
def cli_config_check() -> bool:
1813
# %% check that NetSH EXE is available and WiFi is active
14+
cmd = [get_netsh(), "wlan", "show", "networks", "mode=bssid"]
15+
1916
try:
20-
ret = subprocess.check_output(CMD, text=True, timeout=2)
17+
ret = subprocess.check_output(cmd, text=True, timeout=2)
2118
except subprocess.CalledProcessError as err:
2219
logging.error(err)
2320
return False
@@ -43,8 +40,9 @@ def get_signal() -> str:
4340
returns dict of data parsed from EXE
4441
"""
4542

43+
cmd = [get_netsh(), "wlan", "show", "networks", "mode=bssid"]
4644
try:
47-
ret = subprocess.check_output(CMD, timeout=1.0, text=True)
45+
ret = subprocess.check_output(cmd, timeout=1.0, text=True)
4846
except subprocess.CalledProcessError as err:
4947
logging.error(f"consider slowing scan cadence. {err}")
5048

0 commit comments

Comments
 (0)