Store library settings in the pool index and drop legacy sidecars

This commit is contained in:
2026-07-03 16:06:45 +02:00
parent f0f38d4257
commit 7ae0709f38
13 changed files with 379 additions and 2292 deletions
+11 -34
View File
@@ -1,5 +1,4 @@
from pathlib import Path
import json
from .tag import Tag
# Bump this when the csfd_cache schema changes to force re-fetch on next open.
@@ -11,9 +10,9 @@ class File:
def __init__(self, file_path: Path, tagmanager=None, index=None) -> None:
self.file_path = file_path
self.filename = file_path.name
self.metadata_filename = self.file_path.parent / f".{self.filename}.!tag"
# Optional unified pool index; when set, metadata lives there instead of
# in the sidecar file (see PoolIndex).
# Unified pool index; when set, metadata is persisted there (see
# PoolIndex). Without an index a File is an in-memory object only — no
# metadata is written to disk (there are no per-file sidecars anymore).
self.index = index
self.new = True
self.ignored = False
@@ -38,20 +37,12 @@ class File:
self.get_metadata()
def get_metadata(self) -> None:
if self.index is not None:
record = self.index.get(self.file_path)
if record is None:
self._init_new_metadata()
self.save_metadata()
else:
self._apply_record(record)
return
if not self.metadata_filename.exists():
record = self.index.get(self.file_path) if self.index is not None else None
if record is None:
self._init_new_metadata()
self.save_metadata()
else:
self.load_metadata()
self._apply_record(record)
def _init_new_metadata(self) -> None:
self.new = True
@@ -111,17 +102,9 @@ class File:
self.tags.append(tag)
def save_metadata(self):
data = self._build_record()
"""Persist metadata to the pool index; a no-op for an index-less File."""
if self.index is not None:
self.index.set(self.file_path, data)
return
with open(self.metadata_filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
def load_metadata(self) -> None:
with open(self.metadata_filename, "r", encoding="utf-8") as f:
data = json.load(f)
self._apply_record(data)
self.index.set(self.file_path, self._build_record())
def added_timestamp(self) -> float:
"""Epoch seconds for 'date added' — the stored ``added`` or the file mtime.
@@ -181,27 +164,21 @@ class File:
self.save_metadata()
def delete_metadata(self) -> None:
"""Remove this file's metadata (from the index, or its sidecar file)."""
"""Remove this file's metadata from the pool index (no-op without one)."""
if self.index is not None:
self.index.delete(self.file_path)
elif self.metadata_filename.exists():
self.metadata_filename.unlink()
def relocate(self, new_path: Path) -> None:
"""Point this File at a new path, moving its metadata along.
The physical file must already have been moved/renamed by the caller.
Drops the metadata under the old path (index key or sidecar) and rebinds
to the new path; call ``save_metadata()`` afterwards to write it back.
Drops the metadata under the old index key and rebinds to the new path;
call ``save_metadata()`` afterwards to write it back.
"""
old_metadata_filename = self.metadata_filename
if self.index is not None:
self.index.delete(self.file_path)
self.file_path = Path(new_path)
self.filename = self.file_path.name
self.metadata_filename = self.file_path.parent / f".{self.filename}.!tag"
if self.index is None and old_metadata_filename.exists():
old_metadata_filename.rename(self.metadata_filename)
def set_date(self, date_str: str | None):
"""Nastaví datum (např. '2025-09-25') nebo None pro smazání."""