""" 从 sendmsg/send.conf 读取 [database],填充到 user-star/daily_deduct.conf。 保留 daily_deduct.conf 中已有的 [service] 配置。 运行:python user-star/sync_deduct_conf.py(在项目根或 user-star 下均可) """ import configparser import sys from pathlib import Path USER_STAR_DIR = Path(__file__).resolve().parent SEND_CONF = USER_STAR_DIR.parent / "sendmsg" / "send.conf" DEDUCT_CONF = USER_STAR_DIR / "daily_deduct.conf" DB_KEYS = ("mysql_host", "mysql_port", "mysql_user", "mysql_password", "mysql_database") SERVICE_DEFAULT = {"intimacy_rule_effective_date": "2026-02-13"} def main() -> int: if not SEND_CONF.exists(): print(f"❌ 未找到 {SEND_CONF}") return 1 send_cp = configparser.ConfigParser() send_cp.read(SEND_CONF, encoding="utf-8") if not send_cp.has_section("database"): print("❌ send.conf 中无 [database] 段") return 1 # 现有 daily_deduct.conf 的 [service] existing = configparser.ConfigParser() if DEDUCT_CONF.exists(): existing.read(DEDUCT_CONF, encoding="utf-8") service_date = SERVICE_DEFAULT["intimacy_rule_effective_date"] if existing.has_section("service") and existing.has_option("service", "intimacy_rule_effective_date"): service_date = existing.get("service", "intimacy_rule_effective_date", fallback=service_date).strip() or service_date # 按顺序写 [database](来自 send.conf)再 [service] out = configparser.ConfigParser() out.add_section("database") for key in DB_KEYS: out.set("database", key, send_cp.get("database", key, fallback="").strip()) out.add_section("service") out.set("service", "intimacy_rule_effective_date", service_date) with open(DEDUCT_CONF, "w", encoding="utf-8") as f: f.write("# 每日扣心脚本配置\n") f.write("# [database] 由 sync_deduct_conf.py 从 sendmsg/send.conf 同步,请勿手改。\n") f.write("# [service] 可在此修改。\n\n") out.write(f) print(f"✅ 已从 send.conf 同步 [database] 到 {DEDUCT_CONF}") return 0 if __name__ == "__main__": sys.exit(main())