refactor: extract state management into dedicated module and persist across restarts

Move state handling out of daemon and paths into a new state module.

- Introduce state.py for loading, writing, and maintaining state.json
- Persist recorded file metadata across daemon restarts
- Add cleanup of stale state entries when files are deleted
- Rename cleanup_old_files to cleanup_physical_files for clarity
- Ensure state file is created lazily if missing
This commit is contained in:
2026-01-07 20:15:54 +00:00
parent a135b3f140
commit 23d576c438
3 changed files with 69 additions and 42 deletions

View File

@@ -8,6 +8,7 @@ from importlib import resources
APP_NAME = "rewind"
USER_CONFIG = Path.home() / ".rewind.toml"
STATE_NAME = "state.json"
def load_config() -> dict:
if USER_CONFIG.exists():
@@ -16,25 +17,4 @@ def load_config() -> dict:
# fallback to packaged default
with resources.files("rewind").joinpath("config.toml").open("rb") as f:
return tomllib.load(f)
def state_dir() -> Path:
base = os.path.expanduser("~/.local/share")
path = Path(base) / APP_NAME
path.mkdir(parents=True, exist_ok=True)
return path
def get_state_file_path() -> Path:
return state_dir() / "state.json"
def load_state() -> dict:
if not get_state_file_path().exists():
return {"files": []}
with get_state_file_path().open() as f:
return json.load(f)
def write_state(state: dict) -> None:
tmp = get_state_file_path().with_suffix(".tmp")
with tmp.open("w") as f:
json.dump(state, f, indent=2)
tmp.replace(get_state_file_path()) # atomic
return tomllib.load(f)