
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
ct„jk#  ã               @   sÈ   d Z ddlZddlZddlZddgZdd„ ZejdƒjZG dd	„ d	e	ƒZ
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddg fdd„Zd#dd„Zg fdd„Zdd„ Zdd„ Zd d!„ Zed"krÄeƒ  dS )$z%Mailcap file handling.  See RFC 1524.é    NÚgetcapsÚ	findmatchc             C   s   d| krd| d fS dS d S )NÚlinenor   é   )r   r   © )Úentryr   r   ú/usr/lib64/python3.6/mailcap.pyÚlineno_sort_key
   s    r	   z[^\xa1-\U0010FFFF\w@+=:,./-]c               @   s   e Zd ZdZdS )ÚUnsafeMailcapInputz)Warning raised when refusing unsafe inputN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r
      s   r
   c              C   s˜   i } d}xŠt ƒ D ]€}yt|dƒ}W n tk
r8   wY nX |� t||ƒ\}}W dQ R X x6|jƒ D ]*\}}|| kr||| |< qb| | | | |< qbW qW | 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)Úcapsr   ZmailcapÚfpZmorecapsÚkeyÚvaluer   r   r   r      s    

c              C   sP   dt jkr"t jd } | jt jƒ}n*dt jkr8t jd }nd}|d dddg}|S )z7Return a list of all mailcap files found on the system.ZMAILCAPSÚHOMEÚ.z	/.mailcapz/etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)ÚosÚenvironÚsplitÚpathsep)ZpathstrZmailcapsÚhomer   r   r   r   3   s    


r   c             C   s    t jdtdƒ 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   )r   r   Ú_r   r   r   ÚreadmailcapfileE   s    r%   c       	      C   s  i }xô| j ƒ }|sP |d dks|jƒ dkr.q|}x4|dd… dkrf| j ƒ }|sTd}|dd… | }q4W t|ƒ\}}|oz|s~q|dk	r–||d< |d	7 }|jd
ƒ}x$tt|ƒƒD ]}|| jƒ ||< q®W d
j|ƒjƒ }||krì|| j|ƒ q|g||< qW ||fS )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\
Ú
r   r   ú/éþÿÿÿr*   )	ÚreadlineÚstripÚ	parseliner   ÚrangeÚlenÚjoinÚlowerÚappend)	r   r   r   ÚlineZnextliner   ÚfieldsÚtypesÚjr   r   r   r   M   s6    	  
r   c       
      C   sâ   g }dt | ƒ }}x.||k r@t| ||ƒ\}}|j|ƒ |d }qW t |ƒdk rRdS |d |d |dd…   }}}d|i}x^|D ]V}|jdƒ}|dk r |}d}	n$|d|… jƒ }||d d… jƒ }	||krÎq€|	||< q€W ||fS )	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.
    r   r   r    NÚviewú=r'   )NN)r/   Ú
parsefieldr2   Úfindr,   )
r3   r4   ÚiÚnÚfieldr   r7   ÚrestZfkeyZfvaluer   r   r   r-   v   s*    

 

r-   c             C   sT   |}x:||k r>| | }|dkr"P q|dkr4|d }q|d }qW | ||… j ƒ |fS )z/Separate one key-value pair in a mailcap entry.ú;ú\r    r   )r,   )r3   r;   r<   ÚstartÚcr   r   r   r9   •   s    

r9   r7   z	/dev/nullc       
      C   s˜   t |ƒr"d|f }tj|tƒ dS t| ||ƒ}xd|D ]\}d|krnt|d ||ƒ}|dkrZq4|rntj|ƒdkrnq4t|| |||ƒ}	|	dk	r4|	|fS q4W dS )a  Find 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   Úcommandr   r   r   r   ¥   s     	

c                sl   g }|| kr|| |  }|j dƒ}|d d }|| krB|| |  }ˆ d k	r\‡ fdd„|D ƒ}t|td�}|S )Nr)   r   z/*c                s   g | ]}ˆ |kr|‘qS r   r   )Ú.0rM   )r   r   r   ú
<listcomp>É   s    zlookup.<locals>.<listcomp>)r   )r   Úsortedr	   )r   rH   r   rL   Z	MIMEtypesr   )r   r   rE   À   s    
rE   c             C   s\  d}dt | ƒ }}�xB||k �rV| | }|d }|dkrb|dkrX| ||d … }|d }|| }q| | }|d }|dkr„|| }q|dkr–|| }q|dkrÊt|ƒrÀd|f }tj|tƒ d S || }q|d	k�rH|}	x ||k rø| | d
krø|d }qÚW | |	|… }
|d }t|
|ƒ}t|ƒ�r>d||
f }tj|tƒ d S || }q|d | }qW |S )Nr'   r   r   ú%r@   ÚsÚtz9Refusing to substitute MIME type %r into a shell command.ú{ú}z=Refusing to substitute parameter %r (%s) into a shell command)r/   rD   r!   r"   r
   Ú	findparam)r=   rH   rI   rJ   Úresr;   r<   rB   rK   rA   ÚnameZparamr   r   r   rF   Í   sH      
 







rF   c             C   sF   | j ƒ d } t| ƒ}x,|D ]$}|d |… j ƒ | kr||d … S qW dS )Nr8   r'   )r1   r/   )rY   rJ   r<   Úpr   r   r   rW   ö   s    
rW   c        	      C   sÆ   dd l } tƒ }| jdd … s(t|ƒ d S x˜tdt| jƒdƒD ]‚}| j||d … }t|ƒdk rjtdƒ d S |d }|d }t||d|ƒ\}}|sœtdtƒ q<td|ƒ t	j
|ƒ}|r<td|ƒ q<W d S )	Nr   r   r    z"usage: mailcap [MIMEtype file] ...r7   zNo viewer found forz
Executing:zExit status:)Úsysr   ÚargvÚshowr.   r/   Úprintr   Útyper   rG   )	r[   r   r;   ÚargsrH   ÚfilerN   rM   Ústsr   r   r   rC     s&    

rC   c             C   s¨   t dƒ xtƒ D ]}t d| ƒ qW t ƒ  | s4tƒ } t dƒ t ƒ  t| ƒ}xX|D ]P}t |ƒ | | }x:|D ]2}t|ƒ}x|D ]}t d| || ƒ q|W t ƒ  qjW qPW d S )NzMailcap files:ú	zMailcap entries:z  %-15s)r^   r   r   rQ   )r   ÚfnZckeysr_   rL   rM   ÚkeysÚkr   r   r   r]     s"      


r]   Ú__main__)N)r   r   r!   ÚreÚ__all__r	   ÚcompileÚsearchrD   ÚWarningr
   r   r   r%   r   r-   r9   r   rE   rF   rW   rC   r]   r   r   r   r   r   Ú<module>   s*   )
)