Ñò
mÈKc           @   s¬   d  Z  d 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.iÿÿÿÿ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   r(   t   listmailcapfilest   opent   IOErrort   readmailcapfilet   closet	   iteritems(   t   capst   mailcapt   fpt   morecapst   keyt   value(    (    s   /usr/lib/python2.6/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/mailcap(   t   ost   environt   split(   t   strt   mailcapst   home(    (    s   /usr/lib/python2.6/mailcap.pyR   "   s    
c         C   sH  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	 | 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    t   #t    iþÿÿÿs   \
s   
t   /(	   t   readlinet   stript	   parselineR   t   ranget   lent   joint   lowert   append(   R   R	   t   linet   nextlineR   t   fieldst   typest   j(    (    s   /usr/lib/python2.6/mailcap.pyR   5   s4    	 $  
 c   
      C   s(  g  } d t  |  ƒ } } xA | | j  o3 t |  | | ƒ \ } } | i | ƒ | d } q Wt  | ƒ d j  o d S| d | d | d } } } h | d 6} 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    t
   parsefieldR#   t   Nonet   findR   (
   R$   R&   t   it   nt   fieldR   R)   t   restt   fkeyt   fvalue(    (    s   /usr/lib/python2.6/mailcap.pyR   [   s.       
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   (   R   (   R$   R.   R/   t   startt   c(    (    s   /usr/lib/python2.6/mailcap.pyR+   z   s     
R)   s	   /dev/nullc   	      C   s’   t  |  | | ƒ } xy | D]q } d | j o> t | d | | ƒ } | o t i | ƒ d j o q qj n t | | | | | ƒ } | | f SWd 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   lookupt   substR   t   systemR,   (	   R	   t   MIMEtypeR   t   filenamet   plistt   entriest   eR7   t   command(    (    s   /usr/lib/python2.6/mailcap.pyR   Š   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         S   s
   | |  j S(    (    (   R?   R   (    (    s   /usr/lib/python2.6/mailcap.pyt   <lambda>§   s    (   R   R,   t   filter(   R	   R;   R   R>   t	   MIMEtypes(    (    s   /usr/lib/python2.6/mailcap.pyR8   ž   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   }(   R    t	   findparam(
   R0   R;   R<   R=   t   resR.   R/   R6   R5   t   name(    (    s   /usr/lib/python2.6/mailcap.pyR9   ª   s:     
 
 
 
 
c         C   sQ   |  i  ƒ  d }  t |  ƒ } x. | D]& } | |  i  ƒ  |  j o	 | | Sq# Wd S(   NR*   R   (   R"   R    (   RK   R=   R/   t   p(    (    s   /usr/lib/python2.6/mailcap.pyRI   Ê   s     c    	   	   C   sü   d d  k  }  t ƒ  } |  i d p t | ƒ d  SxÃ t d t |  i ƒ d ƒ D]¦ } |  i | | d !} t | ƒ d j  o
 d GHd  S| d } | d } t | | d | ƒ \ } } | p d Gt GHqN d G| GHt i	 | ƒ } | o d	 G| GHqN qN Wd  S(
   Niÿÿÿÿi   i   s"   usage: mailcap [MIMEtype file] ...i    R)   s   No viewer found fors
   Executing:s   Exit status:(
   t   sysR    t   argvt   showR   R    R   t   typeR   R:   (	   RM   R	   R.   t   argsR;   t   fileR@   R?   t   sts(    (    s   /usr/lib/python2.6/mailcap.pyR7   Õ   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   R    t   keyst   sort(   R	   t   fnt   ckeysRP   R>   R?   RT   t   k(    (    s   /usr/lib/python2.6/mailcap.pyRO   ë   s.    
   
 
 
 t   __main__(   t   __doc__R   t   __all__R    R   R   R   R+   R   R,   R8   R9   RI   R7   RO   t   __name__(    (    (    s   /usr/lib/python2.6/mailcap.pyt   <module>   s   			&		 			