emmental/emmental/texture.py

27 lines
879 B
Python

# Copyright 2023 (c) Anna Schumaker.
"""A cache to hold Gdk.Textures used by cover art."""
import pathlib
from gi.repository import Gdk
class _TextureCache(dict):
"""A custom dictionary for storing texture files."""
def __missing__(self, path: pathlib.Path | None) -> Gdk.Texture:
"""Load a cache item from disk or add a new item entirely."""
texture = Gdk.Texture.new_from_filename(str(path))
self.__setitem__(path, texture)
return texture
def __getitem__(self, path: pathlib.Path | None) -> Gdk.Texture | None:
"""Get a Gdk.Texture cache item from the cache."""
if path is not None and path.is_file():
return super().__getitem__(path)
def drop(self, path: pathlib.Path | None) -> None:
"""Drop a single cache item from the cache."""
self.pop(path, None)
CACHE = _TextureCache()