<s> """<STR_LIT>""" <EOL> import warnings <EOL> import _auth <EOL> import _gzip <EOL> import _opener <EOL> import _response <EOL> import _sockettimeout <EOL> import _urllib2 <EOL> class UserAgentBase ( _opener . OpenerDirector ) : <EOL> """<STR_LIT>""" <EOL> handler_classes = { <EOL> "<STR_LIT:http>" : _urllib2 . HTTPHandler , <EOL> "<STR_LIT>" : _urllib2 . FTPHandler , <EOL> "<STR_LIT:file>" : _urllib2 . FileHandler , <EOL> "<STR_LIT>" : _urllib2 . UnknownHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPErrorProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPRequestUpgradeProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPDefaultErrorHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPBasicAuthHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPDigestAuthHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPRedirectHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPCookieProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPRefreshProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPEquivProcessor , <EOL> "<STR_LIT>" : _urllib2 . ProxyHandler , <EOL> "<STR_LIT>" : _urllib2 . ProxyBasicAuthHandler , <EOL> "<STR_LIT>" : _urllib2 . ProxyDigestAuthHandler , <EOL> "<STR_LIT>" : _urllib2 . HTTPRobotRulesProcessor , <EOL> "<STR_LIT>" : _gzip . HTTPGzipProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPRedirectDebugProcessor , <EOL> "<STR_LIT>" : _urllib2 . HTTPResponseDebugProcessor , <EOL> } <EOL> default_schemes = [ "<STR_LIT:http>" , "<STR_LIT>" , "<STR_LIT:file>" ] <EOL> default_others = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> default_features = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> if hasattr ( _urllib2 , '<STR_LIT>' ) : <EOL> handler_classes [ "<STR_LIT>" ] = _urllib2 . HTTPSHandler <EOL> default_schemes . append ( "<STR_LIT>" ) <EOL> def __init__ ( self ) : <EOL> _opener . OpenerDirector . __init__ ( self ) <EOL> ua_handlers = self . _ua_handlers = { } <EOL> for scheme in ( self . default_schemes + <EOL> self . default_others + <EOL> self . default_features ) : <EOL> klass = self . handler_classes [ scheme ] <EOL> ua_handlers [ scheme ] = klass ( ) <EOL> for handler in ua_handlers . itervalues ( ) : <EOL> self . add_handler ( handler ) <EOL> if "<STR_LIT>" in ua_handlers : <EOL> self . set_handle_refresh ( True ) <EOL> if "<STR_LIT>" in ua_handlers : <EOL> self . set_handle_equiv ( True ) <EOL> pm = ppm = None <EOL> if "<STR_LIT>" in ua_handlers or "<STR_LIT>" in ua_handlers : <EOL> pm = _urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> if ( "<STR_LIT>" in ua_handlers or <EOL> "<STR_LIT>" in ua_handlers ) : <EOL> ppm = _auth . HTTPProxyPasswordMgr ( ) <EOL> self . set_password_manager ( pm ) <EOL> self . set_proxy_password_manager ( ppm ) <EOL> if "<STR_LIT>" in ua_handlers : <EOL> cm = _urllib2 . HTTPSClientCertMgr ( ) <EOL> self . set_client_cert_manager ( cm ) <EOL> def close ( self ) : <EOL> _opener . OpenerDirector . close ( self ) <EOL> self . _ua_handlers = None <EOL> def set_handled_schemes ( self , schemes ) : <EOL> """<STR_LIT>""" <EOL> want = { } <EOL> for scheme in schemes : <EOL> if scheme . startswith ( "<STR_LIT:_>" ) : <EOL> raise ValueError ( "<STR_LIT>" % scheme ) <EOL> if scheme not in self . handler_classes : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> want [ scheme ] = None <EOL> for scheme , oldhandler in self . _ua_handlers . items ( ) : <EOL> if scheme . startswith ( "<STR_LIT:_>" ) : continue <EOL> if scheme not in want : <EOL> self . _replace_handler ( scheme , None ) <EOL> else : <EOL> del want [ scheme ] <EOL> for scheme in want . keys ( ) : <EOL> self . _set_handler ( scheme , True ) <EOL> def set_cookiejar ( self , cookiejar ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , obj = cookiejar ) <EOL> def set_proxies ( self , proxies ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , obj = proxies ) <EOL> def add_password ( self , url , user , password , realm = None ) : <EOL> self . _password_manager . add_password ( realm , url , user , password ) <EOL> def add_proxy_password ( self , user , password , hostport = None , realm = None ) : <EOL> self . _proxy_password_manager . add_password ( <EOL> realm , hostport , user , password ) <EOL> def add_client_certificate ( self , url , key_file , cert_file ) : <EOL> """<STR_LIT>""" <EOL> self . _client_cert_manager . add_key_cert ( url , key_file , cert_file ) <EOL> def set_password_manager ( self , password_manager ) : <EOL> """<STR_LIT>""" <EOL> self . _password_manager = password_manager <EOL> self . _set_handler ( "<STR_LIT>" , obj = password_manager ) <EOL> self . _set_handler ( "<STR_LIT>" , obj = password_manager ) <EOL> def set_proxy_password_manager ( self , password_manager ) : <EOL> """<STR_LIT>""" <EOL> self . _proxy_password_manager = password_manager <EOL> self . _set_handler ( "<STR_LIT>" , obj = password_manager ) <EOL> self . _set_handler ( "<STR_LIT>" , obj = password_manager ) <EOL> def set_client_cert_manager ( self , cert_manager ) : <EOL> """<STR_LIT>""" <EOL> self . _client_cert_manager = cert_manager <EOL> handler = self . _ua_handlers [ "<STR_LIT>" ] <EOL> handler . client_cert_manager = cert_manager <EOL> def set_handle_robots ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , handle ) <EOL> def set_handle_redirect ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , handle ) <EOL> def set_handle_refresh ( self , handle , max_time = None , honor_time = True ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , handle , constructor_kwds = <EOL> { "<STR_LIT>" : max_time , "<STR_LIT>" : honor_time } ) <EOL> def set_handle_equiv ( self , handle , head_parser_class = None ) : <EOL> """<STR_LIT>""" <EOL> if head_parser_class is not None : <EOL> constructor_kwds = { "<STR_LIT>" : head_parser_class } <EOL> else : <EOL> constructor_kwds = { } <EOL> self . _set_handler ( "<STR_LIT>" , handle , constructor_kwds = constructor_kwds ) <EOL> def set_handle_gzip ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> if handle : <EOL> warnings . warn ( <EOL> "<STR_LIT>" , stacklevel = <NUM_LIT:2> ) <EOL> self . _set_handler ( "<STR_LIT>" , handle ) <EOL> def set_debug_redirects ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , handle ) <EOL> def set_debug_responses ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _set_handler ( "<STR_LIT>" , handle ) <EOL> def set_debug_http ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> level = int ( bool ( handle ) ) <EOL> for scheme in "<STR_LIT:http>" , "<STR_LIT>" : <EOL> h = self . _ua_handlers . get ( scheme ) <EOL> if h is not None : <EOL> h . set_http_debuglevel ( level ) <EOL> def _set_handler ( self , name , handle = None , obj = None , <EOL> constructor_args = ( ) , constructor_kwds = { } ) : <EOL> if handle is None : <EOL> handle = obj is not None <EOL> if handle : <EOL> handler_class = self . handler_classes [ name ] <EOL> if obj is not None : <EOL> newhandler = handler_class ( obj ) <EOL> else : <EOL> newhandler = handler_class ( <EOL> * constructor_args , ** constructor_kwds ) <EOL> else : <EOL> newhandler = None <EOL> self . _replace_handler ( name , newhandler ) <EOL> def _replace_handler ( self , name , newhandler = None ) : <EOL> if name is not None : <EOL> handler = self . _ua_handlers . get ( name ) <EOL> if handler : <EOL> try : <EOL> self . handlers . remove ( handler ) <EOL> except ValueError : <EOL> pass <EOL> if newhandler is not None : <EOL> self . add_handler ( newhandler ) <EOL> self . _ua_handlers [ name ] = newhandler <EOL> class UserAgent ( UserAgentBase ) : <EOL> def __init__ ( self ) : <EOL> UserAgentBase . __init__ ( self ) <EOL> self . _seekable = False <EOL> def set_seekable_responses ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> self . _seekable = bool ( handle ) <EOL> def open ( self , fullurl , data = None , <EOL> timeout = _sockettimeout . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> if self . _seekable : <EOL> def bound_open ( fullurl , data = None , <EOL> timeout = _sockettimeout . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> return UserAgentBase . open ( self , fullurl , data , timeout ) <EOL> response = _opener . wrapped_open ( <EOL> bound_open , _response . seek_wrapped_response , fullurl , data , <EOL> timeout ) <EOL> else : <EOL> response = UserAgentBase . open ( self , fullurl , data ) <EOL> return response </s>
