U
    ð„0b¨  ã                   @   s¤   d Z ddlZddlZddgZdd„ 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   © )Úentryr   r   ú/usr/lib/python3.8/mailcap.pyÚlineno_sort_key	   s    r	   c               
   C   s’   i } d}t ƒ D ]~}zt|dƒ}W n tk
r8   Y qY nX |� t||ƒ\}}W 5 Q R X | ¡ D ]*\}}|| krz|| |< 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)Úcapsr   ZmailcapÚfpZmorecapsÚkeyÚvaluer   r   r   r      s    



c                  C   sP   dt jkr"t jd } |  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mailcapsZhomer   r   r   r   -   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   Úreadmailcapfile?   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k	r�||d< |d	7 }| d
¡}tt|ƒƒD ]}||  ¡ ||< q¦d
 |¡ ¡ }||k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   G   s4    	  
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…  ¡ }	||k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)   Ú
parsefieldr,   Úfindr&   )
r-   r.   ÚiÚnÚfieldr   r2   ÚrestZfkeyZfvaluer   r   r   r'   p   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&   )r-   r6   r7   ÚstartÚcr   r   r   r4   �   s    

r4   r2   z	/dev/nullc           	      C   sb   t | ||ƒ}|D ]L}d|kr@t|d ||ƒ}|r@t |¡dkr@qt|| |||ƒ}||f  S 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.

    Útestr   r1   )ÚlookupÚsubstr   Úsystem)	r   ÚMIMEtyper   ÚfilenameÚplistÚentriesÚer>   Úcommandr   r   r   r   Ÿ   s    	c                    sl   g }|| kr|| |  }|  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   )Ú.0rF   ©r   r   r   Ú
<listcomp>¼   s      zlookup.<locals>.<listcomp>rI   )r   Úsortedr	   )r   rB   r   rE   Z	MIMEtypesr   rI   r   r?   ³   s    
r?   c           
      C   s  d}dt | ƒ }}||k �r | | }|d }|dkr^|dkrT| ||d … }|d }|| }q| | }|d }|dkr€|| }q|dkr’|| }q|dkr¤|| }q|dkrò|}||k rÎ| | d	krÎ|d }q°| ||… }	|d }|t|	|ƒ }q|d | }q|S )
Nr!   r   r   ú%r;   ÚsÚtú{ú})r)   Ú	findparam)
r8   rB   rC   rD   Úresr6   r7   r=   r<   Únamer   r   r   r@   À   s6    
  
 



r@   c                 C   sF   |   ¡ d } t| ƒ}|D ](}|d |…   ¡ | kr||d …   S qdS )Nr3   r!   )r+   r)   )rS   rD   r7   Úpr   r   r   rQ   à   s    rQ   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	 
|¡}|r:td|ƒ q:d S )	Nr   r   r   z"usage: mailcap [MIMEtype file] ...r2   zNo viewer found forz
Executing:zExit status:)Úsysr   ÚargvÚshowr(   r)   Úprintr   Útyper   rA   )	rU   r   r6   ÚargsrB   ÚfilerG   rF   Ústsr   r   r   r>   ë   s&    

r>   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)rX   r   r   rK   )r   ÚfnZckeysrY   rE   rF   ÚkeysÚkr   r   r   rW     s"    
  rW   Ú__main__)N)Ú__doc__r   r   Ú__all__r	   r   r   r   r   r'   r4   r   r?   r@   rQ   r>   rW   Ú__name__r   r   r   r   Ú<module>   s$   
)
 