Skip to content

Commit f718163

Browse files
committed
simplify, type anno, lint
1 parent 0c69286 commit f718163

File tree

6 files changed

+31
-43
lines changed

6 files changed

+31
-43
lines changed

src/mozloc/airport.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Airport was removed from macOS 14.4+, so these functions no longer are relevant.
44
"""
55

6-
from __future__ import annotations
7-
import typing as T
86
import logging
97
import subprocess
108
import re
119

12-
from .cmd import get_airport, running_as_root
10+
import pandas
11+
12+
from .exe import get_airport, running_as_root
1313

1414

1515
def cli_config_check() -> bool:
@@ -41,7 +41,7 @@ def get_signal() -> str:
4141
return ret
4242

4343

44-
def parse_signal(raw: str) -> list[dict[str, T.Any]]:
44+
def parse_signal(raw: str) -> pandas.DataFrame:
4545
isroot = running_as_root()
4646

4747
psudo = r"\s*([0-9a-zA-Z\s\-\.]+)\s+([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+(-\d{2,3})"
@@ -68,4 +68,4 @@ def parse_signal(raw: str) -> list[dict[str, T.Any]]:
6868
d["macAddress"] = mat.group(ibssid)
6969
dat.append(d)
7070

71-
return dat
71+
return pandas.DataFrame(dat)

src/mozloc/base.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from __future__ import annotations
21
from time import sleep
32
from pathlib import Path
43
import logging

src/mozloc/cmd.py renamed to src/mozloc/exe.py

-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import annotations
2-
31
import os
42
import functools
53
import shutil
@@ -28,11 +26,3 @@ def get_airport() -> str:
2826
raise EnvironmentError(msg)
2927

3028
return exe
31-
32-
33-
def get_nmcli() -> str:
34-
return get_exe("nmcli")
35-
36-
37-
def get_netsh() -> str:
38-
return get_exe("netsh")

src/mozloc/netman.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
""" Network Manager CLI (nmcli) functions """
22

3-
from __future__ import annotations
4-
import typing as T
53
import subprocess
64
import logging
7-
import pandas
85
import io
96
from time import sleep
107

11-
from .cmd import get_nmcli
8+
import pandas
9+
10+
from .exe import get_exe
1211

1312

1413
def cli_config_check() -> bool:
1514
# %% check that NetworkManager CLI is available and WiFi is active
15+
exe = get_exe("nmcli")
1616

1717
try:
18-
ret = subprocess.check_output([get_nmcli(), "-t", "radio", "wifi"], text=True, timeout=2)
18+
ret = subprocess.check_output([exe, "-t", "radio", "wifi"], text=True, timeout=2)
1919
except subprocess.CalledProcessError as err:
2020
logging.error(err)
2121
return False
@@ -36,7 +36,9 @@ def cli_config_check() -> bool:
3636

3737

3838
def get_signal() -> str:
39-
cmd = [get_nmcli(), "-g", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
39+
exe = get_exe("nmcli")
40+
41+
cmd = [exe, "-g", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
4042
# Debian stretch, Ubuntu 18.04
4143
# cmd = [EXE, "-t", "-f", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
4244
# ubuntu 16.04
@@ -49,7 +51,7 @@ def get_signal() -> str:
4951
sleep(0.5) # nmcli errored for less than about 0.2 sec.
5052
# takes several seconds to update, so do it now.
5153

52-
scan = [get_nmcli(), "device", "wifi", "rescan"]
54+
scan = [exe, "device", "wifi", "rescan"]
5355

5456
try:
5557
ret = subprocess.check_output(scan, timeout=1.0, text=True)
@@ -59,7 +61,7 @@ def get_signal() -> str:
5961
return ret
6062

6163

62-
def parse_signal(raw: str) -> list[dict[str, T.Any]]:
64+
def parse_signal(raw: str) -> pandas.DataFrame:
6365
dat = pandas.read_csv(
6466
io.StringIO(raw),
6567
sep=r"(?<!\\):",

src/mozloc/netsh.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
""" Windows NetSH functions """
22

3-
from __future__ import annotations
4-
import typing as T
53
import subprocess
64
import logging
75
import io
86

9-
from .cmd import get_netsh
7+
import pandas
8+
9+
from .exe import get_exe
1010

1111

1212
def cli_config_check() -> bool:
1313
# %% check that NetSH EXE is available and WiFi is active
14-
cmd = [get_netsh(), "wlan", "show", "networks", "mode=bssid"]
14+
exe = get_exe("netsh")
15+
16+
cmd = [exe, "wlan", "show", "networks", "mode=bssid"]
1517

1618
try:
1719
ret = subprocess.check_output(cmd, text=True, timeout=2)
@@ -39,8 +41,9 @@ def get_signal() -> str:
3941
4042
returns dict of data parsed from EXE
4143
"""
44+
exe = get_exe("netsh")
4245

43-
cmd = [get_netsh(), "wlan", "show", "networks", "mode=bssid"]
46+
cmd = [exe, "wlan", "show", "networks", "mode=bssid"]
4447
try:
4548
ret = subprocess.check_output(cmd, timeout=1.0, text=True)
4649
except subprocess.CalledProcessError as err:
@@ -49,7 +52,7 @@ def get_signal() -> str:
4952
return ret
5053

5154

52-
def parse_signal(raw: str) -> list[dict[str, T.Any]]:
55+
def parse_signal(raw: str) -> pandas.DataFrame:
5356
dat: list[dict[str, str]] = []
5457
out = io.StringIO(raw)
5558

@@ -83,7 +86,7 @@ def parse_signal(raw: str) -> list[dict[str, T.Any]]:
8386
break
8487
break
8588

86-
return dat
89+
return pandas.DataFrame(dat)
8790

8891

8992
def signal_percent_to_dbm(percent: int) -> int:

src/mozloc/web.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import requests
21
import logging
3-
import pandas
4-
import json
5-
import typing as T
62
from datetime import datetime
73

4+
import requests
5+
import pandas
6+
7+
8+
def get_loc_mozilla(dat: pandas.DataFrame, url: str):
89

9-
def get_loc_mozilla(dat: T.Sequence[T.Any], url: str):
10-
match dat:
11-
case pandas.DataFrame():
12-
json_to = dat.to_json(orient="records")
13-
case list():
14-
json_to = json.dumps(dat)
15-
case _:
16-
raise TypeError("Unknown data format")
10+
json_to = dat.to_json(orient="records")
1711

1812
json_to = '{ "wifiAccessPoints":' + json_to + "}"
1913
try:

0 commit comments

Comments
 (0)