�`^c@s�dZddlZddgZd�Zd�Zd�Zd�Zd �Zd
dgd�Zdd
�Z
gd�Zd�Zd�Z
d�Zedkr�e
�ndS(s%Mailcap file handling. See RFC 1524.i�Ntgetcapst findmatchcCs�i}x�t�D]�}yt|d�}Wntk
r?qnX|�t|�}WdQXxE|j�D]7\}}||kr�|||<qf|||||<qfWqW|S(s�Return a dictionary containing the mailcap database.
The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')
to a list of dictionaries corresponding to mailcap entries. The list
collects all the entries for that MIME type from all available mailcap
files. Each dictionary contains key-value pairs for that MIME type,
where the viewing command is stored with the key "view".
trN(tlistmailcapfilestopentIOErrortreadmailcapfilet iteritems(tcapstmailcaptfptmorecapstkeytvalue((s/sys/lib/python2.7/mailcap.pyR s
cCsmdtjkr.tjd}|jd�}n;dtjkrMtjd}nd}|ddddg}|S( s7Return a list of all mailcap files found on the system.tMAILCAPSt:tHOMEt.s /.mailcaps/etc/mailcaps/usr/etc/mailcaps/usr/local/etc/mailcap(tostenvirontsplit(tstrtmailcapsthome((s/sys/lib/python2.7/mailcap.pyR"s
cCs:i}x-|j�}|sPn|ddks |j�dkrGq n|}x=|ddkr�|j�}|s{d}n|d |}qPWt|�\}}|o�|s�q n|jd�}x.tt|��D]}||j�||<q�Wdj|�j�}||kr&||j|�q |g||<q |S(s�Read a mailcap file and return a dictionary keyed by MIME type.
Each MIME type is mapped to an entry consisting of a list of
dictionaries; the list will contain more than one such dictionary
if a given MIME type appears more than once in the mailcap file.
Each dictionary contains key-value pairs for that MIME type, where
the viewing command is stored with the key "view".
it#ti�s\
s
t/( treadlinetstript parselineRtrangetlentjointlowertappend(R
RtlinetnextlineRtfieldsttypestj((s/sys/lib/python2.7/mailcap.pyR5s0 " c
Cs g}dt|�}}x?||krZt|||�\}}|j|�|d}qWt|�dkrqdS|d|d|d}}}i|d6}xu|D]m}|jd�}|dkr�|}d} n$|| j�}||dj�} ||krq�| ||<q�W||fS( s�Parse one entry in a mailcap file and return a dictionary.
The viewing command is stored as the value with the key "view",
and the rest of the fields produce key-value pairs in the dict.
iiitviewt=RN(NN(Rt
parsefieldR"tNonetfindR(
R#R%titntfieldRR(tresttfkeytfvalue((s/sys/lib/python2.7/mailcap.pyR[s*
cCsm|}xM||krU||}|dkr/Pq |dkrH|d}q |d}q W|||!j�|fS(s/Separate one key-value pair in a mailcap entry.t;s\ii(R(R#R-R.tstarttc((s/sys/lib/python2.7/mailcap.pyR*zs
R(s /dev/nullc Cs�t|||�}xt|D]l}d|kret|d||�}|retj|�dkreqqent|||||�}||fSWdS(sFind a match for a mailcap entry.
Return a tuple containing the command line, and the mailcap entry
used; (None, None) if no match is found. This may invoke the
'test' command of several matching entries before deciding which
entry to use.
ttestiN(NN(tlookuptsubstRtsystemR+( RtMIMEtypeRtfilenametplisttentriesteR6tcommand((s/sys/lib/python2.7/mailcap.pyR�s
cCs�g}||kr#|||}n|jd�}|dd}||kr]|||}n|dk r�t|d�|�}n|S(NRis/*cSs
||kS(N((R>R((s/sys/lib/python2.7/mailcap.pyt<lambda>�s(RR+tfilter(RR:RR=t MIMEtypes((s/sys/lib/python2.7/mailcap.pyR7�sc
Cs`d}dt|�}}x@||kr[||}|d}|dkr|dkrr|||d!}|d}n||}q||}|d}|dkr�||}q|dkr�||}q|dkr�||}q|dkrJ|}x*||kr||d kr|d}q�||!} |d}|t| |�}q|d|}qW|S(
NRiit%s\tsttt{t}(Rt findparam(
R/R:R;R<tresR-R.R5R4tname((s/sys/lib/python2.7/mailcap.pyR8�s6
cCsO|j�d}t|�}x,|D]$}|| j�|kr#||Sq#WdS(NR)R(R!R(RJR<R.tp((s/sys/lib/python2.7/mailcap.pyRH�s
c Cs�ddl}t�}|jds0t|�dSx�tdt|j�d�D]�}|j||d!}t|�dkr�dGHdS|d}|d}t||d|�\}}|s�dGtGHqLdG|GHtj |�}|rLd G|GHqLqLWdS(
Ni�iis"usage: mailcap [MIMEtype file] ...iR(sNo viewer found fors
Executing:sExit status:(
tsysRtargvtshowRRRttypeRR9( RLRR-targsR:tfileR?R>tsts((s/sys/lib/python2.7/mailcap.pyR6�s&
"
cCs�dGHxt�D]}d|GHqWH|s5t�}ndGHH|j�}|j�xg|D]_}|GH||}xG|D]?}|j�}|j�x|D]}d|G||GHq�WHqtWqXWdS(NsMailcap files:s sMailcap entries:s %-15s(RRtkeystsort(RtfntckeysROR=R>RStk((s/sys/lib/python2.7/mailcap.pyRN�s&
t__main__(t__doc__Rt__all__RRRRR*RR+R7R8RHR6RNt__name__(((s/sys/lib/python2.7/mailcap.pyt<module>s &
|