diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-03-09 19:53:17 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-03-09 19:53:17 +0100 |
commit | 25152b12cce3f5bc22822a0fd0626f5284350080 (patch) | |
tree | 6fd49ed14835703dedbf2e9cfdf60692eaace1f8 | |
parent | ab8fda2c128eeaa45f71d2e27da7e627c439d251 (diff) | |
download | pyexiv2-25152b12cce3f5bc22822a0fd0626f5284350080.tar.gz |
EXIF Byte two-ways conversion.
-rw-r--r-- | src/pyexiv2.py | 18 | ||||
-rw-r--r-- | unittest/exif.py | 13 |
2 files changed, 27 insertions, 4 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index fbb638c..ccef9b4 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -474,6 +474,9 @@ class ExifTag(MetadataTag): except TypeError: raise ExifValueError(value, xtype) + elif xtype == 'Byte': + return value + elif xtype == 'Short': try: return int(value) @@ -502,8 +505,6 @@ class ExifTag(MetadataTag): except TypeError: raise ExifValueError(fvalue, xtype) - # TODO: other types - raise ExifValueError(value, xtype) @staticmethod @@ -536,6 +537,17 @@ class ExifTag(MetadataTag): else: raise ExifValueError(value, xtype) + elif xtype == 'Byte': + if type(value) is unicode: + try: + return value.encode('utf-8') + except UnicodeEncodeError: + raise ExifValueError(value, xtype) + elif type(value) is str: + return value + else: + raise ExifValueError(value, xtype) + elif xtype == 'Short': if type(value) is int and value >= 0: return str(value) @@ -577,8 +589,6 @@ class ExifTag(MetadataTag): else: raise ExifValueError(value, xtype) - # TODO: other types - raise ExifValueError(value, xtype) def __str__(self): diff --git a/unittest/exif.py b/unittest/exif.py index 419b2dd..a398b27 100644 --- a/unittest/exif.py +++ b/unittest/exif.py @@ -66,6 +66,19 @@ class TestExifTag(unittest.TestCase): # Invalid values self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, None, xtype) + def test_convert_to_python_byte(self): + xtype = 'Byte' + # Valid values + self.assertEqual(ExifTag._convert_to_python('Some text', xtype, None), 'Some text') + + def test_convert_to_string_byte(self): + xtype = 'Byte' + # Valid values + self.assertEqual(ExifTag._convert_to_string('Some text', xtype), 'Some text') + self.assertEqual(ExifTag._convert_to_string(u'Some text', xtype), 'Some text') + # Invalid values + self.failUnlessRaises(ExifValueError, ExifTag._convert_to_string, None, xtype) + def test_convert_to_python_short(self): xtype = 'Short' # Valid values |