a
    yŒ@hœ#  ã                   @   sÈ   d Z ddlZddlZddlZddgZdd„ Ze 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| v rd| d fS dS d S )NÚlinenor   )é   r   © )Úentryr   r   úlib/python3.9/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}t ƒ D ]�}zt|dƒ}W n ty6   Y qY n0 |� t||ƒ\}}W d  ƒ n1 s`0    Y  | ¡ D ]*\}}|| vrŒ|| |< qr| | | | |< qrq| 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v r"t jd } |  t j¡}n*dt jv 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  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 }|   ¡ }|sqî|d dks| ¡ dkr,q|}|dd… dkrb|   ¡ }|sPd}|dd… | }q0t|ƒ\}}|r|sxq|dur�||d< |d	7 }| d
¡}tt|ƒƒD ]}||  ¡ ||< q¦d
 |¡ ¡ }||v râ||  |¡ q|g||< q||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   ú#Ú éþÿÿÿNz\
Ú
r   r   ú/)	ÚreadlineÚstripÚ	parseliner   ÚrangeÚlenÚjoinÚlowerÚappend)	r   r   r   ÚlineZnextliner   ÚfieldsÚtypesÚjr   r   r   r   M   s0    	
r   c           
      C   sÚ   g }dt | ƒ }}||k r>t| ||ƒ\}}| |¡ |d }qt |ƒdk rNdS |d |d |dd…   }}}d|i}|D ]V}| d¡}|dk rš|}d}	n$|d|…  ¡ }||d d…  ¡ }	||v rÈqz|	||< qz||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    ©NNNÚviewú=r'   )r/   Ú
parsefieldr2   Úfindr,   )
r3   r4   ÚiÚnÚfieldr   r8   ÚrestZfkeyZfvaluer   r   r   r-   v   s*    

 

r-   c                 C   sP   |}||k r<| | }|dkr q<q|dkr2|d }q|d }q| ||…   ¡ |fS )z/Separate one key-value pair in a mailcap entry.ú;ú\r    r   )r,   )r3   r<   r=   ÚstartÚcr   r   r   r:   •   s    

r:   r8   z	/dev/nullc           
      C   s˜   t |ƒr"d|f }t |t¡ dS t| ||ƒ}|D ]`}d|v rlt|d ||ƒ}|du rXq2|rlt |¡dkrlq2t|| |||ƒ}	|	dur2|	|f  S q2d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.r7   ÚtestNr   )Ú_find_unsafer!   r"   r
   ÚlookupÚsubstr   Úsystem)
r   ÚMIMEtyper   ÚfilenameÚplistÚmsgÚentriesÚerD   Úcommandr   r   r   r   ¥   s     	
c                    sl   g }|| v r|| |  }|  d¡}|d d }|| v rB|| |  }ˆ d ur\‡ fdd„|D ƒ}t|td�}|S )Nr*   r   z/*c                    s   g | ]}ˆ |v r|‘qS r   r   )Ú.0rN   ©r   r   r   Ú
<listcomp>É   ó    zlookup.<locals>.<listcomp>rQ   )r   Úsortedr	   )r   rI   r   rM   Z	MIMEtypesr   rQ   r   rF   À   s    
rF   c                 C   sR  d}dt | ƒ }}||k �rN| | }|d }|dkr^|dkrT| ||d … }|d }|| }q| | }|d }|dkr€|| }q|dkr’|| }q|dkrÆt|ƒr¼d|f }t |t¡ d S || }q|d	k�r@|}	||k rò| | d
krò|d }qÔ| |	|… }
|d }t|
|ƒ}t|ƒ�r6d||
f }t |t¡ d S || }q|d | }q|S )Nr'   r   r   ú%rA   ÚsÚtz9Refusing to substitute MIME type %r into a shell command.Ú{Ú}z=Refusing to substitute parameter %r (%s) into a shell command)r/   rE   r!   r"   r
   Ú	findparam)r>   rI   rJ   rK   Úresr<   r=   rC   rL   rB   ÚnameZparamr   r   r   rG   Í   sB    










rG   c                 C   sF   |   ¡ d } t| ƒ}|D ](}|d |…   ¡ | kr||d …   S qdS )Nr9   r'   )r1   r/   )r\   rK   r=   Úpr   r   r   rZ   ö   s    rZ   c            	      C   sÎ   dd l } tƒ }| jdd … s(t|ƒ d S 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	 
|¡}t	 |¡}|r:td|ƒ q:d S )	Nr   r   r    z"usage: mailcap [MIMEtype file] ...r8   zNo viewer found forz
Executing:zExit status:)Úsysr   ÚargvÚshowr.   r/   Úprintr   Útyper   rH   Úwaitstatus_to_exitcode)	r^   r   r<   ÚargsrI   ÚfilerO   rN   Ústsr   r   r   rD     s(    


rD   c                 C   s˜   t dƒ tƒ D ]}t d| ƒ qt ƒ  | s0tƒ } t dƒ t ƒ  t| ƒ}|D ]H}t |ƒ | | }|D ].}t|ƒ}|D ]}t d| || ƒ qrt ƒ  qbqJd S )NzMailcap files:ú	zMailcap entries:z  %-15s)ra   r   r   rT   )r   ÚfnZckeysrb   rM   rN   ÚkeysÚkr   r   r   r`     s    
r`   Ú__main__)N)r   r   r!   ÚreÚ__all__r	   ÚcompileÚsearchrE   ÚWarningr
   r   r   r%   r   r-   r:   r   rF   rG   rZ   rD   r`   r   r   r   r   r   Ú<module>   s*   )
)