\d$dZddlZddlZddlZddgZdZejeeddZej d j Z Gd d e Z d Zd ZdZdZdZdZddgfdZddZgfdZdZdZdZedkr edSdS)z%Mailcap file handling. See RFC 1524.Ngetcaps findmatchztThe {name} module is deprecated and will be removed in Python {remove}. See the mimetypes module for an alternative.) )removec"d|vr d|dfSdS)Nlinenor)r)entrys ..\python\lib\mailcap.pylineno_sort_keyrs!5%/!!tz[^\xa1-\U0010FFFF\w@+=:,./-]ceZdZdZdS)UnsafeMailcapInputz)Warning raised when refusing unsafe inputN)__name__ __module__ __qualname____doc__r rr rrs3333rrc2i}d}tD]} t|d}n#t$rY wxYw|5t||\}}dddn #1swxYwY|D]\}}||vr|||<|||z||<|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)capsr mailcapfpmorecapskeyvalues r rrs D F#%% . . gs##BB    H   < </F;; Hf < < < < < < < < < < < < < < <"..** . .JC$;;!S  I-S  . Ks' 44AA A cdtjvr2tjd}|tj}n,dtjvrtjd}nd}|dzdddg}|S)z7Return a list of all mailcap files found on the system.MAILCAPSHOME.z /.mailcapz /etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)pathstrmailcapshomes r rr9spRZ*Z(==,, RZ  :f%DDD;&"$<> Orcbtjdtdt|d\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadN)warningswarnDeprecationWarningr)rr_s r readmailcapfiler4Ks6 MF$a)))r4((GD! Krci} |}|sn+|ddks|dkr>|}|dddkr3|}|sd}|dd|z}|dddk3t|\}}|r|s| ||d <|dz }|d }t t |D]}||||< d |}||vr|||n|g||<C||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 r#Nz\  r /) readlinestrip parseliner)rangelenjoinlowerappend) rr rlinenextliner!fieldstypesjs r rrSsw D!{{}}U 7c>>TZZ\\R// rssmv%%{{}}H ,X9x'Drssmv%%  oo V     %F8  aKF #s5zz"" ( (AQx~~''E!HHhhuoo##%% $;; I  V $ $ $ $DI9!: <rcg}dt|}}||kr4t|||\}}|||dz}||k4t|dkrdS|d|d|dd}}}d|i}|D]g}|d}|dkr|}d} n;|d|}||dzd} ||vrb| ||<h||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. rr r/NNNview=r7)r? parsefieldrBfindr<) rCrEinfieldr!rJrestfkeyfvalues r r=r=|s  F c$iiqA a%%dAq))q e aC a%% 6{{QzQiF122JtCd^F " " JJsOO q55DFF!9??$$D1Q344[&&((F 6>> !F4LL ;rc|}||kr&||}|dkrn|dkr|dz}n|dz}||k&||||fS)z/Separate one key-value pair in a mailcap entry.;\r/r )r<)rCrNrOstartcs r rLrLsn E a%% G 88  $YY!AA!A a%% a=   ! ##rrJz /dev/nullcFt|r"d|d}tj|tdSt |||}|D][}d|vr5t |d||}| |rt j|dkr;t |||||} | | |fcS\dS)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. z&Refusing to use mailcap with filename z . Use a safe temporary filename.rItestNr) _find_unsafer0r1rlookupsubstr'system) rMIMEtyper!filenameplistmsgentrieserZcommands r rrsH\d\d\df c-...zT8S))G    Q;;6He44D|  $1,,#(E::  A:     :rcg}||vr |||z}|d}|ddz}||vr |||z}fd|D}t|t}|S)Nr:rz/*cg|]}|v| Sr r ).0rdr!s r zlookup..s2221r)r!)r)sortedr)rr_r!rc MIMEtypess ` r r\r\sG4DN*s##I|d"H4DN* 2222g222W/222G Nrcd}dt|}}||kr,||}|dz}|dkr|dkr|||dz}|dz}||z}n||}|dz}|dkr||z}n|dkr||z}n|dkr7t|r"d|d }tj|tdS||z}n|d kr|} ||kr#||d kr|dz}||kr ||d k|| |} |dz}t | |} t| r%d | d | d}tj|tdS|| z}n|dz|z}||k,|S)Nr7rr %rVstz!Refusing to substitute MIME type z into a shell command.{}z!Refusing to substitute parameter z (z) into a shell command)r?r[r0r1r findparam) rPr_r`raresrNrOrXrbrWnameparams r r]r]s C c%jjqA a%% !H!A#a 88Dyy!AaC%Lac!'CCaAac!CxxAgcHnc))  YaYaYacCM#'9:::4Hnc!eeaC!A!eeaCU1W~aC!$..&&  ]b]b]bdhdhdhiCM#'9:::4Ek Ci!mE a%%F Jrc|dz}t|}|D].}|d||kr ||dcS/dS)NrKr7)rAr?)rtrarOps r rrrrsd ::<<# D D A  RaR5;;==D QRR5LLL ! 2rc2ddl}t}|jddst|dSt dt |jdD]}|j||dz}t |dkrt ddS|d}|d}t||d|\}}|st dtvt d|tj |}tj |}|rt d|dS) Nrr r/z"usage: mailcap [MIMEtype file] ...rJzNo viewer found forz Executing:z Exit status:) sysrargvshowr>r?printrtyper'r^waitstatus_to_exitcode) ryrrNargsr_filererdstss r rZrZs(JJJ 99D 8ABB< T  1c#(mmQ ' '++x!A# t99q== 6 7 7 7 FF7AwtXvt<<  + ' . . . . , ( ( ()G$$C+C00C +nc***++rctdtD]}td|zt|st}tdtt|}|D]Y}t|||}|D]=}t|}|D]}td|z||t>ZdS)NzMailcap files: zMailcap entries:z %-15s)r|rrrj)rfnckeysr}rcrdkeysks r r{r{s   22%r "2"2"2"2 GGG !   GGG 4LLE d t*  A!99D + +i!mQqT**** GGGG  r__main__)N)rr'r0re__all___DEPRECATION_MSG _deprecatedrrcompilesearchr[Warningrrrr4rr=rLrr\r]rrrZr{r rr rs++  [ !#X/@@@@rz9::A 44444444 4$'''R> $ $ $ #);b6    ,.''''R+++." zDFFFFFr