B L��jk#�@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�,/opt/alt/python37/lib64/python3.7/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}x�t�D]�}yt|d�}Wntk r8wYnX|�t||�\}}WdQRXx6|��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}|�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�readmailcapfileEsr%c Csi}x�|��}|sP|ddks|��dkr.q|}x4|dd�dkrf|��}|sTd}|dd�|}q4Wt|�\}}|r|s~q|dk r�||d<|d 7}|�d �}x$tt|��D]}||��||<q�Wd �|���}||kr�||�|�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�#����Nz\ � rr�/) �readline�strip� parseliner�range�len�join�lower�append) rrr�lineZnextliner�fields�types�jrrrrMs6   rc Cs�g}dt|�}}x.||kr@t|||�\}}|�|�|d}qWt|�dkrRdS|d|d|dd�}}}d|i}x^|D]V}|�d�}|dkr�|}d} n$|d|���}||dd���} ||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 )NNN�view�=r')r/� parsefieldr2�findr,) r3r4�i�n�fieldrr7�restZfkeyZfvaluerrrr-vs*        r-cCsT|}x:||kr>||}|dkr"Pq|dkr4|d}q|d}qW|||���|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}t�|t�dSt|||�}xd|D]\}d|krnt|d||�}|dkrZq4|rnt�|�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.)NN�testNr)� _find_unsafer!r"r �lookup�substr�system) r�MIMEtyper�filename�plist�msg�entries�erC�commandrrrr�s      cslg}||kr|||}|�d�}|dd}||krB|||}�dk r\�fdd�|D�}t|td�}|S)Nr*rz/*csg|]}�|kr|�qSrr)�.0rM)rrr� <listcomp>�szlookup.<locals>.<listcomp>)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}t�|t�dS||}q|d k�rH|} x ||kr�||d kr�|d}q�W|| |�} |d}t| |�} t| ��r>d | | f}t�|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=rHrIrJZresr;r<rBrKrA�nameZparamrrrrF�sH              rFcCsF|��d}t|�}x,|D]$}|d|���|kr||d�SqWdS)Nr8r')r1r/)rXrJr<�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�q<td|�t � |�}|r<td|�q<WdS) Nrrr z"usage: mailcap [MIMEtype file] ...r7zNo viewer found forz Executing:z Exit status:) �sysr�argv�showr.r/�printr�typerrG) rZrr;�argsrH�filerNrM�stsrrrrCs&    rCcCs�td�xt�D]}td|�qWt�|s4t�}td�t�t|�}xX|D]P}t|�||}x:|D]2}t|�}x|D]}td|||�q|Wt�qjWqPWdS)NzMailcap files:� zMailcap entries:z %-15s)r]rrrQ)r�fnZckeysr^rLrM�keys�krrrr\s"    r\�__main__)N)rrr!�re�__all__r �compile�searchrD�Warningr rrr%rr-r9rrErFrWrCr\r rrrr�<module>s* ) )