Skip to content

arvedarved/mailcap_fix

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mailcap Fix

Latest Version Supported Python versions Build

Summary

This package patches the python standard library's mailcap module in order to correctly handle windcard entries.

The bug is documented on the bug tracker here: issue 14977

Installation

$ pip install mailcap-fix

Example

Consider a mailcap file that contains the following two lines

image/*; feh %s
image/jpeg; eog %s

Because the image/* entry is defined first, it should take precedence over the image/jpeg entry when searching for a match. This behavior is defined by RFC 1524. However, the standard library's implementation will always evaluate wildcard entries last.

Before

>>> import mailcap
>>> d = mailcap.getcaps()
>>> # Incorrectly returns the second entry
>>> mailcap.findmatch(d, 'image/jpeg', filename='test.jpg')
('eog test.jpg', {'view': 'eog %s'})

After

>>> import mailcap_fix as mailcap
>>> d = mailcap.getcaps()
>>> # Correctly returns the wildcard entry
>>> mailcap.findmatch(d, 'image/jpeg', filename='test.jpg')
('feh test.jpg', {'view': 'feh %s', 'lineno': 0})

How it works

The goal of this patch is to conform to RFCC 1524, while preserving as much backwards compatibility as possible and without adding any "magic".

mailcap_fix adds a new field lineno to each entry in the mailcap dict. This line number is then used to sort entries in descending order when searching for a match. For backwards compatability, if lino is not present entries will simply not be sorted. Because RFC 1524 defines a whitelist of valid mailcap fieldnames, the addition of lineno should not conflict with any other mailcap fields.

Benchmark

Python 3.4.0
Ubuntu 14.04 LTS 64bit
Intel® Core™ i5-3210M CPU @ 2.50GHz × 4
8 GiB RAM
mailcap.get_caps()
==================== ============ ============
trivial_mailcap 0.081881 ms 0.084525 ms
extended_mailcap 17.746289 ms 18.407623 ms
mailcap.lookup()
==================== =========== ===========
trivial_mailcap 0.000996 ms 0.003144 ms
extended_mailcap 0.000798 ms 0.002731 ms

About

A patched mailcap module that conforms to RFC 1524

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%