import manquant.
[Photo.git] / xmp.py
1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Photo is a part of Plinn - http://plinn.org #
4 # Copyright © 2008 Benoît PIN <benoit.pin@ensmp.fr> #
5 # #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program; if not, write to the Free Software #
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
19 #######################################################################################
20 #
21 #
22
23 from types import StringTypes
24 from logging import getLogger
25 import re
26 console = getLogger('Photo.xmp')
27
28 class XMP(object) :
29 XMP_HEADER = u'<?xpacket begin="\ufeff" id="W5M0MpCehiHzreSzNTczkc9d"?>'
30 XMP_HEADER_PATTERN = u'''<\?xpacket begin=['"]\ufeff['"] id=['"]W5M0MpCehiHzreSzNTczkc9d['"][^\?]*\?>'''
31 XMP_PADDING_LINE = u'\u0020' * 63 + u'\n'
32 XMP_TRAILER = u'<?xpacket end="w"?>'
33
34 _readers = {}
35 _writers = {}
36
37
38
39 def __init__(self, file, content_type='image/jpeg', encoding='utf-8') :
40 try :
41 self.reader = self._readers[content_type]
42 except KeyError:
43 raise NotImplementedError, "%r content type not supported by XMP" % content_type
44
45 try :
46 self.writer = self._writers[content_type]
47 except KeyError :
48 self.writer = None
49 console.info('XMP file opened on read-only mode.')
50
51 self.file = file
52 self.encoding = encoding
53 self.xmp = None
54 self._open()
55
56
57 def __del__(self) :
58 try :
59 self.file.close()
60 except :
61 pass
62
63
64 def _open(self) :
65
66 if type(self.file) in StringTypes :
67 self.file = file(self.file)
68
69 packet = self.reader(self.file)
70
71 if packet is not None :
72 # tests / unwrap
73 reEncodedHeader = re.compile(self.XMP_HEADER_PATTERN.encode(self.encoding))
74 m = reEncodedHeader.match(packet)
75 assert m is not None, "No xmp header found"
76 xmp = packet[m.end():]
77
78 trailer = self.XMP_TRAILER[:-6].encode(self.encoding) # TODO handle read-only mode
79 trailerPos = xmp.find(trailer)
80 assert trailerPos != -1, "No xmp trailer found"
81
82 xmp = xmp[:trailerPos]
83 xmp = xmp.strip()
84 self.xmp = xmp
85 else :
86 self.xmp = None
87
88 def save(self, f=None):
89 original = self.file
90 if f :
91 if type(f) in StringTypes :
92 new = file(f, 'w')
93 else :
94 new = f
95 elif f is None :
96 new = self.file
97
98 self.writer(original, new, self.xmp)
99
100
101 def getXMP(self) :
102 return self.xmp
103
104
105 def setXMP(self, xmp) :
106 self.xmp = xmp
107
108 #
109 # xmp utils
110 #
111
112 @staticmethod
113 def getXmpPadding(size) :
114 # size of trailer in kB
115 return (XMP.XMP_PADDING_LINE * 32 * size)
116
117
118 @staticmethod
119 def genXMPPacket(uXmpData, paddingSize):
120 packet = u''
121
122 packet += XMP.XMP_HEADER
123 packet += uXmpData
124 packet += XMP.getXmpPadding(paddingSize)
125 packet += XMP.XMP_TRAILER
126
127 return packet
128
129
130
131 #
132 # content type registry stuff
133 #
134
135
136 @classmethod
137 def registerReader(cls, content_type, reader) :
138 cls._readers[content_type] = reader
139
140 @classmethod
141 def registerWriter(cls, content_type, writer) :
142 cls._writers[content_type] = writer
143
144 @classmethod
145 def registerWrapper(cls, content_type, wrapper) :
146 """ Registers specific wrapper to prepare data
147 for embedding xmp into specific content_type file.
148 """
149 pass
150
151
152
153 def test() :
154 from xml.dom.minidom import parse
155 data = parse('new.xmp').documentElement.toxml()
156
157 def test1() :
158 original = 'original.jpg'
159 modified = 'modified.jpg'
160
161 x = XMP(original)
162 x.setXMP(data)
163 x.save(modified)
164
165 def test2() :
166 from cStringIO import StringIO
167 sio = StringIO()
168 sio.write(file('modified.jpg').read())
169 sio.seek(0)
170
171 x = XMP(sio)
172 x.setXMP(data)
173 x.save()
174
175 f2 = open('modified2.jpg', 'w')
176 f2.write(sio.read())
177 f2.close()
178
179
180 test1()
181 test2()
182
183
184
185 if __name__ == '__main__' :
186 test()