"""What each conditional dasha's applicability test actually tested (BUG-2088). Astrologer reply 2026-11-20, B10: "table generated" and "condition applies" are two different facts. The report used to print a name and one word. The dasha modules already return the rule family and the inputs they tested (e.g. ``panchottari_dasha.panchottari_applicability``); this module collects them per family for the report: * ``condition``: True / False / None. None means not determined, never "no". * ``inputs``: the values the gate looked at. They come from - ``engine``: the fields the module returned (the reference copy under ``conditional_dasha_reference`` keeps them when the gate fails, and the Ashtottari family carries its gate results); - ``recomputed``: when the gate passed, the public packet keeps only the period table. For gates that read nothing but D1 positions the module's own gate function is called again on the packet's longitudes, and the result is used only if it agrees with the engine's verdict; - ``None``: not carried and not safely recomputable. It stays unknown. * ``table``: ``condition_met`` (a table from the met condition), ``reference_only`` (the table was generated although the condition is not met, for comparison), ``none``, or ``None`` when unknown. The PVR 2014 readings (Panchottari D12-only, Dwisaptati Sama variant) are computed separately in conditional_dasha_pvr_2014 (BUG-2122), not here. """ from __future__ import annotations from typing import Any, Dict, List, Optional try: from chaturshitisama_dasha import chaturshitisama_applicability from dwisaptatisama_dasha import dwisaptatisama_applicability from panchottari_dasha import panchottari_applicability from shastihayani_dasha import shastihayani_applicability from shodashottari_dasha import shodashottari_applicability from shattrimshatsama_dasha import shattrimshatsama_applicability except ImportError: # pragma: no cover - package import from scripts.chaturshitisama_dasha import chaturshitisama_applicability from scripts.dwisaptatisama_dasha import dwisaptatisama_applicability from scripts.panchottari_dasha import panchottari_applicability from scripts.shastihayani_dasha import shastihayani_applicability from scripts.shodashottari_dasha import shodashottari_applicability from scripts.shattrimshatsama_dasha import shattrimshatsama_applicability FAMILY_ORDER = ( "ashtottari", "tribhagi", "tribhagi_40", "shodashottari", "dwadashottari", "dwisaptatisama", "shattrimshatsama", "panchottari", "satabdika", "chaturshitisama", "shastihayani", ) UNCONDITIONAL = ("tribhagi", "tribhagi_40") # The seven-planet sign lords the engine uses for these gates (jyotish_engine.SIGN_LORDS). _SIGN_LORDS = ("Mars", "Venus", "Mercury", "Moon", "Sun", "Mercury", "Venus", "Mars", "Jupiter", "Saturn", "Saturn", "Jupiter") # Input fields each rule family returns. _INPUT_FIELDS = { "bphs_shodashottari_hora_paksha": ("hora_lord", "paksha"), "bphs_dwadashottari_navamsa_lagna_venus_signs": ("navamsa_ascendant_sign_index",), "bphs_dwisaptatisama_lagna_lord_in_first_or_seventh": ("ascendant_sign_index", "lagna_lord_sign_index", "lagna_lord_house"), "bphs_shattrimshatsama_day_sun_hora_or_night_moon_hora": ("birth_is_daytime", "ascendant_hora_lord"), "bphs_panchottari_cancer_rasi_and_d12": ("ascendant_rasi_sign_index", "ascendant_d12_sign_index"), "bphs_satabdika_vargottama_lagna": ("ascendant_rasi_sign_index", "ascendant_navamsa_sign_index"), "bphs_chaturshitisama_tenth_lord_in_tenth": ("ascendant_sign_index", "tenth_house_sign_index", "tenth_lord_sign_index"), "bphs_shastihayani_sun_in_lagna": ("ascendant_sign_index", "sun_sign_index"), } _DEFAULT_RULE = { "shodashottari": "bphs_shodashottari_hora_paksha", "dwadashottari": "bphs_dwadashottari_navamsa_lagna_venus_signs", "dwisaptatisama": "bphs_dwisaptatisama_lagna_lord_in_first_or_seventh", "shattrimshatsama": "bphs_shattrimshatsama_day_sun_hora_or_night_moon_hora", "panchottari": "bphs_panchottari_cancer_rasi_and_d12", "satabdika": "bphs_satabdika_vargottama_lagna", "chaturshitisama": "bphs_chaturshitisama_tenth_lord_in_tenth", "shastihayani": "bphs_shastihayani_sun_in_lagna", } def _dict(value: Any) -> Dict[str, Any]: return value if isinstance(value, dict) else {} def _number(value: Any) -> Optional[float]: if isinstance(value, bool) or not isinstance(value, (int, float)): return None return float(value) def _longitudes(chart: Dict[str, Any]) -> Dict[str, float]: """Ascendant and planet longitudes from ``core_chart``; only real numbers are kept.""" out: Dict[str, float] = {} ascendant = _dict(chart.get("ascendant")) for key in ("lon", "degree_raw"): value = _number(ascendant.get(key)) if value is not None: out["Ascendant"] = value % 360.0 break for name, row in _dict(chart.get("planets")).items(): row = _dict(row) for key in ("lon", "degree_raw", "degree"): value = _number(row.get(key)) if value is not None: out[str(name)] = value % 360.0 break return out def _ascendant_hora_lord(asc: float) -> str: """Hora lord of the Lagna degree, as the engine computes it for the Shattrimshatsama gate (jyotish_engine ``ascendant_hora_lord``: odd signs Sun then Moon, even signs Moon then Sun).""" sign, degree = int(asc // 30) % 12, asc % 30.0 return "Sun" if (sign % 2 == 0 and degree < 15.0) or (sign % 2 == 1 and degree >= 15.0) else "Moon" def _recorded_daytime(*sources: Dict[str, Any]) -> Optional[bool]: """The engine's day/night of birth as the Ashtottari module recorded it (same birth input).""" for source in sources: gates = _dict(_dict(_dict(source).get("applicability")).get("gates")) value = _dict(gates.get("paksha_day_night")).get("birth_is_daytime") if isinstance(value, bool): return value return None def _recompute(key: str, lons: Dict[str, float], daytime: Optional[bool] = None) -> Optional[Dict[str, Any]]: """The module's own gate on the packet's D1 positions; None when an input is missing.""" asc = lons.get("Ascendant") if asc is None: return None asc_sign = int(asc // 30) % 12 try: # BUG-2105: day/night is the engine's recorded value (Ashtottari gate); the hora lord comes # from the packet's Lagna longitude. Used only when it agrees with the engine's verdict. if key == "shattrimshatsama" and isinstance(daytime, bool): return shattrimshatsama_applicability(birth_is_daytime=daytime, ascendant_hora_lord=_ascendant_hora_lord(asc)) if key == "shodashottari" and "Sun" in lons and "Moon" in lons: return shodashottari_applicability(ascendant_longitude=asc, sun_longitude=lons["Sun"], moon_longitude=lons["Moon"]) if key == "panchottari": return panchottari_applicability(ascendant_longitude=asc) if key == "dwisaptatisama" and _SIGN_LORDS[asc_sign] in lons: return dwisaptatisama_applicability( ascendant_longitude=asc, lagna_lord_sign_index=int(lons[_SIGN_LORDS[asc_sign]] // 30) % 12) if key == "chaturshitisama": tenth_lord = _SIGN_LORDS[(asc_sign + 9) % 12] if tenth_lord in lons: return chaturshitisama_applicability( ascendant_longitude=asc, tenth_lord_sign_index=int(lons[tenth_lord] // 30) % 12) if key == "shastihayani" and "Sun" in lons: return shastihayani_applicability(ascendant_longitude=asc, sun_longitude=lons["Sun"]) except (TypeError, ValueError): return None return None def _has_periods(item: Dict[str, Any]) -> bool: return any(isinstance(item.get(key), list) and item.get(key) for key in ("periods", "major")) def _verdict(family: Dict[str, Any], reference: Dict[str, Any]) -> Optional[bool]: if reference.get("classical_applicable") is False: return False status = str(family.get("execution_status") or family.get("status") or "") if family.get("applicable") is True or status == "executed": return True if family.get("applicable") is False or status == "not_applicable": return False return None def _ashtottari_row(family: Dict[str, Any], module: Dict[str, Any]) -> Dict[str, Any]: gate_pack = _dict(family.get("applicability")) or _dict(module.get("applicability")) gates = _dict(gate_pack.get("gates")) source = family or module status = str(source.get("execution_status") or source.get("status") or "") verdict: Optional[bool] if gate_pack.get("status") == "applicable" or source.get("applicable") is True or status == "executed": verdict = True elif gate_pack.get("status") == "not_applicable" or source.get("applicable") is False or status == "not_applicable": verdict = False else: verdict = None inputs: Dict[str, Any] = {} decides = _dict(gates.get("rahu_from_lagna_lord")) for field in ("position_from_lagna_lord", "rahu_in_lagna"): if decides.get(field) is not None: inputs[field] = decides[field] recorded = _dict(gates.get("paksha_day_night")) for field in ("birth_is_daytime", "is_shukla_paksha"): if recorded.get(field) is not None: inputs[field] = recorded[field] if recorded.get("status") in ("pass", "fail"): inputs["recorded_gate_met"] = recorded["status"] == "pass" generated = _has_periods(family) or _has_periods(module) return { "key": "ashtottari", "rule": str(gate_pack.get("rule_family") or source.get("applicability_rule_family") or "") or None, "condition": verdict, "inputs": inputs, "inputs_source": "engine" if inputs else None, "table": _table_state(verdict, generated, family or module), } def _table_state(verdict: Optional[bool], generated: bool, known: Any) -> Optional[str]: if generated: return "condition_met" if verdict is True else "reference_only" if verdict is False else "generated" if not known: return None return "none" def applicability_rows(families: Any, reference: Any, chart: Any, ashtottari_module: Any = None) -> List[Dict[str, Any]]: """One row per conditional dasha family. ``families``: ``dasha_master_pack.families``; ``reference``: ``conditional_dasha_reference``; ``chart``: ``core_chart``; ``ashtottari_module``: the ``ashtottari_dasha`` module (fallback). """ families, reference, chart = _dict(families), _dict(reference), _dict(chart) lons = _longitudes(chart) daytime = _recorded_daytime(families.get("ashtottari"), ashtottari_module, reference.get("ashtottari_dasha")) rows: List[Dict[str, Any]] = [] for key in FAMILY_ORDER: family = _dict(families.get(key)) if key == "ashtottari": rows.append(_ashtottari_row(family, _dict(ashtottari_module))) continue ref = _dict(reference.get(f"{key}_dasha")) if ref.get("error"): ref = {} verdict = _verdict(family, ref) generated = _has_periods(family) or _has_periods(ref) if key in UNCONDITIONAL: rows.append({"key": key, "rule": "unconditional", "condition": None, "inputs": {}, "inputs_source": None, "table": "generated" if generated else ("none" if family else None)}) continue rule = str(ref.get("rule_family") or family.get("rule_family") or "") or None inputs: Dict[str, Any] = {} source = None carrier = ref if ref.get("rule_family") else family if family.get("rule_family") else {} if carrier: for field in _INPUT_FIELDS.get(str(carrier.get("rule_family")), ()): if carrier.get(field) is not None: inputs[field] = carrier[field] source = "engine" if inputs else None if not inputs and verdict is not None: again = _recompute(key, lons, daytime) if again and bool(again.get("applicable")) == verdict and (rule is None or again.get("rule_family") == rule): rule = rule or again.get("rule_family") inputs = {field: again[field] for field in _INPUT_FIELDS.get(str(again.get("rule_family")), ()) if again.get(field) is not None} source = "recomputed" if inputs else None rows.append({ "key": key, "rule": rule or _DEFAULT_RULE.get(key), "condition": verdict, "inputs": inputs, "inputs_source": source, "table": _table_state(verdict, generated, family or ref), }) return rows