aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2010-02-17 22:35:35 +0100
committerOlivier Tilloy <olivier@tilloy.net>2010-02-17 22:35:35 +0100
commit084d3d8e8525f6088ef2ac0ba83d93a5bb5c0daf (patch)
treebc63b61ef3b8fa971b16ed049b737ea37a96c64b
parent6f165359c2e67e35b328fdb5b6ad6aa8f22cdab9 (diff)
downloadpyexiv2-084d3d8e8525f6088ef2ac0ba83d93a5bb5c0daf.tar.gz
Direct value assignment as an alternative to passing a fully-formed tag.
-rw-r--r--src/pyexiv2/metadata.py19
-rw-r--r--test/metadata.py18
2 files changed, 21 insertions, 16 deletions
diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py
index 82fe1f9..0277ca0 100644
--- a/src/pyexiv2/metadata.py
+++ b/src/pyexiv2/metadata.py
@@ -170,10 +170,11 @@ class ImageMetadata(object):
except AttributeError:
raise KeyError(key)
- def _set_exif_tag(self, tag):
+ def _set_exif_tag(self, key, tag):
# Set an EXIF tag. If the tag already exists, its value is overwritten.
if not isinstance(tag, ExifTag):
- raise TypeError('Expecting an ExifTag')
+ # As a handy shortcut, accept direct value assignment.
+ tag = ExifTag(key, tag)
self._image._setExifTagValue(tag.key, tag.raw_value)
self._tags['exif'][tag.key] = tag
if tag.key not in self.exif_keys:
@@ -192,11 +193,12 @@ class ImageMetadata(object):
raise TypeError('Expecting a string')
self._image._setExifTagValue(key, value)
- def _set_iptc_tag(self, tag):
+ def _set_iptc_tag(self, key, tag):
# Set an IPTC tag. If the tag already exists, its values are
# overwritten.
if not isinstance(tag, IptcTag):
- raise TypeError('Expecting an IptcTag')
+ # As a handy shortcut, accept direct value assignment.
+ tag = IptcTag(key, tag)
self._image._setIptcTagValues(tag.key, tag.raw_values)
self._tags['iptc'][tag.key] = tag
if tag.key not in self.iptc_keys:
@@ -219,10 +221,11 @@ class ImageMetadata(object):
raise TypeError('Expecting a list of strings')
self._image._setIptcTagValues(key, values)
- def _set_xmp_tag(self, tag):
+ def _set_xmp_tag(self, key, tag):
# Set an XMP tag. If the tag already exists, its value is overwritten.
if not isinstance(tag, XmpTag):
- raise TypeError('Expecting an XmpTag')
+ # As a handy shortcut, accept direct value assignment.
+ tag = XmpTag(key, tag)
type = tag._tag._getExiv2Type()
if type == 'XmpText':
self._image._setXmpTagTextValue(tag.key, tag.raw_value)
@@ -257,6 +260,8 @@ class ImageMetadata(object):
"""
Set a metadata tag for a given key.
If the tag was previously set, it is overwritten.
+ As a handy shortcut, a value may be passed instead of a fully formed
+ tag. The corresponding tag object will be instantiated.
:param key: metadata key in the dotted form
``familyName.groupName.tagName`` where ``familyName`` may
@@ -270,7 +275,7 @@ class ImageMetadata(object):
"""
family = key.split('.')[0].lower()
try:
- return getattr(self, '_set_%s_tag' % family)(tag)
+ return getattr(self, '_set_%s_tag' % family)(key, tag)
except AttributeError:
raise KeyError(key)
diff --git a/test/metadata.py b/test/metadata.py
index 4ef933d..200c6ea 100644
--- a/test/metadata.py
+++ b/test/metadata.py
@@ -260,7 +260,7 @@ class TestImageMetadata(unittest.TestCase):
tag = ExifTag('Exif.Thumbnail.Orientation', 1)
self.assertEqual(tag.metadata, None)
self.assert_(tag.key not in self.metadata.exif_keys)
- self.metadata._set_exif_tag(tag)
+ self.metadata._set_exif_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assert_(tag.key in self.metadata.exif_keys)
self.assertEqual(self.metadata._tags['exif'], {tag.key: tag})
@@ -275,7 +275,7 @@ class TestImageMetadata(unittest.TestCase):
# Overwrite an existing tag
tag = ExifTag('Exif.Image.DateTime', datetime.datetime(2009, 3, 20, 20, 32, 0))
self.assertEqual(tag.metadata, None)
- self.metadata._set_exif_tag(tag)
+ self.metadata._set_exif_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'], {tag.key: tag})
self.assert_(self.metadata._image.tags['exif'].has_key(tag.key))
@@ -292,7 +292,7 @@ class TestImageMetadata(unittest.TestCase):
self.assertEqual(self.metadata._tags['exif'][key], tag)
new_tag = ExifTag(key, '48 50 50 48 ', _tag=_ExifTagMock(key, 'Undefined', '48 50 50 48 ', '2.20'))
self.assertEqual(new_tag.metadata, None)
- self.metadata._set_exif_tag(new_tag)
+ self.metadata._set_exif_tag(key, new_tag)
self.assertEqual(new_tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'], {key: new_tag})
self.assert_(self.metadata._image.tags['exif'].has_key(key))
@@ -396,7 +396,7 @@ class TestImageMetadata(unittest.TestCase):
tag = IptcTag('Iptc.Application2.Writer', ['Nobody'])
self.assertEqual(tag.metadata, None)
self.assert_(tag.key not in self.metadata.iptc_keys)
- self.metadata._set_iptc_tag(tag)
+ self.metadata._set_iptc_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assert_(tag.key in self.metadata.iptc_keys)
self.assertEqual(self.metadata._tags['iptc'], {tag.key: tag})
@@ -411,7 +411,7 @@ class TestImageMetadata(unittest.TestCase):
# Overwrite an existing tag
tag = IptcTag('Iptc.Application2.Caption', ['A picture.'])
self.assertEqual(tag.metadata, None)
- self.metadata._set_iptc_tag(tag)
+ self.metadata._set_iptc_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'], {tag.key: tag})
self.assert_(self.metadata._image.tags['iptc'].has_key(tag.key))
@@ -428,7 +428,7 @@ class TestImageMetadata(unittest.TestCase):
self.assertEqual(self.metadata._tags['iptc'][key], tag)
new_tag = IptcTag(key, ['A picture.'])
self.assertEqual(new_tag.metadata, None)
- self.metadata._set_iptc_tag(new_tag)
+ self.metadata._set_iptc_tag(key, new_tag)
self.assertEqual(new_tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'], {key: new_tag})
self.assert_(self.metadata._image.tags['iptc'].has_key(key))
@@ -535,7 +535,7 @@ class TestImageMetadata(unittest.TestCase):
'fr-FR': "Ceci n'est pas un titre"})
self.assertEqual(tag.metadata, None)
self.assert_(tag.key not in self.metadata.xmp_keys)
- self.metadata._set_xmp_tag(tag)
+ self.metadata._set_xmp_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assert_(tag.key in self.metadata.xmp_keys)
self.assertEqual(self.metadata._tags['xmp'], {tag.key: tag})
@@ -550,7 +550,7 @@ class TestImageMetadata(unittest.TestCase):
# Overwrite an existing tag
tag = XmpTag('Xmp.dc.format', ('image', 'png'))
self.assertEqual(tag.metadata, None)
- self.metadata._set_xmp_tag(tag)
+ self.metadata._set_xmp_tag(tag.key, tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['xmp'], {tag.key: tag})
self.assert_(self.metadata._image.tags['xmp'].has_key(tag.key))
@@ -567,7 +567,7 @@ class TestImageMetadata(unittest.TestCase):
self.assertEqual(self.metadata._tags['xmp'][key], tag)
new_tag = XmpTag(key, datetime.datetime(2009, 4, 21, 20, 7, 0, tzinfo=FixedOffset('+', 1, 0)))
self.assertEqual(new_tag.metadata, None)
- self.metadata._set_xmp_tag(new_tag)
+ self.metadata._set_xmp_tag(key, new_tag)
self.assertEqual(new_tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['xmp'], {key: new_tag})
self.assert_(self.metadata._image.tags['xmp'].has_key(key))