a
    ;`                     @   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re  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   /usr/lib/python3.9/mailcap.pylineno_sort_key	   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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ur||d< |d	7 }|d
}tt|D ]}||  ||< qd
| }||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   G   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 rqz|	||< qz||fS )	zParse 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|v 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 }|| 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   ).0rF   r   r   r   
<listcomp>       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@      s0    





r@   c                 C   sF   |   d } t| }|D ](}|d |   | kr||d    S qdS )Nr3   r!   )r+   r)   )rT   rD   r7   pr   r   r   rR      s    rR   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|\}}|stdt q:td| t	
|}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   waitstatus_to_exitcode)	rV   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)rY   r   r   rL   )r   fnZckeysrZ   rE   rF   keyskr   r   r   rX     s    
rX   __main__)N)__doc__r   r   __all__r	   r   r   r   r   r'   r4   r   r?   r@   rR   r>   rX   __name__r   r   r   r   <module>   s$   
)
 