<s> """<STR_LIT>""" <EOL> import time <EOL> import collections <EOL> from . compat import cookielib , urlparse , Morsel <EOL> try : <EOL> import threading <EOL> threading <EOL> except ImportError : <EOL> import dummy_threading as threading <EOL> class MockRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request ) : <EOL> self . _r = request <EOL> self . _new_headers = { } <EOL> self . type = urlparse ( self . _r . url ) . scheme <EOL> def get_type ( self ) : <EOL> return self . type <EOL> def get_host ( self ) : <EOL> return urlparse ( self . _r . url ) . netloc <EOL> def get_origin_req_host ( self ) : <EOL> return self . get_host ( ) <EOL> def get_full_url ( self ) : <EOL> return self . _r . url <EOL> def is_unverifiable ( self ) : <EOL> return True <EOL> def has_header ( self , name ) : <EOL> return name in self . _r . headers or name in self . _new_headers <EOL> def get_header ( self , name , default = None ) : <EOL> return self . _r . headers . get ( name , self . _new_headers . get ( name , default ) ) <EOL> def add_header ( self , key , val ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def add_unredirected_header ( self , name , value ) : <EOL> self . _new_headers [ name ] = value <EOL> def get_new_headers ( self ) : <EOL> return self . _new_headers <EOL> @ property <EOL> def unverifiable ( self ) : <EOL> return self . is_unverifiable ( ) <EOL> @ property <EOL> def origin_req_host ( self ) : <EOL> return self . get_origin_req_host ( ) <EOL> @ property <EOL> def host ( self ) : <EOL> return self . get_host ( ) <EOL> class MockResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , headers ) : <EOL> """<STR_LIT>""" <EOL> self . _headers = headers <EOL> def info ( self ) : <EOL> return self . _headers <EOL> def getheaders ( self , name ) : <EOL> self . _headers . getheaders ( name ) <EOL> def extract_cookies_to_jar ( jar , request , response ) : <EOL> """<STR_LIT>""" <EOL> if not ( hasattr ( response , '<STR_LIT>' ) and <EOL> response . _original_response ) : <EOL> return <EOL> req = MockRequest ( request ) <EOL> res = MockResponse ( response . _original_response . msg ) <EOL> jar . extract_cookies ( res , req ) <EOL> def get_cookie_header ( jar , request ) : <EOL> """<STR_LIT>""" <EOL> r = MockRequest ( request ) <EOL> jar . add_cookie_header ( r ) <EOL> return r . get_new_headers ( ) . get ( '<STR_LIT>' ) <EOL> def remove_cookie_by_name ( cookiejar , name , domain = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> clearables = [ ] <EOL> for cookie in cookiejar : <EOL> if cookie . name == name : <EOL> if domain is None or domain == cookie . domain : <EOL> if path is None or path == cookie . path : <EOL> clearables . append ( ( cookie . domain , cookie . path , cookie . name ) ) <EOL> for domain , path , name in clearables : <EOL> cookiejar . clear ( domain , path , name ) <EOL> class CookieConflictError ( RuntimeError ) : <EOL> """<STR_LIT>""" <EOL> class RequestsCookieJar ( cookielib . CookieJar , collections . MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , name , default = None , domain = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _find_no_duplicates ( name , domain , path ) <EOL> except KeyError : <EOL> return default <EOL> def set ( self , name , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> remove_cookie_by_name ( self , name , domain = kwargs . get ( '<STR_LIT>' ) , path = kwargs . get ( '<STR_LIT:path>' ) ) <EOL> return <EOL> if isinstance ( value , Morsel ) : <EOL> c = morsel_to_cookie ( value ) <EOL> else : <EOL> c = create_cookie ( name , value , ** kwargs ) <EOL> self . set_cookie ( c ) <EOL> return c <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> keys = [ ] <EOL> for cookie in iter ( self ) : <EOL> keys . append ( cookie . name ) <EOL> return keys <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for cookie in iter ( self ) : <EOL> values . append ( cookie . value ) <EOL> return values <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> for cookie in iter ( self ) : <EOL> items . append ( ( cookie . name , cookie . value ) ) <EOL> return items <EOL> def list_domains ( self ) : <EOL> """<STR_LIT>""" <EOL> domains = [ ] <EOL> for cookie in iter ( self ) : <EOL> if cookie . domain not in domains : <EOL> domains . append ( cookie . domain ) <EOL> return domains <EOL> def list_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> paths = [ ] <EOL> for cookie in iter ( self ) : <EOL> if cookie . path not in paths : <EOL> paths . append ( cookie . path ) <EOL> return paths <EOL> def multiple_domains ( self ) : <EOL> """<STR_LIT>""" <EOL> domains = [ ] <EOL> for cookie in iter ( self ) : <EOL> if cookie . domain is not None and cookie . domain in domains : <EOL> return True <EOL> domains . append ( cookie . domain ) <EOL> return False <EOL> def get_dict ( self , domain = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> dictionary = { } <EOL> for cookie in iter ( self ) : <EOL> if ( domain is None or cookie . domain == domain ) and ( path is None <EOL> or cookie . path == path ) : <EOL> dictionary [ cookie . name ] = cookie . value <EOL> return dictionary <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _find_no_duplicates ( name ) <EOL> def __setitem__ ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> self . set ( name , value ) <EOL> def __delitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> remove_cookie_by_name ( self , name ) <EOL> def set_cookie ( self , cookie , * args , ** kwargs ) : <EOL> if cookie . value . startswith ( '<STR_LIT:">' ) and cookie . value . endswith ( '<STR_LIT:">' ) : <EOL> cookie . value = cookie . value . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return super ( RequestsCookieJar , self ) . set_cookie ( cookie , * args , ** kwargs ) <EOL> def update ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , cookielib . CookieJar ) : <EOL> for cookie in other : <EOL> self . set_cookie ( cookie ) <EOL> else : <EOL> super ( RequestsCookieJar , self ) . update ( other ) <EOL> def _find ( self , name , domain = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> for cookie in iter ( self ) : <EOL> if cookie . name == name : <EOL> if domain is None or cookie . domain == domain : <EOL> if path is None or cookie . path == path : <EOL> return cookie . value <EOL> raise KeyError ( '<STR_LIT>' % ( name , domain , path ) ) <EOL> def _find_no_duplicates ( self , name , domain = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> toReturn = None <EOL> for cookie in iter ( self ) : <EOL> if cookie . name == name : <EOL> if domain is None or cookie . domain == domain : <EOL> if path is None or cookie . path == path : <EOL> if toReturn is not None : <EOL> raise CookieConflictError ( '<STR_LIT>' % ( name ) ) <EOL> toReturn = cookie . value <EOL> if toReturn : <EOL> return toReturn <EOL> raise KeyError ( '<STR_LIT>' % ( name , domain , path ) ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = self . __dict__ . copy ( ) <EOL> state . pop ( '<STR_LIT>' ) <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( state ) <EOL> if '<STR_LIT>' not in self . __dict__ : <EOL> self . _cookies_lock = threading . RLock ( ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> new_cj = RequestsCookieJar ( ) <EOL> new_cj . update ( self ) <EOL> return new_cj <EOL> def create_cookie ( name , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> result = dict ( <EOL> version = <NUM_LIT:0> , <EOL> name = name , <EOL> value = value , <EOL> port = None , <EOL> domain = '<STR_LIT>' , <EOL> path = '<STR_LIT:/>' , <EOL> secure = False , <EOL> expires = None , <EOL> discard = True , <EOL> comment = None , <EOL> comment_url = None , <EOL> rest = { '<STR_LIT>' : None } , <EOL> rfc2109 = False , ) <EOL> badargs = set ( kwargs ) - set ( result ) <EOL> if badargs : <EOL> err = '<STR_LIT>' <EOL> raise TypeError ( err % list ( badargs ) ) <EOL> result . update ( kwargs ) <EOL> result [ '<STR_LIT>' ] = bool ( result [ '<STR_LIT:port>' ] ) <EOL> result [ '<STR_LIT>' ] = bool ( result [ '<STR_LIT>' ] ) <EOL> result [ '<STR_LIT>' ] = result [ '<STR_LIT>' ] . startswith ( '<STR_LIT:.>' ) <EOL> result [ '<STR_LIT>' ] = bool ( result [ '<STR_LIT:path>' ] ) <EOL> return cookielib . Cookie ( ** result ) <EOL> def morsel_to_cookie ( morsel ) : <EOL> """<STR_LIT>""" <EOL> expires = None <EOL> if morsel [ "<STR_LIT>" ] : <EOL> expires = time . time ( ) + morsel [ "<STR_LIT>" ] <EOL> elif morsel [ '<STR_LIT>' ] : <EOL> expires = morsel [ '<STR_LIT>' ] <EOL> if type ( expires ) == type ( "<STR_LIT>" ) : <EOL> time_template = "<STR_LIT>" <EOL> expires = time . mktime ( time . strptime ( expires , time_template ) ) <EOL> c = create_cookie ( <EOL> name = morsel . key , <EOL> value = morsel . value , <EOL> version = morsel [ '<STR_LIT:version>' ] or <NUM_LIT:0> , <EOL> port = None , <EOL> domain = morsel [ '<STR_LIT>' ] , <EOL> path = morsel [ '<STR_LIT:path>' ] , <EOL> secure = bool ( morsel [ '<STR_LIT>' ] ) , <EOL> expires = expires , <EOL> discard = False , <EOL> comment = morsel [ '<STR_LIT>' ] , <EOL> comment_url = bool ( morsel [ '<STR_LIT>' ] ) , <EOL> rest = { '<STR_LIT>' : morsel [ '<STR_LIT>' ] } , <EOL> rfc2109 = False , ) <EOL> return c <EOL> def cookiejar_from_dict ( cookie_dict , cookiejar = None , overwrite = True ) : <EOL> """<STR_LIT>""" <EOL> if cookiejar is None : <EOL> cookiejar = RequestsCookieJar ( ) <EOL> if cookie_dict is not None : <EOL> names_from_jar = [ cookie . name for cookie in cookiejar ] <EOL> for name in cookie_dict : <EOL> if overwrite or ( name not in names_from_jar ) : <EOL> cookiejar . set_cookie ( create_cookie ( name , cookie_dict [ name ] ) ) <EOL> return cookiejar </s>
