mò
­fIc           @   s©   d  Z  d k Z d d g Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 d
 g  d „ Z d d „ Z
 g  d „ Z d „  Z d „  Z d „  Z e d j o e ƒ  n d S(   s%   Mailcap file handling.  See RFC 1524.Nt   getcapst	   findmatchc          C   sª   h  } x t ƒ  D]’ }  y t |  d ƒ } Wn t j
 o
 q n Xt | ƒ } | i ƒ  xG | i	 ƒ  D]9 \ } } | | j o | | | <qe | | | | | <qe Wq W| S(   s   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".

    t   rN(   t   capst   listmailcapfilest   mailcapt   opent   fpt   IOErrort   readmailcapfilet   morecapst   closet	   iteritemst   keyt   value(   R   R   R
   R   R   R   (    (    t$   /mit/python/lib/python2.4/mailcap.pyR    	   s     	 
 
 c          C   sq   d t  i j o  t  i d } | i d ƒ } n> d t  i j o t  i d }  n d }  |  d d d d g } | S(
   s7   Return a list of all mailcap files found on the system.t   MAILCAPSt   :t   HOMEt   .s	   /.mailcaps   /etc/mailcaps   /usr/etc/mailcaps   /usr/local/etc/mailcapN(   t   ost   environt   strt   splitt   mailcapst   home(   R   R   R   (    (    R   R   "   s     c         C   sJ  h  } x=|  i ƒ  } | p Pn | d d j p | i ƒ  d j o q	 n | } xA | d d j o/ |  i ƒ  } | p
 d } n | d  | } qU Wt | ƒ \ } } | o | p q	 n | i	 d ƒ } x. t t | ƒ ƒ D] } | | i ƒ  | | <qß Wd i | ƒ i ƒ  } | | j o | | i | ƒ q	 | g | | <q	 W| S(
   s  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".
    i   i    t   #t    iþÿÿÿs   \
s   
t   /N(   R   R   t   readlinet   linet   stript   nextlinet	   parselineR   t   fieldsR   t   typest   ranget   lent   jt   joint   lowert   append(   R   R    R"   R&   R   R   R   R#   (    (    R   R	   5   s8        $  
 c   
      C   s-  g  } d t |  ƒ } } xA | | j  o3 t |  | | ƒ \ } } | i | ƒ | d } q Wt | ƒ d j  o d Sn | d | d | d } }	 } h  d |	 <} xy | D]q } | i d ƒ } | d j  o | } d } n% | |  i ƒ  } | | d i ƒ  } | | j o q® | | | <q® W| | f S(	   sÈ   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.
    i    i   i   t   viewt   =R   N(   NN(   R"   R%   R   t   it   nt
   parsefieldt   fieldR)   t   NoneR   R*   t   restt   findt   fkeyt   fvalueR   (
   R   R,   R"   R-   R/   R1   R   R3   R4   R*   (    (    R   R!   [   s0        
c         C   ss   | } xS | | j  oE |  | } | d j o Pq	 | d j o | d } q	 | d } q	 W|  | | !i ƒ  | f S(   s/   Separate one key-value pair in a mailcap entry.t   ;s   \i   i   N(   R,   t   startR-   R   t   cR   (   R   R,   R-   R6   R7   (    (    R   R.   z   s      
R*   s	   /dev/nullc   	      C   s•   t  |  | | ƒ } x| | D]t } d | j o> t | d | | ƒ } | o t
 i | ƒ d j o q qj n t | | | | | ƒ } | | f Sq Wd S(   s  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.

    t   testi    N(   NN(   t   lookupR   t   MIMEtypeR   t   entriest   et   substt   filenamet   plistR8   R   t   systemt   commandR0   (	   R   R:   R   R>   R?   R<   R;   R8   RA   (    (    R   R   Š   s      c         C   s‹   g  } | |  j o | |  | } n | i d ƒ } | d d } | |  j o | |  | } n | d  j	 o t | d „ | ƒ } n | S(   NR   i    s   /*c         C   s
   | |  j S(   N(   R   R<   (   R<   R   (    (    R   t   <lambda>§   s    (   R;   R:   R   R   t	   MIMEtypesR   R0   t   filter(   R   R:   R   RC   R;   (    (    R   R9   ž   s    c   
      C   sq  d } d t |  ƒ } }	 xQ| |	 j  oC|  | } | d } | d j o: | d j o |  | | d !} | d } n | | } q |  | } | d } | d j o | | } q | d j o | | } q | d j o | | } q | d j od | } x- | |	 j  o |  | d	 j o | d } qÿ W|  | | !} | d } | t
 | | ƒ } q | d | } q W| S(
   NR   i    i   t   %s   \t   st   tt   {t   }(   t   resR%   R/   R,   R-   R7   R>   R:   R6   t   namet	   findparamR?   (
   R/   R:   R>   R?   R7   RK   R6   R,   RJ   R-   (    (    R   R=   ª   s4      
c         C   sT   |  i ƒ  d }  t |  ƒ } x1 | D]) } | |  i ƒ  |  j o | | Sq# q# Wd S(   NR+   R   (   RK   R(   R%   R-   R?   t   p(   RK   R?   RM   R-   (    (    R   RL   Ê   s     c    	      C   sÿ   d  k  } t ƒ  } | i d p t | ƒ d  Sn xÆ t d t | i ƒ d ƒ D]© } | i | | d !} t | ƒ d j  o d GHd  Sn | d }  | d } t | |  d | ƒ \ } } | p d Gt GHqN d G| GHt i | ƒ } | o d G| GHqN qN Wd  S(	   Ni   i   s"   usage: mailcap [MIMEtype file] ...i    R*   s   No viewer found fors
   Executing:s   Exit status:(   t   sysR    R   t   argvt   showR$   R%   R,   t   argsR:   t   fileR   RA   R<   t   typeR   R@   t   sts(	   R:   R<   RQ   R   RN   R,   RA   RR   RT   (    (    R   R8   Õ   s(    		
 

	c         C   sÁ   d GHx t  ƒ  D] } d | GHq WH|  p t ƒ  }  n d GHH|  i ƒ  } | i ƒ  xg | D]_ } | GH|  | } xG | D]? } | i ƒ  } | i ƒ  x | D] } d | G| | GHq™ WHqv WqZ Wd  S(   Ns   Mailcap files:s   	s   Mailcap entries:s     %-15s(   R   t   fnR   R    t   keyst   ckeyst   sortRS   R;   R<   t   k(   R   R<   RW   RV   R;   RS   RY   RU   (    (    R   RP   ë   s.    
   
 
 
 t   __main__(   t   __doc__R   t   __all__R    R   R	   R!   R.   R   R0   R9   R=   RL   R8   RP   t   __name__(   R.   R\   RL   R    RP   R   R   R	   R9   R!   R8   R=   R   (    (    R   t   ?   s   				&		 			