ó
BÜXQc           @   sï  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z y d d l Z Wn e	 k
 r} d d l
 Z n Xy e Wn& e k
 r® d d l Z e j Z n Xd d l m Z d d l Z d d l Z d d l Z d d l Z d d l m Z e Z d e j f d „  ƒ  YZ d „  Z d e j f d	 „  ƒ  YZ e e j d
 „ Z d e f d „  ƒ  YZ  d e  f d „  ƒ  YZ! d „  Z" d f  d „  ƒ  YZ# e# ƒ  j$ Z$ e a% e j& ƒ  Z' e e j d „ Z( e e e e j d „ Z) d „  Z* d S(   sí   URL opener.

Copyright 2004-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   Request(   t   isstringliket   ContentTooShortErrorc           B   s   e  Z d  „  Z RS(   c         C   s    t  j j |  | ƒ | |  _ d  S(   N(   t   urllib2t   URLErrort   __init__t   result(   t   selft   reasonR   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR   !   s    (   t   __name__t
   __module__R   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR       s   c         C   sX   y t  |  | ƒ Wn! t k
 r4 t |  | | ƒ n X| | k	 rT t |  | | ƒ n  d  S(   N(   t   getattrt   AttributeErrort   setattr(   t   reqt   namet   valuet   default(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   set_request_attr&   s    t   OpenerDirectorc           B   sw   e  Z d  „  Z d „  Z d „  Z e j d „ Z d
 e j d „ Z	 d „  Z
 d Z d
 d
 d
 e j e d „ Z d	 „  Z RS(   c         C   sJ   t  j j |  ƒ i  |  _ i  |  _ i  |  _ i  |  _ t |  _ g  |  _	 d  S(   N(
   t   _urllib2_forkR   R   t   process_responset   process_requestt   _any_requestt   _any_responset   Truet   _handler_index_validt
   _tempfiles(   R   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR   0   s    					c         C   sh   t  | d ƒ s( t d t | ƒ ƒ ‚ n  | |  j k r; d  St j |  j | ƒ | j |  ƒ t |  _ d  S(   Nt
   add_parents%   expected BaseHandler instance, got %r(	   t   hasattrt	   TypeErrort   typet   handlerst   bisectt   insortR   t   FalseR   (   R   t   handler(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   add_handler;   s    c         C   s"  |  j  r d  Si  } i  } i  } i  } t ƒ  } t ƒ  } g  } x²|  j D]§} t }	 x‚t | ƒ D]t}
 |
 d k rx q` n  |
 d k r | j | ƒ t }	 q` n% |
 d k rÂ | j | ƒ t }	 q` n  |
 j d ƒ } |
 |  } |
 | d } | j d ƒ r^|
 | d j d ƒ | d } |
 | d } y t	 | ƒ } Wn t
 k
 rHn X| j | i  ƒ } nT | d	 k ry| } | } n9 | d
 k r”| } | } n | d k r` | } | } n q` | j | t ƒ  ƒ j | ƒ t }	 q` W|	 sG | j | ƒ qG qG Wx | D] } |  j j | ƒ qùWx; | | g D]- } x$ | j ƒ  D] \ } } | | | <q3Wq WxW | j ƒ  D]I \ } } x: | j ƒ  D], \ } } t | ƒ } | j ƒ  | | | <qwWq^Wx: | j ƒ  D], \ } } t | ƒ } | j ƒ  | | | <q¸W| |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   Nt   redirect_requestt   do_opent
   proxy_opent   any_requestt   any_responset   _i   t   errort   opent   requestt   response(   s   redirect_requests   do_opens
   proxy_open(   R   t   setR    R#   t   dirt   addR   t   findt
   startswitht   intt
   ValueErrort
   setdefaultt   appendt   removet	   iteritemst   listt   sortt   handle_errort   handle_openR   R   R   R   (   R   R=   R>   R   R   R)   R*   t   unwantedR$   t   addedt   metht   iit   schemet	   conditiont   jjt   kindt   lookupR    t   code(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   _maybe_reindex_handlersG   s†    			
			


					c         C   sy   t  | ƒ r* t | | d | d | ƒ} nK | } | d  k	 rL | j | ƒ n  t | d | d  ƒ t | d | t j ƒ | S(   Nt   visitt   timeout(   R   R    t   Nonet   add_dataR   t   _sockettimeoutt   _GLOBAL_DEFAULT_TIMEOUT(   R   t
   url_or_reqt   dataRJ   RK   R   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   _request›   s    
c         C   s{  |  j  | | d  | ƒ } | j ƒ  } |  j ƒ  t |  j j | g  ƒ ƒ } | j |  j ƒ t	 | ƒ } | j
 ƒ  xP | D]H } x? d | d g D]- } t | | d  ƒ }	 |	 r |	 | ƒ } q q Wqv Wt j j }
 |
 |  | | ƒ } t |  j j | g  ƒ ƒ } | j |  j ƒ t	 | ƒ } | j
 ƒ  xS | D]K } xB d | d g D]0 } t | | d  ƒ }	 |	 r?|	 | | ƒ } q?q?Wq(W| S(   NR)   RR   R*   t	   _response(   RR   RL   t   get_typeRI   R0   R   t   gett   updateR   R;   R<   R   R   R   t   _openR   R   (   R   t   fullurlRQ   RK   R   t
   req_schemet   request_processorst	   processort	   meth_nameRA   t   urlopenR/   t   response_processors(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR-   ª   s0    


c         G   s±   | d
 k r< |  j  d } | d } d | } d } | } n |  j  } | d } d } | | | f | } t |  j | ƒ } | r„ | S| r­ | d d	 f | } t |  j | ƒ Sd  S(   Nt   httpt   httpsi   s   http_error_%si   t   _errori    R   t   http_error_default(   s   https   https(   R=   t   applyt   _call_chain(   R   t   protot   argst   dictR\   t   http_errt	   orig_argsR   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR,   Ð   s     

		
i   i   c         C   sÿ  |  j  | | t | ƒ } | j ƒ  } |  j | ƒ }	 z†|	 j ƒ  }
 | d
 k rd | d k rd d
 |
 f S| r| | | d ƒ } nf t j | j ƒ  ƒ d } t	 j
 j | ƒ d } t j | ƒ \ } } |  j j | ƒ t	 j | d ƒ } zÈ | |
 f } |  j } d } d } d } | rDd |
 k r1t |
 d ƒ } n  | | | | ƒ n  xb |	 j | ƒ } | d	 k rfPn  | t | ƒ 7} | j | ƒ | d 7} | rG| | | | ƒ qGqGWd
 | j ƒ  XWd
 |	 j ƒ  X| d k rû| | k  rût d | | f | ƒ ‚ n  | S(   sM  Returns (filename, headers).

        For remote objects, the default filename will refer to a temporary
        file.  Temporary files are removed when the OpenerDirector.close()
        method is called.

        For file: URLs, at present the returned filename is None.  This may
        change in future.

        If the actual number of bytes read is less than indicated by the
        Content-Length header, raises ContentTooShortError (a URLError
        subclass).  The exception's .result attribute contains the (filename,
        headers) that would have been returned.

        t   filet   wbi   i   iÿÿÿÿi    s   content-lengths   Content-Lengtht    Ns1   retrieval incomplete: got only %i out of %i bytes(   RR   R#   RT   R-   t   infoRL   t   _rfc3986t   urlsplitt   get_full_urlt   ost   patht   splitextt   tempfilet   mkstempR   R8   t   fdopent
   BLOCK_SIZER5   t   readt   lent   writet   closeR   (   R   RX   t   filenamet
   reporthookRQ   RK   R-   R   RC   t   fpt   headerst   tfpRr   t   suffixt   fdR   t   bst   sizeRx   t   blocknumt   block(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   retrieveæ   sR    
	

c         C   s~   t  j j |  ƒ d  |  _ |  _ |  _ |  _ |  j rz x6 |  j D]+ } y t	 j
 | ƒ WqA t k
 rk qA XqA W|  j 2n  d  S(   N(   R   R   R{   RL   R-   R,   R‡   R%   R   Rq   t   unlinkt   OSError(   R   R|   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR{   ,  s    	Ni    (   R	   R
   R   R%   RI   RN   RO   RR   RL   R-   R,   Rw   t	   open_fileR‡   R{   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR   /   s   			U%		Dc         C   s„   t  } y |  | | | ƒ } Wn7 t j k
 rU } t } | j d  k rL ‚  n  | } n X| d  k	 rq | | ƒ } n  | s€ | ‚ n  | S(   N(   R   R   t	   HTTPErrorR#   R~   RL   (   R]   t   process_response_objectRX   RQ   RK   t   successR/   R,   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   wrapped_open;  s    
	t   ResponseProcessingOpenerc           B   s#   e  Z d e j d  „ Z d „  Z RS(   c            s1   d  t j ‡  f d † } t | ˆ  j | | | ƒ S(   Nc            s   t  j ˆ  |  | | ƒ S(   N(   R   R-   (   RX   RQ   RK   (   R   (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt
   bound_openQ  s    (   RL   RN   RO   RŽ   RŒ   (   R   RX   RQ   RK   R   (    (   R   s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR-   O  s    c         C   s   | S(   N(    (   R   R/   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyRŒ   W  s    N(   R	   R
   RL   RN   RO   R-   RŒ   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR   M  s   t   SeekableResponseOpenerc           B   s   e  Z d  „  Z RS(   c         C   s   t  j | ƒ S(   N(   RS   t   seek_wrapped_response(   R   R/   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyRŒ   \  s    (   R	   R
   RŒ   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR‘   [  s   c         C   s   t  |  t j t f ƒ S(   N(   t
   isinstancet   typest	   ClassTypeR   (   t   obj(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   isclass`  s    t   OpenerFactoryc        	   B   s   e  Z d  Z e j e j e j e j e j e j	 e j
 e j e j g	 Z e e d ƒ rj e j e j ƒ n  g  Z g  Z e d „ Z d „  Z RS(   s1   This class's interface is quite likely to change.t   HTTPSc         C   s   | |  _  d  S(   N(   t   klass(   R   Rš   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR   y  s    c         G   s  |  j  ƒ  } t |  j ƒ } t ƒ  } xl | D]d } x[ | D]S } t | ƒ rl t | | ƒ r‹ | j | ƒ q‹ q8 t | | ƒ r8 | j | ƒ q8 q8 Wq+ Wx | D] } | j | ƒ qš Wx | D] } | j	 | ƒ  ƒ q¸ Wx3 | D]+ } t | ƒ r÷ | ƒ  } n  | j	 | ƒ qÙ W| S(   sF  Create an opener object from a list of handlers and processors.

        The opener will use several default handlers and processors, including
        support for HTTP and FTP.

        If any of the handlers passed as arguments are subclasses of the
        default handlers, the default handlers will not be used.

        (
   Rš   R;   t   default_classesR0   R—   t
   issubclassR2   R“   R9   R%   (   R   R    t   openerR›   t   skipRš   t   checkt   h(    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   build_opener|  s&    
	(   R	   R
   t   __doc__R   t   ProxyHandlert   UnknownHandlert   HTTPHandlert   HTTPDefaultErrorHandlert   HTTPRedirectHandlert
   FTPHandlert   FileHandlert   HTTPCookieProcessort   HTTPErrorProcessorR›   R   t   httplibR8   t   HTTPSHandlerR    t   replacement_handlersR   R   R¡   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR˜   d  s    c         C   sV   t  d  k rC t j ƒ  z t  d  k r1 t ƒ  a  n  Wd  t j ƒ  Xn  t  j |  | | ƒ S(   N(   t   _openerRL   t   urlopen_lockt   acquireR¡   t   releaseR-   (   t   urlRQ   RK   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyR]   ¡  s    
c         C   s\   t  d  k rC t j ƒ  z t  d  k r1 t ƒ  a  n  Wd  t j ƒ  Xn  t  j |  | | | | ƒ S(   N(   R¯   RL   R°   R±   R¡   R²   R‡   (   R³   R|   R}   RQ   RK   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   urlretrieve¬  s    
c         C   s
   |  a  d  S(   N(   R¯   (   R   (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   install_opener¸  s    (+   R¢   Rq   R   R!   R¬   R”   Rt   t	   threadingt
   _threadingt   ImportErrort   dummy_threadingR0   t	   NameErrort   setst   SetRR   R    RS   Rn   RN   R   t   _utilR   R-   RŠ   R   R   R   R   RL   RO   RŽ   R   R‘   R—   R˜   R¡   R¯   t   LockR°   R]   R´   Rµ   (    (    (    s6   /scratch/rashmi/Condor_Script/src/mechanize/_opener.pyt   <module>	   sD   H		ÿ 	9	