aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2007-03-18 18:13:39 +0100
committerOlivier Tilloy <olivier@tilloy.net>2007-03-18 18:13:39 +0100
commit970ccb88c37684c1322412fc937b6777f35e624c (patch)
tree3e3edc1687c8425236ffda77ac88cca6783677b9 /src
parent7ef2f339bf2b7439d33a51582038ac98867fdee7 (diff)
downloadpyexiv2-970ccb88c37684c1322412fc937b6777f35e624c.tar.gz
Added a __setitem__(key, value) method to class Image in order to allow the use of the dictionary metaphor for write access to metadata tags.
TODO: define the corresponding __delitem__(key) method for completeness.
Diffstat (limited to 'src')
-rw-r--r--src/pyexiv2.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index eefda51..3324351 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -453,6 +453,7 @@ class Image(libpyexiv2.Image):
self.setIptcTag(key, strVal, index)
def __getitem__(self, key):
+ print 'Calling __getitem__(...)'
tagFamily = key[:4]
if tagFamily == 'Exif':
try:
@@ -473,6 +474,59 @@ class Image(libpyexiv2.Image):
else:
raise KeyError('Invalid tag identifier')
+ def __setitem__(self, key, value):
+ print 'Calling __setitem__(...)'
+ tagFamily = key[:4]
+ if tagFamily == 'Exif':
+ if value is not None:
+ self.setExifTagValue(key, value)
+ self.__exifTagsDict[key] = value
+ else:
+ self.deleteExifTag(key)
+ del self.__exifTagsDict[key]
+ elif tagFamily == 'Iptc':
+ # The case of IPTC tags is a bit trickier since some tags are
+ # repeatable. To simplify the process, parameter 'value' is
+ # transformed into a list if it is not already one and then each of
+ # its values is processed (set, that is) in a loop.
+ newValues = value
+ if newValues is None:
+ # Setting the value to None does not really make sense, but can
+ # in a way be seen as equivalent to deleting it, so this
+ # behaviour is simulated by providing an empty list for 'value'.
+ newValues = []
+ if newValues.__class__ is not list:
+ newValues = [newValues]
+ try:
+ oldValues = self.__iptcTagsDict[key]
+ if oldValues.__class__ is not list:
+ oldValues = [oldValues]
+ except KeyError:
+ # The tag is not set yet
+ oldValues = []
+ # This loop processes the values one by one. There are n cases:
+ # * if the two lists are of the exact same size, each item in
+ # oldValues is replaced by its new value in newValues;
+ # * if newValues is longer than oldValues, each item in oldValues
+ # is replaced by its new value in newValues and the new items
+ # are appended at the end of oldValues;
+ # * if newValues is shorter than oldValues, each item in newValues
+ # replaces the corresponding one in oldValues and the trailing
+ # extra items in oldValues are deleted.
+ for i in xrange(max(len(oldValues), len(newValues))):
+ try:
+ self.setIptcTagValue(key, newValues[i], i)
+ except IndexError:
+ self.deleteIptcTag(key, min(len(oldValues), len(newValues)))
+ if len(newValues) > 0:
+ if len(newValues) == 1:
+ newValues = newValues[0]
+ self.__iptcTagsDict[key] = newValues
+ else:
+ del self.__iptcTagsDict[key]
+ else:
+ raise KeyError('Invalid tag identifier')
+
def _test():
print 'testing library pyexiv2...'
# TODO: various tests