U ,a@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,/opt/alt/python38/lib64/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)ZpathstrZmailcapshomerrrr -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 r c 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/infieldrr3restZfkeyZfvaluerrrr(ps*      r(cCsP|}||kr<||}|dkr qszlookup..rJ)rsortedr )rrCrrFZ MIMEtypesrrJrr@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) r9rCrDrEresr7r8r>r=namerrrrAs6       rAcCsF|d}t|}|D](}|d||kr||dSqdS)Nr4r")r,r*)rTrEr8prrrrRs  rRc 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] ...r3zNo viewer found forz Executing:z Exit status:) sysrargvshowr)r*printrtyperrB) rVrr7argsrCfilerHrGstsrrrr?s&    r?cCstdtD]}td|qt|s0t}tdtt|}|D]H}t|||}|D].}t|}|D]}td|||qrtqbqJdS)NzMailcap files: zMailcap entries:z %-15s)rYr rrL)rfnZckeysrZrFrGkeyskrrrrXs" rX__main__)N)__doc__rr__all__r rr r rr(r5rr@rArRr?rX__name__rrrrs$ )