<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import re <EOL> import datetime <EOL> import logging <EOL> import sys <EOL> from unicodedata import normalize <EOL> if sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> from urllib . parse import ( <EOL> quote as _default_quote , unquote as _default_unquote ) <EOL> basestring = str <EOL> long = int <EOL> else : <EOL> from urllib import ( <EOL> quote as _default_quote , unquote as _default_unquote ) <EOL> def _total_seconds ( td ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( td , "<STR_LIT>" ) : <EOL> return td . total_seconds ( ) <EOL> return td . days * <NUM_LIT> * <NUM_LIT> + td . seconds + td . microseconds / <NUM_LIT> <EOL> default_cookie_quote = lambda item : _default_quote ( <EOL> item , safe = '<STR_LIT>' ) <EOL> default_extension_quote = lambda item : _default_quote ( <EOL> item , safe = '<STR_LIT>' ) <EOL> default_unquote = _default_unquote <EOL> def _report_invalid_cookie ( data ) : <EOL> "<STR_LIT>" <EOL> logging . error ( "<STR_LIT>" , data ) <EOL> def _report_unknown_attribute ( name ) : <EOL> "<STR_LIT>" <EOL> logging . error ( "<STR_LIT>" , name ) <EOL> def _report_invalid_attribute ( name , value , reason ) : <EOL> "<STR_LIT>" <EOL> logging . error ( "<STR_LIT>" , reason , name , value ) <EOL> class CookieError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> Exception . __init__ ( self ) <EOL> class InvalidCookieError ( CookieError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None , message = "<STR_LIT>" ) : <EOL> CookieError . __init__ ( self ) <EOL> self . data = data <EOL> self . message = message <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . message , self . data ) <EOL> class InvalidCookieAttributeError ( CookieError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value , reason = None ) : <EOL> CookieError . __init__ ( self ) <EOL> self . name = name <EOL> self . value = value <EOL> self . reason = reason <EOL> def __str__ ( self ) : <EOL> prefix = ( "<STR_LIT>" % self . reason ) if self . reason else "<STR_LIT>" <EOL> if self . name is None : <EOL> return '<STR_LIT>' % ( prefix , self . value ) <EOL> return '<STR_LIT>' % ( prefix , self . name , self . value ) <EOL> class Definitions ( object ) : <EOL> """<STR_LIT>""" <EOL> COOKIE_NAME = r"<STR_LIT>" <EOL> COOKIE_OCTET = r"<STR_LIT>" <EOL> EXTENSION_AV = """<STR_LIT>""" <EOL> SET_COOKIE_HEADER = """<STR_LIT>""" . format ( name = COOKIE_NAME , cookie_av = EXTENSION_AV + "<STR_LIT:;>" , <EOL> cookie_octet = COOKIE_OCTET , value = "<STR_LIT>" ) <EOL> MAX_AGE_AV = "<STR_LIT>" <EOL> LABEL = '<STR_LIT>' . format ( <EOL> let_dig = "<STR_LIT>" , let_dig_hyp = "<STR_LIT>" ) <EOL> DOMAIN = "<STR_LIT>" . format ( label = LABEL ) <EOL> DOMAIN_AV = "<STR_LIT>" . format ( domain = DOMAIN ) <EOL> PATH_AV = '<STR_LIT>' % EXTENSION_AV <EOL> month_list = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> month_abbr_list = [ item [ : <NUM_LIT:3> ] for item in month_list ] <EOL> month_numbers = { } <EOL> for index , name in enumerate ( month_list ) : <EOL> name = name . lower ( ) <EOL> month_numbers [ name [ : <NUM_LIT:3> ] ] = index + <NUM_LIT:1> <EOL> month_numbers [ name ] = index + <NUM_LIT:1> <EOL> MONTH_SHORT = "<STR_LIT>" + "<STR_LIT:|>" . join ( item [ : <NUM_LIT:3> ] for item in month_list ) + "<STR_LIT:)>" <EOL> MONTH_LONG = "<STR_LIT>" + "<STR_LIT:|>" . join ( item for item in month_list ) + "<STR_LIT:)>" <EOL> weekday_list = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> weekday_abbr_list = [ item [ : <NUM_LIT:3> ] for item in weekday_list ] <EOL> WEEKDAY_SHORT = "<STR_LIT>" + "<STR_LIT:|>" . join ( item [ : <NUM_LIT:3> ] for item in weekday_list ) + "<STR_LIT:)>" <EOL> WEEKDAY_LONG = "<STR_LIT>" + "<STR_LIT:|>" . join ( item for item in weekday_list ) + "<STR_LIT:)>" <EOL> DAY_OF_MONTH = "<STR_LIT>" <EOL> DATE = """<STR_LIT>""" <EOL> DATE = DATE . format ( wdy = WEEKDAY_SHORT , weekday = WEEKDAY_LONG , <EOL> day = DAY_OF_MONTH , mon = MONTH_SHORT , month = MONTH_LONG ) <EOL> EXPIRES_AV = "<STR_LIT>" % DATE <EOL> ATTR = """<STR_LIT>""" . format ( expires = EXPIRES_AV , max_age = MAX_AGE_AV , domain = DOMAIN_AV , <EOL> path = PATH_AV , stuff = EXTENSION_AV ) <EOL> COOKIE = """<STR_LIT>""" . format ( name = COOKIE_NAME , value = COOKIE_OCTET ) <EOL> COOKIE_NAME_RE = re . compile ( "<STR_LIT>" % COOKIE_NAME ) <EOL> COOKIE_RE = re . compile ( COOKIE ) <EOL> SET_COOKIE_HEADER_RE = re . compile ( SET_COOKIE_HEADER ) <EOL> ATTR_RE = re . compile ( ATTR ) <EOL> DATE_RE = re . compile ( DATE ) <EOL> DOMAIN_RE = re . compile ( DOMAIN ) <EOL> PATH_RE = re . compile ( '<STR_LIT>' % EXTENSION_AV ) <EOL> EOL = re . compile ( "<STR_LIT>" ) <EOL> def strip_spaces_and_quotes ( value ) : <EOL> """<STR_LIT>""" <EOL> value = value . strip ( ) if value else "<STR_LIT>" <EOL> if value and len ( value ) > <NUM_LIT:1> and ( value [ <NUM_LIT:0> ] == value [ - <NUM_LIT:1> ] == '<STR_LIT:">' ) : <EOL> value = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if not value : <EOL> value = "<STR_LIT>" <EOL> return value <EOL> def parse_string ( data , unquote = default_unquote ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return None <EOL> if isinstance ( data , bytes ) : <EOL> if sys . version_info > ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> data = data . decode ( '<STR_LIT:ascii>' ) <EOL> unquoted = unquote ( data ) <EOL> if isinstance ( unquoted , bytes ) : <EOL> try : <EOL> unquoted = unquoted . decode ( '<STR_LIT:utf-8>' ) <EOL> except : <EOL> unquoted = '<STR_LIT>' <EOL> return unquoted <EOL> def parse_date ( value ) : <EOL> """<STR_LIT>""" <EOL> match = Definitions . DATE_RE . match ( value ) if value else None <EOL> if not match : <EOL> return None <EOL> data = { } <EOL> captured = match . groupdict ( ) <EOL> fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if captured [ '<STR_LIT>' ] : <EOL> for field in fields : <EOL> data [ field ] = captured [ field ] <EOL> else : <EOL> for field in fields : <EOL> data [ field ] = captured [ field + "<STR_LIT:2>" ] <EOL> year = data [ '<STR_LIT>' ] <EOL> if len ( year ) == <NUM_LIT:2> : <EOL> if int ( year ) < <NUM_LIT> : <EOL> year = "<STR_LIT>" + year <EOL> else : <EOL> year = "<STR_LIT>" + year <EOL> year = int ( year ) <EOL> data [ '<STR_LIT>' ] = max ( <NUM_LIT> , min ( year , <NUM_LIT> ) ) <EOL> for field in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if data [ field ] is None : <EOL> data [ field ] = <NUM_LIT:0> <EOL> data [ field ] = int ( data [ field ] ) <EOL> data [ '<STR_LIT>' ] = Definitions . month_numbers [ data [ '<STR_LIT>' ] . lower ( ) ] <EOL> return datetime . datetime ( ** data ) <EOL> def parse_domain ( value ) : <EOL> """<STR_LIT>""" <EOL> value = strip_spaces_and_quotes ( value ) <EOL> if value and value [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> value = value [ <NUM_LIT:1> : ] <EOL> if value : <EOL> assert valid_domain ( value ) <EOL> return value <EOL> def parse_path ( value ) : <EOL> """<STR_LIT>""" <EOL> value = strip_spaces_and_quotes ( value ) <EOL> assert valid_path ( value ) <EOL> return value <EOL> def parse_value ( value , allow_spaces = True , unquote = default_unquote ) : <EOL> "<STR_LIT>" <EOL> if value is None : <EOL> return None <EOL> value = strip_spaces_and_quotes ( value ) <EOL> value = parse_string ( value , unquote = unquote ) <EOL> if not allow_spaces : <EOL> assert '<STR_LIT:U+0020>' not in value <EOL> return value <EOL> def valid_name ( name ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( name , bytes ) : <EOL> name = name . decode ( '<STR_LIT:ascii>' ) <EOL> if not Definitions . COOKIE_NAME_RE . match ( name ) : <EOL> return False <EOL> if name [ <NUM_LIT:0> ] == "<STR_LIT:$>" : <EOL> return False <EOL> return True <EOL> def valid_value ( value , quote = default_cookie_quote , unquote = default_unquote ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return False <EOL> encoded = encode_cookie_value ( value , quote = quote ) <EOL> decoded = parse_string ( encoded , unquote = unquote ) <EOL> decoded_normalized = ( normalize ( "<STR_LIT>" , decoded ) <EOL> if not isinstance ( decoded , bytes ) else decoded ) <EOL> value_normalized = ( normalize ( "<STR_LIT>" , value ) <EOL> if not isinstance ( value , bytes ) else value ) <EOL> if decoded_normalized == value_normalized : <EOL> return True <EOL> return False <EOL> def valid_date ( date ) : <EOL> "<STR_LIT>" <EOL> if not hasattr ( date , '<STR_LIT>' ) : <EOL> return False <EOL> if date . tzinfo is None or _total_seconds ( date . utcoffset ( ) ) < <NUM_LIT> : <EOL> return True <EOL> return False <EOL> def valid_domain ( domain ) : <EOL> "<STR_LIT>" <EOL> domain . encode ( '<STR_LIT:ascii>' ) <EOL> if domain and domain [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> return False <EOL> if Definitions . DOMAIN_RE . match ( domain ) : <EOL> return True <EOL> return False <EOL> def valid_path ( value ) : <EOL> "<STR_LIT>" <EOL> value . encode ( "<STR_LIT:ascii>" ) <EOL> if not ( value and value [ <NUM_LIT:0> ] == "<STR_LIT:/>" ) : <EOL> return False <EOL> if not Definitions . PATH_RE . match ( value ) : <EOL> return False <EOL> return True <EOL> def valid_max_age ( number ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( number , basestring ) : <EOL> try : <EOL> number = long ( number ) <EOL> except ( ValueError , TypeError ) : <EOL> return False <EOL> if number >= <NUM_LIT:0> and number % <NUM_LIT:1> == <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def encode_cookie_value ( data , quote = default_cookie_quote ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return None <EOL> if not isinstance ( data , bytes ) : <EOL> data = data . encode ( "<STR_LIT:utf-8>" ) <EOL> quoted = quote ( data ) <EOL> return quoted <EOL> def encode_extension_av ( data , quote = default_extension_quote ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> return '<STR_LIT>' <EOL> return quote ( data ) <EOL> def render_date ( date ) : <EOL> """<STR_LIT>""" <EOL> if not date : <EOL> return None <EOL> assert valid_date ( date ) <EOL> weekday = Definitions . weekday_abbr_list [ date . weekday ( ) ] <EOL> month = Definitions . month_abbr_list [ date . month - <NUM_LIT:1> ] <EOL> return date . strftime ( "<STR_LIT>" <EOL> ) . format ( day = weekday , month = month ) <EOL> def _parse_request ( header_data , ignore_bad_cookies = False ) : <EOL> """<STR_LIT>""" <EOL> cookies_dict = { } <EOL> for line in Definitions . EOL . split ( header_data . strip ( ) ) : <EOL> matches = Definitions . COOKIE_RE . finditer ( line ) <EOL> matches = [ item for item in matches ] <EOL> for match in matches : <EOL> invalid = match . group ( '<STR_LIT>' ) <EOL> if invalid : <EOL> if not ignore_bad_cookies : <EOL> raise InvalidCookieError ( data = invalid ) <EOL> _report_invalid_cookie ( invalid ) <EOL> continue <EOL> name = match . group ( '<STR_LIT:name>' ) <EOL> values = cookies_dict . get ( name ) <EOL> value = match . group ( '<STR_LIT:value>' ) . strip ( '<STR_LIT:">' ) <EOL> if values : <EOL> values . append ( value ) <EOL> else : <EOL> cookies_dict [ name ] = [ value ] <EOL> if not matches : <EOL> if not ignore_bad_cookies : <EOL> raise InvalidCookieError ( data = line ) <EOL> _report_invalid_cookie ( line ) <EOL> return cookies_dict <EOL> def parse_one_response ( line , ignore_bad_cookies = False , <EOL> ignore_bad_attributes = True ) : <EOL> """<STR_LIT>""" <EOL> cookie_dict = { } <EOL> match = Definitions . SET_COOKIE_HEADER_RE . match ( line ) <EOL> if not match : <EOL> if not ignore_bad_cookies : <EOL> raise InvalidCookieError ( data = line ) <EOL> _report_invalid_cookie ( line ) <EOL> return None <EOL> cookie_dict . update ( { <EOL> '<STR_LIT:name>' : match . group ( '<STR_LIT:name>' ) , <EOL> '<STR_LIT:value>' : match . group ( '<STR_LIT:value>' ) } ) <EOL> for match in Definitions . ATTR_RE . finditer ( match . group ( '<STR_LIT>' ) ) : <EOL> captured = dict ( ( k , v ) for ( k , v ) in match . groupdict ( ) . items ( ) if v ) <EOL> unrecognized = captured . get ( '<STR_LIT>' , None ) <EOL> if unrecognized : <EOL> if not ignore_bad_attributes : <EOL> raise InvalidCookieAttributeError ( None , unrecognized , <EOL> "<STR_LIT>" ) <EOL> _report_unknown_attribute ( unrecognized ) <EOL> continue <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if captured . get ( key ) : <EOL> captured [ key ] = True <EOL> timekeys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if '<STR_LIT>' in captured : <EOL> for key in timekeys : <EOL> del captured [ key ] <EOL> elif '<STR_LIT>' in captured : <EOL> for key in timekeys : <EOL> del captured [ key + "<STR_LIT:2>" ] <EOL> cookie_dict . update ( captured ) <EOL> return cookie_dict <EOL> def _parse_response ( header_data , ignore_bad_cookies = False , <EOL> ignore_bad_attributes = True ) : <EOL> """<STR_LIT>""" <EOL> cookie_dicts = [ ] <EOL> for line in Definitions . EOL . split ( header_data . strip ( ) ) : <EOL> if not line : <EOL> break <EOL> cookie_dict = parse_one_response ( <EOL> line , ignore_bad_cookies = ignore_bad_cookies , <EOL> ignore_bad_attributes = ignore_bad_attributes ) <EOL> if not cookie_dict : <EOL> continue <EOL> cookie_dicts . append ( cookie_dict ) <EOL> if not cookie_dicts : <EOL> if not ignore_bad_cookies : <EOL> raise InvalidCookieError ( data = header_data ) <EOL> _report_invalid_cookie ( header_data ) <EOL> return cookie_dicts <EOL> class Cookie ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value , ** kwargs ) : <EOL> try : <EOL> self . name = name <EOL> except InvalidCookieAttributeError : <EOL> raise InvalidCookieError ( message = "<STR_LIT>" ) <EOL> self . value = value or '<STR_LIT>' <EOL> if kwargs : <EOL> self . _set_attributes ( kwargs , ignore_bad_attributes = False ) <EOL> def _set_attributes ( self , attrs , ignore_bad_attributes = False ) : <EOL> for attr_name , attr_value in attrs . items ( ) : <EOL> if not attr_name in self . attribute_names : <EOL> if not ignore_bad_attributes : <EOL> raise InvalidCookieAttributeError ( <EOL> attr_name , attr_value , <EOL> "<STR_LIT>" % attr_name ) <EOL> _report_unknown_attribute ( attr_name ) <EOL> try : <EOL> setattr ( self , attr_name , attr_value ) <EOL> except InvalidCookieAttributeError as error : <EOL> if not ignore_bad_attributes : <EOL> raise <EOL> _report_invalid_attribute ( attr_name , attr_value , error . reason ) <EOL> continue <EOL> @ classmethod <EOL> def from_dict ( cls , cookie_dict , ignore_bad_attributes = True ) : <EOL> """<STR_LIT>""" <EOL> name = cookie_dict . get ( '<STR_LIT:name>' , None ) <EOL> if not name : <EOL> raise InvalidCookieError ( "<STR_LIT>" ) <EOL> raw_value = cookie_dict . get ( '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> value = cls . attribute_parsers [ '<STR_LIT:value>' ] ( raw_value ) <EOL> cookie = Cookie ( name , value ) <EOL> parsed = { } <EOL> for key , value in cookie_dict . items ( ) : <EOL> if key in ( '<STR_LIT:name>' , '<STR_LIT:value>' ) : <EOL> continue <EOL> parser = cls . attribute_parsers . get ( key ) <EOL> if not parser : <EOL> if not ignore_bad_attributes : <EOL> raise InvalidCookieAttributeError ( <EOL> key , value , "<STR_LIT>" % key ) <EOL> _report_unknown_attribute ( key ) <EOL> continue <EOL> try : <EOL> parsed_value = parser ( value ) <EOL> except Exception as e : <EOL> reason = "<STR_LIT>" % ( parser , e ) <EOL> if not ignore_bad_attributes : <EOL> raise InvalidCookieAttributeError ( <EOL> key , value , reason ) <EOL> _report_invalid_attribute ( key , value , reason ) <EOL> parsed_value = '<STR_LIT>' <EOL> parsed [ key ] = parsed_value <EOL> cookie . _set_attributes ( parsed , ignore_bad_attributes ) <EOL> return cookie <EOL> @ classmethod <EOL> def from_string ( cls , line , ignore_bad_cookies = False , <EOL> ignore_bad_attributes = True ) : <EOL> "<STR_LIT>" <EOL> cookie_dict = parse_one_response ( <EOL> line , ignore_bad_cookies = ignore_bad_cookies , <EOL> ignore_bad_attributes = ignore_bad_attributes ) <EOL> if not cookie_dict : <EOL> return None <EOL> return cls . from_dict ( <EOL> cookie_dict , ignore_bad_attributes = ignore_bad_attributes ) <EOL> def to_dict ( self ) : <EOL> this_dict = { '<STR_LIT:name>' : self . name , '<STR_LIT:value>' : self . value } <EOL> this_dict . update ( self . attributes ( ) ) <EOL> return this_dict <EOL> def validate ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> validator = self . attribute_validators . get ( name , None ) <EOL> if validator : <EOL> return True if validator ( value ) else False <EOL> return True <EOL> def __setattr__ ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> if name in self . attribute_names or name in ( "<STR_LIT:name>" , "<STR_LIT:value>" ) : <EOL> if name == '<STR_LIT:name>' and not value : <EOL> raise InvalidCookieError ( message = "<STR_LIT>" ) <EOL> if value is not None : <EOL> if not self . validate ( name , value ) : <EOL> pass <EOL> object . __setattr__ ( self , name , value ) <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . attribute_names : <EOL> return None <EOL> raise AttributeError ( name ) <EOL> def attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> dictionary = { } <EOL> for python_attr_name , cookie_attr_name in self . attribute_names . items ( ) : <EOL> value = getattr ( self , python_attr_name ) <EOL> renderer = self . attribute_renderers . get ( python_attr_name , None ) <EOL> if renderer : <EOL> value = renderer ( value ) <EOL> if not value : <EOL> continue <EOL> dictionary [ cookie_attr_name ] = value <EOL> return dictionary <EOL> def render_request ( self ) : <EOL> """<STR_LIT>""" <EOL> name , value = self . name , self . value <EOL> renderer = self . attribute_renderers . get ( '<STR_LIT:name>' , None ) <EOL> if renderer : <EOL> name = renderer ( name ) <EOL> renderer = self . attribute_renderers . get ( '<STR_LIT:value>' , None ) <EOL> if renderer : <EOL> value = renderer ( value ) <EOL> return '<STR_LIT>' . join ( ( name , "<STR_LIT:=>" , value ) ) <EOL> def render_response ( self ) : <EOL> """<STR_LIT>""" <EOL> name , value = self . name , self . value <EOL> renderer = self . attribute_renderers . get ( '<STR_LIT:name>' , None ) <EOL> if renderer : <EOL> name = renderer ( name ) <EOL> renderer = self . attribute_renderers . get ( '<STR_LIT:value>' , None ) <EOL> if renderer : <EOL> value = renderer ( value ) <EOL> return '<STR_LIT>' . join ( <EOL> [ '<STR_LIT>' . format ( name , value ) ] + <EOL> [ key if isinstance ( value , bool ) else '<STR_LIT:=>' . join ( ( key , value ) ) <EOL> for key , value in self . attributes ( ) . items ( ) ] <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> attrs = [ '<STR_LIT:name>' , '<STR_LIT:value>' ] + list ( self . attribute_names . keys ( ) ) <EOL> for attr in attrs : <EOL> mine = getattr ( self , attr , None ) <EOL> his = getattr ( other , attr , None ) <EOL> if isinstance ( mine , bytes ) : <EOL> mine = mine . decode ( '<STR_LIT:utf-8>' ) <EOL> if isinstance ( his , bytes ) : <EOL> his = his . decode ( '<STR_LIT:utf-8>' ) <EOL> if mine != his : <EOL> return False <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> attribute_names = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> attribute_renderers = { <EOL> '<STR_LIT:value>' : encode_cookie_value , <EOL> '<STR_LIT>' : render_date , <EOL> '<STR_LIT>' : lambda item : str ( item ) if item is not None else None , <EOL> '<STR_LIT>' : lambda item : True if item else False , <EOL> '<STR_LIT>' : lambda item : True if item else False , <EOL> '<STR_LIT>' : encode_extension_av , <EOL> '<STR_LIT:version>' : lambda item : ( str ( item ) if isinstance ( item , int ) <EOL> else encode_extension_av ( item ) ) , <EOL> } <EOL> attribute_parsers = { <EOL> '<STR_LIT:value>' : parse_value , <EOL> '<STR_LIT>' : parse_date , <EOL> '<STR_LIT>' : parse_domain , <EOL> '<STR_LIT:path>' : parse_path , <EOL> '<STR_LIT>' : lambda item : long ( strip_spaces_and_quotes ( item ) ) , <EOL> '<STR_LIT>' : parse_string , <EOL> '<STR_LIT:version>' : lambda item : int ( strip_spaces_and_quotes ( item ) ) , <EOL> '<STR_LIT>' : lambda item : True if item else False , <EOL> '<STR_LIT>' : lambda item : True if item else False , <EOL> } <EOL> attribute_validators = { <EOL> '<STR_LIT:name>' : valid_name , <EOL> '<STR_LIT:value>' : valid_value , <EOL> '<STR_LIT>' : valid_date , <EOL> '<STR_LIT>' : valid_domain , <EOL> '<STR_LIT:path>' : valid_path , <EOL> '<STR_LIT>' : valid_max_age , <EOL> '<STR_LIT>' : valid_value , <EOL> '<STR_LIT:version>' : lambda number : re . match ( "<STR_LIT>" , str ( number ) ) , <EOL> '<STR_LIT>' : lambda item : item is True or item is False , <EOL> '<STR_LIT>' : lambda item : item is True or item is False , <EOL> } <EOL> class Cookies ( dict , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> dict . __init__ ( self ) <EOL> self . all_cookies = [ ] <EOL> self . add ( * args , ** kwargs ) <EOL> def add ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for cookie in args : <EOL> self . all_cookies . append ( cookie ) <EOL> if cookie . name in self : <EOL> continue <EOL> self [ cookie . name ] = cookie <EOL> for key , value in kwargs . items ( ) : <EOL> cookie = Cookie ( key , value ) <EOL> self . all_cookies . append ( cookie ) <EOL> if key in self : <EOL> continue <EOL> self [ key ] = cookie <EOL> def get_all ( self , key ) : <EOL> return [ cookie for cookie in self . all_cookies <EOL> if cookie . name == key ] <EOL> def parse_request ( self , header_data , ignore_bad_cookies = False ) : <EOL> """<STR_LIT>""" <EOL> cookies_dict = _parse_request ( <EOL> header_data , ignore_bad_cookies = ignore_bad_cookies ) <EOL> cookie_objects = [ ] <EOL> for name , values in cookies_dict . items ( ) : <EOL> for value in values : <EOL> cookie_dict = { '<STR_LIT:name>' : name , '<STR_LIT:value>' : value } <EOL> try : <EOL> cookie = Cookie . from_dict ( cookie_dict ) <EOL> except InvalidCookieError : <EOL> if not ignore_bad_cookies : <EOL> raise <EOL> else : <EOL> cookie_objects . append ( cookie ) <EOL> try : <EOL> self . add ( * cookie_objects ) <EOL> except ( InvalidCookieError ) : <EOL> if not ignore_bad_cookies : <EOL> raise <EOL> _report_invalid_cookie ( header_data ) <EOL> return self <EOL> def parse_response ( self , header_data , ignore_bad_cookies = False , <EOL> ignore_bad_attributes = True ) : <EOL> """<STR_LIT>""" <EOL> cookie_dicts = _parse_response ( <EOL> header_data , <EOL> ignore_bad_cookies = ignore_bad_cookies , <EOL> ignore_bad_attributes = ignore_bad_attributes ) <EOL> cookie_objects = [ ] <EOL> for cookie_dict in cookie_dicts : <EOL> cookie = Cookie . from_dict ( cookie_dict ) <EOL> cookie_objects . append ( cookie ) <EOL> self . add ( * cookie_objects ) <EOL> return self <EOL> @ classmethod <EOL> def from_request ( cls , header_data , ignore_bad_cookies = False ) : <EOL> "<STR_LIT>" <EOL> cookies = Cookies ( ) <EOL> cookies . parse_request ( <EOL> header_data , ignore_bad_cookies = ignore_bad_cookies ) <EOL> return cookies <EOL> @ classmethod <EOL> def from_response ( cls , header_data , ignore_bad_cookies = False , <EOL> ignore_bad_attributes = True ) : <EOL> "<STR_LIT>" <EOL> cookies = Cookies ( ) <EOL> cookies . parse_response ( <EOL> header_data , <EOL> ignore_bad_cookies = ignore_bad_cookies , <EOL> ignore_bad_attributes = ignore_bad_attributes ) <EOL> return cookies <EOL> def render_request ( self , sort = True ) : <EOL> """<STR_LIT>""" <EOL> if not sort : <EOL> return ( "<STR_LIT>" . join ( <EOL> cookie . render_request ( ) for cookie in self . values ( ) ) ) <EOL> return ( "<STR_LIT>" . join ( sorted ( <EOL> cookie . render_request ( ) for cookie in self . values ( ) ) ) ) <EOL> def render_response ( self , sort = True ) : <EOL> """<STR_LIT>""" <EOL> rendered = [ cookie . render_response ( ) for cookie in self . values ( ) ] <EOL> return rendered if not sort else sorted ( rendered ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % ( name , cookie . value ) for <EOL> ( name , cookie ) in self . items ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( other , "<STR_LIT>" ) : <EOL> return False <EOL> try : <EOL> keys = sorted ( set ( self . keys ( ) ) | set ( other . keys ( ) ) ) <EOL> for key in keys : <EOL> if not key in self : <EOL> return False <EOL> if not key in other : <EOL> return False <EOL> if not ( self [ key ] == other [ key ] ) : <EOL> return False <EOL> except ( TypeError , KeyError ) : <EOL> raise <EOL> return True <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) </s>
