from __future__ import annotations import os from enum import Enum, auto from pathlib import Path from typing import overload import gi gi.require_version("GObject", "2.0") from gi.repository import GObject # NOQA: E402 class FileType(Enum): DIRECTORY = auto() VIDEO = auto() class FileItem(GObject.Object): file_type: FileType full_path: Path __gtype_name__ = "FileItem" attrs_changed = GObject.Property(type=int, default=0) def __init__(self, name: str, file_type: FileType, full_path: Path): super().__init__() self.attrs_changed = 0 self.name = name self.file_type = file_type self.full_path = full_path @staticmethod def from_path(path: str | Path) -> FileItem: full_path = Path(path).resolve() if full_path.is_dir(): return FileItem(full_path.name, FileType.DIRECTORY, full_path) if full_path.suffix in (".mkv", ".mp4", ".avi"): return FileItem(full_path.name, FileType.VIDEO, full_path) raise ValueError(f"Unsupported file type: {full_path}") @overload def load_attribute(self, name: str, dfl: str) -> str: ... @overload def load_attribute(self, name: str, dfl: int) -> int: ... def load_attribute(self, name: str, dfl: str | int) -> str | int: try: strval = os.getxattr(self.full_path, f"user.lazy_player.{name}") return type(dfl)(strval) except OSError: return dfl def save_attribute(self, name: str, value: str | float | int | None) -> None: try: if value is None: os.removexattr(self.full_path, f"user.lazy_player.{name}") else: os.setxattr(self.full_path, f"user.lazy_player.{name}", str(value).encode("utf8")) except OSError: pass self.notify("attrs-changed")