"""Reader rows for the functional benefic / malefic table in the data chapters (BUG-2089). The table used to print the packet's keys and codes as they are stored (`ascendant`, `functional_profile`, `pvr_table30_v1`, `used`). The rows here use reader words in both editions. The profile keeps its name and version in words; internal ids (detector name, rule file path, earlier ruling numbers) are not printed. """ from __future__ import annotations import re from typing import Any from .appendix_language import planet_name, sign_name _PROFILE = { "pvr_table30_v1": ("PVR 表 30(第 1 版)", "PVR Table 30 (version 1)"), "bphs_exception_profile": ("BPHS 例外表", "BPHS exception table"), } _PROFILE_SHORT = { "pvr_table30_v1": ("PVR 表 30", "PVR Table 30"), "bphs_exception_profile": ("BPHS 例外表", "the BPHS exception table"), } _STATUS = { "used": ("已使用", "Used"), "executed": ("已使用", "Used"), "partial": ("部分完成", "Partly completed"), "blocked": ("未完成", "Not completed"), } _ROLE = { "benefic": ("吉星", "benefic"), "malefic": ("凶星", "malefic"), "neutral": ("中性", "neutral"), "yogakaraka": ("瑜伽主星", "yogakaraka"), } _ASSERTION = { "parameter_sensitive": ("参数敏感", "parameter-sensitive"), "profile_sensitive": ("口径敏感", "profile-sensitive"), } _YOGAKARAKA_DEFINITION = { "distinct lordships: one of 4/7/10 and one of 5/9": ( "同时管一个角宫(第 4、7、10 宫之一)和一个三方宫(第 5、9 宫之一),两宫不是同一宫", "Owns one of houses 4, 7, 10 and one of houses 5, 9, as two different houses", ), } _CONDITIONS_ZH = { "Neutral/mixed is association-dependent, not an absence of risk.": "中性或混合要看它与哪些行星发生关联,不等于没有风险。", "Natural nature, strength, maraka roles and dasha activation remain separate.": "自然吉凶、力量、与寿命损耗有关的角色、大运是否带动,各自另看。", "Strict yogakarakas do not exhaust the text's broader yoga-producing associations.": "这里的瑜伽主星取严格定义,典籍里更宽的成格关联不止这些。", "Dual lordship needs an integrated judgment of both houses (PVR §13.2).": "管两个宫的行星要把两宫合起来判断。", } _PAKSHA = {"waxing": ("盈月", "waxing"), "waning": ("亏月", "waning")} _PLANET_ORDER = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu") def _pick(pair: tuple[str, str] | None, language: str) -> str | None: if pair is None: return None return pair[0] if language == "zh" else pair[1] def _planets(value: Any, language: str) -> str | None: if not isinstance(value, (list, tuple)): return None names = [planet_name(item, language) for item in value if isinstance(item, str)] if not names: return "本盘没有" if language == "zh" else "None in this chart" return ("、" if language == "zh" else ", ").join(names) def _ordered(mapping: dict) -> list[tuple[str, Any]]: known = [(name, mapping[name]) for name in _PLANET_ORDER if name in mapping] return known + [(name, value) for name, value in mapping.items() if name not in _PLANET_ORDER] def _basis_text(text: str, language: str) -> str: # BUG-2129: the section number of the book is not shown; the profile keeps its name (PVR 表 30). if language != "zh": return text.replace("PVR §13.2 Table 30", "PVR Table 30") text = text.replace("PVR §13.2 Table 30 note: movable-lagna Moon by paksha", "PVR 表 30 附注:动星座上升的月亮按盈亏月判定") return text.replace("PVR §13.2 Table 30", "PVR 表 30") def _profile_short(value: Any, language: str) -> str: return _pick(_PROFILE_SHORT.get(str(value)), language) or ("另一套口径" if language == "zh" else "the other profile") def functional_profile_reader_label(value: Any, language: str) -> str: """Profile name and version in words; an unlisted profile is said to be unlisted, never printed as a key.""" label = _pick(_PROFILE.get(str(value or "")), language) if label: return label if not value: return "口径未注明" if language == "zh" else "Profile not stated" return "口径不在本报告的名称表内" if language == "zh" else "Profile is not in this report's name table" def functional_layer_rows(functional: Any, language: str) -> list[list[str]]: """[label, value] rows for the functional layer table. Rows with no stored value are left out.""" if not isinstance(functional, dict): return [] zh = language == "zh" sep = ";" if zh else "; " rows: list[list[str]] = [] def add(label_zh: str, label_en: str, value: Any) -> None: if value not in (None, ""): rows.append([label_zh if zh else label_en, str(value)]) status = functional.get("status") if status: add("状态", "Status", _pick(_STATUS.get(str(status)), language) or ("状态未在名称表内" if zh else "Status is not in the name table")) if functional.get("ascendant"): add("上升星座", "Ascendant sign", sign_name(functional.get("ascendant"), language)) if functional.get("lagna_lord"): add("上升主星", "Ascendant lord", planet_name(functional.get("lagna_lord"), language)) if "functional_profile" in functional: add("功能吉凶口径", "Functional profile", functional_profile_reader_label(functional.get("functional_profile"), language)) # BUG-2129 (负责人 2026-11-21): the profile's book citation is not shown to the reader. if functional.get("functional_profile") == "pvr_table30_v1" and functional.get("functional_profile_ruling"): add( "口径选用说明", "How the profile is chosen", "默认用 PVR 表 30;BPHS 例外表是另一套具名口径,只在明确选用时参与判断。" if zh else "PVR Table 30 is the default. The BPHS exception table is a separate named profile and takes part only when it is explicitly selected.", ) add("功能吉星", "Functional benefics", _planets(functional.get("functional_benefics"), language)) add("功能凶星", "Functional malefics", _planets(functional.get("functional_malefics"), language)) add("功能中性", "Functional neutrals", _planets(functional.get("functional_neutrals"), language)) add("瑜伽主星", "Yogakarakas", _planets(functional.get("yogakarakas"), language)) if functional.get("blocked_planets"): add("未能判定的行星", "Planets not assessed", _planets(functional.get("blocked_planets"), language)) definition = _pick(_YOGAKARAKA_DEFINITION.get(str(functional.get("yogakaraka_definition") or "")), language) add("瑜伽主星的定义", "Yogakaraka definition", definition) owned = functional.get("owned_houses") if isinstance(owned, dict) and owned: parts = [] for name, houses in _ordered(owned): numbers = [str(item) for item in houses] if isinstance(houses, (list, tuple)) else [] if not numbers: continue parts.append( f"{planet_name(name, language)}:第 {'、'.join(numbers)} 宫" if zh else f"{name}: house {', '.join(numbers)}" if len(numbers) == 1 else f"{name}: houses {', '.join(numbers)}" ) add("各行星所管宫位", "Houses owned by each planet", sep.join(parts)) basis = functional.get("role_basis") if isinstance(basis, dict) and basis: values = {str(value) for value in basis.values()} if len(values) == 1: text = _basis_text(values.pop(), language) else: text = sep.join( f"{planet_name(name, language)}{':' if zh else ': '}{_basis_text(str(value), language)}" for name, value in _ordered(basis) ) add("各行星的判定依据", "Basis for each planet", text) moon = functional.get("movable_lagna_moon") if isinstance(moon, dict): rule = moon.get("rule_zh") if zh else moon.get("rule") paksha = _pick(_PAKSHA.get(str(moon.get("moon_paksha"))), language) role = _pick(_ROLE.get(str(moon.get("role"))), language) tail = "" if paksha and role: tail = f"本盘月亮为{paksha},功能属性为{role}。" if zh else f" In this chart the Moon is {paksha}; its functional nature is {role}." if rule or tail: add("动星座上升的月亮", "Moon for a movable ascendant", (f"{rule}。{tail}" if zh else f"{rule}{tail}") if rule else tail.strip()) divergence = functional.get("profile_divergence") if isinstance(divergence, dict) and divergence.get("status"): selected = _profile_short(divergence.get("selected_profile"), language) other = _profile_short(divergence.get("other_profile"), language) cells = [] for cell in divergence.get("cells") or []: if not isinstance(cell, dict) or not cell.get("planet"): continue mine = _pick(_ROLE.get(str(cell.get(divergence.get("selected_profile")))), language) theirs = _pick(_ROLE.get(str(cell.get(divergence.get("other_profile")))), language) if not mine or not theirs: continue cells.append( f"{planet_name(cell['planet'], language)}:{selected} 为{mine},{other} 为{theirs}" if zh else f"{cell['planet']}: {mine} under {selected}, {theirs} under {other}" ) scored = divergence.get("other_profile_scored") tail = "" if scored is False: tail = f"{other} 只作对照,不参与判断。" if zh else f" {other[:1].upper() + other[1:]} is for comparison only and takes no part in the judgment." if divergence.get("status") == "same": text = f"本盘两套口径({selected}、{other})结果相同。" if zh else f"For this chart {selected} and {other} give the same roles." elif cells: text = ("两套口径有差异:" + ";".join(cells) + "。") if zh else ("The two profiles differ: " + "; ".join(cells) + ".") else: text = "两套口径有差异,差异的行星本次未列出。" if zh else "The two profiles differ; the planets that differ are not listed this time." add("与另一套口径对照", "Comparison with the other profile", text + tail) notes = functional.get("ambiguous_cell_notes") if isinstance(notes, dict) and notes: parts = [] for name, note in _ordered(notes): if not isinstance(note, dict): continue summary = note.get("summary") if zh else note.get("summary_en") reason = note.get("basis") if zh else note.get("basis_en") if not summary: continue applies = _profile_short(note.get("applies_to_profile"), language) if note.get("applies_to_profile") else None pvr = _pick(_ROLE.get(str(note.get("pvr_table_30_role"))), language) extra = [] if reason: extra.append(f"依据:{reason}" if zh else f"basis: {reason}") if applies: extra.append(f"适用于 {applies}" if zh else f"applies to {applies}") if pvr: extra.append(f"PVR 表 30 里为{pvr}" if zh else f"{pvr} in PVR Table 30") body = f"{planet_name(name, language)}{':' if zh else ': '}{summary}" if extra: body += f"({';'.join(extra)})" if zh else f" ({'; '.join(extra)})" parts.append(body) add("有分歧的行星说明", "Notes on planets read differently", sep.join(parts)) if functional.get("effect_on_confidence") or functional.get("confidence_impact"): add( "对把握程度的影响", "Effect on confidence", "功能吉凶与自然吉凶相冲突时,降低把握程度,并写明冲突。" if zh else "Confidence is reduced, and the conflict is stated, when functional and natural indications conflict.", ) label = functional.get("assertion_label") if label: add("结论性质", "Kind of statement", _pick(_ASSERTION.get(str(label)), language) or ("未在名称表内" if zh else "Not in the name table")) note = functional.get("dual_lordship_note") if zh else functional.get("dual_lordship_note_en") if isinstance(note, str): # BUG-2129: the book section is not shown to the reader. note = re.sub(r"^PVR §13\.2[::]\s*", "", note) add("管两个宫的行星", "Planets owning two houses", note) conditions = functional.get("conditions") if isinstance(conditions, (list, tuple)) and conditions: if zh: texts = [_CONDITIONS_ZH[item] for item in conditions if item in _CONDITIONS_ZH] else: texts = [str(item).replace(" (PVR §13.2)", "") for item in conditions if isinstance(item, str) and not re.search(r"[一-鿿]", item)] add("使用条件", "Conditions of use", " ".join(texts) if not zh else "".join(texts)) return rows