degal/exif.py
author Tero Marttila <terom@fixme.fi>
Sun, 14 Jun 2009 23:43:40 +0300
changeset 120 55cb7fc9c8fb
child 121 3bed35e79f41
permissions -rw-r--r--
add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
120
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    The various Exif backends that we have
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
from __future__ import with_statement
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import utils
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
class ExifHandler (object) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
        Our generic interface for Exif data
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
            Load Exif data from the given File.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
        self.file = file
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    def image_tag (self, tag, default=None) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
            Load and return the human-readable unicode-safe value for the given main image tag.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        abstract
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    def image_tags (self, tags) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
            Load and return the given tags for the main image as a sequence of (tag, value) tuples , following the same
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
            rules as image_tag, except there should mainly be only one call to this function, encompassing all the
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
            relevant tags.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
            The returned list should omit those values which were not found.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        for tag in tags :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
            # try and fetch value
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
            value = self.image_tag(tag)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
            # filter out default not-found
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
            if value is not None :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
                yield tag, value
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
try :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    import pyexiv2
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
except ImportError :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    PyExiv2_Handler = None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    class PyExiv2_Handler (ExifHandler) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        NAME = 'pyexiv2'
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
                Load as pyexiv2.Image
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
            # create
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
            self.image = pyexiv2.Image(file.path)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            # load
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
            self.image.readMetadata()
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        def image_tag (self, tag, default=None) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            try :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
                # XXX: this is wrong
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
                return self.image.interpretedExifValue("Exif.Photo.%s" % tag)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
            
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
            except (IndexError, KeyError) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
                return default
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
try :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    from lib import EXIF
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
except ImportError :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    EXIF_Handler = None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    class EXIF_Handler (ExifHandler) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
        NAME = 'EXIFpy'
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        def __init__ (self, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
                Load using EXIF.process_file
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
            """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
            
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
            with file.open('rb') as fh :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
                self.tags = EXIF.process_file(fh)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        def image_tag (self, tag, default=None) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
            # XXX: this is wrong
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
            return self.tags.get('Image %s' % (tag, ), default)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
# ExifHandler implementations to use, in preference order
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
EXIF_HANDLERS = [
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    # reasonably fast, but requires the native library and extension module
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    PyExiv2_Handler,
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    # pure-python, but very, very slow
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    EXIF_Handler,
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
]
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
# default ExifHandler to use
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
EXIF_HANDLER = utils.first(EXIF_HANDLERS)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
# ExifHandler implementations by name
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
EXIF_HANDLERS_BY_NAME = dict((h.NAME, h) for h in EXIF_HANDLERS)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
def load (config, file) :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        Load Exif data using the configured Exif handler, and return the instance.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
        Returns None if there wasn't any Exif handler available for use.
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    """
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    if config.exif_handler :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
        # explicit
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
        exif_handler = config.exif_handler
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
    elif config.exif_handler_name :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
        # by name
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   124
        exif_handler = EXIF_HANDLERS_BY_NAME[config.exif_handler_name]
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
        # default
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
        exif_handler = EXIF_HANDLER
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    if exif_handler :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        # found one, use it
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
        return exif_handler(file)
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
    else :
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
        # nothing :(
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
        return None
55cb7fc9c8fb add new exif.py to abstract between different exif libraries, and add partially working support for pyexiv2 and EXIFpy
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137