mň
­fIc           @   s`  d  Z  d d g Z d Z d Z d Z d Z d Z d Z d	 Z d
 Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z d Z e   Z h  a  d   Z! d Z" d  Z# d! d"  Z$ d! d#  Z% d$   Z& d%   Z' d&   Z( d'   Z) d(   Z* d)   Z+ d*   Z, d+   Z- e. d, j o e-   n d- S(.   s!   Gopher protocol client interface.t   send_selectort
   send_querys   1/s   gopher.micro.umn.eduiF   t   0t   1t   2t   3t   4t   5t   6t   7t   8t   9t   +t   st   et   ct   ht   Tt   Mt   It   wt   qt   gt   :t   ;t   <c         C   so   t  h  j o> x; t D]/ } | d  d j o | d t  t |  <q q Wn |  t  j o t  |  Sn d |  f S(   s=   Map all file types to strings; unknown types become TYPE='x'.i   t   A_s   TYPE=%rN(   t   _type_to_name_mapt   _namest   namet   evalt   gtype(   R   R   (    (    t&   /mit/python/lib/python2.4/gopherlib.pyt   type_to_name)   s        s   
s   	i    c         C   să   d k  } | pC | i d  } | d j o# | |  t | | d  } } qS n | p
 t } n* t |  t d  j o t |  } n | i  | i | i	  } | i | | f  | i |  t  | i d  | i d  S(   sG   Send a selector to a given host and port, return a file with the reply.NR   i    i   t    t   rb(   t   sockett   portt   hostt   findt   it   intt   DEF_PORTt   typet   AF_INETt   SOCK_STREAMR   t   connectt   sendallt   selectort   CRLFt   shutdownt   makefile(   R0   R&   R%   R$   R(   R   (    (    R    R    8   s     	'
c         C   s   t  |  d | | |  S(   s#   Send a selector and a query string.s   	N(   R    R0   t   queryR&   R%   (   R0   R4   R&   R%   (    (    R    R   I   s     c         C   s!   |  d j o d Sn	 |  d Sd S(   sJ   Takes a path as returned by urlparse and returns the appropriate selector.t   /i   N(   t   path(   R6   (    (    R    t   path_to_selectorM   s     c         C   s'   |  d j o d Sn t |  d  Sd S(   sj   Takes a path as returned by urlparse and maps it to a string.
    See section 3.4 of RFC 1738 for details.R5   s   TYPE='unknown'i   N(   R6   R!   (   R6   (    (    R    t   path_to_datatype_nameT   s     c         C   s?  g  } x2|  i   } | p
 d GHPn | d t j o | d  } n  | d t j o | d  } n | d j o Pn | p d GHq	 n | d } | d i t  } t	 |  d j  o d	 | f GHq	 n t	 |  d j o- | d d
 g j o d G| d Gd GHqn | i
 d  | i d |  | i
 |  q	 W| S(   s1   Get a directory in the form of a list of entries.i   s   (Unexpected EOF from server)iţ˙˙˙i˙˙˙˙t   .s   (Empty line from server)i    i   s   (Bad line from server: %r)R   s   (Extra info from server:t   )R"   N(   t   entriest   ft   readlinet   lineR1   R   t   splitt   TABt   partst   lent   appendt   insert(   R<   RA   R;   R   R>   (    (    R    t   get_directory`   s<       
c         C   s   g  } t |  | i  | S(   s@   Get a text file as a list of lines, with trailing CRLF stripped.N(   t   linest   get_alt_textfileR<   RC   (   R<   RF   (    (    R    t   get_textfile   s     c         C   s˘   x |  i   } | p
 d GHPn | d t j o | d  } n  | d t j o | d  } n | d j o Pn | d  d j o | d } n | |  q Wd S(	   sN   Get a text file and pass each line to a function, with trailing CRLF stripped.i   s   (Unexpected EOF from server)iţ˙˙˙i˙˙˙˙R9   i   s   ..N(   R<   R=   R>   R1   t   func(   R<   RI   R>   (    (    R    RG      s        c         C   s   |  i   } | S(   s*   Get a binary file as one solid data block.N(   R<   t   readt   data(   R<   RK   (    (    R    t
   get_binary   s     c         C   s1   x* |  i |  } | p Pn | |  q Wd S(   s4   Get a binary file and pass each block to a function.i   N(   R<   RJ   t	   blocksizeRK   RI   (   R<   RI   RM   RK   (    (    R    t   get_alt_binary   s       c          C   sĘ  d k  } d k } | i | i d d  \ } } t } | d } t } | o | d } | d } n | o | d } | d } t
 |  d j o | d | } } n& d } | o | d } | d } n d } | o | d } | d } q÷ n | t j o t | | |  }	 n t | |  }	 | t j o& t |	  }
 x |
 D] } | GHqIWnk | t t f j o& t |	  }  xH |  D] } | GHqWn2 t |	  } d Gt
 |  Gd Gt | d   d  GHd S(	   s   Trivial test program.Ni   R"   i    s   binary data:s   bytes:id   i(   (   t   syst   getoptt   argvt   optst   argst   DEF_SELECTORR0   R+   t   DEF_HOSTR&   RB   R4   t   A_INDEXR   R<   R    t   A_TEXTRH   RF   t   itemt   A_MENURE   R;   RL   RK   t   repr(   R;   R4   RP   R+   RS   R0   RO   R&   RK   R<   RF   RX   RR   (    (    R    t   test¤   sL     		





    t   __main__N(/   t   __doc__t   __all__RT   RU   R*   RW   RY   t   A_CSOt   A_ERRORt   A_MACBINHEXt
   A_PCBINHEXt   A_UUENCODEDRV   t   A_TELNETt   A_BINARYt   A_DUPLICATEt   A_SOUNDt   A_EVENTt
   A_CALENDARt   A_HTMLt   A_TN3270t   A_MIMEt   A_IMAGEt   A_WHOISt   A_QUERYt   A_GIFt   A_WWWt   A_PLUS_IMAGEt   A_PLUS_MOVIEt   A_PLUS_SOUNDt   dirR   R   R!   R1   R@   R    R   R7   R8   RE   RH   RG   RL   RN   R[   t   __name__(+   R`   R_   Rr   Rp   R   Rn   RU   Rc   Rs   R^   RN   R    Rk   Rj   Ra   RE   R!   R@   R[   Rm   RH   Rh   Ro   Rq   RG   R   Rt   R1   RW   R*   Rg   Rb   R8   Rl   RT   Ri   R7   RV   Rd   RL   Re   Rf   RY   (    (    R    t   ?   s^   					 					(