diff options
-rw-r--r-- | src/exiv2wrapper.cpp | 17 | ||||
-rw-r--r-- | src/exiv2wrapper.hpp | 4 | ||||
-rw-r--r-- | src/exiv2wrapper_python.cpp | 1 | ||||
-rw-r--r-- | src/pyexiv2.py | 10 | ||||
-rw-r--r-- | unittest/metadata.py | 33 | ||||
-rw-r--r-- | unittest/xmp.py | 32 |
6 files changed, 81 insertions, 16 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index b91a2ac..9b0a5da 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -375,6 +375,23 @@ void Image::setXmpTagValue(std::string key, std::string value) _xmpData[key] = value; } +void Image::deleteXmpTag(std::string key) +{ + if(!_dataRead) + { + throw Exiv2::Error(METADATA_NOT_READ); + } + + Exiv2::XmpKey xmpKey = Exiv2::XmpKey(key); + Exiv2::XmpMetadata::iterator i = _xmpData.findKey(xmpKey); + if(i != _xmpData.end()) + { + _xmpData.erase(i); + } + else + throw Exiv2::Error(KEY_NOT_FOUND, key); +} + /* boost::python::tuple Image::getThumbnailData() { diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp index 02c1d1a..56b636e 100644 --- a/src/exiv2wrapper.hpp +++ b/src/exiv2wrapper.hpp @@ -115,6 +115,10 @@ public: void setXmpTagValue(std::string key, std::string value); + // Delete the required XMP tag. + // Throw an exception if the tag was not set. + void deleteXmpTag(std::string key); + // Read and write access to the thumbnail embedded in the image. // Return a tuple containing the format of the thumbnail ("TIFF" or diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp index c7bda03..c4eb9d8 100644 --- a/src/exiv2wrapper_python.cpp +++ b/src/exiv2wrapper_python.cpp @@ -63,6 +63,7 @@ BOOST_PYTHON_MODULE(libexiv2python) .def("xmpKeys", &Image::xmpKeys) .def("getXmpTag", &Image::getXmpTag) .def("setXmpTagValue", &Image::setXmpTagValue) + .def("deleteXmpTag", &Image::deleteXmpTag) // .def("getThumbnailData", &Image::getThumbnailData) // .def("setThumbnailData", &Image::setThumbnailData) diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 677ef7e..309c112 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -1210,8 +1210,14 @@ class ImageMetadata(object): pass def _delete_xmp_tag(self, key): - # TODO - raise NotImplementedError() + if key not in self.xmp_keys: + raise KeyError('Cannot delete an inexistent tag') + self._image.deleteXmpTag(key) + try: + del self._tags['xmp'][key] + except KeyError: + # The tag was not cached. + pass def __delitem__(self, key): """ diff --git a/unittest/metadata.py b/unittest/metadata.py index 62918c5..6d106ec 100644 --- a/unittest/metadata.py +++ b/unittest/metadata.py @@ -82,6 +82,12 @@ class ImageMock(object): def setXmpTagValue(self, key, value): self.tags['xmp'][key] = value + def deleteXmpTag(self, key): + try: + del self.tags['xmp'][key] + except KeyError: + pass + class TestImageMetadata(unittest.TestCase): @@ -555,16 +561,31 @@ class TestImageMetadata(unittest.TestCase): self.assertEqual(self.metadata._image.tags['xmp'][key], value) def test_delete_xmp_tag_inexistent(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_xmp_tags() + key = 'Xmp.xmp.CreatorTool' + self.failUnlessRaises(KeyError, self.metadata._delete_xmp_tag, key) def test_delete_xmp_tag_not_cached(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_xmp_tags() + key = 'Xmp.dc.subject' + self.assertEqual(self.metadata._tags['xmp'], {}) + self.assert_(self.metadata._image.tags['xmp'].has_key(key)) + self.metadata._delete_xmp_tag(key) + self.assertEqual(self.metadata._tags['xmp'], {}) + self.failIf(self.metadata._image.tags['xmp'].has_key(key)) def test_delete_xmp_tag_cached(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_xmp_tags() + key = 'Xmp.dc.subject' + self.assert_(self.metadata._image.tags['xmp'].has_key(key)) + tag = self.metadata._get_xmp_tag(key) + self.assertEqual(self.metadata._tags['xmp'][key], tag) + self.metadata._delete_xmp_tag(key) + self.assertEqual(self.metadata._tags['xmp'], {}) + self.failIf(self.metadata._image.tags['xmp'].has_key(key)) ########################### # Test dictionary interface diff --git a/unittest/xmp.py b/unittest/xmp.py index 4b977ca..be08e80 100644 --- a/unittest/xmp.py +++ b/unittest/xmp.py @@ -318,7 +318,7 @@ class TestXmpTag(unittest.TestCase): # TODO: other types - def test_set_values_no_metadata(self): + def test_set_value_no_metadata(self): tag = XmpTag('Xmp.xmp.ModifyDate', 'ModifyDate', 'Modify Date', 'The date and time the resource was last modified. Note:' \ ' The value of this property is not necessarily the same' \ @@ -329,7 +329,7 @@ class TestXmpTag(unittest.TestCase): tag.value = datetime.datetime(2009, 4, 22, 8, 30, 27, tzinfo=FixedOffset()) self.failIfEqual(tag.value, old_value) - def test_set_values_with_metadata(self): + def test_set_value_with_metadata(self): tag = XmpTag('Xmp.xmp.ModifyDate', 'ModifyDate', 'Modify Date', 'The date and time the resource was last modified. Note:' \ ' The value of this property is not necessarily the same' \ @@ -342,10 +342,26 @@ class TestXmpTag(unittest.TestCase): self.failIfEqual(tag.value, old_value) self.assertEqual(tag.metadata.tags[tag.key], '2009-04-22T08:30:27Z') - def test_del_values_no_metadata(self): - # TODO - raise NotImplementedError() + def test_del_value_no_metadata(self): + tag = XmpTag('Xmp.xmp.ModifyDate', 'ModifyDate', 'Modify Date', + 'The date and time the resource was last modified. Note:' \ + ' The value of this property is not necessarily the same' \ + "as the file's system modification date because it is " \ + 'set before the file is saved.', 'Date', + '2005-09-07T15:09:51-07:00') + del tag.value + self.failIf(hasattr(tag, 'value')) - def test_del_values_with_metadata(self): - # TODO - raise NotImplementedError() + def test_del_value_with_metadata(self): + tag = XmpTag('Xmp.xmp.ModifyDate', 'ModifyDate', 'Modify Date', + 'The date and time the resource was last modified. Note:' \ + ' The value of this property is not necessarily the same' \ + "as the file's system modification date because it is " \ + 'set before the file is saved.', 'Date', + '2005-09-07T15:09:51-07:00') + tag.metadata = ImageMetadataMock() + tag.metadata._set_xmp_tag_value(tag.key, tag.to_string()) + self.assertEqual(tag.metadata.tags, {tag.key: '2005-09-07T15:09:51-07:00'}) + del tag.value + self.failIf(hasattr(tag, 'value')) + self.failIf(tag.metadata.tags.has_key(tag.key)) |