sync_deduct_conf.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. 从 sendmsg/send.conf 读取 [database],填充到 user-star/daily_deduct.conf。
  3. 保留 daily_deduct.conf 中已有的 [service] 配置。
  4. 运行:python user-star/sync_deduct_conf.py(在项目根或 user-star 下均可)
  5. """
  6. import configparser
  7. import sys
  8. from pathlib import Path
  9. USER_STAR_DIR = Path(__file__).resolve().parent
  10. SEND_CONF = USER_STAR_DIR.parent / "sendmsg" / "send.conf"
  11. DEDUCT_CONF = USER_STAR_DIR / "daily_deduct.conf"
  12. DB_KEYS = ("mysql_host", "mysql_port", "mysql_user", "mysql_password", "mysql_database")
  13. SERVICE_DEFAULT = {"intimacy_rule_effective_date": "2026-02-13"}
  14. def main() -> int:
  15. if not SEND_CONF.exists():
  16. print(f"❌ 未找到 {SEND_CONF}")
  17. return 1
  18. send_cp = configparser.ConfigParser()
  19. send_cp.read(SEND_CONF, encoding="utf-8")
  20. if not send_cp.has_section("database"):
  21. print("❌ send.conf 中无 [database] 段")
  22. return 1
  23. # 现有 daily_deduct.conf 的 [service]
  24. existing = configparser.ConfigParser()
  25. if DEDUCT_CONF.exists():
  26. existing.read(DEDUCT_CONF, encoding="utf-8")
  27. service_date = SERVICE_DEFAULT["intimacy_rule_effective_date"]
  28. if existing.has_section("service") and existing.has_option("service", "intimacy_rule_effective_date"):
  29. service_date = existing.get("service", "intimacy_rule_effective_date", fallback=service_date).strip() or service_date
  30. # 按顺序写 [database](来自 send.conf)再 [service]
  31. out = configparser.ConfigParser()
  32. out.add_section("database")
  33. for key in DB_KEYS:
  34. out.set("database", key, send_cp.get("database", key, fallback="").strip())
  35. out.add_section("service")
  36. out.set("service", "intimacy_rule_effective_date", service_date)
  37. with open(DEDUCT_CONF, "w", encoding="utf-8") as f:
  38. f.write("# 每日扣心脚本配置\n")
  39. f.write("# [database] 由 sync_deduct_conf.py 从 sendmsg/send.conf 同步,请勿手改。\n")
  40. f.write("# [service] 可在此修改。\n\n")
  41. out.write(f)
  42. print(f"✅ 已从 send.conf 同步 [database] 到 {DEDUCT_CONF}")
  43. return 0
  44. if __name__ == "__main__":
  45. sys.exit(main())