3 ­_Vjk#ã@sÈdZddlZddlZddlZddgZdd„ZejdƒjZGdd „d e ƒZ d d„Z d d „Z d d„Z dd„Zdd„Zdd„Zddgfdd„Zd#dd„Zgfdd„Zdd„Zdd„Zd d!„Zed"krÄeƒdS)$z%Mailcap file handling. See RFC 1524.éNÚgetcapsÚ findmatchcCsd|krd|dfSdSdS)NÚlinenoré)rr©)Úentryrrú/usr/lib64/python3.6/mailcap.pyÚlineno_sort_key s r z[^\xa1-\U0010FFFF\w@+=:,./-]c@seZdZdZdS)ÚUnsafeMailcapInputz)Warning raised when refusing unsafe inputN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrr sr cCs˜i}d}xŠtƒD]€}yt|dƒ}Wntk r8wYnX|�t||ƒ\}}WdQRXx6|jƒD]*\}}||kr||||<qb|||||<qbWqW|S)a Return 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". rÚrN)ÚlistmailcapfilesÚopenÚOSErrorÚ_readmailcapfileÚitems)ÚcapsrZmailcapÚfpZmorecapsÚkeyÚvaluerrrrs   cCsPdtjkr"tjd}|jtjƒ}n*dtjkr8tjd}nd}|ddddg}|S)z7Return a list of all mailcap files found on the system.ZMAILCAPSÚHOMEÚ.z /.mailcapz /etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)ÚosÚenvironÚsplitÚpathsep)ZpathstrZmailcapsÚhomerrrr3s    rcCs tjdtdƒt|dƒ\}}|S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps insteadéN)ÚwarningsÚwarnÚDeprecationWarningr)rrÚ_rrrÚreadmailcapfileEsr%c Csi}xô|jƒ}|sP|ddks|jƒdkr.q|}x4|d d…dkrf|jƒ}|sTd}|dd …|}q4Wt|ƒ\}}|oz|s~q|dk r–||d<|d 7}|jd ƒ}x$tt|ƒƒD]}||jƒ||<q®Wd j|ƒjƒ}||krì||j|ƒq|g||<qW||fS) a�Read 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\ Ú rrú/éþÿÿÿr*) ÚreadlineÚstripÚ parselinerÚrangeÚlenÚjoinÚlowerÚappend) rrrÚlineZnextlinerÚfieldsÚtypesÚjrrrrMs6   rc Csâg}dt|ƒ}}x.||kr@t|||ƒ\}}|j|ƒ|d}qWt|ƒdkrRdS|d|d|dd…}}}d|i}x^|D]V}|jdƒ}|dkr |}d} n$|d|…jƒ}||dd…jƒ} ||krÎq€| ||<q€W||fS) zÈParse 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 NÚviewú=r')NN)r/Ú parsefieldr2Úfindr,) r3r4ÚiÚnÚfieldrr7ÚrestZfkeyZfvaluerrrr-vs*        r-cCsT|}x:||kr>||}|dkr"Pq|dkr4|d}q|d}qW|||…jƒ|fS)z/Separate one key-value pair in a mailcap entry.ú;ú\r r)r,)r3r;r<ÚstartÚcrrrr9•s   r9r7z /dev/nullc Cs˜t|ƒr"d|f}tj|tƒdSt|||ƒ}xd|D]\}d|krnt|d||ƒ}|dkrZq4|rntj|ƒdkrnq4t|||||ƒ} | dk r4| |fSq4WdS)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. zHRefusing to use mailcap with filename %r. Use a safe temporary filename.NÚtestr)NN)NN)Ú _find_unsafer!r"r ÚlookupÚsubstrÚsystem) rÚMIMEtyperÚfilenameÚplistÚmsgÚentriesÚerCÚcommandrrrr¥s      cslg}||kr|||}|jdƒ}|dd}||krB|||}ˆdk r\‡fdd„|Dƒ}t|td�}|S)Nr)rz/*csg|]}ˆ|kr|‘qSrr)Ú.0rM)rrrú Észlookup..)r)rÚsortedr )rrHrrLZ MIMEtypesr)rrrEÀs     rEc Cs\d}dt|ƒ}}�xB||k�rV||}|d}|dkrb|dkrX|||d…}|d}||}q||}|d}|dkr„||}q|dkr–||}q|dkrÊt|ƒrÀd|f}tj|tƒdS||}q|d k�rH|} x ||krø||d krø|d}qÚW|| |…} |d}t| |ƒ} t| ƒ�r>d | | f}tj|tƒdS|| }q|d|}qW|S) Nr'rrú%r@ÚsÚtz9Refusing to substitute MIME type %r into a shell command.ú{ú}z=Refusing to substitute parameter %r (%s) into a shell command)r/rDr!r"r Ú findparam) r=rHrIrJÚresr;r<rBrKrAÚnameZparamrrrrFÍsH              rFcCsF|jƒd}t|ƒ}x,|D]$}|d|…jƒ|kr||d…SqWdS)Nr8r')r1r/)rYrJr<ÚprrrrWös   rWc CsÆddl}tƒ}|jdd…s(t|ƒdSx˜tdt|jƒdƒD]‚}|j||d…}t|ƒdkrjtdƒdS|d}|d}t||d|ƒ\}}|sœtdtƒqs* ) )