feat(project): Segregate discussion history into sibling TOML file
This commit is contained in:
@@ -121,14 +121,59 @@ def default_project(name: str = "unnamed") -> dict:
|
||||
|
||||
# ── load / save ──────────────────────────────────────────────────────────────
|
||||
|
||||
def get_history_path(project_path: str | Path) -> Path:
|
||||
p = Path(project_path)
|
||||
return p.parent / f"{p.stem}_history.toml"
|
||||
|
||||
|
||||
def load_project(path) -> dict:
|
||||
with open(path, "rb") as f:
|
||||
return tomllib.load(f)
|
||||
proj = tomllib.load(f)
|
||||
|
||||
# Automatic Migration: move legacy 'discussion' to sibling file
|
||||
hist_path = get_history_path(path)
|
||||
if "discussion" in proj:
|
||||
disc = proj.pop("discussion")
|
||||
# Save to history file if it doesn't exist yet (or overwrite to migrate)
|
||||
with open(hist_path, "wb") as f:
|
||||
tomli_w.dump(disc, f)
|
||||
# Save the stripped project file
|
||||
save_project(proj, path)
|
||||
# Restore for the returned dict so GUI works as before
|
||||
proj["discussion"] = disc
|
||||
else:
|
||||
# Load from sibling if it exists
|
||||
if hist_path.exists():
|
||||
proj["discussion"] = load_history(path)
|
||||
|
||||
return proj
|
||||
|
||||
|
||||
def save_project(proj: dict, path):
|
||||
def load_history(project_path: str | Path) -> dict:
|
||||
hist_path = get_history_path(project_path)
|
||||
if hist_path.exists():
|
||||
with open(hist_path, "rb") as f:
|
||||
return tomllib.load(f)
|
||||
return {}
|
||||
|
||||
|
||||
def save_project(proj: dict, path, disc_data: dict | None = None):
|
||||
# Ensure 'discussion' is NOT in the main project dict
|
||||
if "discussion" in proj:
|
||||
# If disc_data wasn't provided, use the one from proj
|
||||
if disc_data is None:
|
||||
disc_data = proj["discussion"]
|
||||
# Remove it so it doesn't get saved to the main file
|
||||
proj = dict(proj) # shallow copy to avoid mutating caller's dict
|
||||
del proj["discussion"]
|
||||
|
||||
with open(path, "wb") as f:
|
||||
tomli_w.dump(proj, f)
|
||||
|
||||
if disc_data:
|
||||
hist_path = get_history_path(path)
|
||||
with open(hist_path, "wb") as f:
|
||||
tomli_w.dump(disc_data, f)
|
||||
|
||||
|
||||
# ── migration helper ─────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user