texture: Add a Texture Cache
The Texture Cache will be used to map filenames to Gdk.Textures loaded into memory. The application can then re-use textures instead of making expensive filesystem calls and loading the same images multiple times. Implements: #51 ("Texture Cache") Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
3b8fb8531e
commit
fa203a72dd
|
@ -0,0 +1,26 @@
|
|||
# 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()
|
|
@ -0,0 +1,41 @@
|
|||
# Copyright 2023 (c) Anna Schumaker.
|
||||
"""Tests our Gdk.Texture cache."""
|
||||
import emmental.texture
|
||||
import pathlib
|
||||
import tests.util
|
||||
import unittest
|
||||
from gi.repository import Gdk
|
||||
|
||||
|
||||
class TestTextureCache(unittest.TestCase):
|
||||
"""Test our custom cache dictionary."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up common variables."""
|
||||
self.cache = emmental.texture._TextureCache()
|
||||
|
||||
def test_init(self):
|
||||
"""Test that the cache dict is initialized properly."""
|
||||
self.assertIsInstance(emmental.texture.CACHE,
|
||||
emmental.texture._TextureCache)
|
||||
self.assertDictEqual(emmental.texture.CACHE, {})
|
||||
|
||||
self.assertIsInstance(self.cache, dict)
|
||||
self.assertDictEqual(self.cache, {})
|
||||
|
||||
def test_drop(self):
|
||||
"""Test dropping items from the cache."""
|
||||
self.cache[tests.util.COVER_JPG]
|
||||
self.cache.drop(tests.util.COVER_JPG)
|
||||
self.assertDictEqual(self.cache, {})
|
||||
|
||||
def test_getitem(self):
|
||||
"""Test getting and creating items in the cache dict."""
|
||||
self.assertIsNone(self.cache[None])
|
||||
self.assertIsNone(self.cache[pathlib.Path("/no/such/path")])
|
||||
self.assertDictEqual(self.cache, {})
|
||||
|
||||
texture = self.cache[tests.util.COVER_JPG]
|
||||
self.assertIsInstance(texture, Gdk.Texture)
|
||||
self.assertDictEqual(self.cache, {tests.util.COVER_JPG: texture})
|
||||
self.assertEqual(self.cache[tests.util.COVER_JPG], texture)
|
Loading…
Reference in New Issue