[pitivi] previewers: Integrate the master cache in the ThumbnailCache class
- From: Alexandru Băluț <alexbalut src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pitivi] previewers: Integrate the master cache in the ThumbnailCache class
- Date: Thu, 6 Oct 2016 12:25:36 +0000 (UTC)
commit 9d1d846b056d2cf1e0cc292175b6bda22cf6d7aa
Author: Alexandru Băluț <alexandru balut gmail com>
Date: Wed Oct 5 13:07:37 2016 +0200
previewers: Integrate the master cache in the ThumbnailCache class
Reviewed-by: Thibault Saunier <tsaunier gnome org>
Differential Revision: https://phabricator.freedesktop.org/D1350
pitivi/medialibrary.py | 4 +-
pitivi/timeline/previewers.py | 62 ++++++++++++++++++++---------------------
tests/test_previewers.py | 22 +++++++++++++-
3 files changed, 52 insertions(+), 36 deletions(-)
---
diff --git a/pitivi/medialibrary.py b/pitivi/medialibrary.py
index a66922d..7137d51 100644
--- a/pitivi/medialibrary.py
+++ b/pitivi/medialibrary.py
@@ -45,7 +45,7 @@ from pitivi.dialogs.clipmediaprops import ClipMediaPropsDialog
from pitivi.dialogs.filelisterrordialog import FileListErrorDialog
from pitivi.mediafilespreviewer import PreviewWidget
from pitivi.settings import GlobalSettings
-from pitivi.timeline.previewers import getThumbnailCache
+from pitivi.timeline.previewers import ThumbnailCache
from pitivi.utils.loggable import Loggable
from pitivi.utils.misc import disconnectAllByFunc
from pitivi.utils.misc import get_proxy_target
@@ -792,7 +792,7 @@ class MediaLibraryWidget(Gtk.Box, Loggable):
thumb_128 = self._getIcon(
"image-x-generic", None, LARGE_SIZE)
else:
- thumb_cache = getThumbnailCache(asset)
+ thumb_cache = ThumbnailCache.get(asset)
thumb_64 = thumb_cache.getPreviewThumbnail()
if not thumb_64:
thumb_64 = self._getIcon("video-x-generic")
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index 1fe8ea9..0c43a0f 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -141,7 +141,7 @@ class ThumbnailBin(PreviewerBin):
def do_set_property(self, prop, value):
if prop.name == 'uri':
self.uri = value
- self.thumb_cache = getThumbnailCache(value)
+ self.thumb_cache = ThumbnailCache.get(self.uri)
else:
raise AttributeError('unknown property %s' % prop.name)
@@ -387,7 +387,7 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
# Maps (quantized) times to Thumbnail objects
self.thumbs = {}
- self.thumb_cache = getThumbnailCache(self.uri)
+ self.thumb_cache = ThumbnailCache.get(self.uri)
self.thumb_width, unused_height = self.thumb_cache.getImagesSize()
self.cpu_usage_tracker = CPUUsageTracker()
@@ -720,40 +720,16 @@ class Thumbnail(Gtk.Image):
self.props.width_request = self.width
self.props.height_request = self.height
-CACHES = {}
-
-
-# pylint: disable=invalid-name
-def getThumbnailCache(obj):
- """Gets a ThumbnailCache for the specified object.
-
- Args:
- obj (str or GES.UriClipAsset): The object for which to get a cache,
- it can be a string ora GES.UriClipAsset
-
- Returns:
- ThumbnailCache: The cache for the object.
- """
- if isinstance(obj, str):
- uri = obj
- elif isinstance(obj, GES.UriClipAsset):
- uri = get_proxy_target(obj).props.id
-
- if uri in CACHES:
- return CACHES[uri]
- else:
- cache = ThumbnailCache(uri)
- CACHES[uri] = cache
- return cache
-
class ThumbnailCache(Loggable):
- """Caches thumbnails by key using LRU policy.
+ """Caches an asset's thumbnails by key, using LRU policy.
Uses a two stage caching mechanism. A limited number of elements are
held in memory, the rest is being cached on disk in an SQLite db.
"""
+ caches_by_uri = {}
+
def __init__(self, uri):
Loggable.__init__(self)
self._filehash = hash_file(Gst.uri_get_location(uri))
@@ -766,6 +742,28 @@ class ThumbnailCache(Loggable):
(Time INTEGER NOT NULL PRIMARY KEY,\
Jpeg BLOB NOT NULL)")
+ @classmethod
+ def get(cls, obj):
+ """Gets a ThumbnailCache for the specified object.
+
+ Args:
+ obj (str or GES.UriClipAsset): The object for which to get a cache,
+ it can be a string representing a URI, or a GES.UriClipAsset.
+
+ Returns:
+ ThumbnailCache: The cache for the object.
+ """
+ if isinstance(obj, str):
+ uri = obj
+ elif isinstance(obj, GES.UriClipAsset):
+ uri = get_proxy_target(obj).props.id
+ else:
+ raise ValueError("Unhandled type: %s" % type(obj))
+
+ if uri not in cls.caches_by_uri:
+ cls.caches_by_uri[uri] = ThumbnailCache(uri)
+ return cls.caches_by_uri[uri]
+
def copy(self, uri):
"""Copies `self` to the specified `uri`.
@@ -863,7 +861,7 @@ class PipelineCpuAdapter(Loggable):
self.rate = 1.0
self.done = False
self.ready = False
- self.lastPos = 0
+ self.last_pos = 0
self._bus_cb_id = None
def start(self):
@@ -896,7 +894,7 @@ class PipelineCpuAdapter(Loggable):
res, position = self.pipeline.query_position(
Gst.Format.TIME)
if res:
- self.lastPos = position
+ self.last_pos = position
self.pipeline.set_state(Gst.State.READY)
self.ready = True
return True
@@ -943,7 +941,7 @@ class PipelineCpuAdapter(Loggable):
Gst.Format.TIME,
Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
Gst.SeekType.SET,
- self.lastPos,
+ self.last_pos,
Gst.SeekType.NONE,
-1)
self.ready = False
diff --git a/tests/test_previewers.py b/tests/test_previewers.py
index 4afeeaf..7c3a1c7 100644
--- a/tests/test_previewers.py
+++ b/tests/test_previewers.py
@@ -18,14 +18,16 @@
# Boston, MA 02110-1301, USA.
import os
import pickle
+import tempfile
from unittest import mock
+from unittest import TestCase
from gi.repository import GES
from gi.repository import Gst
from pitivi.timeline.previewers import get_wavefile_location_for_uri
-from pitivi.timeline.previewers import getThumbnailCache
from pitivi.timeline.previewers import THUMB_HEIGHT
+from pitivi.timeline.previewers import ThumbnailCache
from tests import common
from tests.test_media_library import BaseTestMediaLibrary
@@ -46,7 +48,7 @@ class TestPreviewers(BaseTestMediaLibrary):
sample_uri = common.get_sample_uri(sample_name)
asset = GES.UriClipAsset.request_sync(sample_uri)
- thumb_cache = getThumbnailCache(asset)
+ thumb_cache = ThumbnailCache.get(asset)
width, height = thumb_cache.getImagesSize()
self.assertEqual(height, THUMB_HEIGHT)
self.assertTrue(thumb_cache[0] is not None)
@@ -59,3 +61,19 @@ class TestPreviewers(BaseTestMediaLibrary):
samples = pickle.load(fsamples)
self.assertTrue(bool(samples))
+
+
+class TestThumbnailCache(TestCase):
+
+ def test_get(self):
+ with self.assertRaises(ValueError):
+ ThumbnailCache.get(1)
+ with mock.patch("pitivi.timeline.previewers.xdg_cache_home") as xdg_config_home,\
+ tempfile.TemporaryDirectory() as temp_dir:
+ xdg_config_home.return_value = temp_dir
+ sample_uri = common.get_sample_uri("1sec_simpsons_trailer.mp4")
+ cache = ThumbnailCache.get(sample_uri)
+ self.assertIsNotNone(cache)
+
+ asset = GES.UriClipAsset.request_sync(sample_uri)
+ self.assertEqual(ThumbnailCache.get(asset), cache)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]