3 \@sdZddlZddlZddgZddZddZdd Zd d Zd d ZddZ ddZ ddgfddZ d ddZ gfddZ ddZddZddZedkredS)!z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchcCsd|krd|dfSdSdS)Nlinenor)rr)entryrr/usr/lib64/python3.6/mailcap.pylineno_sort_key s r cCsi}d}xtD]}yt|d}Wntk r8wYnX|t||\}}WdQRXx6|jD]*\}}||kr||||<qb|||||<qbWqW|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   cCsPdtjkr"tjd}|jtj}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 tjdtdt|d\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadN)warningswarnDeprecationWarningr)rr_rrrreadmailcapfile?sr c Csi}x|j}|sP|ddks|jdkr.q|}x4|d ddkrf|j}|sTd}|dd |}q4Wt|\}}|oz|s~q|dk r||d<|d 7}|jd }x$tt|D]}||j||<qWd j|j}||kr||j|q|g||<qW||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#rNz\  rr/r%) readlinestrip parselinerrangelenjoinlowerappend) rrrlineZnextlinerfieldstypesjrrrrGs6   rc Csg}dt|}}x.||kr@t|||\}}|j||d}qWt|dkrRdS|d|d|dd}}}d|i}x^|D]V}|jd}|dkr|}d} n$|d|j}||ddj} ||krq| ||<qW||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. rrrNview=r")NN)r* parsefieldr-findr') r.r/infieldrr2restZfkeyZfvaluerrrr(ps*        r(cCsT|}x:||kr>||}|dkr"Pq|dkr4|d}q|d}qW|||j|fS)z/Separate one key-value pair in a mailcap entry.;\rr)r')r.r6r7startcrrrr4s   r4r2z /dev/nullc Csbt|||}xP|D]H}d|krBt|d||}|rBtj|dkrBqt|||||}||fSWdS)aFind 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. testrN)NN)lookupsubstrsystem) rMIMEtyperfilenameplistentrieser>commandrrrrs   cslg}||kr|||}|jd}|dd}||krB|||}dk r\fdd|D}t|td}|S)Nr$rz/*csg|]}|kr|qSrr).0rF)rrr szlookup..)r)rsortedr )rrBrrEZ MIMEtypesr)rrr?s     r?c Cs d}dt|}}x||kr||}|d}|dkr`|dkrV|||d}|d}||}q||}|d}|dkr||}q|dkr||}q|dkr||}q|dkr|}x ||kr||d kr|d}qW|||} |d}|t| |}q|d|}qW|S) Nr"rr%r;st{})r* findparam) r8rBrCrDresr6r7r=r<namerrrr@s6       r@cCsF|jd}t|}x,|D]$}|d|j|kr||dSqWdS)Nr3r")r,r*)rRrDr7prrrrPs   rPc Csddl}t}|jdds(t|dSxtdt|jdD]}|j||d}t|dkrjtddS|d}|d}t||d|\}}|stdtqs&    r>cCstdxtD]}td|qWt|s4t}tdtt|}xX|D]P}t|||}x:|D]2}t|}x|D]}td|||q|WtqjWqPWdS)NzMailcap files: zMailcap entries:z %-15s)rWr rrJ)rfnZckeysrXrErFkeyskrrrrVs"    rV__main__)N)__doc__rr__all__r rr r rr(r4rr?r@rPr>rV__name__rrrrs$ )