Files
Jyotisha/scripts/western_page/layers.py
T
ClaudeandClaude Fable 5.2 10074c2f30
Independent Staging Quality Gate / validate-frontend (push) Successful in 6m20s
Independent Staging Quality Gate / validate-python (push) Successful in 8m8s
Independent Staging Quality Gate / publish (push) Successful in 3m38s
fix(western): move the chart-page layers into scripts/western_page (BUG-2340 follow-up)
The product-owned module does not belong in the scripts root (the root count
budget guards it); logic unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
2026-11-25 00:48:25 +08:00

125 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Extra layers the chart page's Western tab shows beside the natal wheel (BUG-2340).
`/api/western` (``western_chart_engine.build_tropical_natal_from_request``) adds
these to its natal payload:
- ``classical``: chart sect, essential dignities of the seven traditional
planets and the Lots of Fortune and Spirit, read from the classical layer
(``classical_astrology._sect`` / ``_dignities`` / ``_lots``).
- ``transits``: a dated snapshot for the day the page asks for -- slow-planet
transits to the natal points, the annual profection and the secondary
progressed Moon (``western_timing_engine``). Only when the request names a
``transit_date``; the page sends today's date and its server caches the
result per day.
Both are projections of existing calculations: nothing here is a new rule.
"""
from __future__ import annotations
import re
from typing import Any
try:
from scripts.western_timing_engine import (
calculate_annual_profection,
calculate_secondary_progressions,
calculate_transit_to_natal,
)
except ModuleNotFoundError: # pragma: no cover - direct script execution
from western_timing_engine import (
calculate_annual_profection,
calculate_secondary_progressions,
calculate_transit_to_natal,
)
# Transits worth a line on the page: the slow movers. The fast ones change
# within days and would turn the folded table into noise.
SLOW_TRANSIT_PLANETS = ("jupiter", "saturn", "uranus", "neptune", "pluto")
TRANSIT_ROW_LIMIT = 6
_DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
def _classical_module():
try:
from scripts import classical_astrology
except ModuleNotFoundError: # pragma: no cover - direct script execution
import classical_astrology # type: ignore[no-redef]
return classical_astrology
def _position(row: dict[str, Any]) -> dict[str, Any]:
return {
"longitude": row["longitude"],
"sign": row["sign"],
"degree_in_sign": row["degree_in_sign"],
}
def classical_layer(chart: dict[str, Any]) -> dict[str, Any]:
"""Sect, dignities and the two main lots, without the classical audit trail."""
classical = _classical_module()
sect = classical._sect(chart)
dignities = classical._dignities(chart, sect)
lots = classical._lots(chart, sect)
return {
"status": "used",
"chart_sect": sect["chart_sect"],
"dignities": {
planet: {
"essential_dignities": dict(row["essential_dignities"]),
"bound_lord": row["bound_lord"],
"face_lord": row["face_lord"],
}
for planet, row in dignities.items()
},
"lots": {name: _position(lots[name]) for name in ("fortune", "spirit")},
}
def transit_layer(birth: dict[str, Any], target_date: str) -> dict[str, Any]:
"""Slow transits to the natal chart, the profected year and the progressed Moon on one day."""
transits = calculate_transit_to_natal(target_date=target_date, **birth)
profection = calculate_annual_profection(target_date=target_date, **birth)
progressions = calculate_secondary_progressions(target_date=target_date, **birth)
slow = [row for row in transits["aspects"] if row["transit_planet"] in SLOW_TRANSIT_PLANETS]
return {
"status": "used",
"target_date": target_date,
"aspects": [
{
"transit_planet": row["transit_planet"],
"natal_point": row["natal_point"],
"aspect": row["aspect"],
"orb": row["orb"],
}
for row in slow[:TRANSIT_ROW_LIMIT]
],
"profection": {
"house": profection["profected_house"],
"sign": profection["profected_sign"],
"year_lord": profection["year_lord"],
},
"progressed_moon": _position(progressions["progressed_planets"]["moon"]),
}
def attach_page_layers(chart: dict[str, Any], birth: dict[str, Any], transit_date: Any) -> dict[str, Any]:
"""Add ``classical`` and (with a valid date) ``transits`` to a natal chart in place.
A failure in either layer marks that layer blocked; the natal chart still returns.
"""
try:
chart["classical"] = classical_layer(chart)
except Exception as exc: # noqa: BLE001 - the natal wheel must not fail with it
chart["classical"] = {"status": "blocked", "reason": type(exc).__name__}
date = str(transit_date or "").strip()[:10]
if not _DATE.match(date):
return chart
try:
chart["transits"] = transit_layer(birth, date)
except Exception as exc: # noqa: BLE001
chart["transits"] = {"status": "blocked", "reason": type(exc).__name__}
return chart