Two additions, both about turning a number into something meaningful. A band column. Next to every frequency -- on the live display, in the line-per-hit output, in saunterbrowse's list and details -- is the name of the band it falls in. 421 MHz is the 70 cm amateur band, and being told so is quicker than remembering where the edges are. The names come from the existing preset table, so there is one band plan to keep right rather than two, but naming is not the job that table was shaped for: several presets cover any frequency, some of them whole-tuner sweeps that say nothing. So the candidates are ranked. Sweeps and the "-complete" duplicates are dropped outright. The narrowest of what is left wins, because it says the most -- 146.52 MHz comes back as the 2 m simplex calling channel rather than as the whole 2 m band. Two exceptions where the narrowest would be the wrong answer: ISM yields to the allocation it shares (433.92 is 70 cm first, 915 is 33 cm first), and shortwave broadcast yields to amateur where the two overlap, because 3.9-4.0 and 7.2-7.3 MHz are Region 1 and 3 broadcast but Region 2 amateur, and this plan is documented as Region 2. 6 MHz really is 49 m shortwave and is left alone. The name is written into each capture's sidecar, so it travels with the recording and an edit to the plan later cannot rewrite history, and saunterbrowse searches on it: /70 cm finds the band without anyone having to remember 420-450 MHz. A map. A licence says where its holder is, so a list of callsigns is also a map. Callsigns heard during a scan are now looked up as the transcripts come in, announced on the display, and written to callsigns.kml in the output directory; saunterbrowse --kml builds the same file from recordings already on disk, and the two continue one map rather than starting two. One placemark per station, not one per transmission: the same repeater heard twenty times in an evening is one operator, and twenty pins on one rooftop would say less than one. Each pin carries the callsign, the licensee, the town, the grid square, and every frequency and time it was heard on. The file is read back on open and added to, so later scans build it up rather than replacing it. Where a licence has no coordinates the grid square's centre is used and the placemark says so -- a square is kilometres across where an address is a street. A callsign with no licence at all is still recorded, in a folder that starts switched off, because that a station was heard is worth keeping even when nothing says where. A file already there that is not readable as KML is never overwritten. Also fixed along the way: - The hit list's "no signals recorded yet" placeholder was one cell short of its row, so it landed in the SNR column and wrapped, making the panel taller than the layout had budgeted for and scrolling the display off a short terminal. The identification column can no longer wrap either, which is what _hit_capacity has always assumed. - Licence lookups now record coordinates. The cache is versioned so that entries written before this are asked about again, rather than pinning every station to its grid square for good. - CallsignBook.wait dropped joined threads; an all-night scan calls it after every transcript and the list only ever grew. - Tests redirect XDG_CACHE_HOME, so a run no longer reads or writes the real lookup cache. 676 -> 761 tests.
419 lines
15 KiB
Python
419 lines
15 KiB
Python
"""Finding callsigns in a transcript, and saying who they belong to.
|
|
|
|
The hard part is not the regular expression, it is everything the speech
|
|
recogniser does to a callsign on the way: it breaks it wherever the speaker
|
|
paused, and it writes the phonetic alphabet down as words. So most of these
|
|
are about text that came out of a real recogniser rather than text anyone
|
|
would type.
|
|
|
|
Nothing here touches the network. The lookup is tested against a stub, and
|
|
one test asserts that no request is made when it is switched off.
|
|
"""
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from bandsaunter.callsign import (Callsign, CallsignBook, HEADING,
|
|
NOT_CALLSIGNS, SHAPE, describe_prefix,
|
|
find_callsigns, person_case, report,
|
|
split_postcode)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def no_network(monkeypatch):
|
|
"""Nothing here may contact the licence database for real.
|
|
|
|
Every lookup test uses a stub; this makes forgetting one fail loudly
|
|
rather than quietly pass with somebody's real address in the output.
|
|
"""
|
|
def refuse(self, call):
|
|
raise AssertionError(f"a test tried to look up {call} for real")
|
|
|
|
monkeypatch.setattr(CallsignBook, "_request", refuse)
|
|
|
|
|
|
# -- finding them ------------------------------------------------------------
|
|
|
|
@pytest.mark.parametrize("text,want", [
|
|
("This is W1AW calling CQ", ["W1AW"]),
|
|
("K7RA this is N7XYZ over", ["K7RA", "N7XYZ"]),
|
|
("VE3ABC and G0ABC and 2E0XYZ", ["VE3ABC", "G0ABC", "2E0XYZ"]),
|
|
("net control, W1AW, W1AW again", ["W1AW"]),
|
|
])
|
|
def test_a_callsign_written_properly_is_found(text, want):
|
|
assert find_callsigns(text) == want
|
|
|
|
|
|
@pytest.mark.parametrize("text,want", [
|
|
("Alright, KU 0W. And the HF net will be a couple minutes.", ["KU0W"]),
|
|
("N7 XYZ this is K7 RA", ["N7XYZ", "K7RA"]),
|
|
("W 1 A W standing by", ["W1AW"]),
|
|
])
|
|
def test_a_callsign_broken_across_words_is_still_one_callsign(text, want):
|
|
"""A recogniser breaks it wherever the speaker paused. "KU 0W" is what
|
|
came out of a real recording of somebody saying KU0W."""
|
|
assert find_callsigns(text) == want
|
|
|
|
|
|
@pytest.mark.parametrize("text,want", [
|
|
("kilo uniform zero whiskey, are you there", ["KU0W"]),
|
|
("whiskey one alpha whiskey this is kilo seven romeo alpha",
|
|
["W1AW", "K7RA"]),
|
|
("november seven x-ray yankee zulu", ["N7XYZ"]),
|
|
("kilo uniform zero whisky", ["KU0W"]), # both spellings
|
|
("niner", []),
|
|
])
|
|
def test_the_phonetic_alphabet_is_read_back(text, want):
|
|
"""Spelled out, a callsign arrives as one token per character."""
|
|
assert find_callsigns(text) == want
|
|
|
|
|
|
@pytest.mark.parametrize("text", [
|
|
"7 on alleviate or 3. Can you open 4 and close the rest.",
|
|
"CC1 boy",
|
|
"I bought 3 of them for $5 and a B4U model MP3 player",
|
|
"the 4th Saturday at 1.30pm on 7.242 megahertz",
|
|
"Check out communication of group activities and training.",
|
|
"we need 2 of them and 4 spare",
|
|
"it was on channel 3 for a while",
|
|
])
|
|
def test_ordinary_speech_produces_no_callsigns(text):
|
|
"""Every one of these came out of a real transcript or is one word away
|
|
from something that did. A browser that invents callsigns is worse than
|
|
one that finds none."""
|
|
assert find_callsigns(text) == []
|
|
|
|
|
|
def test_a_join_is_refused_when_a_part_is_an_english_word():
|
|
""""or 3. Can you" fits the shape once the punctuation is gone. Nobody
|
|
spells a callsign with English words."""
|
|
assert find_callsigns("or 3 can") == []
|
|
assert find_callsigns("CC1 boy") == []
|
|
# ... but a single token said in one breath is trusted.
|
|
assert find_callsigns("W1BOY is on frequency") == ["W1BOY"]
|
|
|
|
|
|
def test_a_lone_article_is_not_a_prefix():
|
|
assert find_callsigns("a B4U player") == []
|
|
assert find_callsigns("alpha bravo four uniform") == ["AB4U"]
|
|
|
|
|
|
def test_the_same_callsign_is_reported_once():
|
|
assert find_callsigns("W1AW W1AW W1AW") == ["W1AW"]
|
|
|
|
|
|
def test_they_come_back_in_the_order_they_were_said():
|
|
assert find_callsigns("N7XYZ this is W1AW") == ["N7XYZ", "W1AW"]
|
|
|
|
|
|
def test_the_count_is_capped():
|
|
text = " ".join(f"W{i}AB" for i in range(0, 9)) + " K7RA N7XYZ VE3ABC"
|
|
assert len(find_callsigns(text, max_found=4)) == 4
|
|
|
|
|
|
def test_empty_text_is_not_an_error():
|
|
assert find_callsigns("") == []
|
|
assert find_callsigns(" \n ") == []
|
|
|
|
|
|
def test_every_excluded_word_could_actually_have_matched():
|
|
"""A list of words that could not match anyway would only suggest the
|
|
filter was doing more work than it is."""
|
|
for word in NOT_CALLSIGNS:
|
|
assert SHAPE.match(word), f"{word} never matched in the first place"
|
|
|
|
|
|
# -- what the callsign says about itself -------------------------------------
|
|
|
|
@pytest.mark.parametrize("call,country", [
|
|
("W1AW", "United States"), ("KU0W", "United States"),
|
|
("N7XYZ", "United States"), ("AA1AB", "United States"),
|
|
("VE3ABC", "Canada"), ("XE1ABC", "Mexico"),
|
|
("G0ABC", "United Kingdom"), ("2E0XYZ", "United Kingdom"),
|
|
("JA1ABC", "Japan"), ("VK2DEF", "Australia"),
|
|
("IK4ABC", "Italy"), ("DL1ABC", "Germany"),
|
|
("4X4ABC", "Israel"), ("ZZ9ZZZ", ""),
|
|
])
|
|
def test_the_prefix_names_the_country_without_a_database(call, country):
|
|
"""This works offline and for callsigns no database holds."""
|
|
assert describe_prefix(call)[0] == country
|
|
|
|
|
|
def test_a_one_letter_prefix_does_not_claim_everything():
|
|
""""I" is Italy, but only when what follows is the rest of a prefix."""
|
|
assert describe_prefix("IK4ABC")[0] == "Italy"
|
|
assert describe_prefix("KU0W")[0] == "United States" # not Italy-shaped
|
|
|
|
|
|
def test_the_digit_gives_the_us_district():
|
|
assert "New England" in describe_prefix("W1AW")[1]
|
|
assert "California" in describe_prefix("K6ABC")[1]
|
|
assert describe_prefix("VE3ABC")[1] == "", "districts are a US idea"
|
|
|
|
|
|
# -- tidying the licence record ----------------------------------------------
|
|
|
|
@pytest.mark.parametrize("raw,want", [
|
|
("ROD R GOWDY", "Rod R Gowdy"),
|
|
("PAUL PAKES COOK, III.", "Paul Pakes Cook, III."),
|
|
("ARRL HQ OPERATORS CLUB", "ARRL HQ Operators Club"),
|
|
("JOHN SMITH JR", "John Smith JR"),
|
|
("de Vries, Anna", "de Vries, Anna"), # already mixed: left alone
|
|
])
|
|
def test_licence_names_are_made_readable_without_being_corrupted(raw, want):
|
|
assert person_case(raw) == want
|
|
|
|
|
|
@pytest.mark.parametrize("raw,town,code", [
|
|
("TUCSON, AZ 85742", "Tucson, AZ", "85742"),
|
|
("SEATTLE, WA 98105-3505", "Seattle, WA", "98105-3505"),
|
|
("NEWINGTON, CT 06111", "Newington, CT", "06111"),
|
|
("SOMEWHERE ABROAD", "Somewhere Abroad", ""),
|
|
])
|
|
def test_the_postcode_is_separated_from_the_place(raw, town, code):
|
|
"""The town and state say where somebody is; the postcode is detail."""
|
|
assert split_postcode(raw) == (town, code)
|
|
|
|
|
|
# -- looking them up ---------------------------------------------------------
|
|
|
|
VALID = {
|
|
"status": "VALID", "type": "PERSON", "name": "ROD R GOWDY",
|
|
"current": {"callsign": "KU0W", "operClass": "EXTRA"},
|
|
"previous": {"callsign": "KK7QPA"},
|
|
"trustee": {"callsign": ""},
|
|
"address": {"line1": "5455 W OASIS RD", "line2": "TUCSON, AZ 85742"},
|
|
"location": {"gridsquare": "DM42lj"},
|
|
"otherInfo": {"expiryDate": "08/09/2034"},
|
|
}
|
|
|
|
|
|
class StubBook(CallsignBook):
|
|
"""A book whose lookups are answered from a dict, not the network."""
|
|
|
|
def __init__(self, answers, **kw):
|
|
self.answers = answers
|
|
self.requested = []
|
|
super().__init__(**kw)
|
|
|
|
def _request(self, call):
|
|
self.requested.append(call)
|
|
if call not in self.answers:
|
|
raise OSError("no route to host")
|
|
return self.answers[call]
|
|
|
|
|
|
def book(tmp_path, answers=None, **kw):
|
|
return StubBook(answers if answers is not None else {"KU0W": VALID},
|
|
cache=tmp_path / "cache.json", **kw)
|
|
|
|
|
|
def test_a_lookup_fills_in_the_name_and_the_place(tmp_path):
|
|
b = book(tmp_path)
|
|
b.get("KU0W")
|
|
b.wait(5)
|
|
entry = b.get("KU0W")
|
|
assert entry.known
|
|
assert entry.name == "Rod R Gowdy"
|
|
assert entry.location == "Tucson, AZ"
|
|
assert entry.grid == "DM42lj"
|
|
assert entry.oper_class == "EXTRA"
|
|
assert entry.previous == "KK7QPA"
|
|
|
|
|
|
def test_rendering_never_waits_for_the_network(tmp_path):
|
|
"""A lookup returns at once with what is known, and fills itself in.
|
|
|
|
The stub is deliberately slow: a fast one would finish before the
|
|
assertion and the test would pass whether or not the call blocked.
|
|
"""
|
|
class Slow(StubBook):
|
|
def _request(self, call):
|
|
time.sleep(1.0)
|
|
return super()._request(call)
|
|
|
|
b = Slow({"KU0W": VALID}, cache=tmp_path / "cache.json")
|
|
started = time.time()
|
|
entry = b.get("KU0W")
|
|
assert time.time() - started < 0.3, "the lookup blocked the caller"
|
|
assert entry.status == "pending"
|
|
assert entry.summary() == "looking up…"
|
|
b.wait(10)
|
|
assert b.get("KU0W").known
|
|
|
|
|
|
def test_an_unknown_callsign_says_what_the_prefix_says(tmp_path):
|
|
"""Not in the database is not nothing: the prefix is still information."""
|
|
b = book(tmp_path, {"G0ABC": {"status": "INVALID"}})
|
|
b.get("G0ABC")
|
|
b.wait(5)
|
|
summary = b.get("G0ABC").summary()
|
|
assert "unlisted" in summary and "United Kingdom" in summary
|
|
|
|
|
|
def test_a_lookup_that_fails_is_not_an_error(tmp_path):
|
|
"""Offline, blocked or rate-limited, the transcript still has to show."""
|
|
b = book(tmp_path, {})
|
|
b.get("W1AW")
|
|
b.wait(5)
|
|
entry = b.get("W1AW")
|
|
assert entry.status == "offline"
|
|
assert "United States" in entry.summary()
|
|
|
|
|
|
def test_nothing_is_requested_when_lookup_is_switched_off(tmp_path):
|
|
b = book(tmp_path, online=False)
|
|
entry = b.get("KU0W")
|
|
b.wait(2)
|
|
assert b.requested == [], "a request was made with lookup off"
|
|
assert entry.status == "offline"
|
|
assert "United States" in entry.summary()
|
|
|
|
|
|
def test_a_callsign_is_looked_up_once(tmp_path):
|
|
b = book(tmp_path)
|
|
for _ in range(5):
|
|
b.get("KU0W")
|
|
b.wait(5)
|
|
assert b.requested == ["KU0W"]
|
|
|
|
|
|
def test_results_are_cached_between_runs(tmp_path):
|
|
"""The same net, logged night after night, is looked up once."""
|
|
first = book(tmp_path)
|
|
first.get("KU0W")
|
|
first.wait(5)
|
|
first.save()
|
|
assert (tmp_path / "cache.json").exists()
|
|
|
|
second = book(tmp_path)
|
|
entry = second.get("KU0W")
|
|
assert entry.known, "the cache was not read"
|
|
assert second.requested == [], "a cached callsign was looked up again"
|
|
|
|
|
|
def test_a_stale_cache_entry_is_looked_up_again(tmp_path):
|
|
first = book(tmp_path)
|
|
first.get("KU0W")
|
|
first.wait(5)
|
|
first.save()
|
|
body = json.loads((tmp_path / "cache.json").read_text())
|
|
body["KU0W"]["fetched_at"] = time.time() - 400 * 86_400
|
|
(tmp_path / "cache.json").write_text(json.dumps(body))
|
|
|
|
second = book(tmp_path)
|
|
second.get("KU0W")
|
|
second.wait(5)
|
|
assert second.requested == ["KU0W"]
|
|
|
|
|
|
def test_a_cache_from_another_version_is_ignored_not_fatal(tmp_path):
|
|
(tmp_path / "cache.json").write_text(
|
|
json.dumps({"KU0W": {"call": "KU0W", "somethingelse": 1}}))
|
|
b = book(tmp_path)
|
|
b.get("KU0W")
|
|
b.wait(5)
|
|
assert b.requested == ["KU0W"], "an unusable cache entry was trusted"
|
|
assert b.get("KU0W").known
|
|
|
|
|
|
def test_an_unreadable_cache_is_ignored_not_fatal(tmp_path):
|
|
(tmp_path / "cache.json").write_text("{not json")
|
|
b = book(tmp_path)
|
|
b.get("KU0W")
|
|
b.wait(5)
|
|
assert b.get("KU0W").known
|
|
|
|
|
|
def test_only_resolved_entries_are_cached(tmp_path):
|
|
"""Caching a failure would make one flaky lookup permanent."""
|
|
b = book(tmp_path, {})
|
|
b.get("W1AW")
|
|
b.wait(5)
|
|
b.save()
|
|
body = json.loads((tmp_path / "cache.json").read_text()) \
|
|
if (tmp_path / "cache.json").exists() else {}
|
|
assert "W1AW" not in body
|
|
|
|
|
|
# -- the block that goes under the transcript --------------------------------
|
|
|
|
def test_the_report_is_headed_the_way_it_was_asked_for():
|
|
entry = Callsign(call="W1AW", name="ARRL", location="Newington, CT",
|
|
status="found")
|
|
lines = report([entry])
|
|
assert lines[0] == HEADING == "DETECTED CALLSIGNS:"
|
|
assert "W1AW" in lines[1] and "Newington" in lines[1]
|
|
|
|
|
|
def test_nothing_found_means_no_block_at_all():
|
|
assert report([]) == []
|
|
|
|
|
|
def test_the_report_carries_the_extra_detail():
|
|
entry = Callsign(call="KU0W", name="Rod R Gowdy", location="Tucson, AZ",
|
|
oper_class="EXTRA", grid="DM42lj", status="found")
|
|
line = report([entry], width=0)[1]
|
|
assert "Extra" in line and "DM42lj" in line
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Coordinates, and the cache that holds them
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_a_lookup_records_where_the_licence_says_the_station_is(monkeypatch):
|
|
"""The map is built from these, so they have to survive the parse."""
|
|
body = {"status": "VALID", "name": "ARRL HQ OPERATORS CLUB",
|
|
"address": {"line2": "NEWINGTON, CT 06111"},
|
|
"location": {"latitude": "41.714775", "longitude": "-72.727260",
|
|
"gridsquare": "FN31pr"}}
|
|
entry = Callsign(call="W1AW")
|
|
CallsignBook._apply(entry, body)
|
|
assert entry.position == pytest.approx((41.714775, -72.727260))
|
|
assert not entry.coarse_position
|
|
|
|
|
|
def test_a_licence_with_only_a_grid_square_falls_back_to_it(monkeypatch):
|
|
body = {"status": "VALID", "name": "SOMEONE",
|
|
"address": {"line2": "SOMEWHERE, AZ 85742"},
|
|
"location": {"gridsquare": "DM42lj"}}
|
|
entry = Callsign(call="KU0W")
|
|
CallsignBook._apply(entry, body)
|
|
assert entry.position is not None
|
|
# and it says the position is only as good as the square
|
|
assert entry.coarse_position
|
|
|
|
|
|
def test_a_licence_with_no_position_at_all_has_none():
|
|
body = {"status": "VALID", "name": "SOMEONE", "address": {}, "location": {}}
|
|
entry = Callsign(call="K7XYZ")
|
|
CallsignBook._apply(entry, body)
|
|
assert entry.position is None
|
|
assert not entry.coarse_position
|
|
|
|
|
|
def test_an_entry_cached_before_coordinates_is_looked_up_again(tmp_path):
|
|
"""Otherwise a warm cache pins every station to its grid square for good."""
|
|
from bandsaunter.callsign import CACHE_VERSION
|
|
cache = tmp_path / "callsigns.json"
|
|
cache.write_text(json.dumps({"W1AW": {
|
|
"call": "W1AW", "name": "Arrl Hq Operators Club", "status": "found",
|
|
"grid": "FN31pr", "fetched_at": time.time()}}))
|
|
book = CallsignBook(online=False, cache=cache)
|
|
assert "W1AW" not in book._entries
|
|
|
|
cache.write_text(json.dumps({"W1AW": {
|
|
"call": "W1AW", "name": "Arrl Hq Operators Club", "status": "found",
|
|
"grid": "FN31pr", "fetched_at": time.time(),
|
|
"version": CACHE_VERSION}}))
|
|
assert "W1AW" in CallsignBook(online=False, cache=cache)._entries
|
|
|
|
|
|
def test_an_unlisted_callsign_is_cached_too(tmp_path, monkeypatch):
|
|
"""Asking again every run about a callsign with no licence is waste."""
|
|
from bandsaunter.callsign import CACHE_VERSION
|
|
entry = Callsign(call="XX9ZZ")
|
|
CallsignBook._apply(entry, {"status": "INVALID"})
|
|
assert entry.status == "unlisted"
|
|
assert entry.version == CACHE_VERSION
|