B
    u9a                 @   s   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re  dS )     Ngetcaps	findmatchc             C   s   d| krd| d fS dS d S )Nlinenor   )   r    )entryr   r   /usr/lib/python3.7/mailcap.pylineno_sort_key	   s    r	   c           
   C   s   i } d}xt  D ]}yt|d}W n tk
r8   wY nX | t||\}}W d Q R X x6| D ]*\}}|| kr||| |< qb| | | | |< qbW qW | S )Nr   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 )NZMAILCAPSHOME.z	/.mailcapz/etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)ZpathstrZmailcapshomer   r   r   r   -   s    


r   c             C   s    t dtd t| d \}}|S )Nz2readmailcapfile is deprecated, use getcaps instead   )warningswarnDeprecationWarningr   )r   r   _r   r   r   readmailcapfile?   s    r    c       	      C   s  i }x|   }|sP |d dks| dkr.q|}x4|dd  dkrf|   }|sTd}|d d | }q4W t|\}}|r|s~q|d k	r||d< |d7 }|d	}x$tt|D ]}||  ||< qW d	| }||kr|| | q|g||< qW ||fS )
Nr   # z\

r   r   /)	readlinestrip	parseliner   rangelenjoinlowerappend)	r   r   r   lineZnextliner   fieldstypesjr   r   r   r   G   s6    	  
r   c       
      C   s   g }dt |  }}x.||k r@t| ||\}}|| |d }qW t |dk rRdS |d |d |dd    }}}d|i}x^|D ]V}|d}|dk r|}d}	n$|d |  }||d d   }	||krq|	||< qW ||fS )Nr   r   r   )NNview=r"   )r*   
parsefieldr-   findr'   )
r.   r/   infieldr   r2   restZfkeyZfvaluer   r   r   r(   p   s*    

 

r(   c             C   sT   |}x:||k r>| | }|dkr"P q|dkr4|d }q|d }qW | ||   |fS )N;\r   r   )r'   )r.   r6   r7   startcr   r   r   r4      s    

r4   r2   z	/dev/nullc       	      C   sb   t | ||}xP|D ]H}d|krBt|d ||}|rBt|dkrBqt|| |||}||fS W dS )Ntestr   )NN)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>)r   )r   sortedr	   )r   rB   r   rE   Z	MIMEtypesr   )r   r   r?      s    
r?   c       
      C   s  d}dt |  }}x||k r| | }|d }|dkr`|dkrV| ||d  }|d }|| }q| | }|d }|dkr|| }q|dkr|| }q|dkr|| }q|dkr|}x ||k r| | d	kr|d }qW | || }	|d }|t|	| }q|d | }qW |S )
Nr"   r   r   %r;   st{})r*   	findparam)
r8   rB   rC   rD   Zresr6   r7   r=   r<   namer   r   r   r@      s6      
 


r@   c             C   sF   |   d } t| }x,|D ]$}|d |   | kr||d  S qW dS )Nr3   r"   )r,   r*   )rQ   rD   r7   pr   r   r   rP      s    
rP   c        	      C   s   dd l } t }| jdd  s(t| d S xtdt| jdD ]}| j||d  }t|dk rjtd d S |d }|d }t||d|\}}|stdt q<td| t	
|}|r<td| q<W 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   )	rS   r   r6   argsrB   filerG   rF   stsr   r   r   r>      s&    

r>   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)rV   r   r   rJ   )r   fnZckeysrW   rE   rF   keyskr   r   r   rU     s"      


rU   __main__)N)r   r   __all__r	   r   r   r    r   r(   r4   r   r?   r@   rP   r>   rU   __name__r   r   r   r   <module>   s"   
)
 