~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/pygexiv2.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař, Michal Čihař, Jakub Wilk
  • Date: 2013-06-06 12:05:08 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20130606120508-e3g94vl8w9pw7za7
Tags: 0.8-1
[ Michal Čihař ]
* New upstream release.
  - Uses new Genshi templates (Closes: #696682).
  - Correctly handles wronly encoded artist in EXIF (Closes: #696648).
  - Uses GExiv2 (LP: #1074028).
* Depend on GExiv2 instead of pyexiv2.
* Bump standards to 3.9.4.
* Use debhelper 9.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Lazygal, a lazy static web gallery generator.
 
2
# Copyright (C) 2013 Alexandre Rossi <alexandre.rossi@gmail.com>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program; if not, write to the Free Software Foundation, Inc.,
 
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 
 
18
 
 
19
"""
 
20
This is a transition module to provide pyexiv2 compatibility waiting for
 
21
a properly supported GExiv2 windows port.
 
22
"""
 
23
 
 
24
 
 
25
import datetime
 
26
import logging
 
27
 
 
28
 
 
29
class Metadata_withPyExiv2(object):
 
30
 
 
31
    def __init__(self, image_path):
 
32
        self.pyexiv2md = pyexiv2.metadata.ImageMetadata(image_path)
 
33
        self.pyexiv2md.read()
 
34
 
 
35
    def __getitem__(self, key):
 
36
        try:
 
37
            val = self.pyexiv2md[key].value
 
38
        except ValueError:
 
39
            return None
 
40
        if type(val) is datetime.datetime:
 
41
            val = val.strftime('%Y:%m:%d %H:%M:%S')
 
42
        return val
 
43
 
 
44
    def __setitem__(self, key, value):
 
45
        if value == '' or value is None:
 
46
            del self.pyexiv2md[key]
 
47
            return
 
48
 
 
49
        if key in ('Iptc.Application2.Keywords',
 
50
                   'Xmp.MicrosoftPhoto.LastKeywordXMP',
 
51
                   'Xmp.dc.subject',
 
52
                   'Xmp.digiKam.TagsList', ):
 
53
            if type(value) is not list:
 
54
                value = [value]
 
55
 
 
56
        if key in self.pyexiv2md.exif_keys:
 
57
            tag = pyexiv2.exif.ExifTag(key)
 
58
            if tag.type in ('Long', 'SLong', 'Short', 'SShort', ):
 
59
                value = int(value)
 
60
            elif tag.type == 'Undefined':
 
61
                tag.value = value
 
62
                self.pyexiv2md[key] = tag
 
63
                return
 
64
 
 
65
        self.pyexiv2md[key] = value
 
66
 
 
67
    def __contains__(self, key):
 
68
        return key in self.pyexiv2md
 
69
 
 
70
    def get_tag_interpreted_string(self, key):
 
71
        return self.pyexiv2md[key].human_value
 
72
 
 
73
    def get_tag_multiple(self, key):
 
74
        return self.pyexiv2md[key].value
 
75
 
 
76
    def get_exif_tag_rational(self, key):
 
77
        return self.pyexiv2md[key].value
 
78
 
 
79
    def get_comment(self):
 
80
        return self.pyexiv2md.comment
 
81
 
 
82
    def get_exif_tags(self):
 
83
        return self.pyexiv2md.exif_keys
 
84
 
 
85
    def clear_tag(self, key):
 
86
        del self.pyexiv2md[key]
 
87
 
 
88
    def save_file(self):
 
89
        self.pyexiv2md.write()
 
90
 
 
91
 
 
92
class GExiv2_withPyExiv2(object):
 
93
    Metadata = Metadata_withPyExiv2
 
94
 
 
95
    class LogLevel(object):
 
96
        ERROR = None
 
97
 
 
98
    @staticmethod
 
99
    def log_set_level(dummy):
 
100
        pass
 
101
 
 
102
 
 
103
try:
 
104
    from gi.repository import GExiv2
 
105
except ImportError:
 
106
    import warnings
 
107
    logging.warning('Falling back to try using pyexiv2, which is deprecated.')
 
108
    import pyexiv2
 
109
    warnings.warn("deprecated", DeprecationWarning)
 
110
    GExiv2 = GExiv2_withPyExiv2
 
111
 
 
112
 
 
113
# vim: ts=4 sw=4 expandtab