From f47b027237b1081818615d18ee0475ca8a4663d3 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Wed, 8 Apr 2009 09:29:34 +0200 Subject: Delete an IPTC tag. --- src/pyexiv2.py | 10 ++++++++-- unittest/metadata.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/pyexiv2.py b/src/pyexiv2.py index c66c73c..014b58f 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -1149,8 +1149,14 @@ class ImageMetadata(object): pass def _delete_iptc_tag(self, key): - # TODO - raise NotImplementedError() + if key not in self.iptc_keys: + raise KeyError('Cannot delete an inexistent tag') + self._image.deleteIptcTag(key) + try: + del self._tags['iptc'][key] + except KeyError: + # The tag was not cached. + pass def _delete_xmp_tag(self, key): # TODO diff --git a/unittest/metadata.py b/unittest/metadata.py index 0fc3b4c..ade5599 100644 --- a/unittest/metadata.py +++ b/unittest/metadata.py @@ -67,6 +67,12 @@ class ImageMock(object): def setIptcTagValues(self, key, values): self.tags['iptc'][key] = values + def deleteIptcTag(self, key): + try: + del self.tags['iptc'][key] + except KeyError: + pass + def xmpKeys(self): return self.tags['xmp'].keys() @@ -402,16 +408,31 @@ class TestImageMetadata(unittest.TestCase): self.assertEqual(self.metadata._image.tags['iptc'][key], values) def test_delete_iptc_tag_inexistent(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_iptc_tags() + key = 'Iptc.Application2.LocationCode' + self.failUnlessRaises(KeyError, self.metadata._delete_iptc_tag, key) def test_delete_iptc_tag_not_cached(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_iptc_tags() + key = 'Iptc.Application2.Caption' + self.assertEqual(self.metadata._tags['iptc'], {}) + self.assert_(self.metadata._image.tags['iptc'].has_key(key)) + self.metadata._delete_iptc_tag(key) + self.assertEqual(self.metadata._tags['iptc'], {}) + self.failIf(self.metadata._image.tags['iptc'].has_key(key)) def test_delete_iptc_tag_cached(self): - # TODO - raise(NotImplementedError()) + self.metadata.read() + self._set_iptc_tags() + key = 'Iptc.Application2.Caption' + self.assert_(self.metadata._image.tags['iptc'].has_key(key)) + tag = self.metadata._get_iptc_tag(key) + self.assertEqual(self.metadata._tags['iptc'][key], tag) + self.metadata._delete_iptc_tag(key) + self.assertEqual(self.metadata._tags['iptc'], {}) + self.failIf(self.metadata._image.tags['iptc'].has_key(key)) ########################## # Test XMP-related methods -- cgit