Add statistics and data-consistency menus, recently-added folder
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
||||
"""Auto-generated — do not edit manually."""
|
||||
__version__ = "1.6.0"
|
||||
__version__ = "1.7.0"
|
||||
|
||||
+5
-3
@@ -33,13 +33,15 @@ FOLDER_CONFIG_NAME = ".Curator.!ftag"
|
||||
# category's folders (e.g. "{year} - {title}{ext}"); fields:
|
||||
# title / year / rating / ext / stem / filename. Absent/None =
|
||||
# keep the pool filename. Pool files are never renamed by this.
|
||||
# Grouping folders are prefixed with "- " so DLNA/TV browsers sort the special
|
||||
# folders (Dle …, Tipy dne, Nově přidané) before the genre folders at the root.
|
||||
DEFAULT_TAG_SCHEMA = [
|
||||
{"category": "Žánr", "csfd_field": "genres", "transform": None, "filmoteka_root": ""},
|
||||
{"category": "Rok", "csfd_field": "year", "transform": None, "filmoteka_root": "Dle roku"},
|
||||
{"category": "Rok", "csfd_field": "year", "transform": None, "filmoteka_root": "- Dle roku"},
|
||||
{"category": "Země původu", "csfd_field": "countries", "transform": None,
|
||||
"filmoteka_root": "Dle země původu"},
|
||||
"filmoteka_root": "- Dle země původu"},
|
||||
{"category": "Hodnocení", "csfd_field": "rating", "transform": "decade_band",
|
||||
"filmoteka_root": "Dle hodnocení"},
|
||||
"filmoteka_root": "- Dle hodnocení"},
|
||||
]
|
||||
|
||||
DEFAULT_GLOBAL_CONFIG = {
|
||||
|
||||
@@ -24,6 +24,9 @@ class File:
|
||||
# movie-library fields
|
||||
self.title: str | None = None
|
||||
self.csfd_link: str | None = None
|
||||
# ISO timestamp of when the movie was added to the pool (set on import);
|
||||
# None for older/unknown → falls back to the file's ctime.
|
||||
self.added: str | None = None
|
||||
# Cached CSFD data — avoids re-fetching on every open
|
||||
self.csfd_cache: dict | None = None
|
||||
# full_paths of tags that came from ČSFD (vs. user-added); only these are
|
||||
@@ -60,6 +63,7 @@ class File:
|
||||
self.csfd_cache = None
|
||||
self.csfd_tag_paths = set()
|
||||
self.attributes = {}
|
||||
self.added = None
|
||||
|
||||
def _build_record(self) -> dict:
|
||||
data = {
|
||||
@@ -74,6 +78,7 @@ class File:
|
||||
"title": self.title,
|
||||
"csfd_link": self.csfd_link,
|
||||
"attributes": self.attributes,
|
||||
"added": self.added,
|
||||
}
|
||||
if self.csfd_cache is not None:
|
||||
data["csfd_cache"] = {"version": CSFD_CACHE_VERSION, **self.csfd_cache}
|
||||
@@ -89,6 +94,7 @@ class File:
|
||||
# Legacy records have no provenance → treated as all-user (empty set)
|
||||
self.csfd_tag_paths = set(data.get("csfd_tags", []))
|
||||
self.attributes = dict(data.get("attributes", {}))
|
||||
self.added = data.get("added", None)
|
||||
raw_cache = data.get("csfd_cache")
|
||||
if raw_cache and raw_cache.get("version") == CSFD_CACHE_VERSION:
|
||||
self.csfd_cache = {k: v for k, v in raw_cache.items() if k != "version"}
|
||||
@@ -117,6 +123,25 @@ class File:
|
||||
data = json.load(f)
|
||||
self._apply_record(data)
|
||||
|
||||
def added_timestamp(self) -> float:
|
||||
"""Epoch seconds for 'date added' — the stored ``added`` or the file mtime.
|
||||
|
||||
Used to order the "recently added" folder (newer = larger). The fallback
|
||||
is **mtime**, not ctime: creating the Filmotéka hardlinks bumps the source
|
||||
inode's ctime, which would make ctime-based ordering meaningless, whereas
|
||||
mtime (preserved from the source on import) stays stable.
|
||||
"""
|
||||
if self.added:
|
||||
try:
|
||||
from datetime import datetime
|
||||
return datetime.fromisoformat(self.added).timestamp()
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
return self.file_path.stat().st_mtime
|
||||
except OSError:
|
||||
return 0.0
|
||||
|
||||
def name_context(self) -> dict:
|
||||
"""Fields for a Filmotéka filename template (see config tag schema).
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
from collections import Counter
|
||||
import shutil
|
||||
from .file import File
|
||||
from .tag_manager import TagManager
|
||||
@@ -136,6 +138,83 @@ class FileManager:
|
||||
file_obj = File(each, self.tagmanager, index=self.index)
|
||||
self.filelist.append(file_obj)
|
||||
|
||||
def check_data_consistency(self) -> dict:
|
||||
"""Compare the pool index against the actual files in pool/Filmy.
|
||||
|
||||
Detects drift such as a movie file deleted directly (metadata left behind)
|
||||
or a file dropped into the pool without importing (no metadata).
|
||||
|
||||
Returns a dict with:
|
||||
ok — False if the pool isn't set (with 'error')
|
||||
missing — index keys whose file no longer exists on disk
|
||||
untracked — pool files (pool-relative) with no index entry
|
||||
index_count / disk_count — totals for the summary
|
||||
"""
|
||||
pool = self.pool_dir
|
||||
movies = self.movies_dir
|
||||
if not (pool and movies and movies.is_dir()):
|
||||
return {"ok": False, "error": "Pool není nastaven.",
|
||||
"missing": [], "untracked": []}
|
||||
|
||||
index = self.index or PoolIndex(pool)
|
||||
index_keys = set(index.records.keys())
|
||||
|
||||
disk_keys = set()
|
||||
for f in list_files(movies):
|
||||
if f.name.endswith(METADATA_SUFFIXES):
|
||||
continue
|
||||
disk_keys.add(f.relative_to(pool).as_posix())
|
||||
|
||||
missing = sorted(k for k in index_keys if not (pool / k).is_file())
|
||||
untracked = sorted(disk_keys - index_keys)
|
||||
return {
|
||||
"ok": True,
|
||||
"missing": missing,
|
||||
"untracked": untracked,
|
||||
"index_count": len(index_keys),
|
||||
"disk_count": len(disk_keys),
|
||||
}
|
||||
|
||||
def statistics(self) -> dict:
|
||||
"""Aggregate stats over the loaded pool movies for the stats view.
|
||||
|
||||
Returns a dict with totals and, per tag category, the tag counts sorted
|
||||
by frequency (descending).
|
||||
"""
|
||||
files = self.filelist
|
||||
total_size = 0
|
||||
with_csfd = 0
|
||||
untagged = 0
|
||||
ratings: list[int] = []
|
||||
categories: dict[str, Counter] = {}
|
||||
|
||||
for f in files:
|
||||
try:
|
||||
total_size += f.file_path.stat().st_size
|
||||
except OSError:
|
||||
pass
|
||||
if f.csfd_link:
|
||||
with_csfd += 1
|
||||
if not f.tags:
|
||||
untagged += 1
|
||||
for tag in f.tags:
|
||||
categories.setdefault(tag.category, Counter())[tag.name] += 1
|
||||
rating = (f.csfd_cache or {}).get("rating")
|
||||
if isinstance(rating, (int, float)):
|
||||
ratings.append(rating)
|
||||
|
||||
return {
|
||||
"count": len(files),
|
||||
"total_size": total_size,
|
||||
"with_csfd": with_csfd,
|
||||
"without_csfd": len(files) - with_csfd,
|
||||
"untagged": untagged,
|
||||
"avg_rating": round(sum(ratings) / len(ratings), 1) if ratings else None,
|
||||
"categories": {
|
||||
cat: counter.most_common() for cat, counter in categories.items()
|
||||
},
|
||||
}
|
||||
|
||||
def pooled_with_stem(self, title: str) -> list[File]:
|
||||
"""Pooled movies whose filename stem matches ``title`` (case-insensitive)."""
|
||||
stem = title.strip().lower()
|
||||
@@ -204,6 +283,7 @@ class FileManager:
|
||||
file_obj = File(target, self.tagmanager, index=self.index)
|
||||
file_obj.title = safe_title
|
||||
file_obj.csfd_link = csfd_link or None
|
||||
file_obj.added = datetime.now().isoformat(timespec="seconds")
|
||||
file_obj.save_metadata()
|
||||
|
||||
self.filelist.append(file_obj)
|
||||
|
||||
@@ -274,32 +274,20 @@ class HardlinkManager:
|
||||
|
||||
return success_count, fail_count
|
||||
|
||||
def generate_random_tips(
|
||||
self,
|
||||
files: List[File],
|
||||
count: int = 10,
|
||||
subfolder: str = "Tipy dne",
|
||||
dry_run: bool = False,
|
||||
def _fill_special_folder(
|
||||
self, chosen: List[File], subfolder: str, dry_run: bool
|
||||
) -> Tuple[int, int]:
|
||||
"""(Re)fill ``subfolder`` with ``count`` random files as hardlinks.
|
||||
|
||||
The folder is emptied first so each run picks a fresh random selection.
|
||||
``files`` should be the pool movies only (copy-as-is mirrors excluded).
|
||||
|
||||
Returns:
|
||||
Tuple of (created_links, failed_links)
|
||||
"""
|
||||
"""Empty ``subfolder`` and hardlink ``chosen`` files into it."""
|
||||
base = self.output_dir / subfolder
|
||||
|
||||
if not dry_run and base.exists():
|
||||
for child in base.iterdir():
|
||||
if child.is_file():
|
||||
if child.is_file() or child.is_symlink():
|
||||
try:
|
||||
child.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
chosen = random.sample(files, min(count, len(files)))
|
||||
created = 0
|
||||
fail = 0
|
||||
for file_obj in chosen:
|
||||
@@ -318,9 +306,41 @@ class HardlinkManager:
|
||||
except OSError as e:
|
||||
self.errors.append((file_obj.file_path, str(e)))
|
||||
fail += 1
|
||||
|
||||
return created, fail
|
||||
|
||||
def generate_recently_added(
|
||||
self,
|
||||
files: List[File],
|
||||
count: int = 10,
|
||||
subfolder: str = "- Nově přidané",
|
||||
dry_run: bool = False,
|
||||
) -> Tuple[int, int]:
|
||||
"""(Re)fill ``subfolder`` with the ``count`` most recently added movies.
|
||||
|
||||
Ordering is by ``File.added_timestamp()`` (import time, ctime fallback),
|
||||
newest first. Returns (created_links, failed_links).
|
||||
"""
|
||||
chosen = sorted(files, key=lambda f: f.added_timestamp(), reverse=True)[:count]
|
||||
return self._fill_special_folder(chosen, subfolder, dry_run)
|
||||
|
||||
def generate_random_tips(
|
||||
self,
|
||||
files: List[File],
|
||||
count: int = 10,
|
||||
subfolder: str = "Tipy dne",
|
||||
dry_run: bool = False,
|
||||
) -> Tuple[int, int]:
|
||||
"""(Re)fill ``subfolder`` with ``count`` random files as hardlinks.
|
||||
|
||||
The folder is emptied first so each run picks a fresh random selection.
|
||||
``files`` should be the pool movies only (copy-as-is mirrors excluded).
|
||||
|
||||
Returns:
|
||||
Tuple of (created_links, failed_links)
|
||||
"""
|
||||
chosen = random.sample(files, min(count, len(files)))
|
||||
return self._fill_special_folder(chosen, subfolder, dry_run)
|
||||
|
||||
def _is_same_file(self, path1: Path, path2: Path) -> bool:
|
||||
"""Check if two paths point to the same file (same inode)."""
|
||||
try:
|
||||
@@ -458,23 +478,20 @@ class HardlinkManager:
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
# Build expected paths for each file based on current tags
|
||||
expected_paths: dict[int, set[Path]] = {}
|
||||
# Every link the tag tree *should* contain, given current files+tags.
|
||||
expected_all: set[Path] = set()
|
||||
for file_obj in files:
|
||||
try:
|
||||
inode = file_obj.file_path.stat().st_ino
|
||||
expected_paths[inode] = set()
|
||||
for tag in file_obj.tags:
|
||||
target_dir = self._target_dir(tag, roots, category_transforms)
|
||||
if target_dir is None:
|
||||
continue
|
||||
expected_all.add(target_dir / self._link_name(
|
||||
file_obj, tag, category_filename_templates))
|
||||
|
||||
for tag in file_obj.tags:
|
||||
target_dir = self._target_dir(tag, roots, category_transforms)
|
||||
if target_dir is None:
|
||||
continue
|
||||
expected_paths[inode].add(target_dir / self._link_name(
|
||||
file_obj, tag, category_filename_templates))
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
# Scan only the tag-tree's own top-level folders (skip copy-as-is mirrors)
|
||||
# Scan only the tag-tree's own top-level folders (skip copy-as-is mirrors).
|
||||
# Inside them, any file that isn't an expected link is obsolete — whether
|
||||
# a tag was removed, the movie was replaced (orphan inode), or the folder
|
||||
# itself is a leftover from a renamed layout.
|
||||
top_dirs = self._managed_top_dirs(
|
||||
files, roots, category_transforms, reserved_subfolders)
|
||||
for top in self.output_dir.iterdir():
|
||||
@@ -483,18 +500,16 @@ class HardlinkManager:
|
||||
if top_dirs is not None and top.name not in top_dirs:
|
||||
continue
|
||||
|
||||
# Depth-agnostic: genres sit one level deep, "Dle roku"/"Dle země
|
||||
# původu" two levels deep — walk all files under the managed folder.
|
||||
for link_file in top.rglob("*"):
|
||||
if not link_file.is_file():
|
||||
continue
|
||||
try:
|
||||
link_inode = link_file.stat().st_ino
|
||||
if link_inode in expected_paths:
|
||||
if link_file not in expected_paths[link_inode]:
|
||||
obsolete.append((link_file, inode_to_file[link_inode].file_path))
|
||||
except OSError:
|
||||
if link_file in expected_all:
|
||||
continue
|
||||
try:
|
||||
source = inode_to_file[link_file.stat().st_ino].file_path
|
||||
except (OSError, KeyError):
|
||||
source = link_file # orphan (no current pool movie)
|
||||
obsolete.append((link_file, source))
|
||||
|
||||
return obsolete
|
||||
|
||||
|
||||
+111
-7
@@ -30,6 +30,12 @@ from src.core.tag import Tag
|
||||
from src.constants import APP_TITLE
|
||||
from src.core.hardlink_manager import HardlinkManager
|
||||
|
||||
# Special auto-generated Filmotéka folders. The "- " prefix makes DLNA/TV
|
||||
# browsers sort them ahead of the genre folders at the output root.
|
||||
TIPS_SUBFOLDER = "- Tipy dne" # random selection, refreshed each run
|
||||
RECENT_SUBFOLDER = "- Nově přidané" # most recently added movies
|
||||
TIPS_COUNT = 15 # random tips
|
||||
RECENT_COUNT = 10 # newest by date added
|
||||
|
||||
|
||||
class ImportMoviesDialog(QDialog):
|
||||
@@ -408,6 +414,44 @@ class TagSchemaDialog(QDialog):
|
||||
self.accept()
|
||||
|
||||
|
||||
class StatsDialog(QDialog):
|
||||
"""Read-only overview of the pool: totals + per-category tag breakdown."""
|
||||
|
||||
def __init__(self, parent: QWidget, stats: dict, size_text: str) -> None:
|
||||
super().__init__(parent)
|
||||
self.setWindowTitle("Statistiky knihovny")
|
||||
self.setMinimumSize(460, 560)
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
avg = stats["avg_rating"]
|
||||
summary = (
|
||||
f"<b>Filmů v poolu:</b> {stats['count']}<br>"
|
||||
f"<b>Celková velikost:</b> {size_text}<br>"
|
||||
f"<b>Průměrné hodnocení ČSFD:</b> "
|
||||
f"{str(avg) + ' %' if avg is not None else '—'}<br>"
|
||||
f"<b>S ČSFD odkazem:</b> {stats['with_csfd']} · "
|
||||
f"<b>bez:</b> {stats['without_csfd']}<br>"
|
||||
f"<b>Bez štítků:</b> {stats['untagged']}"
|
||||
)
|
||||
layout.addWidget(QLabel(summary))
|
||||
|
||||
tree = QTreeWidget()
|
||||
tree.setHeaderLabels(["Kategorie / štítek", "Počet"])
|
||||
tree.setColumnWidth(0, 300)
|
||||
for category in sorted(stats["categories"]):
|
||||
entries = stats["categories"][category]
|
||||
cat_item = QTreeWidgetItem([f"{category} ({len(entries)})", ""])
|
||||
tree.addTopLevelItem(cat_item)
|
||||
for name, count in entries:
|
||||
cat_item.addChild(QTreeWidgetItem([name, str(count)]))
|
||||
layout.addWidget(tree)
|
||||
|
||||
buttons = QDialogButtonBox(QDialogButtonBox.Close)
|
||||
buttons.rejected.connect(self.reject)
|
||||
buttons.accepted.connect(self.accept)
|
||||
layout.addWidget(buttons)
|
||||
|
||||
|
||||
class QtApp(QMainWindow):
|
||||
def __init__(self, filehandler: FileManager, tagmanager: TagManager) -> None:
|
||||
super().__init__()
|
||||
@@ -470,6 +514,11 @@ class QtApp(QMainWindow):
|
||||
settings_menu = bar.addMenu("&Nastavení")
|
||||
self._add_action(settings_menu, "Tag schéma…", self.edit_tag_schema)
|
||||
|
||||
self._add_action(settings_menu, "Statistiky…", self.show_statistics)
|
||||
|
||||
tests_menu = bar.addMenu("&Testy")
|
||||
self._add_action(tests_menu, "Kontrola konzistence dat…", self.check_consistency)
|
||||
|
||||
def _add_action(self, menu: QMenu, text: str, slot, shortcut: str | None = None) -> QAction:
|
||||
action = QAction(text, self)
|
||||
if shortcut:
|
||||
@@ -957,7 +1006,8 @@ class QtApp(QMainWindow):
|
||||
QMessageBox.information(self, "Filmotéka", "Pool je prázdný.")
|
||||
return
|
||||
manager = HardlinkManager(out)
|
||||
reserved = set(self.filehandler.copyasis_folders) | {"Tipy dne"}
|
||||
reserved = (set(self.filehandler.copyasis_folders)
|
||||
| {TIPS_SUBFOLDER, RECENT_SUBFOLDER})
|
||||
created, create_fail, removed, remove_fail = manager.sync_structure(
|
||||
files,
|
||||
category_roots=self.filehandler.filmoteka_category_roots(),
|
||||
@@ -978,22 +1028,26 @@ class QtApp(QMainWindow):
|
||||
mirrored += m_created
|
||||
mirror_fail += m_failed
|
||||
|
||||
# "Tipy dne": 10 random pool movies (copy-as-is folders excluded)
|
||||
tips, tips_fail = manager.generate_random_tips(files, count=10)
|
||||
# Special folders (pool only; copy-as-is excluded): random tips + newest
|
||||
tips, tips_fail = manager.generate_random_tips(
|
||||
files, count=TIPS_COUNT, subfolder=TIPS_SUBFOLDER)
|
||||
recent, recent_fail = manager.generate_recently_added(
|
||||
files, count=RECENT_COUNT, subfolder=RECENT_SUBFOLDER)
|
||||
|
||||
msg = (
|
||||
f"Filmy — vytvořeno: {created}, odebráno zastaralých: {removed}\n"
|
||||
f"Copy-as-is — zrcadleno: {mirrored}\n"
|
||||
f"Tipy dne — náhodně: {tips}"
|
||||
f"Tipy dne — náhodně: {tips}, Nově přidané: {recent}"
|
||||
)
|
||||
if create_fail or remove_fail or mirror_fail or tips_fail:
|
||||
msg += f"\nSelhalo: {create_fail + remove_fail + mirror_fail + tips_fail}"
|
||||
failed = create_fail + remove_fail + mirror_fail + tips_fail + recent_fail
|
||||
if failed:
|
||||
msg += f"\nSelhalo: {failed}"
|
||||
QMessageBox.warning(self, "Filmotéka dokončena s chybami", msg)
|
||||
else:
|
||||
QMessageBox.information(self, "Filmotéka vygenerována", msg)
|
||||
self.status.showMessage(
|
||||
f"Filmotéka: filmy +{created}/-{removed}, copy-as-is +{mirrored}, "
|
||||
f"tipy +{tips}", 5000
|
||||
f"tipy +{tips}, nově +{recent}", 5000
|
||||
)
|
||||
|
||||
def edit_copyasis_folders(self) -> None:
|
||||
@@ -1021,6 +1075,56 @@ class QtApp(QMainWindow):
|
||||
8000,
|
||||
)
|
||||
|
||||
def show_statistics(self) -> None:
|
||||
if not self.filehandler.filelist:
|
||||
QMessageBox.information(self, "Statistiky", "Pool je prázdný.")
|
||||
return
|
||||
stats = self.filehandler.statistics()
|
||||
StatsDialog(self, stats, self._format_size(stats["total_size"])).exec()
|
||||
|
||||
def check_consistency(self) -> None:
|
||||
"""Test: does the pool index match the actual files in pool/Filmy?"""
|
||||
if not self.filehandler.movies_dir:
|
||||
QMessageBox.information(self, "Testy", "Nejprve nastavte pool.")
|
||||
return
|
||||
result = self.filehandler.check_data_consistency()
|
||||
if not result["ok"]:
|
||||
QMessageBox.warning(self, "Kontrola konzistence", result["error"])
|
||||
return
|
||||
|
||||
missing, untracked = result["missing"], result["untracked"]
|
||||
if not missing and not untracked:
|
||||
QMessageBox.information(
|
||||
self, "Kontrola konzistence dat",
|
||||
f"✅ Vše v pořádku — index a pool si odpovídají.\n\n"
|
||||
f"Záznamů v indexu: {result['index_count']}\n"
|
||||
f"Souborů v poolu: {result['disk_count']}",
|
||||
)
|
||||
return
|
||||
|
||||
detail: list[str] = []
|
||||
if missing:
|
||||
detail.append(
|
||||
f"■ Metadata bez souboru ({len(missing)}) — soubor byl smazán "
|
||||
"přímo, záznam v indexu zůstal:")
|
||||
detail += [f" • {k}" for k in missing]
|
||||
detail.append("")
|
||||
if untracked:
|
||||
detail.append(
|
||||
f"■ Soubory bez metadat ({len(untracked)}) — přidané do poolu "
|
||||
"mimo import:")
|
||||
detail += [f" • {k}" for k in untracked]
|
||||
|
||||
box = QMessageBox(self)
|
||||
box.setIcon(QMessageBox.Warning)
|
||||
box.setWindowTitle("Kontrola konzistence — nalezeny nesrovnalosti")
|
||||
box.setText(
|
||||
f"Metadata bez souboru: {len(missing)}\n"
|
||||
f"Soubory bez metadat: {len(untracked)}\n\n"
|
||||
"Podrobnosti zobrazíš přes „Show Details…\".")
|
||||
box.setDetailedText("\n".join(detail))
|
||||
box.exec()
|
||||
|
||||
def closeEvent(self, event) -> None: # noqa: N802 — Qt override
|
||||
self.filehandler.global_config["window_geometry"] = f"{self.width()}x{self.height()}"
|
||||
from src.core.config import save_global_config
|
||||
|
||||
Reference in New Issue
Block a user