~khwalker/entertainer/config-keybindings

« back to all changes in this revision

Viewing changes to entertainerlib/utils/thumbnailer.py

The thumbnailers have all been consolidated into a single module.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Interface for image and video thumbnailers'''
2
 
 
3
 
__licence__ = "GPLv2"
4
 
__copyright__ = "2008, Paul Hummer"
5
 
__author__ = "Paul Hummer <paul@ironlionsoftware.no.spam.com>"
6
 
 
7
 
import os
8
 
 
9
 
try:
10
 
    import hashlib
11
 
except ImportError:
12
 
    import md5
13
 
 
14
 
from entertainerlib.exceptions import ThumbnailerException
15
 
from entertainerlib.utils.configuration import Configuration
16
 
 
17
 
class Thumbnailer(object):
18
 
    """ Thumbnailer interface for video and image thumbnailers. """
19
 
 
20
 
    MAX_SIZE = 512
21
 
    THUMB_QUALITY = 85
22
 
 
23
 
    def __init__(self, filename, thumb_type):
24
 
 
25
 
        self.config = Configuration()
26
 
        thumb_dir = os.path.join(self.config.THUMB_DIR, thumb_type)
27
 
        self.filename = filename
28
 
        if hashlib:
29
 
            filehash = hashlib.md5()
30
 
        else:
31
 
            filehash = md5.new()
32
 
        filehash.update(self.filename)
33
 
        self.filename_hash = filehash.hexdigest()
34
 
 
35
 
        if not os.path.exists(self.filename):
36
 
            raise ThumbnailerException(
37
 
                'File to thumbnail does not exist : %s' % self.filename)
38
 
 
39
 
        if os.path.exists(thumb_dir):
40
 
            if os.path.isfile(filename):
41
 
                self._thumb_file =  os.path.join(thumb_dir,
42
 
                    self.filename_hash + '.jpg')
43
 
            else:
44
 
                raise ThumbnailerException(
45
 
                    'Thumbnailer filename is a folder : %s' % self.filename)
46
 
        else:
47
 
            raise ThumbnailerException('Unknown thumbnail type : %s' % (
48
 
                thumb_type))
49
 
 
50
 
    def get_hash(self):
51
 
        '''Get the hash of the filename'''
52
 
        return self.filename_hash
53
 
 
54
 
    def create_thumbnail(self):
55
 
        """
56
 
        Implement this method in deriving classes.
57
 
 
58
 
        Method should create a new thumbnail and save it to the Entertainer's
59
 
        thumbnail directory in JPEG format. Thumbnail filename should be a MD5
60
 
        hash of the absolute path of the original file.
61
 
        """
62
 
        raise NotImplementedError
63