
    Rhu$                        d Z ddlZddlZddlZddgZdZ ej                  eed       d Z ej                  d	      j                  Z G d
 de      Zd Zd Zd Zd Zd Zd Zddg fdZddZg fdZd Zd Zd Zedk(  r e        yy)z%Mailcap file handling.  See RFC 1524.    Ngetcaps	findmatchztThe {name} module is deprecated and will be removed in Python {remove}. See the mimetypes module for an alternative.)      )removec                     d| v rd| d   fS y)Nlinenor   )   r    )entrys    /usr/lib/python3.12/mailcap.pylineno_sort_keyr      s    5%/!!    z[^\xa1-\U0010FFFF\w@+=:,./-]c                       e Zd ZdZy)UnsafeMailcapInputz)Warning raised when refusing unsafe inputN)__name__
__module____qualname____doc__r   r   r   r   r      s    3r   r   c                  
   i } d}t               D ]V  }	 t        |d      }|5  t        ||      \  }}ddd       j	                         D ]  \  }}|| vr|| |<   | |   |z   | |<    X | S # t        $ r Y fw xY w# 1 sw Y   IxY w)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values          r   r   r      s     DF#% .	gs#B  	</F;Hf	<"..* 	.JC$;!S	 I-S			.. K  			< 	<s   A*A9*	A65A69B	c                      dt         j                  v r4t         j                  d   } | j                  t         j                        }|S dt         j                  v rt         j                  d   }nd}|dz   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homes      r   r   r   9   sv     RZZ**Z(==, O RZZ::f%D D;&"$<>Or   c                 Z    t        j                  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   _s      r   readmailcapfiler4   K   s+    MMF$a)r4(GD!Kr   c                 (   i }| j                         x}r|d   dk(  s|j                         dk(  r.|}|dd dk(  r%| j                         }|sd}|dd |z   }|dd dk(  r%t        |      \  }}|r|sp|
||d<   |d	z  }|j                  d
      }t	        t        |            D ]  }||   j                         ||<    d
j                  |      j                         }||v r||   j                  |       n|g||<   | j                         x}r||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   linenextliner!   fieldstypesjs	            r   r   r   S   s<    D++-
$
7c>TZZ\R/rsmv%{{}HX9x'D rsmv%
  oV%F8aKF		#s5z" 	(AQx~~'E!H	(hhuo##%$;IV$DI5 ++-
$
6 <r   c                 x   g }dt        |       }}||k  r,t        | ||      \  }}|j                  |       |dz   }||k  r,t        |      dk  ry|d   |d   |dd }}}d|i}|D ]P  }|j                  d      }|dk  r|}d}	n)|d| j	                         }||dz   d j	                         }	||v rL|	||<   R ||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=r7   )r?   
parsefieldrB   findr<   )
rC   rE   infieldr!   rJ   restfkeyfvalues
             r   r=   r=   z   s     Fc$iqA
a%dAq)qeaC a% 6{QQiF12JtCd^F "JJsOq5DF!9??$D1Q34[&&(F6>!F4L" ;r   c                 |    |}||k  r!| |   }|dk(  rn|dk(  r|dz   }n|dz   }||k  r!| || j                         |fS )z/Separate one key-value pair in a mailcap entry.;\r/   r
   )r<   )rC   rN   rO   startcs        r   rL   rL      s_    E
a%G8$Y!A!A a% a= !##r   rJ   z	/dev/nullc                    t        |      r!d|d}t        j                  |t               yt	        | ||      }|D ]M  }d|v r.t        |d   ||      }||rt        j                  |      dk7  r5t        ||   |||      }	|	I|	|fc S  y)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.

    z&Refusing to use mailcap with filename z . Use a safe temporary filename.rI   testr   )_find_unsafer0   r1   r   lookupsubstr'   system)
r   MIMEtyper!   filenameplistmsgentrieserZ   commands
             r   r   r      s     H\dfc-.T8S)G 	Q;6He4D|		$1,#(E:A:	 r   c                     g }|| v r|| |   z   }|j                  d      }|d   dz   }|| v r|| |   z   }||D cg c]	  }||v s| }}t        |t              }|S c c}w )Nr:   r   z/*)r!   )r)   sortedr   )r   r_   r!   rc   	MIMEtypesrd   s         r   r\   r\      s    G4DN*s#I|d"H4DN*
%2122W/2GN 3s   	AAc                 *   d}dt        |       }}||k  r| |   }|dz   }|dk7  r|dk(  r| ||dz    }|dz   }||z   }n| |   }|dz   }|dk(  r||z   }n|dk(  r||z   }n|dk(  r2t        |      r!d|d	}t        j                  |t               y ||z   }nz|d
k(  rm|}	||k  r| |   dk7  r|dz   }||k  r	| |   dk7  r| |	| }
|dz   }t        |
|      }t        |      r$d|d|
d}t        j                  |t               y ||z   }n|dz   |z   }||k  r|S )Nr7   r   r
   %rV   stz!Refusing to substitute MIME type z into a shell command.{}z!Refusing to substitute parameter z (z) into a shell command)r?   r[   r0   r1   r   	findparam)rP   r_   r`   ra   resrN   rO   rX   rb   rW   nameparams               r   r]   r]      se   
Cc%jqA
a%!H!A#a8Dy!AaCLac!'CaAac!CxAgcHnc)YacCMM#'9:Hnc!eaC!A !eaCU1~aC!$.&]bdhiCMM#'9:Ek
 Ci!mE a%F Jr   c                     | j                         dz   } t        |       }|D ]  }|d | j                         | k(  s||d  c S  y)NrK   r7   )rA   r?   )rq   ra   rO   ps       r   ro   ro      sN    ::<#DD	A Ra5;;=D QR5L r   c                     dd l } t               }| j                  dd  st        |       y t	        dt        | j                        d      D ]  }| j                  ||dz    }t        |      dk  rt        d        y |d   }|d   }t        ||d|      \  }}|st        dt               ^t        d|       t        j                  |      }t        j                  |      }|st        d|        y )	Nr   r
   r/   z"usage: mailcap [MIMEtype file] ...rJ   zNo viewer found forz
Executing:zExit status:)sysr   argvshowr>   r?   printr   typer'   r^   waitstatus_to_exitcode)	rv   r   rN   argsr_   filere   rd   stss	            r   rZ   rZ     s    9D88AB<T
1c#((mQ' +xx!A#t9q=677AwtXvt<
'.,())G$C++C0Cnc*+r   c                 X   t        d       t               D ]  }t        d|z           t                | s
t               } t        d       t                t        |       }|D ]G  }t        |       | |   }|D ]0  }t        |      }|D ]  }t        d|z  ||           t                2 I y )NzMailcap files:	zMailcap entries:z  %-15s)ry   r   r   rg   )r   fnckeysrz   rc   rd   keysks           r   rx   rx     s    	
 2%r	"22	G		
	G4LE dt* 	A!9D +i!mQqT*+G		r   __main__)N)r   r'   r0   re__all___DEPRECATION_MSG_deprecatedr   r   compilesearchr[   Warningr   r   r   r4   r   r=   rL   r   r\   r]   ro   rZ   rx   r   r   r   <module>r      s    + 	  	[
!#    X/ @ rzz9:AA4 44$%N>$  #);b 6 ,. 'R+." zF r   