diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2007-03-18 21:57:25 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2007-03-18 21:57:25 +0100 |
commit | 2971857b6a9adbd94940b24f3638f5bcea93f17a (patch) | |
tree | 35b13b48a96316e6994c779db0ae6b2cdf23789a | |
parent | 970ccb88c37684c1322412fc937b6777f35e624c (diff) | |
download | pyexiv2-2971857b6a9adbd94940b24f3638f5bcea93f17a.tar.gz |
Added a __delitem__(key) method to class Image in order to allow the use of the dictionary metaphor for write access to metadata tags.
-rw-r--r-- | src/pyexiv2.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 3324351..ec4a892 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -454,6 +454,8 @@ class Image(libpyexiv2.Image): def __getitem__(self, key): print 'Calling __getitem__(...)' + if key.__class__ is not str: + raise TypeError('Key must be of type string') tagFamily = key[:4] if tagFamily == 'Exif': try: @@ -472,10 +474,12 @@ class Image(libpyexiv2.Image): self.__iptcTagsDict[key] = value return value else: - raise KeyError('Invalid tag identifier') + raise IndexError("'" + key + "': invalid tag identifier") def __setitem__(self, key, value): print 'Calling __setitem__(...)' + if key.__class__ is not str: + raise TypeError('Key must be of type string') tagFamily = key[:4] if tagFamily == 'Exif': if value is not None: @@ -525,7 +529,26 @@ class Image(libpyexiv2.Image): else: del self.__iptcTagsDict[key] else: - raise KeyError('Invalid tag identifier') + raise IndexError("'" + key + "': invalid tag identifier") + + def __delitem__(self, key): + print 'Calling __delitem__(...)' + if key.__class__ is not str: + raise TypeError('Key must be of type string') + tagFamily = key[:4] + if tagFamily == 'Exif': + self.deleteExifTag(key) + del self.__exifTagsDict[key] + elif tagFamily == 'Iptc': + try: + oldValues = self.__iptcTagsDict[key] + except KeyError: + oldValues = self.getIptcTag(key) + for i in xrange(len(oldValues)): + self.deleteIptcTag(key, 0) + del self.__iptcTagsDict[key] + else: + raise IndexError("'" + key + "': invalid tag identifier") def _test(): print 'testing library pyexiv2...' |