U e5dk#@sdZddlZddlZddlZddgZddZedjZGdd d e Z d dZ d d Z d dZ ddZddZddZddgfddZd#ddZgfddZddZddZd d!Zed"kredS)$z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchcCsd|krd|dfSdSdS)Nlinenor)r)entryrr/usr/lib64/python3.8/mailcap.pylineno_sort_key s r z[^\xa1-\U0010FFFF\w@+=:,./-]c@seZdZdZdS)UnsafeMailcapInputz)Warning raised when refusing unsafe inputN)__name__ __module__ __qualname____doc__rrrrr sr 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)ZpathstrZmailcapshomerrrr3s    rcCs tdtdt|d\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadN)warningswarnDeprecationWarningr)rr_rrrreadmailcapfileEs 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) rrrlineZnextlinerfieldstypesjrrrrMs4    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. rrr NNNview=r')r/ parsefieldr2findr,) r3r4infieldrr8restZfkeyZfvaluerrrr-vs*      r-cCsP|}||kr<||}|dkr qszlookup..rQ)rsortedr )rrIrrMZ MIMEtypesrrQrrFs     rFc CsRd}dt|}}||krN||}|d}|dkr^|dkrT|||d}|d}||}q||}|d}|dkr||}q|dkr||}q|dkrt|rd|f}t|tdS||}q|d kr@|} ||kr||d kr|d}q|| |} |d}t| |} t| r6d | | f}t|tdS|| }q|d|}q|S) Nr'rr%rAstz9Refusing to substitute MIME type %r into a shell command.{}z=Refusing to substitute parameter %r (%s) into a shell command)r/rEr!r"r findparam) r>rIrJrKresr<r=rCrLrBnameZparamrrrrGsH               rGcCsF|d}t|}|D](}|d||kr||dSqdS)Nr9r')r1r/)r[rKr=prrrrYs  rYc Csddl}t}|jdds(t|dStdt|jdD]}|j||d}t|dkrjtddS|d}|d}t||d|\}}|stdtq:td|t |}|r:td|q:dS) Nrrr z"usage: mailcap [MIMEtype file] ...r8zNo viewer found forz Executing:z Exit status:) sysrargvshowr.r/printrtyperrH) r]rr<argsrIfilerOrNstsrrrrDs&    rDcCstdtD]}td|qt|s0t}tdtt|}|D]H}t|||}|D].}t|}|D]}td|||qrtqbqJdS)NzMailcap files: zMailcap entries:z %-15s)r`rrrS)rfnZckeysrarMrNkeyskrrrr_s" r___main__)N)rrr!re__all__r compilesearchrEWarningr rrr%rr-r:rrFrGrYrDr_r rrrrs* ) )