U e5dk#ã@sÈdZddlZddlZddlZddgZdd„Ze d¡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)ér©)Úentryrrú/usr/lib64/python3.8/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 c Cs’i}d}tƒD]~}zt|dƒ}Wntk r8YqYnX|�t||ƒ\}}W5QRX| ¡D]*\}}||krz|||<q`|||||<q`q|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Úr)ÚlistmailcapfilesÚopenÚOSErrorÚ_readmailcapfileÚitems)ÚcapsrZmailcapÚfpZmorecapsÚkeyÚvaluerrrrs    cCsPdtjkr"tjd}| tj¡}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 t dtd¡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ÚreadmailcapfileEs ÿr%c Csöi}| ¡}|sqî|ddks| ¡dkr,q|}|dd…dkrb| ¡}|sPd}|dd…|}q0t|ƒ\}}|r|sxq|dk r�||d<|d 7}| d ¡}tt|ƒƒD]}|| ¡||<q¦d  |¡ ¡}||krâ|| |¡q|g||<q||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ú#ÚéþÿÿÿNz\ Ú rrú/) ÚreadlineÚstripÚ parselinerÚrangeÚlenÚjoinÚlowerÚappend) rrrÚlineZnextlinerÚfieldsÚtypesÚjrrrrMs4    rc CsÚg}dt|ƒ}}||kr>t|||ƒ\}}| |¡|d}qt|ƒdkrNdS|d|d|dd…}}}d|i}|D]V}| d¡}|dkrš|}d} n$|d|… ¡}||dd… ¡} ||krÈqz| ||<qz||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 ©NNNÚviewú=r')r/Ú parsefieldr2Úfindr,) r3r4ÚiÚnÚfieldrr8ÚrestZfkeyZfvaluerrrr-vs*      r-cCsP|}||kr<||}|dkr qÉszlookup..rQ)rÚsortedr )rrIrrMZ MIMEtypesrrQrrFÀs     rFc CsRd}dt|ƒ}}||k�rN||}|d}|dkr^|dkrT|||d…}|d}||}q||}|d}|dkr€||}q|dkr’||}q|dkrÆt|ƒr¼d|f}t |t¡dS||}q|d k�r@|} ||krò||d krò|d}qÔ|| |…} |d}t| |ƒ} t| ƒ�r6d | | f}t |t¡dS|| }q|d|}q|S) Nr'rrú%rAÚsÚtz9Refusing 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>rIrJrKÚresr<r=rCrLrBÚnameZparamrrrrGÍsH               rGcCsF| ¡d}t|ƒ}|D](}|d|… ¡|kr||d…SqdS)Nr9r')r1r/)r[rKr=ÚprrrrYös  rYc CsÄddl}tƒ}|jdd…s(t|ƒdStdt|jƒdƒD]„}|j||d…}t|ƒdkrjtdƒdS|d}|d}t||d|ƒ\}}|sœtdtƒq:td|ƒt   |¡}|r:td|ƒq:dS) Nrrr z"usage: mailcap [MIMEtype file] ...r8zNo viewer found forz Executing:z Exit status:) ÚsysrÚargvÚshowr.r/ÚprintrÚtyperrH) r]rr<ÚargsrIÚfilerOrNÚstsrrrrDs&    rDcCs˜tdƒtƒD]}td|ƒqtƒ|s0tƒ}tdƒtƒt|ƒ}|D]H}t|ƒ||}|D].}t|ƒ}|D]}td|||ƒqrtƒqbqJdS)NzMailcap files:ú zMailcap entries:z %-15s)r`rrrS)rÚfnZckeysrarMrNÚkeysÚkrrrr_s" r_Ú__main__)N)rrr!ÚreÚ__all__r ÚcompileÚsearchrEÚWarningr rrr%rr-r:rrFrGrYrDr_r rrrrÚs* ) )