ó
è¾bc           @   sê   d  Z  d d l Z d d l Z d d l Z d d g Z e j d ƒ j Z d e f d „  ƒ  YZ	 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	   findmatchs   [^\xa1-\xff\w@+=:,./-]t   UnsafeMailcapInputc           B   s   e  Z d  Z RS(   s)   Warning raised when refusing unsafe input(   t   __name__t
   __module__t   __doc__(    (    (    s   /usr/lib/python2.7/mailcap.pyR      s   c          C   s©   i  }  xœ t  ƒ  D]‘ } y t | d ƒ } Wn t k
 r? q n X|  t | ƒ } Wd QXxE | j ƒ  D]7 \ } } | |  k r‹ | |  | <qf |  | | |  | <qf 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   listmailcapfilest   opent   IOErrort   readmailcapfilet	   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	 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    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
   >   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!   d   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'   R1   R2   t   startt   c(    (    s   /usr/lib/python2.7/mailcap.pyR.   ƒ   s    
R,   s	   /dev/nullc   
      C   sÛ   t  | ƒ r- d | f } t j | t ƒ d St |  | | ƒ } x• | D] } d | k r¤ t | d | | ƒ } | d k r€ qF n  | r¤ t j | ƒ d k r¤ qF q¤ n  t | | | | | ƒ }	 |	 d k	 rF |	 | f SqF 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.

    sH   Refusing to use mailcap with filename %r. Use a safe temporary filename.t   testi    N(   NN(   NN(	   t   _find_unsafet   warningst   warnR   R/   t   lookupt   substR   t   system(
   R   t   MIMEtypeR   t   filenamet   plistt   msgt   entriest   eR:   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(    (   RF   R   (    (    s   /usr/lib/python2.7/mailcap.pyt   <lambda>·   R   (   R   R/   t   filter(   R   RA   R   RE   t	   MIMEtypes(    (    s   /usr/lib/python2.7/mailcap.pyR>   ®   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t | ƒ rþ d | f } t j | t ƒ d  S| | } q | d	 k r­| }	 x* | | k  rI|  | d
 k rI| d } q W|  |	 | !}
 | d } t |
 | ƒ } t | ƒ r d | |
 f } t j | t ƒ d  S| | } q | d | } q W| S(   NR   i    i   t   %s   \t   st   ts9   Refusing to substitute MIME type %r into a shell command.t   {t   }s=   Refusing to substitute parameter %r (%s) into a shell command(   R#   R;   R<   R=   R   R/   t	   findparam(   R3   RA   RB   RC   t   resR1   R2   R9   RD   R8   t   namet   param(    (    s   /usr/lib/python2.7/mailcap.pyR?   º   sH    
 
 
 

c         C   sO   |  j  ƒ  d }  t |  ƒ } x, | D]$ } | |  j  ƒ  |  k r# | | Sq# Wd S(   NR-   R   (   R%   R#   (   RR   RC   R2   t   p(    (    s   /usr/lib/python2.7/mailcap.pyRP   ã   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@   (	   RU   R   R1   t   argsRA   t   fileRG   RF   t   sts(    (    s   /usr/lib/python2.7/mailcap.pyR:   î   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   ckeysRX   RE   RF   R\   t   k(    (    s   /usr/lib/python2.7/mailcap.pyRW     s&      


t   __main__(   R   R   R<   t   ret   __all__t   compilet   searchR;   t   WarningR   R    R   R
   R!   R.   R   R/   R>   R?   RP   R:   RW   R   (    (    (    s   /usr/lib/python2.7/mailcap.pyt   <module>   s&   			&		)			