Files
longhikes/web/olt/records.py
T
swaitsandClaude Fable 5.2 abfbeb00bd
Validate / validate (push) Canceled after 0s
Add the Long Hikes static site (longhikes.org)
web/ is a static site generator for the dataset, in Python's standard
library only. `python3 web/build.py --out _site` writes a page for each
of the 138 records, a finder on the home page with a map of the 85
lines, region and route indexes, and the repository's documents as
pages. Each line is offered as the record's GeoJSON and as a GPX
simplified to 5 m, with two archives of everything.

The site's name and address are set in web/site.toml: Long Hikes, at
https://longhikes.org. The dataset keeps its own name.

netlify.toml and web/static/_headers configure the Netlify site
(content types, download dispositions, caching, a content security
policy with no inline script). It is deployed with the Netlify CLI
from a local build, because Netlify cannot build from this forge.

MapLibre GL JS and the two typefaces are vendored under web/assets/;
map tiles come from OpenFreeMap. Tests: python3 -m unittest discover
-s web/tests (164).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 12:20:32 -07:00

65 lines
2.0 KiB
Python

"""Load the trail records (one trail.toml per directory) with derived fields."""
import os
import tomllib
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SHIELD_NAMES = ("shield.svg", "shield.png")
def trail_kind(rec):
return rec.get("kind") or "designated"
def trail_status(rec):
"""Same rule as the status column of trails.csv (tools/build_index.py)."""
return "included" if rec.get("source", {}).get("line_file") else "record-only"
def find_files(root, tid, rec):
"""Return the data files that exist for a trail, as paths relative to root."""
d = os.path.join(root, tid)
files = {}
def rel(name):
return f"{tid}/{name}"
line_file = rec.get("source", {}).get("line_file") or ""
if line_file and os.path.isfile(os.path.join(d, line_file)):
files["line"] = rel(line_file)
if os.path.isfile(os.path.join(d, "alternates.geojson")):
files["alternates"] = rel("alternates.geojson")
extracts = {}
for name in sorted(os.listdir(d)):
if name.startswith("line-") and name.endswith(".geojson"):
extracts[name[len("line-"):-len(".geojson")]] = rel(name)
if extracts:
files["extracts"] = extracts
for name in SHIELD_NAMES:
if os.path.isfile(os.path.join(d, name)):
files["shield"] = rel(name)
break
return files
def load_record(root, tid):
with open(os.path.join(root, tid, "trail.toml"), "rb") as f:
rec = tomllib.load(f)
length_km = rec.get("length_km", 0)
rec["derived"] = {
"kind": trail_kind(rec),
"has_line": bool(rec.get("source", {}).get("line_file")),
"length_mi": int(round(length_km / 1.609344)),
"files": find_files(root, tid, rec),
"status": trail_status(rec),
}
return rec
def load_records(root=ROOT):
"""Every */trail.toml under root, sorted by id."""
ids = sorted(
d for d in os.listdir(root)
if os.path.isfile(os.path.join(root, d, "trail.toml"))
)
return [load_record(root, tid) for tid in ids]