o
    …9jœ#  ã                   @   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rdeƒ  dS dS )$z%Mailcap file handling.  See RFC 1524.é    NÚgetcapsÚ	findmatchc                 C   s   d| v r
d| d fS dS )NÚlinenor   )é   r   © )Úentryr   r   ú/usr/lib/python3.10/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 ]F}zt|dƒ}W n	 ty   Y qw |� t||ƒ\}}W d  ƒ n1 s.w   Y  | ¡ D ]\}}|| vrD|| |< q7| | | | |< q7q| 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   ÚmailcapÚfpÚmorecapsÚkeyÚvaluer   r   r   r      s"   

ÿÿ
üc                  C   sR   dt jv rt jd } |  t j¡}|S dt jv rt jd }nd}|d dddg}|S )z7Return a list of all mailcap files found on the system.ÚMAILCAPSÚHOMEÚ.z	/.mailcapz/etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)ÚosÚenvironÚsplitÚpathsep)ÚpathstrÚ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||fS |d dks| ¡ dkrq|}|dd… dkr<|   ¡ }|s,d}|dd… | }|dd… dks$t|ƒ\}}|rF|sGq|durS||d	< |d7 }| d
¡}tt|ƒƒD ]
}||  ¡ ||< q^d
 |¡ ¡ }||v r|||  |¡ n|g||< q)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   ú/)	ÚreadlineÚstripÚ	parseliner    ÚrangeÚlenÚjoinÚlowerÚappend)	r   r   r   ÚlineÚnextliner   ÚfieldsÚtypesÚjr   r   r   r   M   s6   	çý

är   c           
      C   sà   g }dt | ƒ}}||k r"t| ||ƒ\}}| |¡ |d }||k st |ƒdk r*dS |d |d |dd… }}}d|i}|D ]+}| d¡}|dk rP|}d}	n|d|…  ¡ }||d d…  ¡ }	||v rgq@|	||< q@||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,   )r4   Ú
parsefieldr7   Úfindr1   )
r8   r:   ÚiÚnÚfieldr   r>   ÚrestÚfkeyÚfvaluer   r   r   r2   v   s,   
ý 

r2   c                 C   sT   |}||k r | | }|dkrn|dkr|d }n|d }||k s| ||…   ¡ |fS )z/Separate one key-value pair in a mailcap entry.ú;ú\r%   r   )r1   )r8   rB   rC   ÚstartÚcr   r   r   r@   •   s   
ùr@   r>   z	/dev/nullc           
      C   s˜   t |ƒrd|f }t |t¡ dS t| ||ƒ}|D ]0}d|v r6t|d ||ƒ}|du r,q|r6t |¡dkr6qt|| |||ƒ}	|	durI|	|f  S q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.r=   ÚtestNr   )Ú_find_unsafer&   r'   r
   ÚlookupÚsubstr   Úsystem)
r   ÚMIMEtyper   ÚfilenameÚplistÚmsgÚentriesÚerL   Úcommandr   r   r   r   ¥   s"   	
ÿc                    sl   g }|| v r|| |  }|  d¡}|d d }|| v r!|| |  }ˆ d ur.‡ fdd„|D ƒ}t|td�}|S )Nr/   r   z/*c                    s   g | ]}ˆ |v r|‘qS r   r   )Ú.0rV   ©r   r   r   Ú
<listcomp>É   s    zlookup.<locals>.<listcomp>rY   )r    Úsortedr	   )r   rQ   r   rU   Ú	MIMEtypesr   rY   r   rN   À   s   
rN   c                 C   sd  d}dt | ƒ}}||k r°| | }|d }|dkr.|dkr)| ||d … }|d }|| }n~| | }|d }|dkr?|| }nm|dkrH|| }nd|dkrbt|ƒr]d|f }t |t¡ d S || }nJ|d	kr¦|}	||k r€| | d
kr€|d }||k r€| | d
ksr| |	|… }
|d }t|
|ƒ}t|ƒr¡d||
f }t |t¡ d S || }n|d | }||k s|S )Nr,   r   r   ú%rI   ÚsÚtz9Refusing to substitute MIME type %r into a shell command.ú{ú}z=Refusing to substitute parameter %r (%s) into a shell command)r4   rM   r&   r'   r
   Ú	findparam)rD   rQ   rR   rS   ÚresrB   rC   rK   rT   rJ   ÚnameÚparamr   r   r   rO   Í   sF   




ÿ

Þ#rO   c                 C   sF   |   ¡ d } t| ƒ}|D ]}|d |…   ¡ | kr ||d …   S qdS )Nr?   r,   )r6   r4   )rd   rS   rC   Úpr   r   r   rb   ö   s   ÿrb   c            	      C   sÎ   dd l } tƒ }| jdd … st|ƒ d S tdt| jƒdƒD ]G}| j||d … }t|ƒdk r5tdƒ  d S |d }|d }t||d|ƒ\}}|sNtdtƒ qtd|ƒ t	 
|¡}t	 |¡}|rdtd|ƒ qd S )	Nr   r   r%   z"usage: mailcap [MIMEtype file] ...r>   zNo viewer found forz
Executing:zExit status:)Úsysr   ÚargvÚshowr3   r4   Úprintr   Útyper   rP   Úwaitstatus_to_exitcode)	rg   r   rB   ÚargsrQ   ÚfilerW   rV   Ústsr   r   r   rL     s,   



€ñrL   c                 C   s˜   t dƒ tƒ D ]}t d| ƒ qt ƒ  | stƒ } t dƒ t ƒ  t| ƒ}|D ]$}t |ƒ | | }|D ]}t|ƒ}|D ]}t d| || ƒ q9t ƒ  q1q%d S )NzMailcap files:ú	zMailcap entries:z  %-15s)rj   r   r   r[   )r   ÚfnÚckeysrk   rU   rV   ÚkeysÚkr   r   r   ri     s"   
üýri   Ú__main__)N)r   r   r&   ÚreÚ__all__r	   ÚcompileÚsearchrM   ÚWarningr
   r   r   r*   r   r2   r@   r   rN   rO   rb   rL   ri   r   r   r   r   r   Ú<module>   s.    )
)
ÿ