diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-01-28 23:41:04 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-01-28 23:41:04 +0100 |
commit | 9f315477dacd0df820a6912e7218f63e7601ccec (patch) | |
tree | 5c3df6c71b311c2301c8ce7cd6b40bc1e371642d | |
parent | 926a9db178e8d62b4bb15940c5df80d8814c4d11 (diff) | |
download | pyexiv2-9f315477dacd0df820a6912e7218f63e7601ccec.tar.gz |
XMP Text to string conversion + unit tests.
-rw-r--r-- | src/pyexiv2.py | 30 | ||||
-rw-r--r-- | unittest/xmp.py | 11 |
2 files changed, 32 insertions, 9 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index a147340..8fa4a48 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -643,16 +643,30 @@ class XmpTag(MetadataTag): def _convert_to_string(value, xtype): """ Convert a value to its corresponding string representation. - Fallback to str(value) if no standard-compliant conversion can be done. + + @param value: the value to be converted + @type value: depends on xtype (DOCME) + @param xtype: the XMP type of the value + @type xtype: C{str} + + @return: the value converted to its corresponding string representation + @rtype: C{str} + + @raise L{XmpValueError}: if the conversion fails """ - if xtype == 'Boolean': - if value == True: - return 'True' - elif value == False: - return 'False' + if xtype == 'Boolean' and type(value) is bool: + return str(value) + + elif xtype == 'Text': + if type(value) is unicode: + try: + return value.encode('utf-8') + except UnicodeEncodeError: + raise XmpValueError(value, xtype) + elif type(value) is str: + return value - # Default fallback conversion - return str(value) + raise XmpValueError(value, xtype) def __str__(self): """ diff --git a/unittest/xmp.py b/unittest/xmp.py index 8aaa523..d8293fa 100644 --- a/unittest/xmp.py +++ b/unittest/xmp.py @@ -52,7 +52,7 @@ class TestXmpTag(unittest.TestCase): self.assertEqual(XmpTag._convert_to_string(True, xtype), 'True') self.assertEqual(XmpTag._convert_to_string(False, xtype), 'False') # Invalid values - self.assertEqual(XmpTag._convert_to_string('invalid', xtype), 'invalid') + self.failUnlessRaises(XmpValueError, XmpTag._convert_to_string, 'invalid', xtype) def test_convert_to_python_choice(self): pass @@ -161,6 +161,15 @@ class TestXmpTag(unittest.TestCase): self.assertEqual(XmpTag._convert_to_python('Some text with exotic chàräctérʐ.', xtype), u'Some text with exotic chàräctérʐ.') + def test_convert_to_string_text(self): + xtype = 'Text' + # Valid values + self.assertEqual(XmpTag._convert_to_string(u'Some text', xtype), 'Some text') + self.assertEqual(XmpTag._convert_to_string(u'Some text with exotic chàräctérʐ.', xtype), + 'Some text with exotic chàräctérʐ.') + self.assertEqual(XmpTag._convert_to_string('Some text with exotic chàräctérʐ.', xtype), + 'Some text with exotic chàräctérʐ.') + def test_convert_to_python_uri(self): xtype = 'URI' # Valid values |