QQnote-skill/scripts/init_private_config.py

71 lines
2.3 KiB
Python

#!/usr/bin/env python3
from __future__ import annotations
import argparse
import getpass
import json
import os
from pathlib import Path
SKILL_DIR = Path(__file__).resolve().parents[1]
DEFAULT_CONFIG = SKILL_DIR / "config" / "config.local.json"
def ask_secret(label: str, existing: str = "") -> str:
suffix = " [keep existing]" if existing else ""
value = getpass.getpass(f"{label}{suffix}: ").strip()
return value or existing
def ask_text(label: str, default: str = "") -> str:
suffix = f" [{default}]" if default else ""
value = input(f"{label}{suffix}: ").strip()
return value or default
def load_existing(path: Path) -> dict:
if not path.exists():
return {}
return json.loads(path.read_text(encoding="utf-8"))
def write_private_config(path: Path, data: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
try:
os.chmod(path, 0o600)
except OSError:
pass
def main() -> int:
parser = argparse.ArgumentParser(description="Create private local config for zotero-obsidian-literature.")
parser.add_argument("--config", default=str(DEFAULT_CONFIG))
args = parser.parse_args()
path = Path(args.config).expanduser().resolve()
existing = load_existing(path)
zotero = existing.get("zotero", {}) if isinstance(existing.get("zotero"), dict) else {}
llm = existing.get("awesomegpt", {}) if isinstance(existing.get("awesomegpt"), dict) else {}
data = {
"zotero": {
"api_key": ask_secret("Zotero Web API key", zotero.get("api_key", "")),
"user_id": ask_text("Zotero user ID, optional", zotero.get("user_id", "")),
},
"awesomegpt": {
"api_key": ask_secret("DeepSeek/AwesomeGPT API key", llm.get("api_key", "")),
"base_url": ask_text("DeepSeek/AwesomeGPT base URL", llm.get("base_url", "https://api.deepseek.com")),
"model": ask_text("DeepSeek/AwesomeGPT model", llm.get("model", "deepseek-v4-pro")),
},
}
write_private_config(path, data)
print(f"Wrote private config: {path}")
print("Do not commit this file. It is ignored by the skill .gitignore.")
return 0
if __name__ == "__main__":
raise SystemExit(main())