lazy-player/lazy_player/file_model.py

57 lines
1.4 KiB
Python
Raw Normal View History

2025-03-08 23:34:19 +01:00
from __future__ import annotations
import os
from enum import Enum, auto
2025-03-09 11:49:42 +01:00
from pathlib import Path
2025-03-09 09:57:24 +01:00
from typing import overload
2025-03-08 23:34:19 +01:00
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):
2025-03-09 09:51:10 +01:00
file_type: FileType
2025-03-09 11:49:42 +01:00
full_path: Path
2025-03-09 09:51:10 +01:00
2025-03-08 23:34:19 +01:00
__gtype_name__ = "FileItem"
2025-03-09 10:52:34 +01:00
attrs_changed = GObject.Property(type=int, default=0)
2025-03-09 11:49:42 +01:00
def __init__(self, name: str, file_type: FileType, full_path: Path):
2025-03-08 23:34:19 +01:00
super().__init__()
2025-03-09 10:52:34 +01:00
self.attrs_changed = 0
2025-03-08 23:34:19 +01:00
self.name = name
self.file_type = file_type
self.full_path = full_path
2025-03-09 09:57:24 +01:00
@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)
2025-03-09 10:08:26 +01:00
except OSError:
2025-03-09 09:57:24 +01:00
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"))
2025-03-09 10:08:26 +01:00
except OSError:
pass
2025-03-09 10:52:34 +01:00
self.notify("attrs-changed")