Skip to content

Commit 7a2f1c3

Browse files
committed
fix: address atlas review followups
1 parent c465985 commit 7a2f1c3

4 files changed

Lines changed: 97 additions & 3 deletions

File tree

scripts/build_atlas_route.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import argparse
66
import html
77
import json
8+
import os
89
import re
910
import shutil
1011
from pathlib import Path
@@ -136,10 +137,27 @@ def _image_for_audio(audio_path: Path) -> Path | None:
136137
return images[0] if images else None
137138

138139

140+
def _is_excluded_dir_name(name: str) -> bool:
141+
return name.startswith(".") or name in EXCLUDED_DIRS
142+
143+
144+
def _iter_audio_paths(root: Path):
145+
audio_paths: list[Path] = []
146+
for current_dir, dirnames, filenames in os.walk(root):
147+
dirnames[:] = sorted(name for name in dirnames if not _is_excluded_dir_name(name))
148+
for filename in sorted(filenames):
149+
if filename.startswith("."):
150+
continue
151+
audio_path = Path(current_dir) / filename
152+
if audio_path.is_file() and audio_path.suffix.lower() in AUDIO_SUFFIXES:
153+
audio_paths.append(audio_path)
154+
yield from sorted(audio_paths)
155+
156+
139157
def discover_media(root: Path) -> list[dict[str, str]]:
140158
root = root.resolve()
141159
media: list[dict[str, str]] = []
142-
for audio_path in sorted(path for path in root.rglob("*") if path.is_file() and path.suffix.lower() in AUDIO_SUFFIXES):
160+
for audio_path in _iter_audio_paths(root):
143161
relative_audio = audio_path.relative_to(root)
144162
if not _is_sound_library_path(relative_audio):
145163
continue

scripts/serve_atlas.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class QuietAtlasServer(http.server.ThreadingHTTPServer):
2525
allow_reuse_address = True
2626

2727

28+
def preview_entry_path(directory: Path) -> str:
29+
directory = directory.resolve()
30+
if (directory / "atlas" / "index.html").exists():
31+
return "/atlas/"
32+
if (directory / "index.html").exists():
33+
return "/"
34+
return "/"
35+
36+
2837
def main() -> None:
2938
parser = argparse.ArgumentParser(description="Serve the Yellowstone atlas locally.")
3039
parser.add_argument("port", nargs="?", type=int, default=4173)
@@ -36,10 +45,11 @@ def main() -> None:
3645
)
3746
args = parser.parse_args()
3847

39-
handler = functools.partial(QuietAtlasHandler, directory=str(args.directory))
48+
directory = args.directory.resolve()
49+
handler = functools.partial(QuietAtlasHandler, directory=str(directory))
4050
server = QuietAtlasServer(("", args.port), handler)
4151

42-
print(f"Serving atlas preview at http://localhost:{args.port}/atlas/")
52+
print(f"Serving atlas preview from {directory} at http://localhost:{args.port}{preview_entry_path(directory)}")
4353
try:
4454
server.serve_forever()
4555
except KeyboardInterrupt:

tests/test_atlas_preview_server.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""Checks for the local atlas preview helper."""
22

3+
import sys
34
from pathlib import Path
45

6+
from scripts import serve_atlas
7+
58
ROOT = Path(__file__).resolve().parents[1]
69
SERVER_PATH = ROOT / "scripts" / "serve_atlas.py"
710
MAKEFILE_PATH = ROOT / "Makefile"
@@ -25,6 +28,51 @@ def test_preview_server_enables_address_reuse_before_binding():
2528
assert "server.allow_reuse_address = True" not in script
2629

2730

31+
def test_preview_server_reports_root_url_when_serving_atlas_directory(tmp_path, monkeypatch, capsys):
32+
atlas_dir = tmp_path / "atlas"
33+
atlas_dir.mkdir()
34+
(atlas_dir / "index.html").write_text("<!doctype html>", encoding="utf-8")
35+
36+
class FakeServer:
37+
def __init__(self, address, handler):
38+
self.address = address
39+
self.handler = handler
40+
41+
def serve_forever(self):
42+
raise KeyboardInterrupt
43+
44+
monkeypatch.setattr(serve_atlas, "QuietAtlasServer", FakeServer)
45+
monkeypatch.setattr(sys, "argv", ["serve_atlas.py", "4321", "--directory", str(atlas_dir)])
46+
47+
serve_atlas.main()
48+
49+
output = capsys.readouterr().out
50+
assert f"Serving atlas preview from {atlas_dir.resolve()} at http://localhost:4321/" in output
51+
assert "http://localhost:4321/atlas/" not in output
52+
53+
54+
def test_preview_server_reports_atlas_url_when_serving_repo_root(tmp_path, monkeypatch, capsys):
55+
atlas_dir = tmp_path / "atlas"
56+
atlas_dir.mkdir()
57+
(atlas_dir / "index.html").write_text("<!doctype html>", encoding="utf-8")
58+
59+
class FakeServer:
60+
def __init__(self, address, handler):
61+
self.address = address
62+
self.handler = handler
63+
64+
def serve_forever(self):
65+
raise KeyboardInterrupt
66+
67+
monkeypatch.setattr(serve_atlas, "QuietAtlasServer", FakeServer)
68+
monkeypatch.setattr(sys, "argv", ["serve_atlas.py", "4321", "--directory", str(tmp_path)])
69+
70+
serve_atlas.main()
71+
72+
output = capsys.readouterr().out
73+
assert f"Serving atlas preview from {tmp_path.resolve()} at http://localhost:4321/atlas/" in output
74+
75+
2876
def test_makefile_exposes_atlas_preview_target():
2977
makefile = MAKEFILE_PATH.read_text(encoding="utf-8")
3078

tests/test_atlas_route_builder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ def test_discover_media_pairs_audio_with_same_folder_image_and_ignores_site_file
3030
assert media[0]["title"] == "American Coots"
3131

3232

33+
def test_discover_media_prunes_hidden_and_excluded_directories_before_walk(tmp_path, monkeypatch):
34+
(tmp_path / "American Coots").mkdir()
35+
(tmp_path / "American Coots" / "American Coots.mp3").write_bytes(b"audio")
36+
(tmp_path / ".git" / "objects").mkdir(parents=True)
37+
(tmp_path / ".git" / "objects" / "ignored.mp3").write_bytes(b"not sound library media")
38+
(tmp_path / "atlas").mkdir()
39+
(tmp_path / "atlas" / "ignored.mp3").write_bytes(b"not sound library media")
40+
41+
def fail_rglob(self, pattern):
42+
raise AssertionError("discover_media should prune excluded directories before walking")
43+
44+
monkeypatch.setattr(Path, "rglob", fail_rglob)
45+
46+
media = discover_media(tmp_path)
47+
48+
assert [entry["audioPath"] for entry in media] == ["American Coots/American Coots.mp3"]
49+
50+
3351
def test_merge_route_preserves_curated_stops_and_appends_publishable_entries(tmp_path):
3452
existing_route = [
3553
{

0 commit comments

Comments
 (0)