Let a season be an estimate: the validator takes a method ending in its confidence and a climate source, refuses an ordinary page naming it, and requires a season on every trail

This commit is contained in:
swaits committed 2026-11-20 12:55:30 -07:00
1 parent 748186d306
commit ebccfb2e33
5 files changed
+160 -32

No files matched your search

+2 -2
View File
@@ -412,7 +412,7 @@ def sourced_trails(root, fetch, log=None):
out = []
for tid, rec in _records(root):
hike = rec.get("hike") or {}
if not hike.get("season") or hike.get("season_basis") == "estimate":
if not season.read_from_a_page(hike):
continue
points, kind = sample_points(root, rec)
med = season_model.medians(normals_at(points, fetch))
@@ -622,7 +622,7 @@ def _phrases(root):
out = {}
for tid, rec in _records(root):
hike = rec.get("hike") or {}
if hike.get("season") and hike.get("season_basis") != "estimate":
if season.read_from_a_page(hike):
out[tid] = str(hike.get("season"))
_PHRASES[root] = out
return _PHRASES[root]
+60 -4
View File
@@ -4,9 +4,41 @@ Python 3 standard library only. tools/validate.py imports this module; it
must not import validate.py. The fields are documented in docs/schema.md.
"""
import re
import urllib.parse
BASES = ("primary", "sources", "source", "estimate")
PROVENANCE_KEYS = frozenset({"season_basis", "season_method"})
SEASON_REQUIRED = False
SEASON_REQUIRED = True
# The host of the climate data an estimate cites (tools/season_climate.SOURCE).
CLIMATE_HOSTS = frozenset({"www.chelsa-climate.org"})
CONFIDENCES = ("high", "medium", "low")
_CONFIDENCE_END = re.compile(r"Confidence: (high|medium|low)\.\s*$")
def _is_climate(source):
host = urllib.parse.urlparse(str(source.get("url", ""))).hostname
return host in CLIMATE_HOSTS
def confidence_of(method):
""""high", "medium" or "low" from the end of a season_method, else ""."""
found = _CONFIDENCE_END.search(str(method or ""))
return found.group(1) if found else ""
def read_from_a_page(hike):
"""True when a page states the season: one is present, its basis is not
"estimate", and no method records that it was worked out by judgement
(the seasons set from pages that disagree carry one)."""
return bool(hike.get("season")) and hike.get("season_basis") in (
"primary", "sources", "source") and not hike.get("season_method")
def check_present(tid, rec, errors):
"""Every record has a hike table (check_season then requires its season)."""
if not isinstance(rec.get("hike"), dict):
errors.append(f"{tid}: hike.season is required")
def _naming(sources, fact):
@@ -51,10 +83,18 @@ def check_season(tid, hike, errors):
errors.append(f"{tid}: hike.season_method must not be empty")
elif len(method) >= 400:
errors.append(f"{tid}: hike.season_method must be under 400 characters")
elif basis == "estimate" and not _CONFIDENCE_END.search(method):
errors.append(
f"{tid}: hike.season_method of an estimate must end with "
"\"Confidence: high.\", \"Confidence: medium.\" or \"Confidence: low.\"")
elif basis == "estimate":
errors.append(f"{tid}: hike.season_method is required with season_basis \"estimate\"")
naming_season = _naming(sources, "season")
if basis != "estimate" and any(_is_climate(x) for x in naming_season):
errors.append(
f"{tid}: a source naming season may not cite climate data unless "
"hike.season_basis is \"estimate\"")
if basis in ("primary", "source"):
if not naming_season:
errors.append(
@@ -69,16 +109,32 @@ def check_season(tid, hike, errors):
elif basis == "sources":
publishers = {str(s.get("publisher", "")).strip().casefold()
for s in naming_season if str(s.get("publisher", "")).strip()}
if len(publishers) < 2:
if not naming_season:
errors.append(
f"{tid}: hike.season_basis \"sources\" needs a [[hike.source]] naming season")
elif has_method:
pass # set by judgement from pages that differ; they may share a publisher
elif len(publishers) < 2:
errors.append(
f"{tid}: hike.season_basis \"sources\" needs two sources from different "
"publishers naming season")
elif basis == "estimate":
naming = {id(s) for s in _naming(sources, "season") + _naming(sources, "months")}
naming = {id(x): x for x in _naming(sources, "season") + _naming(sources, "months")}
if len(naming) != 1:
errors.append(
f"{tid}: hike.season_basis \"estimate\" needs exactly one source naming "
"season or months")
"season or months, and it must cite the climate data (no page states a season)")
else:
only = next(iter(naming.values()))
facts = only.get("facts") or []
if not _is_climate(only):
errors.append(
f"{tid}: hike.season_basis \"estimate\" needs its source to cite the "
"climate data (CHELSA, with ETOPO); an ordinary page naming the season "
"makes the basis wrong")
if "season" not in facts or "months" not in facts:
errors.append(
f"{tid}: the climate source of an estimate must name both season and months")
# ---------------------------------------------------------------------------
+94 -23
View File
@@ -20,6 +20,22 @@ def src(publisher, facts=("season", "months"), url="https://example.org/trail"):
return {"url": url, "publisher": publisher, "facts": list(facts)}
CLIMATE_URL = "https://www.chelsa-climate.org/datasets/chelsa_climatologies"
CLIMATE = "WSL (CHELSA) and NOAA NCEI (ETOPO)"
METHOD = "Snow clears from the high ground in June. Confidence: medium."
def climate_src(**kw):
return src(CLIMATE, url=CLIMATE_URL, **kw)
def estimate(**fields):
fields.setdefault("season_basis", "estimate")
fields.setdefault("season_method", METHOD)
fields.setdefault("source", [climate_src()])
return check(**fields)
def check(**fields):
hike = {
"season": "June to September",
@@ -98,42 +114,97 @@ class SeasonBasisTest(unittest.TestCase):
errors = check(source=[src("Park Service", facts=("days",))])
self.assertTrue(errors)
def test_estimate_needs_a_method_and_refuses_one_otherwise(self):
self.assertTrue(check(season_basis="estimate"))
def test_an_estimate_with_a_method_and_a_climate_source_passes(self):
self.assertEqual(estimate(), [])
def test_an_estimate_needs_a_method(self):
errors = estimate(season_method=None)
self.assertTrue(any("season_method" in e for e in errors), errors)
self.assertTrue(estimate(season_method=""))
self.assertTrue(estimate(season_method="x" * 401))
def test_an_estimate_method_ends_with_its_confidence(self):
for word in ("high", "medium", "low"):
self.assertEqual(estimate(season_method=f"Set by snow. Confidence: {word}."), [])
errors = estimate(season_method="Set by snow.")
self.assertTrue(any("Confidence" in e for e in errors), errors)
self.assertTrue(estimate(season_method="Set by snow. Confidence: certain."))
def test_a_method_without_an_estimate_is_still_an_error_on_a_primary(self):
self.assertTrue(check(season_method=METHOD))
def test_an_estimate_refuses_an_ordinary_page_naming_the_season(self):
two = [climate_src(), src("Park Service", url="https://example.org/p")]
errors = estimate(source=two)
self.assertTrue(errors)
only = [src("Park Service", url="https://example.org/p")]
errors = estimate(source=only)
self.assertTrue(any("climate" in e for e in errors), errors)
def test_an_estimate_with_no_naming_source_is_an_error(self):
self.assertTrue(estimate(source=[climate_src(facts=("days",))]))
def test_an_estimate_source_names_both_season_and_months(self):
errors = estimate(source=[climate_src(facts=("season",))])
self.assertTrue(any("months" in e for e in errors), errors)
def test_a_sourced_season_may_not_cite_the_climate_data(self):
errors = check(source=[climate_src()])
self.assertTrue(any("climate" in e for e in errors), errors)
def test_the_climate_host_is_the_one_the_estimator_reads(self):
import urllib.parse
import season_climate
self.assertEqual(
check(season_basis="estimate", season_method="Estimated from climate normals."), [])
self.assertTrue(check(season_basis="estimate", season_method=""))
self.assertTrue(check(season_basis="estimate", season_method="x" * 401))
self.assertTrue(check(season_method="Estimated from climate normals."))
season.CLIMATE_HOSTS,
frozenset({urllib.parse.urlparse(season_climate.SOURCE["url"]).hostname}))
def test_estimate_with_two_naming_sources_is_an_error(self):
two = [src("Climate", url="https://c.example/1"), src("Other", url="https://o.example/1")]
errors = check(season_basis="estimate", season_method="Estimated from climate.", source=two)
def test_sources_judged_from_pages_that_differ_may_share_a_publisher(self):
two = [src("The Trek", url="https://t.example/1"), src("The Trek", url="https://t.example/2")]
errors = check(season_basis="sources", source=two)
self.assertTrue(errors)
self.assertEqual(
check(season_basis="sources", source=two,
season_method="The pages differ. Confidence: low."), [])
def test_judged_from_pages_that_differ_needs_a_page(self):
errors = check(season_basis="sources", source=[src("The Trek", facts=("days",))],
season_method="The pages differ. Confidence: low.")
self.assertTrue(errors)
def test_estimate_with_no_naming_source_is_an_error(self):
errors = check(season_basis="estimate", season_method="Estimated from climate.",
source=[src("Climate", facts=("days",))])
def test_a_season_read_from_a_page_has_no_method_and_no_estimate_basis(self):
self.assertTrue(season.read_from_a_page({"season": "x", "season_basis": "primary"}))
self.assertFalse(season.read_from_a_page({"season": "x", "season_basis": "estimate"}))
self.assertFalse(season.read_from_a_page(
{"season": "x", "season_basis": "sources", "season_method": "Judged."}))
self.assertFalse(season.read_from_a_page({"season_basis": "primary"}))
def test_the_confidence_is_read_from_the_method(self):
self.assertEqual(season.confidence_of("Set by snow. Confidence: low."), "low")
self.assertEqual(season.confidence_of("Set by snow."), "")
def test_a_hike_without_season_is_an_error(self):
errors = check(season=None, months=None, season_basis=None, source=None)
self.assertTrue(any("season" in e for e in errors), errors)
def test_a_record_with_no_hike_is_an_error(self):
errors = []
season.check_present("t", {}, errors)
self.assertTrue(errors)
def test_a_hike_without_season_passes_until_required(self):
self.assertEqual(check(season=None, months=None, season_basis=None, source=None), [])
def test_required_flag_makes_it_an_error(self):
with mock.patch.object(season, "SEASON_REQUIRED", True):
errors = check(season=None, months=None, season_basis=None, source=None)
self.assertTrue(any("season" in e for e in errors), errors)
errors = []
season.check_present("t", {"hike": {"season": "x"}}, errors)
self.assertEqual(errors, [])
def test_method_without_season_is_an_error(self):
errors = check(season=None, months=None, season_basis=None, season_method="Some method.")
self.assertTrue(errors)
def test_every_record_with_a_season_passes_the_validator(self):
def test_every_record_passes_the_validator(self):
errors = []
for tid in refresh.trail_ids(V.ROOT):
rec = refresh.load_toml(os.path.join(V.ROOT, tid, "trail.toml"))
if (rec.get("hike") or {}).get("season"):
V.check_hike(tid, rec, errors)
V.check_hike(tid, rec, errors)
season.check_present(tid, rec, errors)
self.assertEqual(errors, [])
def test_provenance_keys_need_no_source_fact(self):
+1
View File
@@ -934,6 +934,7 @@ def main():
check_guides(tid, guides, errors)
check_links(tid, rec, errors)
check_hike(tid, rec, errors)
season.check_present(tid, rec, errors)
check_elevation(tid, tdir, rec, errors)
trail_history.check_entries(tid, rec, errors)