diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-02-25 19:57:55 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-02-25 19:57:55 +0100 |
commit | d38afdc4e6ae5209dca82d46b6d3bb6944f9e6fb (patch) | |
tree | b81977993375d69e8dd421b4f0d3704c074950ad | |
parent | 79af14a148ea4d7b79db5a0f01b4db4ab590d1b1 (diff) | |
download | pyexiv2-d38afdc4e6ae5209dca82d46b6d3bb6944f9e6fb.tar.gz |
EXIF Short and Short to string conversion.
-rw-r--r-- | src/pyexiv2.py | 89 | ||||
-rwxr-xr-x | unittest/TestsRunner.py | 6 |
2 files changed, 82 insertions, 13 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index fd91bb4..b31bf2c 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -377,6 +377,16 @@ class MetadataTag(object): return r +class ExifValueError(ValueError): + def __init__(self, value, xtype): + self.value = value + self.xtype = xtype + + def __str__(self): + return 'Invalid value for EXIF type [%s]: [%s]' % \ + (self.xtype, self.value) + + class ExifTag(MetadataTag): """ @@ -388,14 +398,12 @@ class ExifTag(MetadataTag): """ Constructor. """ - MetadataTag.__init__(self, key, name, label, description, xtype, value) + super(ExifTag, self).__init__(key, name, label, description, xtype, value) self.fvalue = fvalue - self.__convert_value_to_python_type() + self.value = ExifTag._convert_to_python(self._value, self.xtype) + """ def __convert_value_to_python_type(self): - """ - Convert the stored value from a string to the matching Python type. - """ if self.xtype == 'Byte': pass elif self.xtype == 'Ascii': @@ -418,13 +426,69 @@ class ExifTag(MetadataTag): # Undefined but do not store their value as expected. # This should fix bug #173387. pass + """ + + @staticmethod + def _convert_to_python(value, xtype): + """ + 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} + + @return: the value converted to its corresponding python type + @rtype: depends on xtype (DOCME) + + @raise L{ExifValueError}: if the conversion fails + """ + if xtype == 'Short': + try: + return int(value) + except ValueError: + raise ExifValueError(value, xtype) + + # TODO: other types + + raise ExifValueError(value, 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 EXIF type of the value + @type xtype: C{str} + + @return: the value converted to its corresponding string representation + @rtype: C{str} + + @raise L{ExifValueError}: if the conversion fails + """ + if xtype == 'Short': + if type(value) is int: + return str(value) + else: + raise ExifValueError(value, xtype) + + # TODO: other types + + raise ExifValueError(value, xtype) def __str__(self): """ Return a string representation of the EXIF tag. """ - r = MetadataTag.__str__(self) - r += os.linesep + 'Formatted value = ' + self.fvalue + 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 + \ + 'Value = ' + str(self.value) + os.linesep + \ + 'Formatted value = ' + self.fvalue return r @@ -520,7 +584,7 @@ class IptcTag(MetadataTag): # Binary data, return it unmodified return value - raise NotImplementedError('IPTC conversion for type [%s]' % xtype) + raise IptcValueError(value, xtype) @staticmethod def _convert_to_string(value, xtype): @@ -801,8 +865,11 @@ class XmpTag(MetadataTag): else: raise XmpValueError(value, xtype) - elif xtype == 'Boolean' and type(value) is bool: - return str(value) + elif xtype == 'Boolean': + if type(value) is bool: + return str(value) + else: + raise XmpValueError(value, xtype) elif xtype == 'Date': if type(value) is datetime.date: @@ -877,7 +944,7 @@ class XmpTag(MetadataTag): else: raise XmpValueError(value, xtype) - raise XmpValueError(value, xtype) + raise NotImplementedError('XMP conversion for type [%s]' % xtype) def __str__(self): """ diff --git a/unittest/TestsRunner.py b/unittest/TestsRunner.py index 1b5edf6..551f176 100755 --- a/unittest/TestsRunner.py +++ b/unittest/TestsRunner.py @@ -35,8 +35,9 @@ import unittest #from Bug175070_TestCase import Bug175070_TestCase #from Bug183332_TestCase import Bug183332_TestCase #from Bug183618_TestCase import Bug183618_TestCase -from xmp import TestXmpTag +from exif import TestExifTag from iptc import TestIptcTag +from xmp import TestXmpTag if __name__ == '__main__': @@ -49,7 +50,8 @@ if __name__ == '__main__': #suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(Bug175070_TestCase)) #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(TestExifTag)) suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestIptcTag)) + suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestXmpTag)) # Run the test suite unittest.TextTestRunner(verbosity=2).run(suite) |