;
Êâ"Ic               @   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 o e ƒ  n d S(   u%   Mailcap file handling.  See RFC 1524.i    Nu   getcapsu	   findmatchc              C   s«   i  }  xž t  ƒ  D]“ } y t | d ƒ } Wn t k
 o w Yn Xt | ƒ } | j ƒ  xG | j ƒ  D]9 \ } } | |  k o | |  | <qf |  | | |  | <qf Wq W|  S(   u   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".

    u   r(   u   listmailcapfilesu   openu   IOErroru   readmailcapfileu   closeu   items(   u   capsu   mailcapu   fpu   morecapsu   keyu   value(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   getcaps	   s    

 	
 c              C   sq   d t  j k o  t  j d }  |  j d ƒ } n> d t  j k o t  j d } n d } | d d d d g } | S(	   u7   Return a list of all mailcap files found on the system.u   MAILCAPSu   :u   HOMEu   .u	   /.mailcapu   /etc/mailcapu   /usr/etc/mailcapu   /usr/local/etc/mailcap(   u   osu   environu   split(   u   stru   mailcapsu   home(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   listmailcapfiles"   s    
c             C   sT  i  } xG|  j  ƒ  } | p Pn | d d k p | j ƒ  d k o q	 n | } xM | d d … d k o5 |  j  ƒ  } | p
 d } n | d d … | } qU Wt | ƒ \ } } | o | p q	 n | j d ƒ } x. t t | ƒ ƒ D] } | | j ƒ  | | <që Wd j | ƒ j ƒ  } | | k o | | j | ƒ q	 | g | | <q	 | S(	   u  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    u   #u    iþÿÿÿNu   \
u   
u   /(	   u   readlineu   stripu	   parselineu   splitu   rangeu   lenu   joinu   loweru   append(   u   fpu   capsu   lineu   nextlineu   keyu   fieldsu   typesu   j(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   readmailcapfile5   s4    	 $  
 c       
      C   s:  g  } d t  |  ƒ } } xA | | k  o3 t |  | | ƒ \ } } | j | ƒ | d } q Wt  | ƒ d k  o d S| d | d | d d … } } } i | d 6} x… | D]} } | j d ƒ } | d k  o | } d }	 n1 | d | … j ƒ  } | | d d … j ƒ  }	 | | k o q¯ |	 | | <q¯ W| | f S(	   uÈ   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   Nu   viewu   =u    (   NN(   u   lenu
   parsefieldu   appendu   Noneu   findu   strip(
   u   lineu   fieldsu   iu   nu   fieldu   keyu   viewu   restu   fkeyu   fvalue(    (    u$   /mit/python/lib/python3.0/mailcap.pyu	   parseline[   s.     & 
c             C   sv   | } xS | | k  oE |  | } | d k o Pq	 | d k o | d } q	 | d } q	 W|  | | … j  ƒ  | f S(   u/   Separate one key-value pair in a mailcap entry.u   ;u   \i   i   (   u   strip(   u   lineu   iu   nu   startu   c(    (    u$   /mit/python/lib/python3.0/mailcap.pyu
   parsefieldz   s     
u   viewu	   /dev/nullc       	      C   s’   t  |  | | ƒ } xy | D]q } d | k o> t | d | | ƒ } | o t j | ƒ d k o q qj n t | | | | | ƒ } | | f SWd S(   u  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.

    u   testi    N(   NN(   u   lookupu   substu   osu   systemu   None(	   u   capsu   MIMEtypeu   keyu   filenameu   plistu   entriesu   eu   testu   command(    (    u$   /mit/python/lib/python3.0/mailcap.pyu	   findmatchŠ   s    	 c             C   s‹   g  } | |  k o | |  | } n | j  d ƒ } | d d } | |  k o | |  | } n | d  k	 o t | d „ | ƒ } n | S(   Nu   /i    u   /*c             S   s
   | |  k S(    (    (   u   eu   key(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   <lambda>§   s    (   u   splitu   Noneu   filter(   u   capsu   MIMEtypeu   keyu   entriesu	   MIMEtypes(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   lookupž   s    c       
      C   sw  d } d t  |  ƒ } } xW| | k  oI|  | } | d } | d k o= | d k o" |  | | d … } | d } n | | } q |  | } | d } | d k o | | } q | d k o | | } q | d k o | | } q | d k og | } x- | | k  o |  | d	 k o | d } qW|  | | … }	 | d } | t |	 | ƒ } q | d | } q W| S(
   Nu    i    i   u   %u   \u   su   tu   {u   }(   u   lenu	   findparam(
   u   fieldu   MIMEtypeu   filenameu   plistu   resu   iu   nu   cu   startu   name(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   substª   s:     
 
 
 
 
c             C   s]   |  j  ƒ  d }  t |  ƒ } x: | D]2 } | d  | … j  ƒ  |  k o | | d  … Sq# Wd S(   Nu   =u    (   u   loweru   len(   u   nameu   plistu   nu   p(    (    u$   /mit/python/lib/python3.0/mailcap.pyu	   findparamÊ   s     c        	      C   s  d d  l  }  t ƒ  } |  j d d  … p t | ƒ d  Sx× t d t |  j ƒ d ƒ D]º } |  j | | d … } t | ƒ d k  o t d ƒ d  S| d } | d } t | | d | ƒ \ } } | p t d t ƒ qT t d | ƒ t	 j
 | ƒ } | o t d | ƒ qT qT Wd  S(	   Ni    i   i   u"   usage: mailcap [MIMEtype file] ...u   viewu   No viewer found foru
   Executing:u   Exit status:(   u   sysu   getcapsu   argvu   showu   rangeu   lenu   printu	   findmatchu   typeu   osu   system(	   u   sysu   capsu   iu   argsu   MIMEtypeu   fileu   commandu   eu   sts(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   testÕ   s(    	
 


c          	   C   së   t  d ƒ x t ƒ  D] } t  d | ƒ q Wt  ƒ  |  p t ƒ  }  n t  d ƒ t  ƒ  |  j ƒ  } | j ƒ  xv | D]n } t  | ƒ |  | } xQ | D]I } | j ƒ  } | j ƒ  x# | D] } t  d | | | ƒ q¹ Wt  ƒ  q– Wqu Wd  S(   Nu   Mailcap files:u   	u   Mailcap entries:u     %-15s(   u   printu   listmailcapfilesu   getcapsu   keysu   sort(   u   capsu   fnu   ckeysu   typeu   entriesu   eu   keysu   k(    (    u$   /mit/python/lib/python3.0/mailcap.pyu   showë   s.    

   

 

 
 u   __main__(   u   __doc__u   osu   __all__u   getcapsu   listmailcapfilesu   readmailcapfileu	   parselineu
   parsefieldu	   findmatchu   Noneu   lookupu   substu	   findparamu   testu   showu   __name__(    (    (    u$   /mit/python/lib/python3.0/mailcap.pyu   <module>   s   			&		 			