diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-03-02 09:57:28 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-03-02 09:57:28 +0100 |
commit | 6c1dd24fc1fe68390f76df69b8542c0652fd5d79 (patch) | |
tree | 975346689828ad0ffc706f5d51a2312b759e2bce | |
parent | 55fbb1a4cb21200c4f8b080bcfa755c6799db196 (diff) | |
download | pyexiv2-6c1dd24fc1fe68390f76df69b8542c0652fd5d79.tar.gz |
EXIF Undefined conversion.
-rw-r--r-- | src/pyexiv2.py | 20 | ||||
-rw-r--r-- | unittest/exif.py | 62 |
2 files changed, 50 insertions, 32 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index f86f0af..ac1c480 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -407,7 +407,7 @@ class ExifTag(MetadataTag): """ super(ExifTag, self).__init__(key, name, label, description, xtype, value) self.fvalue = fvalue - self.value = ExifTag._convert_to_python(value, xtype) + self.value = ExifTag._convert_to_python(value, xtype, fvalue) """ def __convert_value_to_python_type(self): @@ -436,14 +436,16 @@ class ExifTag(MetadataTag): """ @staticmethod - def _convert_to_python(value, xtype): + def _convert_to_python(value, xtype, fvalue): """ 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 EXIF type of the value - @type xtype: C{str} + @param value: the value to be converted, as a string + @type value: C{str} + @param xtype: the EXIF type of the value + @type xtype: C{str} + @param fvalue: the value formatted as a human-readable string by exiv2 + @type fvalue: C{str} @return: the value converted to its corresponding python type @rtype: depends on xtype (DOCME) @@ -477,6 +479,12 @@ class ExifTag(MetadataTag): except ValueError: raise ExifValueError(value, xtype) + elif xtype == 'Undefined': + try: + return unicode(fvalue, 'utf-8') + except TypeError: + raise ExifValueError(fvalue, xtype) + # TODO: other types raise ExifValueError(value, xtype) diff --git a/unittest/exif.py b/unittest/exif.py index 72fb089..86eab63 100644 --- a/unittest/exif.py +++ b/unittest/exif.py @@ -34,20 +34,20 @@ class TestExifTag(unittest.TestCase): def test_convert_to_python_ascii(self): xtype = 'Ascii' # Valid values: datetimes - self.assertEqual(ExifTag._convert_to_python('2009-03-01 12:46:51', xtype), + self.assertEqual(ExifTag._convert_to_python('2009-03-01 12:46:51', xtype, None), datetime.datetime(2009, 03, 01, 12, 46, 51)) - self.assertEqual(ExifTag._convert_to_python('2009:03:01 12:46:51', xtype), + self.assertEqual(ExifTag._convert_to_python('2009:03:01 12:46:51', xtype, None), datetime.datetime(2009, 03, 01, 12, 46, 51)) - self.assertEqual(ExifTag._convert_to_python('2009-03-01T12:46:51Z', xtype), + self.assertEqual(ExifTag._convert_to_python('2009-03-01T12:46:51Z', xtype, None), datetime.datetime(2009, 03, 01, 12, 46, 51)) # Valid values: strings - self.assertEqual(ExifTag._convert_to_python('Some text.', xtype), u'Some text.') - self.assertEqual(ExifTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype), + self.assertEqual(ExifTag._convert_to_python('Some text.', xtype, None), u'Some text.') + self.assertEqual(ExifTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype, None), u'Some text with exotic chàräctérʐ.') # Invalid values: datetimes - self.assertEqual(ExifTag._convert_to_python('2009-13-01 12:46:51', xtype), + self.assertEqual(ExifTag._convert_to_python('2009-13-01 12:46:51', xtype, None), u'2009-13-01 12:46:51') - self.assertEqual(ExifTag._convert_to_python('2009-12-01', xtype), + self.assertEqual(ExifTag._convert_to_python('2009-12-01', xtype, None), u'2009-12-01') def test_convert_to_string_ascii(self): @@ -69,13 +69,13 @@ class TestExifTag(unittest.TestCase): def test_convert_to_python_short(self): xtype = 'Short' # Valid values - self.assertEqual(ExifTag._convert_to_python('23', xtype), 23) - self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628) + self.assertEqual(ExifTag._convert_to_python('23', xtype, None), 23) + self.assertEqual(ExifTag._convert_to_python('+5628', xtype, None), 5628) # Invalid values - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype, None) def test_convert_to_string_short(self): xtype = 'Short' @@ -89,13 +89,13 @@ class TestExifTag(unittest.TestCase): def test_convert_to_python_long(self): xtype = 'Long' # Valid values - self.assertEqual(ExifTag._convert_to_python('23', xtype), 23) - self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628) + self.assertEqual(ExifTag._convert_to_python('23', xtype, None), 23) + self.assertEqual(ExifTag._convert_to_python('+5628', xtype, None), 5628) # Invalid values - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype, None) def test_convert_to_string_long(self): xtype = 'Long' @@ -110,14 +110,14 @@ class TestExifTag(unittest.TestCase): def test_convert_to_python_slong(self): xtype = 'SLong' # Valid values - self.assertEqual(ExifTag._convert_to_python('23', xtype), 23) - self.assertEqual(ExifTag._convert_to_python('+5628', xtype), 5628) - self.assertEqual(ExifTag._convert_to_python('-437', xtype), -437) + self.assertEqual(ExifTag._convert_to_python('23', xtype, None), 23) + self.assertEqual(ExifTag._convert_to_python('+5628', xtype, None), 5628) + self.assertEqual(ExifTag._convert_to_python('-437', xtype, None), -437) # Invalid values - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype) - self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '5,64', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '47.0001', xtype, None) + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, '1E3', xtype, None) def test_convert_to_string_slong(self): xtype = 'SLong' @@ -128,3 +128,13 @@ class TestExifTag(unittest.TestCase): # Invalid values self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 'invalid', xtype) self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, 3.14, xtype) + + def test_convert_to_python_undefined(self): + xtype = 'Undefined' + # Valid values + self.assertEqual(ExifTag._convert_to_python('48 49 48 48 ', xtype, '1.00'), + u'1.00') + self.assertEqual(ExifTag._convert_to_python('3 ', xtype, 'Digital still camera'), + u'Digital still camera') + # Invalid values + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_python, 'abc', xtype, None) |