From 9564f01d928d2a654a4dd3933025e20266adddab Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Tue, 24 Feb 2009 19:54:14 +0100 Subject: IPTC Short to string conversion. --- src/pyexiv2.py | 25 +++++++++++++++++++++++++ unittest/iptc.py | 10 ++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/pyexiv2.py b/src/pyexiv2.py index afc2201..2c49db9 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -522,6 +522,31 @@ class IptcTag(MetadataTag): raise NotImplementedError('IPTC conversion for type [%s]' % xtype) + @staticmethod + def _convert_to_string(value, xtype): + """ + Convert a value to its corresponding string representation. + + @param value: the value to be converted + @type value: depends on xtype (DOCME) + @param xtype: the IPTC type of the value + @type xtype: C{str} + + @return: the value converted to its corresponding string representation + @rtype: C{str} + + @raise L{IptcValueError}: if the conversion fails + """ + if xtype == 'Short': + if type(value) is int: + return str(value) + else: + raise IptcValueError(value, xtype) + + # TODO: other types + + raise IptcValueError(value, xtype) + def __str__(self): """ Return a string representation of the IPTC tag. diff --git a/unittest/iptc.py b/unittest/iptc.py index dee5383..c0ed7d7 100644 --- a/unittest/iptc.py +++ b/unittest/iptc.py @@ -43,6 +43,16 @@ class TestIptcTag(unittest.TestCase): self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '47.0001', xtype) self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '1E3', xtype) + def test_convert_to_string_short(self): + xtype = 'Short' + # Valid values + self.assertEqual(IptcTag._convert_to_string(123, xtype), '123') + self.assertEqual(IptcTag._convert_to_string(-57, xtype), '-57') + # Invalid values + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, '3.14', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, '1E3', xtype) + def test_convert_to_python_string(self): xtype = 'String' # Valid values -- cgit