aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2010-12-22 19:36:01 +0100
committerOlivier Tilloy <olivier@tilloy.net>2010-12-22 19:36:01 +0100
commit27f94886b6c043609366dd2f5604566d1264852d (patch)
treedf72aab1d5e02fa9b5568497bd54dbc5d13cf392
parent59ee4d6f36aaaea03dc9ffcacaf338903392db06 (diff)
parent51057f871adbc671546bb13f8bc7f7e2d25dad09 (diff)
downloadpyexiv2-27f94886b6c043609366dd2f5604566d1264852d.tar.gz
Add pickling support to tags.
-rw-r--r--src/pyexiv2/exif.py9
-rw-r--r--src/pyexiv2/iptc.py9
-rw-r--r--src/pyexiv2/xmp.py9
-rwxr-xr-xtest/TestsRunner.py2
-rw-r--r--test/pickling.py117
5 files changed, 146 insertions, 0 deletions
diff --git a/src/pyexiv2/exif.py b/src/pyexiv2/exif.py
index 0e4d308..4f3fe03 100644
--- a/src/pyexiv2/exif.py
+++ b/src/pyexiv2/exif.py
@@ -465,6 +465,15 @@ class ExifTag(ListenerInterface):
right = self._raw_value
return '<%s = %s>' % (left, right)
+ # Support for pickling.
+ def __getstate__(self):
+ return (self.key, self.raw_value)
+
+ def __setstate__(self, state):
+ key, raw_value = state
+ self._tag = libexiv2python._ExifTag(key)
+ self.raw_value = raw_value
+
class ExifThumbnail(object):
diff --git a/src/pyexiv2/iptc.py b/src/pyexiv2/iptc.py
index 128f77e..7a768fd 100644
--- a/src/pyexiv2/iptc.py
+++ b/src/pyexiv2/iptc.py
@@ -380,3 +380,12 @@ class IptcTag(ListenerInterface):
right = self._raw_values
return '<%s = %s>' % (left, right)
+ # Support for pickling.
+ def __getstate__(self):
+ return (self.key, self.raw_value)
+
+ def __setstate__(self, state):
+ key, raw_value = state
+ self._tag = libexiv2python._IptcTag(key)
+ self.raw_value = raw_value
+
diff --git a/src/pyexiv2/xmp.py b/src/pyexiv2/xmp.py
index 8223a24..ab2edeb 100644
--- a/src/pyexiv2/xmp.py
+++ b/src/pyexiv2/xmp.py
@@ -467,6 +467,15 @@ class XmpTag(object):
right = self._raw_value
return '<%s = %s>' % (left, right)
+ # Support for pickling.
+ def __getstate__(self):
+ return (self.key, self.raw_value)
+
+ def __setstate__(self, state):
+ key, raw_value = state
+ self._tag = libexiv2python._XmpTag(key)
+ self.raw_value = raw_value
+
def register_namespace(name, prefix):
"""
diff --git a/test/TestsRunner.py b/test/TestsRunner.py
index 8585295..198bb27 100755
--- a/test/TestsRunner.py
+++ b/test/TestsRunner.py
@@ -40,6 +40,7 @@ from buffer import TestBuffer
from encoding import TestEncodings
from utils import TestConversions
from usercomment import TestUserCommentReadWrite, TestUserCommentAdd
+from pickling import TestPicklingTags
def run_unit_tests():
@@ -59,6 +60,7 @@ def run_unit_tests():
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestConversions))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentReadWrite))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUserCommentAdd))
+ suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestPicklingTags))
# Run the test suite
return unittest.TextTestRunner(verbosity=2).run(suite)
diff --git a/test/pickling.py b/test/pickling.py
new file mode 100644
index 0000000..d98c146
--- /dev/null
+++ b/test/pickling.py
@@ -0,0 +1,117 @@
+# -*- coding: utf-8 -*-
+
+# ******************************************************************************
+#
+# Copyright (C) 2010 Olivier Tilloy <olivier@tilloy.net>
+#
+# This file is part of the pyexiv2 distribution.
+#
+# pyexiv2 is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# pyexiv2 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with pyexiv2; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
+#
+# Author: Olivier Tilloy <olivier@tilloy.net>
+#
+# ******************************************************************************
+
+from pyexiv2.exif import ExifTag
+from pyexiv2.iptc import IptcTag
+from pyexiv2.xmp import XmpTag
+from pyexiv2.utils import Rational, FixedOffset
+
+import unittest
+import pickle
+import datetime
+
+
+class TestPicklingTags(unittest.TestCase):
+
+ def test_pickle_exif_tag(self):
+ tags = []
+ tags.append(ExifTag('Exif.Image.DateTime',
+ datetime.datetime(2010, 12, 22, 19, 21, 0)))
+ tags.append(ExifTag('Exif.GPSInfo.GPSDateStamp', datetime.date.today()))
+ tags.append(ExifTag('Exif.Image.Copyright', '(C) 2010 Santa Claus'))
+ tags.append(ExifTag('Exif.GPSInfo.GPSVersionID', '0'))
+ tags.append(ExifTag('Exif.Pentax.Temperature', '14'))
+ tags.append(ExifTag('Exif.Photo.UserComment', 'foo bar baz'))
+ tags.append(ExifTag('Exif.Image.BitsPerSample', 8))
+ tags.append(ExifTag('Exif.Image.TimeZoneOffset', 7))
+ tags.append(ExifTag('Exif.Image.ImageWidth', 7492))
+ tags.append(ExifTag('Exif.OlympusCs.ManometerReading', 29))
+ tags.append(ExifTag('Exif.Image.XResolution', Rational(7, 3)))
+ tags.append(ExifTag('Exif.Image.BaselineExposure', Rational(-7, 3)))
+ tags.append(ExifTag('Exif.Photo.ExifVersion', '0100'))
+ for tag in tags:
+ s = pickle.dumps(tag)
+ t = pickle.loads(s)
+ self.assert_(isinstance(t, ExifTag))
+ self.assertEqual(t.key, tag.key)
+ self.assertEqual(t.type, tag.type)
+ self.assertEqual(t.name, tag.name)
+ self.assertEqual(t.label, tag.label)
+ self.assertEqual(t.description, tag.description)
+ self.assertEqual(t.section_name, tag.section_name)
+ self.assertEqual(t.section_description, tag.section_description)
+ self.assertEqual(t.raw_value, tag.raw_value)
+ self.assertEqual(t.value, tag.value)
+ self.assertEqual(t.human_value, tag.human_value)
+
+ def test_pickle_iptc_tag(self):
+ tags = []
+ tags.append(IptcTag('Iptc.Envelope.FileFormat', [23]))
+ tags.append(IptcTag('Iptc.Application2.Subject', ['foo', 'bar', 'baz']))
+ tags.append(IptcTag('Iptc.Envelope.DateSent', [datetime.date.today()]))
+ tags.append(IptcTag('Iptc.Envelope.TimeSent',
+ [datetime.time(23, 37, 4, tzinfo=FixedOffset('+', 6, 0))]))
+ tags.append(IptcTag('Iptc.Application2.Preview', ['01001101']))
+ for tag in tags:
+ s = pickle.dumps(tag)
+ t = pickle.loads(s)
+ self.assert_(isinstance(t, IptcTag))
+ self.assertEqual(t.key, tag.key)
+ self.assertEqual(t.type, tag.type)
+ self.assertEqual(t.name, tag.name)
+ self.assertEqual(t.title, tag.title)
+ self.assertEqual(t.description, tag.description)
+ self.assertEqual(t.photoshop_name, tag.photoshop_name)
+ self.assertEqual(t.repeatable, tag.repeatable)
+ self.assertEqual(t.record_name, tag.record_name)
+ self.assertEqual(t.record_description, tag.record_description)
+ self.assertEqual(t.raw_value, tag.raw_value)
+ self.assertEqual(t.value, tag.value)
+
+ def test_pickle_xmp_tag(self):
+ tags = []
+ tags.append(XmpTag('Xmp.dc.subject', ['foo', 'bar', 'baz']))
+ tags.append(XmpTag('Xmp.xmpRights.Marked', True))
+ tags.append(XmpTag('Xmp.xmp.CreateDate', datetime.date.today()))
+ tags.append(XmpTag('Xmp.xmpMM.SaveID', 34))
+ tags.append(XmpTag('Xmp.dc.format', ('image', 'jpeg')))
+ tags.append(XmpTag('Xmp.photoshop.CaptionWriter', 'John Doe'))
+ tags.append(XmpTag('Xmp.dc.source', 'bleh'))
+ tags.append(XmpTag('Xmp.xmpMM.DocumentID', 'http://example.com'))
+ tags.append(XmpTag('Xmp.xmp.BaseURL', 'http://example.com'))
+ tags.append(XmpTag('Xmp.xmpDM.videoPixelAspectRatio', Rational(5, 3)))
+ for tag in tags:
+ s = pickle.dumps(tag)
+ t = pickle.loads(s)
+ self.assert_(isinstance(t, XmpTag))
+ self.assertEqual(t.key, tag.key)
+ self.assertEqual(t.type, tag.type)
+ self.assertEqual(t.name, tag.name)
+ self.assertEqual(t.title, tag.title)
+ self.assertEqual(t.description, tag.description)
+ self.assertEqual(t.raw_value, tag.raw_value)
+ self.assertEqual(t.value, tag.value)
+