<s> """<STR_LIT>""" <EOL> from collections import deque <EOL> import cStringIO <EOL> import re <EOL> from google . net . proto2 . python . internal import type_checkers <EOL> from google . net . proto2 . python . public import descriptor <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> _INTEGER_CHECKERS = ( type_checkers . Uint32ValueChecker ( ) , <EOL> type_checkers . Int32ValueChecker ( ) , <EOL> type_checkers . Uint64ValueChecker ( ) , <EOL> type_checkers . Int64ValueChecker ( ) ) <EOL> _FLOAT_INFINITY = re . compile ( '<STR_LIT>' , re . IGNORECASE ) <EOL> _FLOAT_NAN = re . compile ( '<STR_LIT>' , re . IGNORECASE ) <EOL> class ParseError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def MessageToString ( message , as_utf8 = False , as_one_line = False , <EOL> pointy_brackets = False ) : <EOL> out = cStringIO . StringIO ( ) <EOL> PrintMessage ( message , out , as_utf8 = as_utf8 , as_one_line = as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> result = out . getvalue ( ) <EOL> out . close ( ) <EOL> if as_one_line : <EOL> return result . rstrip ( ) <EOL> return result <EOL> def PrintMessage ( message , out , indent = <NUM_LIT:0> , as_utf8 = False , as_one_line = False , <EOL> pointy_brackets = False ) : <EOL> for field , value in message . ListFields ( ) : <EOL> if field . label == descriptor . FieldDescriptor . LABEL_REPEATED : <EOL> for element in value : <EOL> PrintField ( field , element , out , indent , as_utf8 , as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> else : <EOL> PrintField ( field , value , out , indent , as_utf8 , as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> def PrintField ( field , value , out , indent = <NUM_LIT:0> , as_utf8 = False , as_one_line = False , <EOL> pointy_brackets = False ) : <EOL> """<STR_LIT>""" <EOL> out . write ( '<STR_LIT:U+0020>' * indent ) <EOL> if field . is_extension : <EOL> out . write ( '<STR_LIT:[>' ) <EOL> if ( field . containing_type . GetOptions ( ) . message_set_wire_format and <EOL> field . type == descriptor . FieldDescriptor . TYPE_MESSAGE and <EOL> field . message_type == field . extension_scope and <EOL> field . label == descriptor . FieldDescriptor . LABEL_OPTIONAL ) : <EOL> out . write ( field . message_type . full_name ) <EOL> else : <EOL> out . write ( field . full_name ) <EOL> out . write ( '<STR_LIT:]>' ) <EOL> elif field . type == descriptor . FieldDescriptor . TYPE_GROUP : <EOL> out . write ( field . message_type . name ) <EOL> else : <EOL> out . write ( field . name ) <EOL> if field . cpp_type != descriptor . FieldDescriptor . CPPTYPE_MESSAGE : <EOL> out . write ( '<STR_LIT>' ) <EOL> PrintFieldValue ( field , value , out , indent , as_utf8 , as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> if as_one_line : <EOL> out . write ( '<STR_LIT:U+0020>' ) <EOL> else : <EOL> out . write ( '<STR_LIT:\n>' ) <EOL> def PrintFieldValue ( field , value , out , indent = <NUM_LIT:0> , as_utf8 = False , <EOL> as_one_line = False , pointy_brackets = False ) : <EOL> """<STR_LIT>""" <EOL> if pointy_brackets : <EOL> openb = '<STR_LIT:<>' <EOL> closeb = '<STR_LIT:>>' <EOL> else : <EOL> openb = '<STR_LIT:{>' <EOL> closeb = '<STR_LIT:}>' <EOL> if field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_MESSAGE : <EOL> if as_one_line : <EOL> out . write ( '<STR_LIT>' % openb ) <EOL> PrintMessage ( value , out , indent , as_utf8 , as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> out . write ( closeb ) <EOL> else : <EOL> out . write ( '<STR_LIT>' % openb ) <EOL> PrintMessage ( value , out , indent + <NUM_LIT:2> , as_utf8 , as_one_line , <EOL> pointy_brackets = pointy_brackets ) <EOL> out . write ( '<STR_LIT:U+0020>' * indent + closeb ) <EOL> elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_ENUM : <EOL> enum_value = field . enum_type . values_by_number . get ( value , None ) <EOL> if enum_value is not None : <EOL> out . write ( enum_value . name ) <EOL> else : <EOL> out . write ( str ( value ) ) <EOL> elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_STRING : <EOL> out . write ( '<STR_LIT>' ) <EOL> if type ( value ) is unicode : <EOL> out_value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> out_value = value <EOL> if field . type == descriptor . FieldDescriptor . TYPE_BYTES : <EOL> out_as_utf8 = False <EOL> else : <EOL> out_as_utf8 = as_utf8 <EOL> out . write ( _CEscape ( out_value , out_as_utf8 ) ) <EOL> out . write ( '<STR_LIT>' ) <EOL> elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_BOOL : <EOL> if value : <EOL> out . write ( '<STR_LIT:true>' ) <EOL> else : <EOL> out . write ( '<STR_LIT:false>' ) <EOL> else : <EOL> out . write ( str ( value ) ) <EOL> def _ParseOrMerge ( text , message , allow_multiple_scalars ) : <EOL> """<STR_LIT>""" <EOL> tokenizer = _Tokenizer ( text ) <EOL> while not tokenizer . AtEnd ( ) : <EOL> _MergeField ( tokenizer , message , allow_multiple_scalars ) <EOL> def Parse ( text , message ) : <EOL> """<STR_LIT>""" <EOL> _ParseOrMerge ( text , message , False ) <EOL> def Merge ( text , message ) : <EOL> """<STR_LIT>""" <EOL> _ParseOrMerge ( text , message , True ) <EOL> def _MergeField ( tokenizer , message , allow_multiple_scalars ) : <EOL> """<STR_LIT>""" <EOL> message_descriptor = message . DESCRIPTOR <EOL> if tokenizer . TryConsume ( '<STR_LIT:[>' ) : <EOL> name = [ tokenizer . ConsumeIdentifier ( ) ] <EOL> while tokenizer . TryConsume ( '<STR_LIT:.>' ) : <EOL> name . append ( tokenizer . ConsumeIdentifier ( ) ) <EOL> name = '<STR_LIT:.>' . join ( name ) <EOL> if not message_descriptor . is_extendable : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % <EOL> message_descriptor . full_name ) <EOL> field = message . Extensions . _FindExtensionByName ( name ) <EOL> if not field : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % name ) <EOL> elif message_descriptor != field . containing_type : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % ( <EOL> name , message_descriptor . full_name ) ) <EOL> tokenizer . Consume ( '<STR_LIT:]>' ) <EOL> else : <EOL> name = tokenizer . ConsumeIdentifier ( ) <EOL> field = message_descriptor . fields_by_name . get ( name , None ) <EOL> if not field : <EOL> field = message_descriptor . fields_by_name . get ( name . lower ( ) , None ) <EOL> if field and field . type != descriptor . FieldDescriptor . TYPE_GROUP : <EOL> field = None <EOL> if ( field and field . type == descriptor . FieldDescriptor . TYPE_GROUP and <EOL> field . message_type . name != name ) : <EOL> field = None <EOL> if not field : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % ( <EOL> message_descriptor . full_name , name ) ) <EOL> if field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_MESSAGE : <EOL> tokenizer . TryConsume ( '<STR_LIT::>' ) <EOL> if tokenizer . TryConsume ( '<STR_LIT:<>' ) : <EOL> end_token = '<STR_LIT:>>' <EOL> else : <EOL> tokenizer . Consume ( '<STR_LIT:{>' ) <EOL> end_token = '<STR_LIT:}>' <EOL> if field . label == descriptor . FieldDescriptor . LABEL_REPEATED : <EOL> if field . is_extension : <EOL> sub_message = message . Extensions [ field ] . add ( ) <EOL> else : <EOL> sub_message = getattr ( message , field . name ) . add ( ) <EOL> else : <EOL> if field . is_extension : <EOL> sub_message = message . Extensions [ field ] <EOL> else : <EOL> sub_message = getattr ( message , field . name ) <EOL> sub_message . SetInParent ( ) <EOL> while not tokenizer . TryConsume ( end_token ) : <EOL> if tokenizer . AtEnd ( ) : <EOL> raise tokenizer . ParseErrorPreviousToken ( '<STR_LIT>' % ( end_token ) ) <EOL> _MergeField ( tokenizer , sub_message , allow_multiple_scalars ) <EOL> else : <EOL> _MergeScalarField ( tokenizer , message , field , allow_multiple_scalars ) <EOL> if not tokenizer . TryConsume ( '<STR_LIT:U+002C>' ) : <EOL> tokenizer . TryConsume ( '<STR_LIT:;>' ) <EOL> def _MergeScalarField ( tokenizer , message , field , allow_multiple_scalars ) : <EOL> """<STR_LIT>""" <EOL> tokenizer . Consume ( '<STR_LIT::>' ) <EOL> value = None <EOL> if field . type in ( descriptor . FieldDescriptor . TYPE_INT32 , <EOL> descriptor . FieldDescriptor . TYPE_SINT32 , <EOL> descriptor . FieldDescriptor . TYPE_SFIXED32 ) : <EOL> value = tokenizer . ConsumeInt32 ( ) <EOL> elif field . type in ( descriptor . FieldDescriptor . TYPE_INT64 , <EOL> descriptor . FieldDescriptor . TYPE_SINT64 , <EOL> descriptor . FieldDescriptor . TYPE_SFIXED64 ) : <EOL> value = tokenizer . ConsumeInt64 ( ) <EOL> elif field . type in ( descriptor . FieldDescriptor . TYPE_UINT32 , <EOL> descriptor . FieldDescriptor . TYPE_FIXED32 ) : <EOL> value = tokenizer . ConsumeUint32 ( ) <EOL> elif field . type in ( descriptor . FieldDescriptor . TYPE_UINT64 , <EOL> descriptor . FieldDescriptor . TYPE_FIXED64 ) : <EOL> value = tokenizer . ConsumeUint64 ( ) <EOL> elif field . type in ( descriptor . FieldDescriptor . TYPE_FLOAT , <EOL> descriptor . FieldDescriptor . TYPE_DOUBLE ) : <EOL> value = tokenizer . ConsumeFloat ( ) <EOL> elif field . type == descriptor . FieldDescriptor . TYPE_BOOL : <EOL> value = tokenizer . ConsumeBool ( ) <EOL> elif field . type == descriptor . FieldDescriptor . TYPE_STRING : <EOL> value = tokenizer . ConsumeString ( ) <EOL> elif field . type == descriptor . FieldDescriptor . TYPE_BYTES : <EOL> value = tokenizer . ConsumeByteString ( ) <EOL> elif field . type == descriptor . FieldDescriptor . TYPE_ENUM : <EOL> value = tokenizer . ConsumeEnum ( field ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' % field . type ) <EOL> if field . label == descriptor . FieldDescriptor . LABEL_REPEATED : <EOL> if field . is_extension : <EOL> message . Extensions [ field ] . append ( value ) <EOL> else : <EOL> getattr ( message , field . name ) . append ( value ) <EOL> else : <EOL> if field . is_extension : <EOL> if not allow_multiple_scalars and message . HasExtension ( field ) : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % <EOL> ( message . DESCRIPTOR . full_name , field . full_name ) ) <EOL> else : <EOL> message . Extensions [ field ] = value <EOL> else : <EOL> if not allow_multiple_scalars and message . HasField ( field . name ) : <EOL> raise tokenizer . ParseErrorPreviousToken ( <EOL> '<STR_LIT>' % <EOL> ( message . DESCRIPTOR . full_name , field . name ) ) <EOL> else : <EOL> setattr ( message , field . name , value ) <EOL> class _Tokenizer ( object ) : <EOL> """<STR_LIT>""" <EOL> _WHITESPACE = re . compile ( '<STR_LIT>' , re . MULTILINE ) <EOL> _TOKEN = re . compile ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _IDENTIFIER = re . compile ( r'<STR_LIT>' ) <EOL> def __init__ ( self , text_message ) : <EOL> self . _text_message = text_message <EOL> self . _position = <NUM_LIT:0> <EOL> self . _line = - <NUM_LIT:1> <EOL> self . _column = <NUM_LIT:0> <EOL> self . _token_start = None <EOL> self . token = '<STR_LIT>' <EOL> self . _lines = deque ( text_message . split ( '<STR_LIT:\n>' ) ) <EOL> self . _current_line = '<STR_LIT>' <EOL> self . _previous_line = <NUM_LIT:0> <EOL> self . _previous_column = <NUM_LIT:0> <EOL> self . _SkipWhitespace ( ) <EOL> self . NextToken ( ) <EOL> def AtEnd ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . token <EOL> def _PopLine ( self ) : <EOL> while len ( self . _current_line ) <= self . _column : <EOL> if not self . _lines : <EOL> self . _current_line = '<STR_LIT>' <EOL> return <EOL> self . _line += <NUM_LIT:1> <EOL> self . _column = <NUM_LIT:0> <EOL> self . _current_line = self . _lines . popleft ( ) <EOL> def _SkipWhitespace ( self ) : <EOL> while True : <EOL> self . _PopLine ( ) <EOL> match = self . _WHITESPACE . match ( self . _current_line , self . _column ) <EOL> if not match : <EOL> break <EOL> length = len ( match . group ( <NUM_LIT:0> ) ) <EOL> self . _column += length <EOL> def TryConsume ( self , token ) : <EOL> """<STR_LIT>""" <EOL> if self . token == token : <EOL> self . NextToken ( ) <EOL> return True <EOL> return False <EOL> def Consume ( self , token ) : <EOL> """<STR_LIT>""" <EOL> if not self . TryConsume ( token ) : <EOL> raise self . _ParseError ( '<STR_LIT>' % token ) <EOL> def ConsumeIdentifier ( self ) : <EOL> """<STR_LIT>""" <EOL> result = self . token <EOL> if not self . _IDENTIFIER . match ( result ) : <EOL> raise self . _ParseError ( '<STR_LIT>' ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeInt32 ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseInteger ( self . token , is_signed = True , is_long = False ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeUint32 ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseInteger ( self . token , is_signed = False , is_long = False ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeInt64 ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseInteger ( self . token , is_signed = True , is_long = True ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeUint64 ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseInteger ( self . token , is_signed = False , is_long = True ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeFloat ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseFloat ( self . token ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeBool ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = ParseBool ( self . token ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeString ( self ) : <EOL> """<STR_LIT>""" <EOL> the_bytes = self . ConsumeByteString ( ) <EOL> try : <EOL> return unicode ( the_bytes , '<STR_LIT:utf-8>' ) <EOL> except UnicodeDecodeError , e : <EOL> raise self . _StringParseError ( e ) <EOL> def ConsumeByteString ( self ) : <EOL> """<STR_LIT>""" <EOL> the_list = [ self . _ConsumeSingleByteString ( ) ] <EOL> while self . token and self . token [ <NUM_LIT:0> ] in ( '<STR_LIT>' , '<STR_LIT:">' ) : <EOL> the_list . append ( self . _ConsumeSingleByteString ( ) ) <EOL> return '<STR_LIT>' . join ( the_list ) <EOL> def _ConsumeSingleByteString ( self ) : <EOL> """<STR_LIT>""" <EOL> text = self . token <EOL> if len ( text ) < <NUM_LIT:1> or text [ <NUM_LIT:0> ] not in ( '<STR_LIT>' , '<STR_LIT:">' ) : <EOL> raise self . _ParseError ( '<STR_LIT>' ) <EOL> if len ( text ) < <NUM_LIT:2> or text [ - <NUM_LIT:1> ] != text [ <NUM_LIT:0> ] : <EOL> raise self . _ParseError ( '<STR_LIT>' ) <EOL> try : <EOL> result = _CUnescape ( text [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ConsumeEnum ( self , field ) : <EOL> try : <EOL> result = ParseEnum ( field , self . token ) <EOL> except ValueError , e : <EOL> raise self . _ParseError ( str ( e ) ) <EOL> self . NextToken ( ) <EOL> return result <EOL> def ParseErrorPreviousToken ( self , message ) : <EOL> """<STR_LIT>""" <EOL> return ParseError ( '<STR_LIT>' % ( <EOL> self . _previous_line + <NUM_LIT:1> , self . _previous_column + <NUM_LIT:1> , message ) ) <EOL> def _ParseError ( self , message ) : <EOL> """<STR_LIT>""" <EOL> return ParseError ( '<STR_LIT>' % ( <EOL> self . _line + <NUM_LIT:1> , self . _column + <NUM_LIT:1> , message ) ) <EOL> def _StringParseError ( self , e ) : <EOL> return self . _ParseError ( '<STR_LIT>' + str ( e ) ) <EOL> def NextToken ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _previous_line = self . _line <EOL> self . _previous_column = self . _column <EOL> self . _column += len ( self . token ) <EOL> self . _SkipWhitespace ( ) <EOL> if not self . _lines and len ( self . _current_line ) <= self . _column : <EOL> self . token = '<STR_LIT>' <EOL> return <EOL> match = self . _TOKEN . match ( self . _current_line , self . _column ) <EOL> if match : <EOL> token = match . group ( <NUM_LIT:0> ) <EOL> self . token = token <EOL> else : <EOL> self . token = self . _current_line [ self . _column ] <EOL> def _CEscape ( text , as_utf8 ) : <EOL> def escape ( c ) : <EOL> o = ord ( c ) <EOL> if o == <NUM_LIT:10> : return r'<STR_LIT:\n>' <EOL> if o == <NUM_LIT> : return r'<STR_LIT:\r>' <EOL> if o == <NUM_LIT:9> : return r'<STR_LIT:\t>' <EOL> if o == <NUM_LIT> : return r"<STR_LIT>" <EOL> if o == <NUM_LIT> : return r'<STR_LIT>' <EOL> if o == <NUM_LIT> : return r'<STR_LIT:\\>' <EOL> if not as_utf8 and ( o >= <NUM_LIT> or o < <NUM_LIT:32> ) : <EOL> return r'<STR_LIT>' % o <EOL> return c <EOL> return '<STR_LIT>' . join ( [ escape ( c ) for c in text ] ) <EOL> _CUNESCAPE_HEX = re . compile ( r'<STR_LIT>' ) <EOL> def _CUnescape ( text ) : <EOL> def ReplaceHex ( m ) : <EOL> if len ( m . group ( <NUM_LIT:1> ) ) & <NUM_LIT:1> : <EOL> return m . group ( <NUM_LIT:1> ) + '<STR_LIT>' + m . group ( <NUM_LIT:2> ) <EOL> return m . group ( <NUM_LIT:0> ) <EOL> result = _CUNESCAPE_HEX . sub ( ReplaceHex , text ) <EOL> return result . decode ( '<STR_LIT>' ) <EOL> def ParseInteger ( text , is_signed = False , is_long = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = int ( text , <NUM_LIT:0> ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % text ) <EOL> checker = _INTEGER_CHECKERS [ <NUM_LIT:2> * int ( is_long ) + int ( is_signed ) ] <EOL> checker . CheckValue ( result ) <EOL> return result <EOL> def ParseFloat ( text ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return float ( text ) <EOL> except ValueError : <EOL> if _FLOAT_INFINITY . match ( text ) : <EOL> if text [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> return float ( '<STR_LIT>' ) <EOL> else : <EOL> return float ( '<STR_LIT>' ) <EOL> elif _FLOAT_NAN . match ( text ) : <EOL> return float ( '<STR_LIT>' ) <EOL> else : <EOL> try : <EOL> return float ( text . rstrip ( '<STR_LIT:f>' ) ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % text ) <EOL> def ParseBool ( text ) : <EOL> """<STR_LIT>""" <EOL> if text in ( '<STR_LIT:true>' , '<STR_LIT:t>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> elif text in ( '<STR_LIT:false>' , '<STR_LIT:f>' , '<STR_LIT:0>' ) : <EOL> return False <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def ParseEnum ( field , value ) : <EOL> """<STR_LIT>""" <EOL> enum_descriptor = field . enum_type <EOL> try : <EOL> number = int ( value , <NUM_LIT:0> ) <EOL> except ValueError : <EOL> enum_value = enum_descriptor . values_by_name . get ( value , None ) <EOL> if enum_value is None : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> enum_descriptor . full_name , value ) ) <EOL> else : <EOL> enum_value = enum_descriptor . values_by_number . get ( number , None ) <EOL> if enum_value is None : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> enum_descriptor . full_name , number ) ) <EOL> return enum_value . number </s>
