ó
5Þ	Pc           @   sª   d  Z  d d l 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 k r¦ e ƒ  n  d S(   s%   Mailcap file handling.  See RFC 1524.iÿÿÿÿNt   getcapst	   findmatchc          C   s¦   i  }  x™ t  ƒ  D]Ž } y t | d ƒ } Wn t k
 r? q n Xt | ƒ } | j ƒ  xE | j ƒ  D]7 \ } } | |  k rˆ | |  | <qc |  | | |  | <qc 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.7/mailcap.pyR    	   s    

c          C   sm   d t  j k r. t  j d }  |  j d ƒ } n; d t  j k rM t  j 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.7/mailcap.pyR   "   s    
c         C   s:  i  } x-|  j  ƒ  } | s Pn  | d d k s	 | j ƒ  d k rG q	 n  | } x= | d d k rŒ |  j  ƒ  } | s{ d } n  | d  | } qP Wt | ƒ \ } } | o¨ | s± q	 n  | j d ƒ } x. t t | ƒ ƒ D] } | | j ƒ  | | <qÓ Wd j | ƒ j ƒ  } | | k r&| | j | ƒ 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.7/mailcap.pyR   5   s0    	 " 	c   
      C   s   g  } d t  |  ƒ } } x? | | k  rZ t |  | | ƒ \ } } | j | ƒ | d } q Wt  | ƒ d k  rq d S| d | d | d } } } i | d 6} xu | D]m } | j d ƒ } | d k  rÕ | } d }	 n$ | |  j ƒ  } | | d j ƒ  }	 | | k r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.7/mailcap.pyR   [   s*     	c         C   sm   | } xM | | k  rU |  | } | d k r/ Pq	 | d k rH | d } q	 | d } q	 W|  | | !j  ƒ  | 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.7/mailcap.pyR+   z   s    
R)   s	   /dev/nullc   	      C   s   t  |  | | ƒ } xt | D]l } d | k re t | d | | ƒ } | re t j | ƒ d k re q qe 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.7/mailcap.pyR   Š   s    		c         C   s…   g  } | |  k r# | |  | } n  | j  d ƒ } | d d } | |  k r] | |  | } n  | d  k	 r t | d „ | ƒ } n  | S(   NR   i    s   /*c         S   s
   | |  k S(   N(    (   R?   R   (    (    s   /usr/lib/python2.7/mailcap.pyt   <lambda>§   s    (   R   R,   t   filter(   R	   R;   R   R>   t	   MIMEtypes(    (    s   /usr/lib/python2.7/mailcap.pyR8   ž   s    c   
      C   s`  d } d t  |  ƒ } } x@| | k  r[|  | } | d } | d k r | d k rr |  | | d !} | d } n  | | } q |  | } | d } | d k r¬ | | } q | d k rÅ | | } q | d k rÞ | | } q | d k rJ| } x* | | k  r|  | d	 k r| 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.7/mailcap.pyR9   ª   s6    
 
 
 

c         C   sO   |  j  ƒ  d }  t |  ƒ } x, | D]$ } | |  j  ƒ  |  k r# | | Sq# Wd S(   NR*   R   (   R"   R    (   RK   R=   R/   t   p(    (    s   /usr/lib/python2.7/mailcap.pyRI   Ê   s    c    	      C   sô   d d  l  }  t ƒ  } |  j d s0 t | ƒ d  Sx½ t d t |  j ƒ d ƒ D]  } |  j | | d !} t | ƒ d k  r d GHd  S| d } | d } t | | d | ƒ \ } } | sÂ d Gt GHqL d G| GHt j	 | ƒ } | rL d	 G| GHqL qL 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.7/mailcap.pyR7   Õ   s&    	
"

	c         C   s¿   d GHx t  ƒ  D] } d | GHq WH|  s5 t ƒ  }  n  d GHH|  j ƒ  } | j ƒ  xg | D]_ } | GH|  | } xG | D]? } | j ƒ  } | j ƒ  x | D] } d | G| | GHq— WHqt WqX 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.7/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.7/mailcap.pyt   <module>   s   			&		 			