ó
BÜXQc           @   s	  d  Z  d d l Z d d l Z d d l Z d d l m Z d „  Z d d d „  ƒ  YZ d e f d „  ƒ  YZ d	 d d
 „  ƒ  YZ	 d e	 f d „  ƒ  YZ
 d d d „  ƒ  YZ d g  d d d d „ Z d g  d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   sz  Response classes.

The seek_wrapper code is not used if you're using UserAgent with
.set_seekable_responses(False), or if you're using the urllib2-level interface
HTTPEquivProcessor.  Class closeable_response is instantiated by some handlers
(AbstractHTTPHandler), but the closeable_response interface is only depended
upon by Browser-level code.  Function upgrade_response is only used if you're
using Browser.


Copyright 2006 John J. Lee <jjl@pobox.com>

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD or ZPL 2.1 licenses (see the file COPYING.txt
included with the distribution).

iÿÿÿÿN(   t   StringIOc         C   s?   |  j  ƒ  } |  j d d ƒ z |  j  ƒ  SWd  |  j | ƒ Xd  S(   Ni    i   (   t   tellt   seek(   t   file_t   pos(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   len_of_seekable   s
    t   seek_wrapperc           B   s§   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z	 d	 „  Z
 d
 „  Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z e Z d „  Z RS(   sò  Adds a seek method to a file object.

    This is only designed for seeking on readonly file-like objects.

    Wrapped file-like object must have a read method.  The readline method is
    only supported if that method is present on the wrapped object.  The
    readlines method is always supported.  xreadlines and iteration are
    supported only for Python 2.2 and above.

    Public attributes:

    wrapped: the wrapped file object
    is_closed: true iff .close() has been called

    WARNING: All other attributes of the wrapped object (ie. those that are not
    one of wrapped, read, readline, readlines, xreadlines, __iter__ and next)
    are passed through unaltered, which may or may not make sense for your
    particular file object.

    c         C   sO   | |  _  t g |  _ t g |  _ t |  j  d ƒ |  _ t ƒ  |  _ d |  _ d  S(   Nt   readlinei    (	   t   wrappedt   Falset"   _seek_wrapper__read_complete_statet   _seek_wrapper__is_closed_statet   hasattrt   _seek_wrapper__have_readlineR    t   _seek_wrapper__cachet   _seek_wrapper__pos(   t   selfR   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __init__I   s    	c         C   s"   |  j  j ƒ  t |  j j ƒ  ƒ k S(   N(   R   R   t   lenR   t   getvalue(   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt	   invariantQ   s    c         C   s   |  j  j ƒ  t |  _ d  S(   N(   R   t   closet   Truet	   is_closed(   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   W   s    c         C   sc   | d k r |  j  d S| d k r. |  j d S|  j j d ƒ } | rS t | | ƒ St |  j | ƒ S(   NR   i    t   read_completeR   (   R   R
   t   __dict__t   gett   getattrt	   __class__(   R   t   nameR   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __getattr__[   s    c         C   sa   | d k r" t  | ƒ |  j d <n; | d k rP |  j s] t  | ƒ |  j d <q] n | |  j | <d  S(   NR   i    R   (   t   boolR   R   R
   R   (   R   R   t   value(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __setattr__g   s    	i    c         C   s“  | d k s t  ‚ | d k rB | d k  r9 t d ƒ ‚ n  d  } nŒ | d k rr | d k  ri t d ƒ ‚ n  | } n. |  j } | | k  r– t d ƒ ‚ n  | | } t |  j ƒ } | | } | d k  rÎ d } n  | d k r†|  j j d d ƒ | d  k rF| d k st  ‚ |  j j |  j j	 ƒ  ƒ t
 |  _ |  j j ƒ  | |  _ q|  j j	 | ƒ } | sjt
 |  _ n |  j j | ƒ | |  _ n	 | |  _ d  S(   Ni    i   i   s   negative seek offsets   seek to before start of file(   i    i   i   (   t   AssertionErrort
   ValueErrort   NoneR   R   R   R   t   writeR   t   readR   R   R   (   R   t   offsett   whencet   to_readt   destR   t   endt   data(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   p   s<     	 		

		c         C   s   |  j  S(   N(   R   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   ž   s    c         C   s:   |  j  |  j ƒ } |  j | _ |  j | _ |  j | _ | S(   N(   R   R   R   R
   R   (   R   t   cpy(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __copy__¡   s
    c         C   s8   |  j  } z |  j d ƒ |  j d ƒ SWd  | |  _  Xd  S(   Ni    iÿÿÿÿ(   R   R   R&   (   R   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   get_data¨   s
    	iÿÿÿÿc         C   sR  |  j  } t |  j ƒ } | | } | | k rg | d k rg |  j j | ƒ | | |  _  |  j j | ƒ S|  j j d d ƒ | d k r« |  j j |  j j ƒ  ƒ t |  _ nP | | } | d k sÇ t	 ‚ |  j j | ƒ } | së t |  _ n |  j j | ƒ |  j j | ƒ |  j j | ƒ } |  j j
 ƒ  |  _  |  j  | t | ƒ k sNt	 ‚ | S(   Niÿÿÿÿi    i   (   R   R   R   R   R&   R%   R   R   R   R"   R   R   (   R   t   sizeR   R+   t	   availableR)   R,   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR&   °   s,    	

c         C   sÇ   |  j  s t d ƒ ‚ n  |  j } |  j j d d ƒ |  j j ƒ  } | sU t |  _ n |  j j	 | ƒ |  j j | ƒ |  j j ƒ  } | d k rª | |  } | | |  _ n | } | t
 | ƒ |  _ | S(   Ns$   no readline method on wrapped objecti    i   iÿÿÿÿ(   R   t   NotImplementedErrorR   R   R   R   R   R   R   R%   R   (   R   R0   R   R,   t   r(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   Ï   s     		
c         C   sv   |  j  } |  j j d d ƒ |  j j |  j j ƒ  ƒ t |  _ |  j j | ƒ |  j j | ƒ } |  j j	 ƒ  |  _  | S(   Ni    i   (
   R   R   R   R%   R   R&   R   R   t	   readlinesR   (   R   t   sizehintR   R,   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR4   ç   s    		c         C   s   |  S(   N(    (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __iter__ñ   s    c         C   s%   |  j  ƒ  } | d k r! t ‚ n  | S(   Nt    (   R   t   StopIteration(   R   t   line(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   nextò   s     	c         C   s,   d |  j  j t t t |  ƒ ƒ ƒ |  j f S(   Ns$   <%s at %s whose wrapped object = %r>(   R   t   __name__t   hext   abst   idR   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __repr__ù   s    (   R;   t
   __module__t   __doc__R   R   R   R   R!   R   R   R.   R/   R&   R   R4   R6   R:   t
   xreadlinesR?   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   .   s"   						.			
		t   response_seek_wrapperc           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sL   
    Supports copying response objects and setting response body data.

    c         C   s&   t  j |  | ƒ |  j j ƒ  |  _ d  S(   N(   R   R   R   t   infot   _headers(   R   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR     s    c         C   s+   t  j |  ƒ } t j |  j ƒ  ƒ | _ | S(   N(   R   R.   t   copyRD   RE   (   R   R-   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR.   	  s    c         C   s   |  j  S(   N(   RE   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRD     s    c         C   s   |  j  j ƒ  S(   N(   R   t   geturl(   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRG     s    c         C   sO   |  j  d ƒ |  j ƒ  |  j ƒ  t ƒ  } |  _ | j | ƒ |  j  d ƒ d  S(   Ni    (   R   R&   R   R    R   R%   (   R   R,   t   cache(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   set_data  s    

(   R;   R@   RA   R   R.   RD   RG   RI   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRC   þ   s   				t   eoffilec           B   s;   e  Z d  d „ Z d  d „ Z d „  Z d „  Z d „  Z RS(   iÿÿÿÿc         C   s   d S(   NR7   (    (   R   R0   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR&   &  s    c         C   s   d S(   NR7   (    (   R   R0   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   '  s    c         C   s   |  S(   N(    (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR6   (  s    c         C   s   d S(   NR7   (    (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR:   )  s    c         C   s   d  S(   N(    (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   *  s    (   R;   R@   R&   R   R6   R:   R   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRJ   $  s
   		t   eofresponsec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   _urlRE   t   codet   msg(   R   t   urlt   headersRM   RN   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   -  s    			c         C   s   |  j  S(   N(   RL   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRG   2  s    c         C   s   |  j  S(   N(   RE   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRD   3  s    (   R;   R@   R   RG   RD   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRK   ,  s   		t   closeable_responsec           B   sS   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z RS(	   sâ  Avoids unnecessarily clobbering urllib.addinfourl methods on .close().

    Only supports responses returned by mechanize.HTTPHandler.

    After .close(), the following methods are supported:

    .read()
    .readline()
    .info()
    .geturl()
    .__iter__()
    .next()
    .close()

    and the following attributes are supported:

    .code
    .msg

    Also supports pickling (but the stdlib currently does something to prevent
    it: http://python.org/sf/1144636).

    c         C   s5   |  j  | ƒ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   _set_fpRE   RL   RM   RN   (   R   t   fpRP   RO   RM   RN   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   Q  s
    			c         C   s   | |  _  |  j  j |  _ |  j  j |  _ t |  j  d ƒ rK |  j  j |  _ n  t |  j  d ƒ ro |  j  j |  _ n d „  |  _ |  j  j |  _ |  j  j |  _ d  S(   NR4   t   filenoc           S   s   d  S(   N(   R$   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   <lambda>`  s    (   RS   R&   R   R   R4   RT   R6   R:   (   R   RS   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRR   X  s    	 c         C   s,   d |  j  j t t t |  ƒ ƒ ƒ |  j f S(   Ns   <%s at %s whose fp = %r>(   R   R;   R<   R=   R>   RS   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR?   d  s    c         C   s   |  j  S(   N(   RE   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRD   h  s    c         C   s   |  j  S(   N(   RL   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRG   k  s    c         C   sE   |  j  } | j ƒ  t |  j |  j |  j |  j ƒ } |  j | ƒ d  S(   N(   RS   R   RK   RL   RE   RM   RN   RR   (   R   R   t   new_wrapped(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   n  s
    	
c         C   s>   |  j  j ƒ  } t |  j |  j |  j |  j ƒ } | | d <| S(   NR   (   R   RF   RK   RL   RE   RM   RN   (   R   t   stateRV   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   __getstate__u  s
    
N(   R;   R@   RA   R$   RQ   R   RR   R?   RD   RG   R   RX   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRQ   6  s   						s	   test datas   http://example.com/iÈ   t   OKc         C   s   t  |  | | | | ƒ S(   N(   t   make_response(   R,   RP   RO   RM   RN   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   test_response‹  s    c         C   s#   | d g 7} t  |  | | | | ƒ S(   Ns   Content-types	   text/html(   s   Content-types	   text/html(   RZ   (   R,   RP   RO   RM   RN   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   test_html_response  s    c         C   s4   t  | ƒ } t t |  ƒ | | | | ƒ } t | ƒ S(   s  Convenient factory for objects implementing response interface.

    data: string containing response body data
    headers: sequence of (name, value) pairs
    url: URL of response
    code: integer response code (e.g. 200)
    msg: string response code message (e.g. "OK")

    (   t   make_headersRQ   R    RC   (   R,   RP   RO   RM   RN   t   mime_headersR3   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRZ   ”  s    
c         C   sD   g  } x |  D] } | j  d | ƒ q Wt j t d j | ƒ ƒ ƒ S(   s2   
    headers: sequence of (name, value) pairs
    s   %s: %ss   
(   t   appendt	   mimetoolst   MessageR    t   join(   RP   t   hdr_textt
   name_value(    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR]   £  s    c            s”   t  |  t j ƒ rŠ t |  d ƒ rŠ |  j j d k rC |  j j ‰  n d |  j j |  j j f ‰  d t |  j f ‡  f d †  ƒ  Y} | } n t } | S(   NR   t   __builtin__s   %s.%st   httperror_seek_wrapperc              s    e  Z ˆ  Z d  „  Z d „  Z RS(   c         S   s2   t  j |  | ƒ | j ƒ  |  _ | j ƒ  |  _ d  S(   N(   RC   R   RD   t   hdrsRG   t   filename(   R   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR   Á  s    c         S   s2   d |  j  j |  j t t t |  ƒ ƒ ƒ |  j f S(   Ns2   <%s (%s instance) at %s whose wrapped object = %r>(   R   R;   t   _exc_class_nameR<   R=   R>   R   (   R   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyR?   Ç  s    (   R;   R@   Ri   R   R?   (    (   t   exc_class_name(    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyRf   »  s   	(   t
   isinstancet   urllib2t	   HTTPErrorR   R   R@   R;   RC   (   t   responseRf   t   wrapper_class(    (   Rj   s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   get_seek_wrapper_class°  s    "	c         C   sC   t  |  d ƒ s* t |  ƒ } | |  ƒ }  n  t  |  d ƒ s? t ‚ |  S(   s:  Return a copy of response that supports seekable response interface.

    Accepts responses from both mechanize and urllib2 handlers.

    Copes with both ordinary response instances and HTTPError instances (which
    can't be simply wrapped due to the requirement of preserving the exception
    base class).
    R   R/   (   R   Rp   R"   (   Rn   Ro   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   seek_wrapped_responseÓ  s
    	c         C   s   t  |  ƒ } t |  d ƒ r[ t |  d ƒ s9 | |  ƒ }  n  t |  d ƒ sN t ‚ t j |  ƒ Sy |  j } Wn t k
 r d } n Xy |  j } Wn t k
 r¨ d } n Xd } t |  d d ƒ } | rÓ | ƒ  } n  t	 |  j
 |  j ƒ  |  j ƒ  | | ƒ }  | |  ƒ }  | r|  j | ƒ n  |  S(   só  Return a copy of response that supports Browser response interface.

    Browser response interface is that of "seekable responses"
    (response_seek_wrapper), plus the requirement that responses must be
    useable after .close() (closeable_response).

    Accepts responses from both mechanize and urllib2 handlers.

    Copes with both ordinary response instances and HTTPError instances (which
    can't be simply wrapped due to the requirement of preserving the exception
    base class).
    RQ   R   R/   N(   Rp   R   R"   RF   RM   t   AttributeErrorR$   RN   R   RQ   RS   RD   RG   RI   (   Rn   Ro   RM   RN   R,   R/   (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   upgrade_responseâ  s0    

$(    (    (    (   RA   RF   R`   Rl   t	   cStringIOR    R   R   RC   RJ   RK   RQ   R[   R\   RZ   R]   Rp   Rq   Rs   (    (    (    s8   /scratch/rashmi/Condor_Script/src/mechanize/_response.pyt   <module>   s"   $	Ð&
U			#	