<s> """<STR_LIT>""" <EOL> import time , re <EOL> from cookielib import ( _warn_unhandled_exception , FileCookieJar , LoadError , <EOL> Cookie , MISSING_FILENAME_TEXT , <EOL> join_header_words , split_header_words , <EOL> iso2time , time2isoz ) <EOL> def lwp_cookie_str ( cookie ) : <EOL> """<STR_LIT>""" <EOL> h = [ ( cookie . name , cookie . value ) , <EOL> ( "<STR_LIT:path>" , cookie . path ) , <EOL> ( "<STR_LIT>" , cookie . domain ) ] <EOL> if cookie . port is not None : h . append ( ( "<STR_LIT:port>" , cookie . port ) ) <EOL> if cookie . path_specified : h . append ( ( "<STR_LIT>" , None ) ) <EOL> if cookie . port_specified : h . append ( ( "<STR_LIT>" , None ) ) <EOL> if cookie . domain_initial_dot : h . append ( ( "<STR_LIT>" , None ) ) <EOL> if cookie . secure : h . append ( ( "<STR_LIT>" , None ) ) <EOL> if cookie . expires : h . append ( ( "<STR_LIT>" , <EOL> time2isoz ( float ( cookie . expires ) ) ) ) <EOL> if cookie . discard : h . append ( ( "<STR_LIT>" , None ) ) <EOL> if cookie . comment : h . append ( ( "<STR_LIT>" , cookie . comment ) ) <EOL> if cookie . comment_url : h . append ( ( "<STR_LIT>" , cookie . comment_url ) ) <EOL> keys = cookie . _rest . keys ( ) <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> h . append ( ( k , str ( cookie . _rest [ k ] ) ) ) <EOL> h . append ( ( "<STR_LIT:version>" , str ( cookie . version ) ) ) <EOL> return join_header_words ( [ h ] ) <EOL> class LWPCookieJar ( FileCookieJar ) : <EOL> """<STR_LIT>""" <EOL> def as_lwp_str ( self , ignore_discard = True , ignore_expires = True ) : <EOL> """<STR_LIT>""" <EOL> now = time . time ( ) <EOL> r = [ ] <EOL> for cookie in self : <EOL> if not ignore_discard and cookie . discard : <EOL> continue <EOL> if not ignore_expires and cookie . is_expired ( now ) : <EOL> continue <EOL> r . append ( "<STR_LIT>" % lwp_cookie_str ( cookie ) ) <EOL> return "<STR_LIT:\n>" . join ( r + [ "<STR_LIT>" ] ) <EOL> def save ( self , filename = None , ignore_discard = False , ignore_expires = False ) : <EOL> if filename is None : <EOL> if self . filename is not None : filename = self . filename <EOL> else : raise ValueError ( MISSING_FILENAME_TEXT ) <EOL> f = open ( filename , "<STR_LIT:w>" ) <EOL> try : <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( self . as_lwp_str ( ignore_discard , ignore_expires ) ) <EOL> finally : <EOL> f . close ( ) <EOL> def _really_load ( self , f , filename , ignore_discard , ignore_expires ) : <EOL> magic = f . readline ( ) <EOL> if not re . search ( self . magic_re , magic ) : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT:file>" % filename ) <EOL> raise LoadError ( msg ) <EOL> now = time . time ( ) <EOL> header = "<STR_LIT>" <EOL> boolean_attrs = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> value_attrs = ( "<STR_LIT:version>" , <EOL> "<STR_LIT:port>" , "<STR_LIT:path>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> line = f . readline ( ) <EOL> if line == "<STR_LIT>" : break <EOL> if not line . startswith ( header ) : <EOL> continue <EOL> line = line [ len ( header ) : ] . strip ( ) <EOL> for data in split_header_words ( [ line ] ) : <EOL> name , value = data [ <NUM_LIT:0> ] <EOL> standard = { } <EOL> rest = { } <EOL> for k in boolean_attrs : <EOL> standard [ k ] = False <EOL> for k , v in data [ <NUM_LIT:1> : ] : <EOL> if k is not None : <EOL> lc = k . lower ( ) <EOL> else : <EOL> lc = None <EOL> if ( lc in value_attrs ) or ( lc in boolean_attrs ) : <EOL> k = lc <EOL> if k in boolean_attrs : <EOL> if v is None : v = True <EOL> standard [ k ] = v <EOL> elif k in value_attrs : <EOL> standard [ k ] = v <EOL> else : <EOL> rest [ k ] = v <EOL> h = standard . get <EOL> expires = h ( "<STR_LIT>" ) <EOL> discard = h ( "<STR_LIT>" ) <EOL> if expires is not None : <EOL> expires = iso2time ( expires ) <EOL> if expires is None : <EOL> discard = True <EOL> domain = h ( "<STR_LIT>" ) <EOL> domain_specified = domain . startswith ( "<STR_LIT:.>" ) <EOL> c = Cookie ( h ( "<STR_LIT:version>" ) , name , value , <EOL> h ( "<STR_LIT:port>" ) , h ( "<STR_LIT>" ) , <EOL> domain , domain_specified , h ( "<STR_LIT>" ) , <EOL> h ( "<STR_LIT:path>" ) , h ( "<STR_LIT>" ) , <EOL> h ( "<STR_LIT>" ) , <EOL> expires , <EOL> discard , <EOL> h ( "<STR_LIT>" ) , <EOL> h ( "<STR_LIT>" ) , <EOL> rest ) <EOL> if not ignore_discard and c . discard : <EOL> continue <EOL> if not ignore_expires and c . is_expired ( now ) : <EOL> continue <EOL> self . set_cookie ( c ) <EOL> except IOError : <EOL> raise <EOL> except Exception : <EOL> _warn_unhandled_exception ( ) <EOL> raise LoadError ( "<STR_LIT>" % <EOL> ( filename , line ) ) </s>
