aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/pyexiv2.py42
-rw-r--r--test/iptc.py81
2 files changed, 67 insertions, 56 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index 88d08f2..5f17e92 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -786,7 +786,7 @@ class IptcTag(MetadataTag):
def _set_values(self, new_values):
if self.metadata is not None:
- raw_values = map(lambda x: IptcTag._convert_to_string(x, self.type), new_values)
+ raw_values = map(self._convert_to_string, new_values)
self.metadata._set_iptc_tag_values(self.key, raw_values)
# Make values a notifying list if needed
if isinstance(new_values, NotifyingList):
@@ -815,7 +815,7 @@ class IptcTag(MetadataTag):
def _convert_to_python(self, value):
"""
- Convert a raw value to its corresponding python type.
+ Convert one raw value to its corresponding python type.
@param value: the raw value to be converted
@type value: C{str}
@@ -875,47 +875,44 @@ class IptcTag(MetadataTag):
raise IptcValueError(value, self.type)
- @staticmethod
- def _convert_to_string(value, xtype):
+ def _convert_to_string(self, value):
"""
- Convert a value to its corresponding string representation, suitable to
- pass to libexiv2.
+ Convert one value to its corresponding string representation, suitable
+ to pass to libexiv2.
@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}
+ @type value: depends on C{self.type} (DOCME)
@return: the value converted to its corresponding string representation
@rtype: C{str}
@raise IptcValueError: if the conversion fails
"""
- if xtype == 'Short':
+ if self.type == 'Short':
if type(value) is int:
return str(value)
else:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
- elif xtype == 'String':
+ elif self.type == 'String':
if type(value) is unicode:
try:
return value.encode('utf-8')
except UnicodeEncodeError:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
elif type(value) is str:
return value
else:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
- elif xtype == 'Date':
+ elif self.type == 'Date':
if type(value) in (datetime.date, datetime.datetime):
# ISO 8601 date format
return value.strftime('%Y%m%d')
else:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
- elif xtype == 'Time':
+ elif self.type == 'Time':
if type(value) in (datetime.time, datetime.datetime):
r = value.strftime('%H%M%S')
if value.tzinfo is not None:
@@ -924,15 +921,15 @@ class IptcTag(MetadataTag):
r += '+0000'
return r
else:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
- elif xtype == 'Undefined':
+ elif self.type == 'Undefined':
if type(value) is str:
return value
else:
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
- raise IptcValueError(value, xtype)
+ raise IptcValueError(value, self.type)
def to_string(self):
"""
@@ -941,8 +938,7 @@ class IptcTag(MetadataTag):
@rtype: C{list} of C{str}
"""
- return map(lambda x: IptcTag._convert_to_string(x, self.type),
- self.values)
+ return map(self._convert_to_string, self.values)
def __str__(self):
"""
diff --git a/test/iptc.py b/test/iptc.py
index a630c24..99e660e 100644
--- a/test/iptc.py
+++ b/test/iptc.py
@@ -70,13 +70,16 @@ class TestIptcTag(unittest.TestCase):
self.failUnlessRaises(IptcValueError, tag._convert_to_python, '1E3')
def test_convert_to_string_short(self):
- xtype = 'Short'
+ type = 'Short'
+
# Valid values
- self.assertEqual(IptcTag._convert_to_string(123, xtype), '123')
- self.assertEqual(IptcTag._convert_to_string(-57, xtype), '-57')
+ tag = IptcTagMock('Iptc.Envelope.FileFormat', type)
+ self.assertEqual(tag._convert_to_string(123), '123')
+ self.assertEqual(tag._convert_to_string(-57), '-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, tag._convert_to_string, 'invalid')
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, 3.14)
def test_convert_to_python_string(self):
type = 'String'
@@ -91,15 +94,18 @@ class TestIptcTag(unittest.TestCase):
self.failUnlessRaises(IptcValueError, tag._convert_to_python, None)
def test_convert_to_string_string(self):
- xtype = 'String'
+ type = 'String'
+
# Valid values
- self.assertEqual(IptcTag._convert_to_string(u'Some text', xtype), 'Some text')
- self.assertEqual(IptcTag._convert_to_string(u'Some text with exotic chàräctérʐ.', xtype),
+ tag = IptcTagMock('Iptc.Application2.Subject', type)
+ self.assertEqual(tag._convert_to_string(u'Some text'), 'Some text')
+ self.assertEqual(tag._convert_to_string(u'Some text with exotic chàräctérʐ.'),
'Some text with exotic chàräctérʐ.')
- self.assertEqual(IptcTag._convert_to_string('Some text with exotic chàräctérʐ.', xtype),
+ self.assertEqual(tag._convert_to_string('Some text with exotic chàräctérʐ.'),
'Some text with exotic chàräctérʐ.')
+
# Invalid values
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, None)
def test_convert_to_python_date(self):
type = 'Date'
@@ -118,19 +124,22 @@ class TestIptcTag(unittest.TestCase):
self.failUnlessRaises(IptcValueError, tag._convert_to_python, '2009-02-24T22:12:54')
def test_convert_to_string_date(self):
- xtype = 'Date'
+ type = 'Date'
+
# Valid values
- self.assertEqual(IptcTag._convert_to_string(datetime.date(2009, 2, 4), xtype),
+ tag = IptcTagMock('Iptc.Envelope.DateSent', type)
+ self.assertEqual(tag._convert_to_string(datetime.date(2009, 2, 4)),
'20090204')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(1999, 10, 13), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13)),
'19991013')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2009, 2, 4)),
'20090204')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4, 10, 52, 37), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2009, 2, 4, 10, 52, 37)),
'20090204')
+
# Invalid values
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype)
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, 'invalid')
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, None)
def test_convert_to_python_time(self):
type = 'Time'
@@ -153,30 +162,33 @@ class TestIptcTag(unittest.TestCase):
self.failUnlessRaises(IptcValueError, tag._convert_to_python, '081242+0000')
def test_convert_to_string_time(self):
- xtype = 'Time'
+ type = 'Time'
+
# Valid values
- self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4), xtype),
+ tag = IptcTagMock('Iptc.Envelope.TimeSent', type)
+ self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4)),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, 574), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, 574)),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset()), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset())),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30))),
'105204+0530')
- self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0))),
'105204-0400')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4)),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478)),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset()), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset())),
'105204+0000')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30))),
'105204+0530')
- self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype),
+ self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0))),
'105204-0400')
+
# Invalid values
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype)
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, 'invalid')
def test_convert_to_python_undefined(self):
type = 'Undefined'
@@ -189,14 +201,17 @@ class TestIptcTag(unittest.TestCase):
'�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}')
def test_convert_to_string_undefined(self):
- xtype = 'Undefined'
+ type = 'Undefined'
+
# Valid values
- self.assertEqual(IptcTag._convert_to_string('Some binary data.', xtype),
+ tag = IptcTagMock('Iptc.Envelope.CharacterSet', type)
+ self.assertEqual(tag._convert_to_string('Some binary data.'),
'Some binary data.')
- self.assertEqual(IptcTag._convert_to_string('�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}', xtype),
+ self.assertEqual(tag._convert_to_string('�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}'),
'�lj1�eEϟ�u����ᒻ;C(�SpI]���QI�}')
+
# Invalid values
- self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype)
+ self.failUnlessRaises(IptcValueError, tag._convert_to_string, None)
def test_set_values_no_metadata(self):
tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \