Skip to content

Commit

Permalink
simplify, type anno, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 11, 2024
1 parent 0c69286 commit f718163
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 43 deletions.
10 changes: 5 additions & 5 deletions src/mozloc/airport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Airport was removed from macOS 14.4+, so these functions no longer are relevant.
"""

from __future__ import annotations
import typing as T
import logging
import subprocess
import re

from .cmd import get_airport, running_as_root
import pandas

from .exe import get_airport, running_as_root


def cli_config_check() -> bool:
Expand Down Expand Up @@ -41,7 +41,7 @@ def get_signal() -> str:
return ret


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

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

return dat
return pandas.DataFrame(dat)
1 change: 0 additions & 1 deletion src/mozloc/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import annotations
from time import sleep
from pathlib import Path
import logging
Expand Down
10 changes: 0 additions & 10 deletions src/mozloc/cmd.py → src/mozloc/exe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

import os
import functools
import shutil
Expand Down Expand Up @@ -28,11 +26,3 @@ def get_airport() -> str:
raise EnvironmentError(msg)

return exe


def get_nmcli() -> str:
return get_exe("nmcli")


def get_netsh() -> str:
return get_exe("netsh")
18 changes: 10 additions & 8 deletions src/mozloc/netman.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
""" Network Manager CLI (nmcli) functions """

from __future__ import annotations
import typing as T
import subprocess
import logging
import pandas
import io
from time import sleep

from .cmd import get_nmcli
import pandas

from .exe import get_exe


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

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


def get_signal() -> str:
cmd = [get_nmcli(), "-g", "SSID,BSSID,FREQ,SIGNAL", "device", "wifi"]
exe = get_exe("nmcli")

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

scan = [get_nmcli(), "device", "wifi", "rescan"]
scan = [exe, "device", "wifi", "rescan"]

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


def parse_signal(raw: str) -> list[dict[str, T.Any]]:
def parse_signal(raw: str) -> pandas.DataFrame:
dat = pandas.read_csv(
io.StringIO(raw),
sep=r"(?<!\\):",
Expand Down
17 changes: 10 additions & 7 deletions src/mozloc/netsh.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
""" Windows NetSH functions """

from __future__ import annotations
import typing as T
import subprocess
import logging
import io

from .cmd import get_netsh
import pandas

from .exe import get_exe


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

cmd = [exe, "wlan", "show", "networks", "mode=bssid"]

try:
ret = subprocess.check_output(cmd, text=True, timeout=2)
Expand Down Expand Up @@ -39,8 +41,9 @@ def get_signal() -> str:
returns dict of data parsed from EXE
"""
exe = get_exe("netsh")

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


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

Expand Down Expand Up @@ -83,7 +86,7 @@ def parse_signal(raw: str) -> list[dict[str, T.Any]]:
break
break

return dat
return pandas.DataFrame(dat)


def signal_percent_to_dbm(percent: int) -> int:
Expand Down
18 changes: 6 additions & 12 deletions src/mozloc/web.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import requests
import logging
import pandas
import json
import typing as T
from datetime import datetime

import requests
import pandas


def get_loc_mozilla(dat: pandas.DataFrame, url: str):

def get_loc_mozilla(dat: T.Sequence[T.Any], url: str):
match dat:
case pandas.DataFrame():
json_to = dat.to_json(orient="records")
case list():
json_to = json.dumps(dat)
case _:
raise TypeError("Unknown data format")
json_to = dat.to_json(orient="records")

json_to = '{ "wifiAccessPoints":' + json_to + "}"
try:
Expand Down

0 comments on commit f718163

Please sign in to comment.