diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-04-09 09:31:36 +0200 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-04-09 09:31:36 +0200 |
commit | c455d8b72459c7e5c106389054b61d332f19ba60 (patch) | |
tree | 6c5cd3b1642f8f9edd8adefaa88e10dc52b6ec72 | |
parent | d33806fab7b3aebca64c1c60bc8151c334cc680b (diff) | |
download | pyexiv2-c455d8b72459c7e5c106389054b61d332f19ba60.tar.gz |
Custom values get/set/del for IPTC tags.
-rw-r--r-- | src/pyexiv2.py | 20 | ||||
-rw-r--r-- | unittest/iptc.py | 50 |
2 files changed, 61 insertions, 9 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 782bd44..63202da 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -534,7 +534,25 @@ class IptcTag(MetadataTag): """ super(IptcTag, self).__init__(key, name, label, description, xtype, values) # FIXME: make values either a tuple (immutable) or a notifying list - self.values = map(lambda x: IptcTag._convert_to_python(x, xtype), values) + self._values = map(lambda x: IptcTag._convert_to_python(x, xtype), values) + + def _get_values(self): + return self._values + + def _set_values(self, new_values): + if self.metadata is not None: + raw_value = map(lambda x: IptcTag._convert_to_string(x, self.xtype), new_values) + self.metadata._set_iptc_tag_values(self.key, raw_value) + self._values = new_values + + def _del_values(self): + if self.metadata is not None: + self.metadata._delete_iptc_tag(self.key) + del self._values + + # DOCME + values = property(fget=_get_values, fset=_set_values, fdel=_del_values, + doc=None) @staticmethod def _convert_to_python(value, xtype): diff --git a/unittest/iptc.py b/unittest/iptc.py index 0f98b8c..38826d8 100644 --- a/unittest/iptc.py +++ b/unittest/iptc.py @@ -29,6 +29,20 @@ from pyexiv2 import IptcTag, IptcValueError, FixedOffset import datetime +class ImageMetadataMock(object): + + tags = {} + + def _set_iptc_tag_values(self, key, values): + self.tags[key] = values + + def _delete_iptc_tag(self, key): + try: + del self.tags[key] + except KeyError: + pass + + class TestIptcTag(unittest.TestCase): def test_convert_to_python_short(self): @@ -162,17 +176,37 @@ class TestIptcTag(unittest.TestCase): self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype) def test_set_values_no_metadata(self): - # TODO - raise NotImplementedError() + tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \ + 'city of object data origin according to guidelines ' \ + 'established by the provider.', 'String', ['Seattle']) + old_values = tag.values + tag.values = ['Barcelona'] + self.failIfEqual(tag.values, old_values) def test_set_values_with_metadata(self): - # TODO - raise NotImplementedError() + tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \ + 'city of object data origin according to guidelines ' \ + 'established by the provider.', 'String', ['Seattle']) + tag.metadata = ImageMetadataMock() + old_values = tag.values + tag.values = ['Barcelona'] + self.failIfEqual(tag.values, old_values) + self.assertEqual(tag.metadata.tags[tag.key], ['Barcelona']) def test_del_values_no_metadata(self): - # TODO - raise NotImplementedError() + tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \ + 'city of object data origin according to guidelines ' \ + 'established by the provider.', 'String', ['Seattle']) + del tag.values + self.failIf(hasattr(tag, 'values')) def test_del_values_with_metadata(self): - # TODO - raise NotImplementedError() + tag = IptcTag('Iptc.Application2.City', 'City', 'City', 'Identifies ' \ + 'city of object data origin according to guidelines ' \ + 'established by the provider.', 'String', ['Seattle']) + tag.metadata = ImageMetadataMock() + tag.metadata._set_iptc_tag_values(tag.key, tag.to_string()) + self.assertEqual(tag.metadata.tags, {tag.key: ['Seattle']}) + del tag.values + self.failIf(hasattr(tag, 'values')) + self.failIf(tag.metadata.tags.has_key(tag.key)) |