diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-02-24 09:33:53 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-02-24 09:33:53 +0100 |
commit | e1ced824d8897bf1a44922e51823592e43988db5 (patch) | |
tree | a6bf4a26b7ff8c080aebf61e15d766c57c38dd38 | |
parent | 642e3d6734d214699cc6ced43bfc3446d0360f85 (diff) | |
download | pyexiv2-e1ced824d8897bf1a44922e51823592e43988db5.tar.gz |
IPTC Short conversion.
-rw-r--r-- | src/pyexiv2.py | 72 | ||||
-rwxr-xr-x | unittest/TestsRunner.py | 2 | ||||
-rw-r--r-- | unittest/iptc.py | 45 | ||||
-rw-r--r-- | unittest/xmp.py | 1 |
4 files changed, 104 insertions, 16 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index a00e4db..fa1b3a2 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -428,6 +428,16 @@ class ExifTag(MetadataTag): return r +class IptcValueError(ValueError): + def __init__(self, value, xtype): + self.value = value + self.xtype = xtype + + def __str__(self): + return 'Invalid value for IPTC type [%s]: [%s]' % \ + (self.xtype, self.value) + + class IptcTag(MetadataTag): """ @@ -439,30 +449,60 @@ class IptcTag(MetadataTag): """ Constructor. """ - MetadataTag.__init__(self, key, name, label, description, xtype, values) - self.__convert_values_to_python_type() + super(IptcTag, self).__init__(key, name, label, description, xtype, values) + self.values = map(lambda x: IptcTag._convert_to_python(x, self.xtype), self._value) + +# def __convert_values_to_python_type(self): +# """ +# Convert the stored values from strings to the matching Python type. +# """ +# if self.xtype == 'Short': +# self.value = map(int, self._value) +# elif self.xtype == 'String': +# pass +# elif self.xtype == 'Date': +# self.value = map(StringToDate, self._value) +# elif self.xtype == 'Time': +# self.value = map(StringToTime, self._value) +# elif self.xtype == 'Undefined': +# pass - def __convert_values_to_python_type(self): + @staticmethod + def _convert_to_python(value, xtype): """ - Convert the stored values from strings to the matching Python type. + Convert a value to its corresponding python type. + + @param value: the value to be converted, as a string + @type value: C{str} + @param xtype: the IPTC type of the value + @type xtype: C{str} + + @return: the value converted to its corresponding python type + @rtype: depends on xtype (DOCME) + + @raise L{IptcValueError}: if the conversion fails """ - if self.xtype == 'Short': - self.value = map(int, self._value) - elif self.xtype == 'String': - pass - elif self.xtype == 'Date': - self.value = map(StringToDate, self._value) - elif self.xtype == 'Time': - self.value = map(StringToTime, self._value) - elif self.xtype == 'Undefined': - pass + if xtype == 'Short': + try: + return int(value) + except ValueError: + raise IptcValueError(value, xtype) + + # TODO: other types + + raise NotImplementedError('IPTC conversion for type [%s]' % xtype) def __str__(self): """ Return a string representation of the IPTC tag. """ - r = MetadataTag.__str__(self) - return r.replace('Raw value = ', 'Raw values = ') + r = 'Key = ' + self.key + os.linesep + \ + 'Name = ' + self.name + os.linesep + \ + 'Label = ' + self.label + os.linesep + \ + 'Description = ' + self.description + os.linesep + \ + 'Type = ' + self.xtype + os.linesep + \ + 'Values = ' + str(self.values) + return r class XmpValueError(ValueError): diff --git a/unittest/TestsRunner.py b/unittest/TestsRunner.py index 5e15d48..1b5edf6 100755 --- a/unittest/TestsRunner.py +++ b/unittest/TestsRunner.py @@ -36,6 +36,7 @@ import unittest #from Bug183332_TestCase import Bug183332_TestCase #from Bug183618_TestCase import Bug183618_TestCase from xmp import TestXmpTag +from iptc import TestIptcTag if __name__ == '__main__': @@ -49,5 +50,6 @@ if __name__ == '__main__': #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183332_TestCase)) #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug183618_TestCase)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpTag)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestIptcTag)) # Run the test suite unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/unittest/iptc.py b/unittest/iptc.py new file mode 100644 index 0000000..4ed0dc3 --- /dev/null +++ b/unittest/iptc.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2009 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> +# +# ****************************************************************************** + +import unittest +from pyexiv2 import IptcTag, IptcValueError + + +class TestIptcTag(unittest.TestCase): + + def test_convert_to_python_short(self): + xtype = 'Short' + # Valid values + self.assertEqual(IptcTag._convert_to_python('23', xtype), 23) + self.assertEqual(IptcTag._convert_to_python('+5628', xtype), 5628) + self.assertEqual(IptcTag._convert_to_python('-4', xtype), -4) + # Invalid values + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'abc', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '5,64', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '47.0001', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '1E3', xtype) + + # TODO: other types diff --git a/unittest/xmp.py b/unittest/xmp.py index e770b06..970f8fb 100644 --- a/unittest/xmp.py +++ b/unittest/xmp.py @@ -28,6 +28,7 @@ import unittest from pyexiv2 import XmpTag, XmpValueError, FixedOffset import datetime + class TestXmpTag(unittest.TestCase): def test_convert_to_python_bag(self): |