Feature request: inherit ImageMetadata from collections.MutableMapping

Bug #648624 reported by Olivier Tilloy
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pyexiv2
Fix Released
Wishlist
Antti Siira

Bug Description

Hi,

would it be possible to make ImageMetadata inherit from collections.MutableMapping and provide the two missing class functions __len__ and __iter__. This way you get a bundle of nice features such as metadata.get(key, default), metadata.iteritems(), 'Some.Tag.Name' in metadata, [key for key in metadata] and so forth.

Example implementations for missing functions:
def __iter__(self):
    for key in self.exif_keys:
        yield key
    for key in self.iptc_keys:
        yield key
    for key in self.xmp_keys:
        yield key

def __len__(self):
    return len( [key for key in self] )

Related branches

Revision history for this message
Olivier Tilloy (osomon) wrote :

This was originally question #127006, filed by Antti Siira.

Revision history for this message
Olivier Tilloy (osomon) wrote :

Documentation for the collections module: http://docs.python.org/library/collections.html.

Changed in pyexiv2:
importance: Undecided → Wishlist
status: New → Confirmed
Revision history for this message
Antti Siira (antasi) wrote :

If I implement the feature and create the unit tests, how do I contribute the code back to the code base? I have not used Launchpad before.

The actual implementation is really as simple as shown above, since all the difficult parts have already been done.

Revision history for this message
Olivier Tilloy (osomon) wrote :

If you are willing to use Launchpad’s workflow (probably requires a little more work on your part, but will definitely ease the review process and subsequent tweaks), then you need to branch from lp:pyexiv2, implement the feature, commit locally (atomic commits will ease the review as well), push to launchpad (e.g. lp:~antasi/pyexiv2/mutablemapping), and then propose this branch for merging. I (and others) will then be able to review, possibly suggest changes, and finally merge in trunk.

There is some very good documentation on the process there:
https://help.launchpad.net/Code/FindingAndDownloading
https://help.launchpad.net/Code/UploadingABranch
https://help.launchpad.net/Code/Review

Alternatively, if you don’t feel comfortable with the whole process, you can just attach a patch against lp:pyexiv2 to this bug report.

Thanks in advance for your contribution!

Olivier Tilloy (osomon)
Changed in pyexiv2:
assignee: nobody → Antti Siira (antasi)
status: Confirmed → In Progress
Olivier Tilloy (osomon)
Changed in pyexiv2:
status: In Progress → Fix Committed
Olivier Tilloy (osomon)
Changed in pyexiv2:
milestone: none → 0.3
Olivier Tilloy (osomon)
Changed in pyexiv2:
status: Fix Committed → Fix Released
Revision history for this message
webmeischda (webmeischda) wrote :

The current implementation of this feature breaks compatibility with Python 2.5, as collections.MutableMapping has first been introduced in Python 2.6 (see http://docs.python.org/release/2.6.6/library/collections.html). The changelog (http://tilloy.net/dev/pyexiv2/news.html) currently claims Python 2.5 as minimal dependency and it would be nice if it could stay this way so that newer versions of pyexiv2 can still be used e.g. on Debian Lenny.

Revision history for this message
Olivier Tilloy (osomon) wrote :

Python’s documentation doesn’t explicitly state that MutableMapping was introduced in Python 2.6, it only says that the collections module is new in version 2.4. Could it be that it is incomplete in that regard?

Revision history for this message
Antti Siira (antasi) wrote :

It seems that collections module does not contain MutableMapping in Python 2.5.1.

It would not be that difficult to implement the necessary functions oneself and keep the functionality. On the other hand, 2.5 series is getting quite old and even Debian stable, Squeeze, now has 2.6.

Revision history for this message
Olivier Tilloy (osomon) wrote :

webmeischda you are right, the ABCs in the collections module were introduced in python 2.6 (http://docs.python.org/whatsnew/2.6.html#pep-3119-abstract-base-classes). The documentation is seriously flawed in that regard.

I am not sure what the best course of action is here. Since pyexiv2 0.3.0 has already been released and has already been packaged in several distributions, it is probably better to amend its changelog to bump the minimal supported version to 2.6. For other distributions that do not ship a version of python > 2.5, pyexiv2 can probably easily be patched to remove this strong dependency. The following patch should work (disclaimer: not tested!):

=== modified file 'src/pyexiv2/metadata.py'
--- src/pyexiv2/metadata.py 2010-12-19 23:41:38 +0000
+++ src/pyexiv2/metadata.py 2011-02-07 09:10:28 +0000
@@ -31,7 +31,6 @@
 import os
 import sys
 from errno import ENOENT
-from collections import MutableMapping
 from itertools import chain
 import codecs

@@ -43,7 +42,7 @@
 from pyexiv2.preview import Preview

-class ImageMetadata(MutableMapping):
+class ImageMetadata(object):

     """
     A container for all the metadata embedded in an image.

Revision history for this message
Olivier Tilloy (osomon) wrote :

Antti, you’re absolutely right, the new Debian stable (Squeeze, released yesterday) now ships python 2.6.6 by default, and supporting older versions is not really on the agenda.

Re: implementing the necessary function oneself and keep the functionality, I believe the patch I posted above is sufficient in that regard. Needs testing though.

In any case, I’ll update pyexiv2’s changelog.

Revision history for this message
Olivier Tilloy (osomon) wrote :

I also updated the website (http://tilloy.net/dev/pyexiv2/) and the online documentation to reflect the dependency of pyexiv2 0.3.0 on Python ≥ 2.6.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.