
[[\                 @   s   d  Z  d d l Z d d g Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d g  d d  Z d d d  Z	 g  d d  Z
 d d   Z d d   Z d d   Z e d k r e   n  d S)z%Mailcap file handling.  See RFC 1524.    Ngetcaps	findmatchc              C   s   i  }  x t    D] } y t | d  } Wn t k
 r@ w Yn X|  t |  } Wd QXxE | j   D]7 \ } } | |  k r | |  | <qg |  | | |  | <qg Wq W|  S)a  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".

    rN)listmailcapfilesopenOSErrorreadmailcapfileitems)capsZmailcapfpZmorecapskeyvalue r   /usr/lib/python3.4/mailcap.pyr   	   s    
c              C   sp   d t  j k r1 t  j d }  |  j t  j  } n; d t  j k rP t  j d } n d } | d d d d g } | S)z7Return a list of all mailcap files found on the system.ZMAILCAPSHOME.z	/.mailcapz/etc/mailcapz/usr/etc/mailcapz/usr/local/etc/mailcap)osenvironsplitpathsep)ZpathstrZmailcapsZhomer   r   r   r   "   s    
r   c             C   sF  i  } x9|  j    } | s Pn  | d d k s	 | j   d k rG q	 n  | } xI | d	 d  d k r |  j    } | s d } n  | d d
  | } qP Wt |  \ } } | o | s q	 n  | j d  } x. t t |   D] } | | j   | | <q Wd j |  j   } | | k r2| | j |  q	 | g | | <q	 | S)a  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".
    r   #    Nz\

/r   )	readlinestrip	parseliner   rangelenjoinlowerappend)r   r
   lineZnextliner   fieldstypesjr   r   r   r   5   s0    	 " 	r   c       
      C   s2  g  } d t  |   } } x? | | k  rZ t |  | |  \ } } | j |  | d } q Wt  |  d k  rq d S| d | d | d d  } } } i | d 6} x | D]y } | j d  } | d k  r | } d }	 n0 | d |  j   } | | d d  j   }	 | | k rq |	 | | <q W| | f S)	zParse 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.
    r      r   Nview=r   )NN)r    
parsefieldr#   findr   )
r$   r%   infieldr   r)   restZfkeyZfvaluer   r   r   r   [   s*    &	r   c             C   sp   | } xM | | k  rU |  | } | d k r/ Pq	 | d k rH | d } q	 | d } q	 W|  | |  j    | f S)z/Separate one key-value pair in a mailcap entry.;\r   r(   )r   )r$   r-   r.   startcr   r   r   r+   z   s    
r+   r)   z	/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)a  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.

    testr   N)NN)lookupsubstr   system)	r
   MIMEtyper   filenameplistentrieser5   commandr   r   r   r      s    		c                s   g  } | |  k r# | |  | } n  | j  d  } | d d } | |  k r] | |  | } n    d  k	 r   f d d   | D } n  | S)Nr   r   z/*c                s"   g  |  ] }   | k r |  q Sr   r   ).0r=   )r   r   r   
<listcomp>   s   	 zlookup.<locals>.<listcomp>)r   )r
   r9   r   r<   Z	MIMEtypesr   )r   r   r6      s    r6   c       
      C   sf  d } d t  |   } } xF| | k  ra|  | } | d } | d k r | d k ru |  | | d  } | d } n  | | } q |  | } | d } | d k r | | } q | d k r | | } q | d k r | | } q | d k rP| } x* | | k  r|  | d	 k r| d } q W|  | |  }	 | d } | t |	 |  } q | d | } q W| S)
Nr   r   r(   %r2   st{})r    	findparam)
r/   r9   r:   r;   Zresr-   r.   r4   r3   namer   r   r   r7      s6    
 
 
 

r7   c             C   s[   |  j    d }  t |   } x8 | D]0 } | d  |  j    |  k r# | | d   Sq# Wd S)Nr*   r   )r"   r    )rG   r;   r.   pr   r   r   rF      s    rF   c        	      C   s  d d  l  }  t   } |  j d d   s6 t |  d  Sx t d t |  j  d  D] } |  j | | d  } t |  d k  r t d  d  S| d } | d } t | | d |  \ } } | s t d t  qR t d |  t	 j
 |  } | rR t d |  qR qR Wd  S)	Nr   r(   r   z"usage: mailcap [MIMEtype file] ...r)   zNo viewer found forz
Executing:zExit status:)sysr   argvshowr   r    printr   typer   r8   )	rI   r
   r-   argsr9   filer>   r=   stsr   r   r   r5      s&    	
"


r5   c             C   s   t  d  x t   D] } t  d |  q Wt    |  sE t   }  n  t  d  t    t |   } xl | D]d } t  |  |  | } xG | D]? } t |  } x# | D] } t  d | | |  q Wt    q Wqi Wd  S)NzMailcap files:	zMailcap entries:z  %-15s)rL   r   r   sorted)r
   fnZckeysrM   r<   r=   keyskr   r   r   rK      s"    
  


rK   __main__)__doc__r   __all__r   r   r   r   r+   r   r6   r7   rF   r5   rK   __name__r   r   r   r   <module>   s   & 