~ubuntu-branches/ubuntu/saucy/moovida-plugins-good/saucy-proposed

« back to all changes in this revision

Viewing changes to elisa/plugins/base/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2009-08-16 13:12:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090816131252-ky8ukfq1i0ft33o0
Tags: 1.0.6-0ubuntu1
New upstream release. (LP: #414267)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Moovida - Home multimedia server
 
3
# Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com).
 
4
# All rights reserved.
 
5
#
 
6
# This file is available under one of two license agreements.
 
7
#
 
8
# This file is licensed under the GPL version 3.
 
9
# See "LICENSE.GPL" in the root of this distribution including a special
 
10
# exception to use Moovida with Fluendo's plugins.
 
11
#
 
12
# The GPL part of Moovida is also available under a commercial licensing
 
13
# agreement from Fluendo.
 
14
# See "LICENSE.Moovida" in the root directory of this distribution package
 
15
# for details on that license.
 
16
#
 
17
# Author: Olivier Tilloy <olivier@fluendo.com>
 
18
 
 
19
"""
 
20
Basic utility functions.
 
21
"""
 
22
 
 
23
from elisa.core import common
 
24
from elisa.core.utils import defer, caching
 
25
 
 
26
import os.path
 
27
 
 
28
 
 
29
def get_and_cache_image(image_uri):
 
30
    """
 
31
    Get and cache an image for which we know a resource provider is available.
 
32
 
 
33
    If the image is local or already cached on disk, do not perform the
 
34
    request.
 
35
 
 
36
    The get request that may be performed to obtain the image is expected to
 
37
    return a L{elisa.plugins.base.models.media.RawDataModel}.
 
38
 
 
39
    @param image_uri: the URI of the image
 
40
    @type image_uri:  L{elisa.core.media_uri.MediaUri}
 
41
 
 
42
    @return: a deferred fired when the image was cached, with the full path to
 
43
             the cached file
 
44
    @rtype:  L{elisa.core.utils.defer.Deferred} with C{unicode}
 
45
    """
 
46
    cache_file = caching.get_cached_image_path(image_uri)
 
47
    if os.path.exists(cache_file):
 
48
        return defer.succeed(cache_file)
 
49
 
 
50
    def cache_data_to_file(model, cache_file):
 
51
        return caching.cache_to_file(model.data, cache_file)
 
52
 
 
53
    model, dfr = common.application.resource_manager.get(image_uri)
 
54
    dfr.addCallback(cache_data_to_file, cache_file)
 
55
    return dfr
 
56
 
 
57
 
 
58
def get_and_cache_thumbnail(item, thumbnail_uri):
 
59
    """
 
60
    Get and cache a thumbnail for a given item.
 
61
 
 
62
    If the thumbnail is local or already cached on disk, do not perform the
 
63
    request.
 
64
 
 
65
    The given thumbnail URI is assumed to have a corresponding resource
 
66
    provider registered and available (in case it is not a local resource).
 
67
 
 
68
    The get request that may be performed to obtain the thumbnail is expected
 
69
    to return a L{elisa.plugins.base.models.media.RawDataModel}.
 
70
 
 
71
    @param item:          any model for which we want to request a thumbnail
 
72
    @param thumbnail_uri: the URI to the distant thumbnail
 
73
    @type thumbnail_uri:  L{elisa.core.media_uri.MediaUri}
 
74
 
 
75
    @return: a deferred fired when the thumbnail was cached, with the full path
 
76
             to the cached file
 
77
    @rtype:  L{elisa.core.utils.defer.Deferred} with C{unicode}
 
78
    """
 
79
    # This is a two-level caching mechanism:
 
80
    #  1) First check if the item has a "thumbnail_path" attribute. If it does,
 
81
    #     trust that the referenced thumbnail exists and is valid;
 
82
    #  2) If not, compute the thumbnail path and get it where needed.
 
83
    try:
 
84
        # Note: the reference to the local path of the thumbnail being stored
 
85
        # in the item's "thumbnail_path" attribute is a convention that was
 
86
        # historically used here and there in various places of the code.
 
87
        # Change where it is stored at your own risk! It may (or may not) break
 
88
        # thumbnailing in various places, depending on how it was first
 
89
        # written.
 
90
        return defer.succeed(item.thumbnail_path)
 
91
    except AttributeError:
 
92
        def set_thumbnail_path(thumbnail_path, item):
 
93
            item.thumbnail_path = thumbnail_path
 
94
            return thumbnail_path
 
95
 
 
96
        dfr = get_and_cache_image(thumbnail_uri)
 
97
        dfr.addCallback(set_thumbnail_path, item)
 
98
        return dfr