U \k^@sdZddlZddlZddgZddZddZdd Zd d Zd d ZddZ ddZ ddgfddZ d ddZ gfddZ ddZddZddZedkredS)!z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchcCsd|krd|dfSdSdS)Nlinenor)r)entryrr/usr/lib/python3.8/mailcap.pylineno_sort_key s r c Csi}d}tD]~}zt|d}Wntk r8YqYnX|t||\}}W5QRX|D]*\}}||krz|||<q`|||||<q`q|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". rr)listmailcapfilesopenOSError_readmailcapfileitems)capsrZmailcapfpZmorecapskeyvaluerrrrs    cCsPdtjkr"tjd}|tj}n*dtjkr8tjd}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|dk r||d<|d 7}|d }tt|D]}||||<qd |}||kr|||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) rrrlineZnextlinerfieldstypesjrrrrGs4    rc Csg}dt|}}||kr>t|||\}}|||d}qt|dkrNdS|d|d|dd}}}d|i}|D]V}|d}|dkr|}d} n$|d|}||dd} ||krqz| ||<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}||kr|||}|d}|dd}||krB|||}dk r\fdd|D}t|td}|S)Nr$rz/*csg|]}|kr|qSrr).0rFrrr szlookup..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@s6       r@cCsF|d}t|}|D](}|d||kr||dSqdS)Nr3r!)r+r))rSrDr7prrrrQs  rQc Csddl}t}|jdds(t|dStdt|jdD]}|j||d}t|dkrjtddS|d}|d}t||d|\}}|stdtq:td|t |}|r:td|q:dS) Nrrrz"usage: mailcap [MIMEtype file] ...r2zNo viewer found forz Executing:z Exit status:) sysrargvshowr(r)printrtyperrA) rUrr6argsrBfilerGrFstsrrrr>s&    r>cCstdtD]}td|qt|s0t}tdtt|}|D]H}t|||}|D].}t|}|D]}td|||qrtqbqJdS)NzMailcap files: zMailcap entries:z %-15s)rXr rrK)rfnZckeysrYrErFkeyskrrrrWs" rW__main__)N)__doc__rr__all__r rr rrr'r4rr?r@rQr>rW__name__rrrrs$ )