Framework for handling file attachments by mimetype

Review Request #2967 — Created March 18, 2012 and submitted — Latest diff uploaded

Information

Review Board

Reviewers

This is meant as an overview of the design for a framework handling file attachments according to different mimetypes (e.g., generating a thumbnail, determining the icon url, etc.). The FileAttachment class will have some notion of a handler, which will be called at the appropriate points (a la strategy patter). For mimetype-specific parsing, I chose mimeparse, although I suspect the spec may be amenable to simple string/regular-expression matching.

I was thinking of creating a MimetypeHandler class that will have a factory classmethod that will allow it to iterate over all the subclasses and find the appropriate handler. A handler indicates this capability through a matches classmethod. Ideally, each subclass will operate on a subset of the mimetypes that the parent class operates on. I'm not sure if using subclasses is the best way to do this, as the class will be registered automatically once you install the extension. I've worked around it as follows, but I'm not sure if it is a good way to do it:
    def matches(self, mimetype):
        ext_manager = get_extension_manager()
        for e in ext_manager.get_enabled_extensions():
            if e.__class__ == TextMimetypeHandler:
                return mimetype[0] == 'text'
        return False

I've also included a working extension for text mimetypes.
This works on a LinuxMint 10 (linux 2.6.35-22) virtual machine with Review board 1.7a on Firefox 3.6.16.