
BXQc           @   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 d d l Z d e j f d     YZ	 d e	 f d     YZ
 d S(   s3  Convenient HTTP UserAgent class.

This is a subclass of urllib2.OpenerDirector.


Copyright 2003-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).

iNt   UserAgentBasec        
   B   s$  e  Z d  Z i e j d 6e j d 6e j d 6e j d 6e j d 6e j	 d 6e j
 d 6e j d 6e j d	 6e j d
 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6e j d 6Z d d d g Z d d d g Z d	 d
 d d d d d d d d g
 Z e e d  r>e j e d <e j d  n  d   Z d   Z  d   Z! d   Z" d+ d+ d  Z$ d+ d  Z% d+ d+ d  Z& d   Z' d   Z( d   Z) d    Z* d!   Z+ d"   Z, d+ e- d#  Z. d+ d$  Z/ d%   Z0 d&   Z1 d'   Z2 d(   Z3 d+ d+ d, i  d)  Z4 d+ d*  Z5 RS(-   s;  Convenient user-agent class.

    Do not use .add_handler() to add a handler for something already dealt with
    by this code.

    The only reason at present for the distinction between UserAgent and
    UserAgentBase is so that classes that depend on .seek()able responses
    (e.g. mechanize.Browser) can inherit from UserAgentBase.  The subclass
    UserAgent exposes a .set_seekable_responses() method that allows switching
    off the adding of a .seek() method to responses.

    Public attributes:

    addheaders: list of (name, value) pairs specifying headers to send with
     every request, unless they are overridden in the Request instance.

     >>> ua = UserAgentBase()
     >>> ua.addheaders = [
     ...  ("User-agent", "Mozilla/5.0 (compatible)"),
     ...  ("From", "responsible.person@example.com")]

    t   httpt   ftpt   filet   _unknownt   _http_errort   _http_default_errort
   _basicautht   _digestautht	   _redirectt   _cookiest   _refresht   _equivt   _proxyt   _proxy_basicautht   _proxy_digestautht   _robotst   _gzipt   _debug_redirectt   _debug_response_bodyt   HTTPSHandlert   httpsc         C   sS  t  j j |   i  } |  _ x9 |  j |  j |  j D]  } |  j | } |   | | <q5 Wx! | j   D] } |  j	 |  qf Wd | k r |  j
 t  n  d | k r |  j t  n  d  } } d | k s d | k r t j   } n  d | k s d | k rt j   } n  |  j |  |  j |  d | k rOt j   } |  j |  n  d  S(   NR   R   R   R   R   R   R   (   t   _openert   OpenerDirectort   __init__t   _ua_handlerst   default_schemest   default_otherst   default_featurest   handler_classest
   itervaluest   add_handlert   set_handle_refresht   Truet   set_handle_equivt   Nonet   _urllib2t   HTTPPasswordMgrWithDefaultRealmt   _autht   HTTPProxyPasswordMgrt   set_password_managert   set_proxy_password_managert   HTTPSClientCertMgrt   set_client_cert_manager(   t   selft   ua_handlerst   schemet   klasst   handlert   pmt   ppmt   cm(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR   [   s0    
c         C   s   t  j j |   d  |  _ d  S(   N(   R   R   t   closeR#   R   (   R,   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR4   |   s    c         C   s   i  } xX | D]P } | j  d  r5 t d |   n  | |  j k rS t d   n  d | | <q WxX |  j j   D]G \ } } | j  d  r qq n  | | k r |  j | d  qq | | =qq Wx$ | j   D] } |  j | t	  q Wd S(   s   Set sequence of URL scheme (protocol) strings.

        For example: ua.set_handled_schemes(["http", "ftp"])

        If this fails (with ValueError) because you've passed an unknown
        scheme, the set of handled schemes will not be changed.

        t   _s   not a scheme '%s's   unknown scheme '%s'N(
   t
   startswitht
   ValueErrorR   R#   R   t   itemst   _replace_handlert   keyst   _set_handlerR!   (   R,   t   schemest   wantR.   t
   oldhandler(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_handled_schemes   s    	 c         C   s   |  j  d d | d S(   s#   Set a mechanize.CookieJar, or None.R
   t   objN(   R;   (   R,   t	   cookiejar(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_cookiejar   s    c      	   C   s)   |  j  d t d t d | d |  d S(   s  Configure proxy settings.

        proxies: dictionary mapping URL scheme to proxy specification.  None
          means use the default system-specific settings.
        proxy_bypass: function taking hostname, returning whether proxy should
          be used.  None means use the default system-specific settings.

        The default is to try to obtain proxy settings from the system (see the
        documentation for urllib.urlopen for information about the
        system-specific methods used -- note that's urllib, not urllib2).

        To avoid all use of proxies, pass an empty proxies dict.

        >>> ua = UserAgentBase()
        >>> def proxy_bypass(hostname):
        ...     return hostname == "noproxy.com"
        >>> ua.set_proxies(
        ...     {"http": "joe:password@myproxy.example.com:3128",
        ...      "ftp": "proxy.example.com"},
        ...     proxy_bypass)

        R   t   constructor_kwdst   proxiest   proxy_bypassN(   R;   R!   t   dict(   R,   RD   RE   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_proxies   s    c         C   s   |  j  j | | | |  d  S(   N(   t   _password_managert   add_password(   R,   t   urlt   usert   passwordt   realm(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyRI      s    c         C   s   |  j  j | | | |  d  S(   N(   t   _proxy_password_managerRI   (   R,   RK   RL   t   hostportRM   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   add_proxy_password   s    	c         C   s   |  j  j | | |  d S(   s  Add an SSL client certificate, for HTTPS client auth.

        key_file and cert_file must be filenames of the key and certificate
        files, in PEM format.  You can use e.g. OpenSSL to convert a p12 (PKCS
        12) file to PEM format:

        openssl pkcs12 -clcerts -nokeys -in cert.p12 -out cert.pem
        openssl pkcs12 -nocerts -in cert.p12 -out key.pem


        Note that client certificate password input is very inflexible ATM.  At
        the moment this seems to be console only, which is presumably the
        default behaviour of libopenssl.  In future mechanize may support
        third-party libraries that (I assume) allow more options here.

        N(   t   _client_cert_managert   add_key_cert(   R,   RJ   t   key_filet	   cert_file(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   add_client_certificate   s    c         C   s3   | |  _  |  j d d | |  j d d | d S(   s9   Set a mechanize.HTTPPasswordMgrWithDefaultRealm, or None.R   R@   R   N(   RH   R;   (   R,   t   password_manager(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR(      s    	c         C   s3   | |  _  |  j d d | |  j d d | d S(   s.   Set a mechanize.HTTPProxyPasswordMgr, or None.R   R@   R   N(   RN   R;   (   R,   RV   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR)      s    	c         C   s#   | |  _  |  j d } | | _ d S(   s+   Set a mechanize.HTTPClientCertMgr, or None.R   N(   RQ   R   t   client_cert_manager(   R,   t   cert_managerR0   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR+      s    	c         C   s   |  j  d |  d S(   s-   Set whether to observe rules from robots.txt.R   N(   R;   (   R,   t   handle(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_handle_robots   s    c         C   s   |  j  d |  d S(   s,   Set whether to handle HTTP 30x redirections.R	   N(   R;   (   R,   RY   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_handle_redirect   s    c         C   s(   |  j  d | d i | d 6| d 6d S(   s+   Set whether to handle HTTP Refresh headers.R   RC   t   max_timet
   honor_timeN(   R;   (   R,   RY   R\   R]   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR       s    c         C   s<   | d k	 r i | d 6} n i  } |  j d | d | d S(   s   Set whether to treat HTML http-equiv headers like HTTP headers.

        Response objects may be .seek()able if this is set (currently returned
        responses are, raised HTTPError exception responses are not).

        t   head_parser_classR   RC   N(   R#   R;   (   R,   RY   R^   RC   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR"      s    c         C   s0   | r t  j d d d n  |  j d |  d S(   s(   Handle gzip transfer encoding.

        s'   gzip transfer encoding is experimental!t
   stackleveli   R   N(   t   warningst   warnR;   (   R,   RY   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_handle_gzip  s    c         C   s   |  j  d |  d S(   s  Log information about HTTP redirects (including refreshes).

        Logging is performed using module logging.  The logger name is
        "mechanize.http_redirects".  To actually print some debug output,
        eg:

        import sys, logging
        logger = logging.getLogger("mechanize.http_redirects")
        logger.addHandler(logging.StreamHandler(sys.stdout))
        logger.setLevel(logging.INFO)

        Other logger names relevant to this module:

        "mechanize.http_responses"
        "mechanize.cookies"

        To turn on everything:

        import sys, logging
        logger = logging.getLogger("mechanize")
        logger.addHandler(logging.StreamHandler(sys.stdout))
        logger.setLevel(logging.INFO)

        R   N(   R;   (   R,   RY   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_debug_redirects  s    c         C   s   |  j  d |  d S(   s  Log HTTP response bodies.

        See docstring for .set_debug_redirects() for details of logging.

        Response objects may be .seek()able if this is set (currently returned
        responses are, raised HTTPError exception responses are not).

        R   N(   R;   (   R,   RY   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_debug_responses*  s    	c         C   sU   t  t |   } x< d D]4 } |  j j |  } | d k	 r | j |  q q Wd S(   s!   Print HTTP headers to sys.stdout.R   R   N(   s   https   https(   t   intt   boolR   t   getR#   t   set_http_debuglevel(   R,   RY   t   levelR.   t   h(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_debug_http4  s
    c         C   su   | d  k r | d  k	 } n  | r[ |  j | } | d  k	 rI | |  } qa | | |   } n d  } |  j | |  d  S(   N(   R#   R   R9   (   R,   t   nameRY   R@   t   constructor_argsRC   t   handler_classt
   newhandler(    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR;   <  s    c         C   s   | d  k	 rR |  j j |  } | rR y |  j j |  WqO t k
 rK qO XqR n  | d  k	 r{ |  j |  | |  j | <n  d  S(   N(   R#   R   Rg   t   handlerst   removeR7   R   (   R,   Rl   Ro   R0   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR9   K  s    
N(    (6   t   __name__t
   __module__t   __doc__R$   t   HTTPHandlert
   FTPHandlert   FileHandlert   UnknownHandlert   HTTPErrorProcessort   HTTPDefaultErrorHandlert   HTTPBasicAuthHandlert   HTTPDigestAuthHandlert   HTTPRedirectHandlert   HTTPCookieProcessort   HTTPRefreshProcessort   HTTPEquivProcessort   ProxyHandlert   ProxyBasicAuthHandlert   ProxyDigestAuthHandlert   HTTPRobotRulesProcessorR   t   HTTPGzipProcessort   HTTPRedirectDebugProcessort   HTTPResponseDebugProcessorR   R   R   R   t   hasattrR   t   appendR   R4   R?   RB   R#   RG   RI   RP   RU   R(   R)   R+   RZ   R[   R!   R    R"   Rb   Rc   Rd   Rk   R;   R9   (    (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR       sj   

















			!												
	t	   UserAgentc           B   s,   e  Z d    Z d   Z d e j d  Z RS(   c         C   s   t  j |   t |  _ d  S(   N(   R    R   t   Falset	   _seekable(   R,   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR   \  s    c         C   s   t  |  |  _ d S(   s"   Make response objects .seek()able.N(   Rf   R   (   R,   RY   (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   set_seekable_responses`  s    c            s[     j  rB d  t j   f d  } t j | t j | | |  } n t j	   | |  } | S(   Nc            s   t  j   |  | |  S(   N(   R    t   open(   t   fullurlt   datat   timeout(   R,   (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt
   bound_openg  s    (
   R   R#   t   _sockettimeoutt   _GLOBAL_DEFAULT_TIMEOUTR   t   wrapped_opent	   _responset   seek_wrapped_responseR    R   (   R,   R   R   R   R   t   response(    (   R,   s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR   d  s    	N(   Rr   Rs   R   R   R#   R   R   R   (    (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyR   Z  s   		(   Rt   R`   R&   R   R   R   R   R$   R   R    R   (    (    (    s9   /scratch/rashmi/Condor_Script/src/mechanize/_useragent.pyt   <module>   s    C