???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................
Yf                 @   s   d  Z  d d l 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   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   d S) z%Mailcap file handling.  See RFC 1524.    Ngetcaps	findmatchc             C   s"   d |  k r d |  d f Sd Sd  S)Nlinenor      )r   r    )entryr   r   ,/opt/alt/python35/lib64/python3.5/mailcap.pylineno_sort_key	   s    r	   c              C   s   i  }  d } x t    D] } y t | d  } Wn t k
 rF w Yn X|  t | |  \ } } Wd QRXxE | j   D]7 \ } } | |  k r | |  | <qw |  | | |  | <qw 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".

    r   rN)listmailcapfilesopenOSError_readmailcapfileitems)capsr   ZmailcapfpZmorecapskeyvaluer   r   r   r      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mailcapshomer   r   r   r   -   s    
r   c             C   s,   t  j d t d  t |  d  \ } } | S)z?Read a mailcap file and return a dictionary keyed by MIME type.z2readmailcapfile is deprecated, use getcaps instead   N)warningswarnDeprecationWarningr   )r   r   _r   r   r   readmailcapfile?   s    	
r    c       	      C   sa  i  } xN|  j    } | s P| d d k s	 | j   d k rA q	 | } xF | d d  d k r |  j    } | sx d } | d d  | } qJ Wt |  \ } } | o | s q	 | d k	 r | | d <| d	 7} | j d
  } x. t t |   D] } | | j   | | <q Wd
 j |  j   } | | k rF| | j |  q	 | g | | <q	 W| | f 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   # r   Nz\

r   r   /r%   )	readlinestrip	parseliner   rangelenjoinlowerappend)	r   r   r   lineZnextliner   fieldstypesjr   r   r   r   G   s6    	 " 

r   c       
      C   s1  g  } d t  |   } } x? | | k  rZ t |  | |  \ } } | j |  | d } q Wt  |  d k  rq d S| d | d | d d  } } } d | i } 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   r   Nview=r"   )NN)r*   
parsefieldr-   findr'   )
r.   r/   infieldr   r2   restZfkeyZfvaluer   r   r   r(   p   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.   r6   r7   startcr   r   r   r4      s    
r4   r2   z	/dev/nullc       	      C   s   t  |  | |  } xn | D]f } d | k r_ t | d | |  } | r_ t j |  d k r_ q 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er>   commandr   r   r   r      s    	c                s   g  } | |  k r  | |  | } | j  d  } | d d } | |  k rW | |  | }   d  k	 r|   f d d   | D } t | d t } | S)Nr$   r   z/*c                s"   g  |  ] }   | k r |  q Sr   r   ).0rF   )r   r   r   
<listcomp>   s   	 zlookup.<locals>.<listcomp>r   )r   sortedr	   )r   rB   r   rE   Z	MIMEtypesr   )r   r   r?      s    r?   c       
      C   sc  d } d t  |   } } xC| | k  r^|  | } | d } | d k r | d k rr |  | | d  } | d } | | } q |  | } | d } | d k r | | } q | d k r | | } q | d k r | | } q | d k rM| } x* | | k  r|  | d	 k r| d } q W|  | |  }	 | d } | t |	 |  } q | d | } q W| S)
Nr"   r   r   %r;   st{})r*   	findparam)
r8   rB   rC   rD   resr6   r7   r=   r<   namer   r   r   r@      s6    
 
 

 

r@   c             C   s[   |  j    d }  t |   } x8 | D]0 } | d  |  j    |  k r# | | d   Sq# Wd S)Nr3   r"   )r,   r*   )rR   rD   r7   pr   r   r   rP      s    rP   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 Wd  S)	Nr   r   r   z"usage: mailcap [MIMEtype file] ...r2   zNo viewer found forz
Executing:zExit status:)sysr   argvshowr)   r*   printr   typer   rA   )	rT   r   r6   argsrB   filerG   rF   stsr   r   r   r>      s&    	
"


r>   c             C   s   t  d  x t   D] } t  d |  q Wt    |  sB t   }  t  d  t    t |   } xl | D]d } t  |  |  | } xG | D]? } t |  } x# | D] } t  d | | |  q Wt    q Wqf Wd  S)NzMailcap files:	zMailcap entries:z  %-15s)rW   r   r   rJ   )r   fnZckeysrX   rE   rF   keyskr   r   r   rV     s"    
  	


rV   __main__)__doc__r   r   __all__r	   r   r   r    r   r(   r4   r   r?   r@   rP   r>   rV   __name__r   r   r   r   <module>   s$   
) 