aboutsummaryrefslogtreecommitdiffstats
path: root/src/pyexiv2.py
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2007-03-15 22:44:30 +0100
committerOlivier Tilloy <olivier@tilloy.net>2007-03-15 22:44:30 +0100
commit7ef2f339bf2b7439d33a51582038ac98867fdee7 (patch)
treeb664fbdabaa78b18da0c28471ca9ab76504b7658 /src/pyexiv2.py
parentc9f24f0d2755199345f448b9d4ebbaa436788d0d (diff)
downloadpyexiv2-7ef2f339bf2b7439d33a51582038ac98867fdee7.tar.gz
Added a __getitem__(key) method to class Image in order to allow the use of the dictionary metaphor to access metadata tags.
TODO: define the corresponding __setitem__(key, value) and __delitem__(key) methods for completeness.
Diffstat (limited to 'src/pyexiv2.py')
-rw-r--r--src/pyexiv2.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index 9435ba7..eefda51 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -286,6 +286,11 @@ class Image(libpyexiv2.Image):
setIptcTagValue -- set the value associated to a key in IPTC metadata
"""
+ def __init__(self, filename):
+ libpyexiv2.Image.__init__(self, filename)
+ self.__exifTagsDict = {}
+ self.__iptcTagsDict = {}
+
def getExifTagValue(self, key):
"""
Get the value associated to a key in EXIF metadata.
@@ -447,6 +452,27 @@ class Image(libpyexiv2.Image):
strVal = str(value)
self.setIptcTag(key, strVal, index)
+ def __getitem__(self, key):
+ tagFamily = key[:4]
+ if tagFamily == 'Exif':
+ try:
+ return self.__exifTagsDict[key]
+ except KeyError:
+ value = self.getExifTagValue(key)
+ self.__exifTagsDict[key] = value
+ return value
+ elif tagFamily == 'Iptc':
+ try:
+ return self.__iptcTagsDict[key]
+ except KeyError:
+ value = self.getIptcTagValue(key)
+ if len(value) == 1:
+ value = value[0]
+ self.__iptcTagsDict[key] = value
+ return value
+ else:
+ raise KeyError('Invalid tag identifier')
+
def _test():
print 'testing library pyexiv2...'
# TODO: various tests