Owner 2026-11-21: after BUG-2115 the full-data report still printed source lines and book references (Goel 出处 line, the PVR 2014 article in the conditional-dasha note, PVR / Light on Life / BPHS verse in the calculation notes, Tithi lord table source, rashi-dasha 出处 lines, Narayana p.43 label and p.50 / Table 13 direction notes, functional profile source row, Ashtakavarga book calibration). Method names stay; the book, page, chapter and ruling round go. Engine labels for the consultation card are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2U3BH1SxwAGyhhbe1
207 lines
12 KiB
Python
207 lines
12 KiB
Python
"""Birth Tithi lord for the personal report (BUG-2089, 占星师 2026-11-20 问题 12).
|
||
|
||
The engine computes the Tithi lord in the full reading (``scripts/tithi_lord.py``, PVR
|
||
Table 3) but the public report packet never carried it, and the report said so (BUG-1419).
|
||
This module carries it under ``worksheets.birth_and_parameters.tithi_lord`` and prints it.
|
||
|
||
Three things stay apart, as she requires:
|
||
- the lord's ORIGINAL dignity in the natal chart, and — in its own field — any
|
||
debilitation-cancellation condition; the cancellation never overwrites the dignity;
|
||
- the Tithi lord, the Tithi deity and the Moon's nakshatra lord (only the first is here);
|
||
- Full Moon and New Moon, whose lords differ in the table (Saturn / Rahu).
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
from typing import Any
|
||
|
||
SOURCE_PROFILE = "pvr_integrated_approach_table_3"
|
||
PLANETS = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu")
|
||
_PAKSHA = {"Shukla": ("白分(上半月,月亮渐盈)", "Shukla (waxing half)"), "Krishna": ("黑分(下半月,月亮渐亏)", "Krishna (waning half)")}
|
||
_DEBILITATION = {"Sun": "Libra", "Moon": "Scorpio", "Mars": "Cancer", "Mercury": "Virgo",
|
||
"Jupiter": "Capricorn", "Venus": "Pisces", "Saturn": "Aries"}
|
||
|
||
|
||
def _record(value: Any) -> dict:
|
||
return value if isinstance(value, dict) else {}
|
||
|
||
|
||
def _number(value: Any) -> float | None:
|
||
return float(value) if isinstance(value, (int, float)) and not isinstance(value, bool) else None
|
||
|
||
|
||
def _birth_tithi(packet: dict, planets: dict) -> tuple[int, str] | None:
|
||
"""(number inside the half-month 1–15, paksha) from the packet's own Panchanga, else from the two longitudes."""
|
||
try:
|
||
from scripts.tithi_lord import calc_tithi
|
||
except ModuleNotFoundError: # pragma: no cover - scripts/ on sys.path
|
||
from tithi_lord import calc_tithi
|
||
birth = _record(_record(packet.get("worksheets")).get("birth_and_parameters"))
|
||
module = _record(birth.get("panchanga"))
|
||
tithi = _record(_record(module.get("raw") if isinstance(module.get("raw"), dict) else module).get("tithi"))
|
||
number, paksha = tithi.get("tithi_in_paksha"), tithi.get("paksha")
|
||
if type(number) is int and 1 <= number <= 15 and paksha in _PAKSHA:
|
||
return number, paksha
|
||
sun = _number(_record(planets.get("Sun")).get("lon", _record(planets.get("Sun")).get("longitude")))
|
||
moon = _number(_record(planets.get("Moon")).get("lon", _record(planets.get("Moon")).get("longitude")))
|
||
if sun is None or moon is None:
|
||
return None
|
||
number, paksha, _inside, _diff = calc_tithi(sun, moon)
|
||
return number, paksha
|
||
|
||
|
||
def build_tithi_lord(packet: dict) -> dict:
|
||
packet = _record(packet)
|
||
worksheets = _record(packet.get("worksheets"))
|
||
planets = _record(_record(worksheets.get("d1_rasi_bhava")).get("planets")) or _record(_record(packet.get("core_chart")).get("planets"))
|
||
found = _birth_tithi(packet, planets)
|
||
if found is None:
|
||
return {"status": "blocked", "missing": ["birth_tithi"]}
|
||
try:
|
||
from scripts.tithi_lord import TITHI_NAMES, get_tithi_lord
|
||
from scripts.yogas_doshas import neecha_bhanga_assessment
|
||
except ModuleNotFoundError: # pragma: no cover - scripts/ on sys.path
|
||
from tithi_lord import TITHI_NAMES, get_tithi_lord
|
||
from yogas_doshas import neecha_bhanga_assessment
|
||
number, paksha = found
|
||
_index, lord = get_tithi_lord(number, paksha)
|
||
name = "Purnima" if (number, paksha) == (15, "Shukla") else "Amavasya" if (number, paksha) == (15, "Krishna") \
|
||
else f"{paksha} {str(TITHI_NAMES.get(number, number)).split(' (')[0]}"
|
||
panchanga = _record(_record(_record(worksheets.get("birth_and_parameters")).get("panchanga")).get("raw"))
|
||
listed = _record(panchanga.get("tithi"))
|
||
if (listed.get("tithi_in_paksha"), listed.get("paksha")) == (number, paksha) and isinstance(listed.get("full_name"), str):
|
||
# Same spelling as the birth-calendar table of this report.
|
||
name = listed["full_name"]
|
||
row = _record(planets.get(lord))
|
||
out = {
|
||
"status": "computed",
|
||
"tithi_in_paksha": number,
|
||
"tithi_number": number + (15 if paksha == "Krishna" else 0),
|
||
"paksha": paksha,
|
||
"name": name,
|
||
"lord": lord,
|
||
"source_profile": SOURCE_PROFILE,
|
||
# The lord's own natal dignity, exactly as the chart states it. None = the chart has no
|
||
# position for this planet; it is not replaced by a guess.
|
||
"lord_original_dignity": row.get("status") or row.get("dignity"),
|
||
"lord_sign": row.get("sign"),
|
||
"lord_house": row.get("house") if type(row.get("house")) is int else None,
|
||
}
|
||
if not row.get("sign"):
|
||
out["cancellation"] = {"status": "not_assessed"}
|
||
elif _DEBILITATION.get(lord) != row.get("sign"):
|
||
out["cancellation"] = {"status": "not_applicable"}
|
||
else:
|
||
verdict = neecha_bhanga_assessment(lord, row.get("sign"), planets)
|
||
if verdict.get("status") != "executed":
|
||
out["cancellation"] = {"status": "not_assessed"}
|
||
else:
|
||
conditions = [item for item in verdict.get("conditions") or [] if isinstance(item, dict)]
|
||
out["cancellation"] = {
|
||
"status": "conditions_met" if verdict.get("default_rule_met") else "self_reference_only" if conditions else "none_met",
|
||
"profile": verdict.get("profile"),
|
||
"conditions": conditions,
|
||
}
|
||
return out
|
||
|
||
|
||
def attach_tithi_lord(packet: dict) -> dict | None:
|
||
"""Add ``birth_and_parameters.tithi_lord``; an existing value is kept."""
|
||
worksheets = _record(packet).get("worksheets")
|
||
if not isinstance(worksheets, dict):
|
||
return None
|
||
birth = worksheets.setdefault("birth_and_parameters", {})
|
||
if not isinstance(birth, dict):
|
||
return None
|
||
if not isinstance(birth.get("tithi_lord"), dict):
|
||
birth["tithi_lord"] = build_tithi_lord(packet)
|
||
return birth["tithi_lord"]
|
||
|
||
|
||
def _name_zh(module: dict) -> str:
|
||
# The Chinese edition's term replacement reads "Dasha" inside "Dashami" as 大运, so the
|
||
# Chinese name is written in Chinese; the Sanskrit name stays in the English edition.
|
||
number, paksha = module.get("tithi_in_paksha"), module.get("paksha")
|
||
if number == 15:
|
||
return "满月日" if paksha == "Shukla" else "新月日"
|
||
return f"{'白分' if paksha == 'Shukla' else '黑分'}第 {number} 日"
|
||
|
||
|
||
def _dignity_one_language(value: Any, language: str) -> str:
|
||
"""The engine's bilingual dignity (``入旺(Exalted)``) in one language; anything else unchanged."""
|
||
match = re.fullmatch(r"([^()()]+?)\s*[((]([A-Za-z][A-Za-z \-]*)[))]", str(value or "").strip())
|
||
if not match:
|
||
return str(value or "")
|
||
return match.group(2).strip() if language == "en" else match.group(1).strip()
|
||
|
||
|
||
def tithi_lord_lines(module: Any, language: str, dignity_label=None) -> list[str]:
|
||
"""Markdown: a caption with the method, then one [item, value] table."""
|
||
module = _record(module)
|
||
zh = language == "zh"
|
||
try:
|
||
from scripts.report_render.appendix_language import planet_name, sign_name
|
||
from scripts.report_render.neecha_witness import condition_text, neecha_roles, witness_list_text
|
||
except ModuleNotFoundError: # pragma: no cover
|
||
from report_render.appendix_language import planet_name, sign_name
|
||
from report_render.neecha_witness import condition_text, neecha_roles, witness_list_text
|
||
heading = "#### 出生 Tithi 的主星" if zh else "#### Lord of the birth Tithi"
|
||
if module.get("status") != "computed":
|
||
return [heading, "", "本次没有算出出生 Tithi 的主星:缺少出生 Tithi 或日月黄经;这里不推算。" if zh
|
||
else "The lord of the birth Tithi was not computed this time: the birth Tithi or the Sun and Moon longitudes are missing. Nothing is derived here.", ""]
|
||
lord = module.get("lord")
|
||
lord_text = planet_name(lord, language)
|
||
if lord == "Rahu" and zh:
|
||
lord_text = "罗睺"
|
||
paksha = _PAKSHA.get(str(module.get("paksha")), ("", ""))[0 if zh else 1]
|
||
raw_dignity = module.get("lord_original_dignity")
|
||
if raw_dignity:
|
||
dignity = (dignity_label or _dignity_one_language)(raw_dignity, language)
|
||
else:
|
||
dignity = "星盘没有给出这颗星的尊贵状态" if zh else "The chart gives no dignity for this planet"
|
||
cancellation = _record(module.get("cancellation"))
|
||
state = cancellation.get("status")
|
||
conditions = [item for item in cancellation.get("conditions") or [] if isinstance(item, dict)]
|
||
condition_words = (";" if zh else "; ").join(condition_text(item, language) for item in conditions)
|
||
roles = neecha_roles({"name": "Neecha Bhanga", "planets": [lord], "cancellation_conditions": conditions}) if conditions else None
|
||
if state == "conditions_met":
|
||
cancel = (f"有化解条件:{condition_words}。见证星:{witness_list_text(roles, language)}。原始尊贵仍是落陷,化解条件不改写它。" if zh
|
||
else f"Cancellation conditions are met: {condition_words}. Witnesses: {witness_list_text(roles, language)}. The original dignity is still debilitated; the cancellation does not rewrite it.")
|
||
elif state == "self_reference_only":
|
||
cancel = (f"只有自我参照的条件({condition_words}),不算成立。" if zh
|
||
else f"Only a self-reference condition is present ({condition_words}); it does not count as met.")
|
||
elif state == "none_met":
|
||
cancel = "主星落陷,本规则的化解条件一项也没有成立。" if zh else "The lord is debilitated and none of this rule's cancellation conditions is met."
|
||
elif state == "not_applicable":
|
||
cancel = "不适用:主星不在落陷星座。" if zh else "Not applicable: the lord is not in its debilitation sign."
|
||
else:
|
||
cancel = "未判断:缺少主星的星座位置。" if zh else "Not assessed: the lord's sign position is missing."
|
||
place = ""
|
||
if module.get("lord_sign"):
|
||
house = module.get("lord_house")
|
||
place = (f"{sign_name(module.get('lord_sign'), language)}" + (f",第 {house} 宫" if house else "")) if zh \
|
||
else (f"{sign_name(module.get('lord_sign'), language)}" + (f", house {house}" if house else ""))
|
||
caption = (
|
||
"算法:Tithi 由月亮与太阳的黄经差每 12° 一段得出,共 30 个;主星按 Tithi 主星表查得,"
|
||
"满月的主星是土星,新月的主星是罗睺。这里只列 Tithi 主星,不是 Tithi 之神,也不是月亮所在星宿的主星;仅有主星不构成应期。"
|
||
if zh else
|
||
"Method: a Tithi is each 12° of the Moon's longitude minus the Sun's, 30 in all; the lord is read from the Tithi lord table, "
|
||
"where the Full Moon's lord is Saturn and the New Moon's is Rahu. Only the Tithi lord is listed here — "
|
||
"not the Tithi deity and not the lord of the Moon's nakshatra; the lord alone does not make a timing window."
|
||
)
|
||
rows = [
|
||
("Tithi 序号(1–30)" if zh else "Tithi number (1-30)", module.get("tithi_number")),
|
||
("半月" if zh else "Paksha", paksha),
|
||
("半月内序号(1–15)" if zh else "Number in the paksha (1-15)", module.get("tithi_in_paksha")),
|
||
("名称" if zh else "Name", _name_zh(module) if zh else module.get("name")),
|
||
("主星" if zh else "Lord", lord_text),
|
||
("主星在本命盘的位置" if zh else "Lord's natal position", place or ("星盘没有给出" if zh else "Not given by the chart")),
|
||
("主星的原始尊贵" if zh else "Lord's original dignity", dignity),
|
||
("落陷化解条件(另列,不改写原始尊贵)" if zh else "Debilitation cancellation (separate; never rewrites the dignity)", cancel),
|
||
]
|
||
lines = [heading, "", caption, "", "| 项目 | 内容 |" if zh else "| Item | Value |", "|------|------|"]
|
||
lines.extend(f"| {label} | {value} |" for label, value in rows)
|
||
lines.append("")
|
||
return lines
|