aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2009-02-24 19:54:14 +0100
committerOlivier Tilloy <olivier@tilloy.net>2009-02-24 19:54:14 +0100
commit9564f01d928d2a654a4dd3933025e20266adddab (patch)
tree48de0b3a146ffa8c755749f4d1769133b76cb6ea
parentb7dfe1a41b0afca712384d67271fef0a4a860df4 (diff)
downloadpyexiv2-9564f01d928d2a654a4dd3933025e20266adddab.tar.gz
IPTC Short to string conversion.
-rw-r--r--src/pyexiv2.py25
-rw-r--r--unittest/iptc.py10
2 files changed, 35 insertions, 0 deletions
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