aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2009-01-28 20:20:23 +0100
committerOlivier Tilloy <olivier@tilloy.net>2009-01-28 20:20:23 +0100
commite3861f261f21a53cb7db84261db78215a25cac6a (patch)
tree2d7d0a8dae5b9754f24e0a501a206b90e211e483
parente729a212a72a9dbbcaa4a896194290c1bf258ae0 (diff)
downloadpyexiv2-e3861f261f21a53cb7db84261db78215a25cac6a.tar.gz
Raise exceptions when failing to convert to a python type.
This should be handled client-side.
-rw-r--r--src/pyexiv2.py75
-rw-r--r--unittest/xmp.py34
2 files changed, 65 insertions, 44 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index 2c00e2d..cdd55cc 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -464,6 +464,16 @@ class IptcTag(MetadataTag):
return r.replace('Raw value = ', 'Raw values = ')
+class XmpValueError(ValueError):
+ def __init__(self, value, xtype):
+ self.value = value
+ self.xtype = xtype
+
+ def __str__(self):
+ return 'Invalid value for XMP type [%s]: [%s]' % \
+ (self.xtype, self.value)
+
+
class XmpTag(MetadataTag):
"""
@@ -488,10 +498,17 @@ class XmpTag(MetadataTag):
def _convert_to_python(value, xtype):
"""
Convert a value to its corresponding python type.
- Return the value unchanged if the conversion fails.
- """
- # TODO: use try except blocks and logging to log conversion errors
+ @param value: the value to be converted, as a string
+ @type value: C{str}
+ @param xtype: the XMP type of the value
+ @type xtype: C{str}
+
+ @return: the value converted to its corresponding python type
+ @rtype: depends on xtype (DOCME)
+
+ @raise L{XmpValueError}: if the conversion fails
+ """
if xtype.startswith('bag '):
values = value.split(', ')
return map(lambda x: XmpTag._convert_to_python(x, xtype[4:]), values)
@@ -502,19 +519,20 @@ class XmpTag(MetadataTag):
elif value == 'False':
return False
else:
- return value
+ raise XmpValueError(value, xtype)
elif xtype == 'Choice':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
+
elif xtype == 'Colorant':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
elif xtype == 'Date':
match = XmpTag._date_re.match(value)
if match is None:
- return value
+ raise XmpValueError(value, xtype)
gd = match.groupdict()
if gd['month'] is not None:
month = int(gd['month'])
@@ -525,7 +543,10 @@ class XmpTag(MetadataTag):
else:
day = 1
if gd['time'] is None:
- return datetime.date(int(gd['year']), month, day)
+ try:
+ return datetime.date(int(gd['year']), month, day)
+ except ValueError:
+ raise XmpValueError(value, xtype)
else:
if gd['seconds'] is not None:
seconds = int(gd['seconds'])
@@ -540,39 +561,43 @@ class XmpTag(MetadataTag):
else:
tzinfo = FixedOffset(gd['sign'], int(gd['ohours']),
int(gd['ominutes']))
- return datetime.datetime(int(gd['year']), month, day,
- int(gd['hours']), int(gd['minutes']),
- seconds, microseconds, tzinfo)
+ try:
+ return datetime.datetime(int(gd['year']), month, day,
+ int(gd['hours']), int(gd['minutes']),
+ seconds, microseconds, tzinfo)
+ except ValueError:
+ raise XmpValueError(value, xtype)
elif xtype == 'Dimensions':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
+
elif xtype == 'Font':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
elif xtype == 'Integer':
try:
return int(value)
- except:
- return value
+ except ValueError:
+ raise XmpValueError(value, xtype)
elif xtype == 'Lang Alt':
matches = value.split('lang="')
nb = len(matches)
if nb < 2 or matches[0] != '':
- return value
+ raise XmpValueError(value, xtype)
result = {}
for i, match in enumerate(matches[1:]):
try:
qualifier, text = match.split('" ', 1)
except ValueError:
- return value
+ raise XmpValueError(value, xtype)
else:
if not text.rstrip().endswith(','):
if (i < nb - 2):
# If not the last match, it should end with a comma
- return value
+ raise XmpValueError(value, xtype)
else:
result[qualifier] = text
else:
@@ -581,29 +606,29 @@ class XmpTag(MetadataTag):
elif xtype == 'Locale':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
elif xtype == 'MIMEType':
try:
mtype, msubtype = value.split('/', 1)
except ValueError:
- return value
+ raise XmpValueError(value, xtype)
else:
return {'type': mtype, 'subtype': msubtype}
elif xtype == 'Real':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
elif xtype in ('ProperName', 'Text'):
try:
return unicode(value, 'utf-8')
except TypeError:
- return value
+ raise XmpValueError(value, xtype)
elif xtype == 'Thumbnail':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
elif xtype in ('URI', 'URL'):
# TODO: use urlparse?
@@ -611,9 +636,9 @@ class XmpTag(MetadataTag):
elif xtype == 'XPath':
# TODO
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
- return value
+ raise NotImplementedError('XMP conversion for type [%s]' % xtype)
@staticmethod
def _convert_to_string(value, xtype):
diff --git a/unittest/xmp.py b/unittest/xmp.py
index 09aca55..ef13bbe 100644
--- a/unittest/xmp.py
+++ b/unittest/xmp.py
@@ -25,7 +25,7 @@
# ******************************************************************************
import unittest
-from pyexiv2 import XmpTag, FixedOffset
+from pyexiv2 import XmpTag, XmpValueError, FixedOffset
import datetime
class TestXmpTag(unittest.TestCase):
@@ -44,7 +44,7 @@ class TestXmpTag(unittest.TestCase):
self.assertEqual(XmpTag._convert_to_python('True', xtype), True)
self.assertEqual(XmpTag._convert_to_python('False', xtype), False)
# Invalid values: not converted
- self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'invalid', xtype)
def test_convert_to_string_boolean(self):
xtype = 'Boolean'
@@ -97,11 +97,11 @@ class TestXmpTag(unittest.TestCase):
datetime.datetime(1999, 10, 13, 5, 3, 54, 721000, tzinfo=FixedOffset('-', 6, 0)),
datetime.timedelta(0))
# Invalid values
- self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid')
- self.assertEqual(XmpTag._convert_to_python('11/10/1983', xtype), '11/10/1983')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'invalid', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, '11/10/1983', xtype)
# FIXME: the following test should not fail: having hours without minutes is not a valid syntax.
- self.assertEqual(XmpTag._convert_to_python('2009-01-22T21', xtype), '2009-01-22T21')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, '2009-01-22T21', xtype)
def test_convert_to_python_integer(self):
xtype = 'Integer'
@@ -110,10 +110,10 @@ class TestXmpTag(unittest.TestCase):
self.assertEqual(XmpTag._convert_to_python('+5628', xtype), 5628)
self.assertEqual(XmpTag._convert_to_python('-4', xtype), -4)
# Invalid values
- self.assertEqual(XmpTag._convert_to_python('abc', xtype), 'abc')
- self.assertEqual(XmpTag._convert_to_python('5,64', xtype), '5,64')
- self.assertEqual(XmpTag._convert_to_python('47.0001', xtype), '47.0001')
- self.assertEqual(XmpTag._convert_to_python('1E3', xtype), '1E3')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'abc', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, '5,64', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, '47.0001', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, '1E3', xtype)
def test_convert_to_python_langalt(self):
xtype = 'Lang Alt'
@@ -127,10 +127,10 @@ class TestXmpTag(unittest.TestCase):
self.assertEqual(XmpTag._convert_to_python('lang="x-default" some text, lang="fr-FR" du texte, lang="es-ES" un texto', xtype),
{'x-default': 'some text', 'fr-FR': 'du texte', 'es-ES': 'un texto'})
# Invalid values
- self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid')
- self.assertEqual(XmpTag._convert_to_python('lang="malformed', xtype), 'lang="malformed')
- self.assertEqual(XmpTag._convert_to_python('xlang="x-default" some text', xtype), 'xlang="x-default" some text')
- self.assertEqual(XmpTag._convert_to_python('lang="x-default" some text, xlang="fr-FR" du texte', xtype), 'lang="x-default" some text, xlang="fr-FR" du texte')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'invalid', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'lang="malformed', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'xlang="x-default" some text', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'lang="x-default" some text, xlang="fr-FR" du texte', xtype)
def test_convert_to_python_mimetype(self):
xtype = 'MIMEType'
@@ -140,25 +140,21 @@ class TestXmpTag(unittest.TestCase):
self.assertEqual(XmpTag._convert_to_python('video/ogg', xtype),
{'type': 'video', 'subtype': 'ogg'})
# Invalid values
- self.assertEqual(XmpTag._convert_to_python('invalid', xtype), 'invalid')
- self.assertEqual(XmpTag._convert_to_python('image-jpeg', xtype), 'image-jpeg')
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'invalid', xtype)
+ self.failUnlessRaises(XmpValueError, XmpTag._convert_to_python, 'image-jpeg', xtype)
def test_convert_to_python_propername(self):
xtype = 'ProperName'
# Valid values
self.assertEqual(XmpTag._convert_to_python('Gérard', xtype), u'Gérard')
- self.assertEqual(XmpTag._convert_to_python(u'François Pignon', xtype), u'François Pignon')
self.assertEqual(XmpTag._convert_to_python('Python Software Foundation', xtype), u'Python Software Foundation')
def test_convert_to_python_text(self):
xtype = 'Text'
# Valid values
self.assertEqual(XmpTag._convert_to_python('Some text.', xtype), u'Some text.')
- self.assertEqual(XmpTag._convert_to_python(u'Some text.', xtype), u'Some text.')
self.assertEqual(XmpTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype),
u'Some text with exotic chàräctérʐ.')
- self.assertEqual(XmpTag._convert_to_python(u'Some text with exotic chàräctérʐ.', xtype),
- u'Some text with exotic chàräctérʐ.')
def test_convert_to_python_uri(self):
xtype = 'URI'