a ;`@sdZddlZddlZddgZddZddZdd Zd d Zd d ZddZ ddZ ddgfddZ d ddZ gfddZ ddZddZddZedkredS)!z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchcCsd|vrd|dfSdSdS)Nlinenor)r)entryrr/usr/lib/python3.9/mailcap.pylineno_sort_key s r c Csi}d}tD]}zt|d}Wnty6YqYn0|t||\}}Wdn1s`0Y|D]*\}}||vr|||<qr|||||<qrq|S)aReturn 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". rrN)listmailcapfilesopenOSError_readmailcapfileitems)capsrZmailcapfpZmorecapskeyvaluerrrrs    , cCsPdtjvr"tjd}|tj}n*dtjvr8tjd}nd}|ddddg}|S)z7Return a list of all mailcap files found on the system.ZMAILCAPSHOME.z /.mailcapz /etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)ZpathstrZmailcapsZhomerrrr -s    r cCs tdtdt|d\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadN)warningswarnDeprecationWarningr)rr_rrrreadmailcapfile?s rc Csi}|}|sq|ddks|dkr,q|}|dddkrb|}|sPd}|dd|}q0t|\}}|r|sxq|dur||d<|d 7}|d }tt|D]}||||<qd |}||vr|||q|g||<q||fS) aRead 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". r#Nz\  rr/) readlinestrip parselinerrangelenjoinlowerappend) rrrlineZnextlinerfieldstypesjrrrrGs0    rc Csg}dt|}}||kr>t|||\}}|||d}qt|dkrNdS|d|d|dd}}}d|i}|D]V}|d}|dkr|}d} n$|d|}||dd} ||vrqz| ||<qz||fS) zParse 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. rrrNNNview=r!)r) parsefieldr,findr&) r-r.infieldrr2restZfkeyZfvaluerrrr'ps*      r'cCsP|}||kr<||}|dkr qcommandrrrrs  cslg}||vr|||}|d}|dd}||vrB|||}dur\fdd|D}t|td}|S)Nr$rz/*csg|]}|vr|qSrr).0rFrrr zlookup..rI)rsortedr )rrBrrEZ MIMEtypesrrIrr?s     r?c Csd}dt|}}||kr||}|d}|dkr^|dkrT|||d}|d}||}q||}|d}|dkr||}q|dkr||}q|dkr||}q|dkr|}||kr||d kr|d}q|||} |d}|t| |}q|d|}q|S) Nr!rr%r;st{})r) findparam) r8rBrCrDresr6r7r=r<namerrrr@s0       r@cCsF|d}t|}|D](}|d||kr||dSqdS)Nr3r!)r+r))rTrDr7prrrrRs  rRc Csddl}t}|jdds(t|dStdt|jdD]}|j||d}t|dkrjtddS|d}|d}t||d|\}}|stdtq:td|t |}t |}|r:td|q:dS) Nrrrz"usage: mailcap [MIMEtype file] ...r2zNo viewer found forz Executing:z Exit status:) sysrargvshowr(r)printrtyperrAwaitstatus_to_exitcode) rVrr6argsrBfilerGrFstsrrrr>s(     r>cCstdtD]}td|qt|s0t}tdtt|}|D]H}t|||}|D].}t|}|D]}td|||qrtqbqJdS)NzMailcap files: zMailcap entries:z %-15s)rYr rrL)rfnZckeysrZrErFkeyskrrrrXs rX__main__)N)__doc__rr__all__r rr rrr'r4rr?r@rRr>rX__name__rrrrs$ )