3 SLj @s dZdZddlZddlZddlZejr*eZddlm Z ddl m Z ddl m Z ddd d d gZ e je je je jfZejd ejZejd ejZee jje jjgZedOZdZGdddeZGdddeZ Gddde!Z"dPddZ#ddZ$dQddZ%dRdd Z&dSdd Z'ddZ(Gd d!d!e!Z)dTd"d#Z*dUd$d Z+dVd%d&Z,dWd'd(Z-Gd)d*d*e!Z.d+d,Z/d-d.Z0d/d0Z1d1d2Z2Gd3d4d4e!Z3e3Z4d5d6Z5d7d8Z6d9d:Z7d;d<Z8d=d>Z9d?d@Z:dXdAdBZ;dYdCdDZdIdJZ?dKdLZ@dMdNZAdS)\abContains routines for printing protocol messages in text format. Simple usage example: # Create a proto object and serialize it to a text proto string. message = my_proto_pb2.MyMessage(foo='bar') text_proto = text_format.MessageToString(message) # Parse a text proto string. message = text_format.Parse(text_proto, my_proto_pb2.MyMessage()) z kenton@google.com (Kenton Varda)N) type_checkers) descriptor) text_encodingMessageToString PrintMessage PrintFieldPrintFieldValueMergez-?inf(?:inity)?f?znanf?'"zgoogle.protobuf.Anyc@seZdZdZdS)Errorz'Top-level module error for text_format.N)__name__ __module__ __qualname____doc__rr!/usr/lib/python3.6/text_format.pyr Isr cs2eZdZdZd fdd ZddZddZZS) ParseErrorz3Thrown in case of text parsing or tokenizing error.Ncsr|dk r:|dk r:t|}|dk r.|dj|7}dj||}|dk rTtt|j|ntt|j||_||_dS)Nz:{0}z {0} : {1})strformatsuperr__init___line_column)selfmessagelinecolumnloc) __class__rrrPs zParseError.__init__cCs|jS)N)r)rrrrGetLine]szParseError.GetLinecCs|jS)N)r)rrrr GetColumn`szParseError.GetColumn)NNN)r rrrrr r! __classcell__rr)rrrMs rc@s,eZdZddZddZddZddZd S) TextWritercCs tjrtj|_n tj|_dS)N)sixPY2ioBytesIO_writerStringIO)ras_utf8rrrrfs zTextWriter.__init__cCs(tjrt|tjr|jd}|jj|S)Nzutf-8)r$r% isinstance text_typeencoder(write)rvalrrrr.ls  zTextWriter.writecCs |jjS)N)r(close)rrrrr0rszTextWriter.closecCs |jjS)N)r(getvalue)rrrrr1uszTextWriter.getvalueN)r rrrr.r0r1rrrrr#dsr#Fc CsLt|} t| ||||||||| } | j|| j} | j|rH| jS| S)aConvert protobuf message to text format. Floating point values can be formatted compactly with 15 digits of precision (which is the most that IEEE 754 "double" can guarantee) using float_format='.15g'. To ensure that converting to text and back to a proto will result in an identical value, float_format='.17g' should be used. Args: message: The protocol buffers message. as_utf8: Produce text output in UTF8 format. as_one_line: Don't introduce newlines between fields. pointy_brackets: If True, use angle brackets instead of curly braces for nesting. use_index_order: If True, print fields of a proto message using the order defined in source code instead of the field number. By default, use the field number order. float_format: If set, use this to specify floating point number formatting (per the "Format Specification Mini-Language"); otherwise, str() is used. use_field_number: If True, print field numbers instead of names. descriptor_pool: A DescriptorPool used to resolve Any types. indent: The indent level, in terms of spaces, for pretty print. message_formatter: A function(message, indent, as_one_line): unicode|None to custom format selected sub-messages (usually based on message type). Use to pretty print parts of the protobuf for easier diffing. Returns: A string of the text formatted protocol buffer message. )r#_Printerrr1r0rstrip) rr* as_one_linepointy_bracketsuse_index_order float_formatuse_field_numberdescriptor_poolindentmessage_formatteroutprinterresultrrrrys&  cCs"|jtjjko |jjo |jjjS)N)typerFieldDescriptor TYPE_MESSAGE message_typeZ has_options GetOptionsZ map_entry)fieldrrr _IsMapEntrysrEc Cs(t||||||||| | } | j|dS)N)r2r) rr<r:r*r4r5r6r7r8r9r;r=rrrrs c Cs&t|||||||| } | j||dS)z%Print a single field name/value pair.N)r2r) rDvaluer<r:r*r4r5r6r7r;r=rrrrs  c Cs&t|||||||| } | j||dS)z0Print a single field value (not including name).N)r2r) rDrFr<r:r*r4r5r6r7r;r=rrrrs  c Csd|dkrddlm}|j}ddlm}|j}y|j|}Wntk rRdSX|j|}|S)a!Returns a protobuf message instance. Args: type_name: Fully-qualified protobuf message type name string. descriptor_pool: DescriptorPool instance. Returns: A Message instance of type matching type_name, or None if the a Descriptor wasn't found matching type_name. Nr)r9)symbol_database)google.protobufr9ZDefaultrGZFindMessageTypeByNameKeyErrorZ GetPrototype)Z type_namer9Zpool_modrGZdatabasemessage_descriptorrBrrr_BuildMessageFromTypeNames    rKc @sJeZdZdZdddZddZd d Zd d Zd dZddZ ddZ dS)r2z)Text format printer for protocol message.rFNc Cs@||_||_||_||_||_||_||_||_| |_| |_ dS)aZInitialize the Printer. Floating point values can be formatted compactly with 15 digits of precision (which is the most that IEEE 754 "double" can guarantee) using float_format='.15g'. To ensure that converting to text and back to a proto will result in an identical value, float_format='.17g' should be used. Args: out: To record the text format result. indent: The indent level for pretty print. as_utf8: Produce text output in UTF8 format. as_one_line: Don't introduce newlines between fields. pointy_brackets: If True, use angle brackets instead of curly braces for nesting. use_index_order: If True, print fields of a proto message using the order defined in source code instead of the field number. By default, use the field number order. float_format: If set, use this to specify floating point number formatting (per the "Format Specification Mini-Language"); otherwise, str() is used. use_field_number: If True, print field numbers instead of names. descriptor_pool: A DescriptorPool used to resolve Any types. message_formatter: A function(message, indent, as_one_line): unicode|None to custom format selected sub-messages (usually based on message type). Use to pretty print parts of the protobuf for easier diffing. N) r<r:r*r4r5r6r7r8r9r;) rr<r:r*r4r5r6r7r8r9r;rrrrs%z_Printer.__init__cCsht|j|j}|r`|j|j|jjd|jd|jf|j ||jj|j rVdnddSdSdS)z5Serializes if message is a google.protobuf.Any field.z%s[%s]  TFN) rKZTypeNamer9MergeFromStringrFr<r.r:Ztype_url_PrintMessageFieldValuer4)rrZpacked_messagerrr_TryPrintAsAnyMessage.s  z_Printer._TryPrintAsAnyMessagecCsV|j||j|j}|dkrdS|j}|jd|j|j||j|jrLdnddS)NFrLrMT)r;r:r4r<r.)rrZ formattedr<rrr_TryCustomFormatMessage;s z _Printer._TryCustomFormatMessagecCs|jr|j|rdS|jjtkr.|j|r.dS|j}|jrL|jdddx|D]z\}}t |rxht |D]$}|j |||d}|j ||qlWqR|j tjjkrx&|D]}|j ||qWqR|j ||qRWdS)zeConvert protobuf message to text format. Args: message: The protocol buffers message. NcSs |djS)Nr)index)xrrrSsz'_Printer.PrintMessage..)key)rUrF)r;rQ DESCRIPTOR full_name_ANY_FULL_TYPE_NAMErPZ ListFieldsr6sortrEsorted GetEntryClassrlabelrr@LABEL_REPEATED)rrZfieldsrDrFrUZ entry_submsgelementrrrrFs"   z_Printer.PrintMessagecCs|j}|jd|j|jr.|jt|jn|jr|jd|jjj rv|j t j j krv|jt j jkrv|j|jjn |j|j|jdn*|j t j jkr|j|jjn |j|j|jt j jkr|jd|j|||jr|jdn |jddS)z%Print a single field name/value pair.rL[]z: rMN)r<r.r:r8rnumber is_extensioncontaining_typerCZmessage_set_wire_formatr?rr@rAr\ZLABEL_OPTIONALrBrW TYPE_GROUPnamecpp_typeCPPTYPE_MESSAGErr4)rrDrFr<rrrrds*        z_Printer.PrintFieldcCs|jrd}d}nd}d}|jrF|jjd||j||jj|nL|jjd||jd7_|j||jd8_|jjd|j|dS) N<>{}z %s z %s rL)r5r4r<r.rr:)rrFZopenbZclosebrrrrOs  z _Printer._PrintMessageFieldValuecCs<|j}|jtjjkr"|j|n|jtjjkrf|jjj |d}|dk rV|j |j n|j t |n|jtjj kr|j dt|tjr|jd}n|}|jtjjkrd}n|j}|j tj|||j dnf|jtjjkr|r|j dn |j dn<|jtkr*|jdk r*|j dj|j|n|j t |dS)zPrint a single field value (not including name). For repeated fields, the value should be a single element. Args: field: The descriptor of the field to be printed. value: The value of the field. Nr zutf-8Ftruefalsez{1:{0}})r<rfrr@rgrOZ CPPTYPE_ENUM enum_typevalues_by_numbergetr.rerZCPPTYPE_STRINGr+r$r,r-r? TYPE_BYTESr*rZCEscapeZ CPPTYPE_BOOL _FLOAT_TYPESr7r)rrDrFr< enum_valueZ out_valueZ out_as_utf8rrrrs2       z_Printer.PrintFieldValue) rFFFFNFNN) r rrrrrPrQrrrOrrrrrr2s  &  !r2cCs>t|ts&tjr|jd}n |jd}t|jd||||dS)aParses a text representation of a protocol message into a message. NOTE: for historical reasons this function does not clear the input message. This is different from what the binary msg.ParseFrom(...) does. Example a = MyProto() a.repeated_field.append('test') b = MyProto() text_format.Parse(repr(a), b) text_format.Parse(repr(a), b) # repeated_field contains ["test", "test"] # Binary version: b.ParseFromString(a.SerializeToString()) # repeated_field is now "test" Caller is responsible for clearing the message as needed. Args: text: Message text representation. message: A protocol buffer message to merge into. allow_unknown_extension: if True, skip over missing extensions and keep parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. Returns: The same message passed as argument. Raises: ParseError: On text parsing problems. zutf-8rM)r9)r+rr$PY3decoder- ParseLinessplit)textrallow_unknown_extensionallow_field_numberr9rrrParses%    r|cCs>t|ts&tjr|jd}n |jd}t|jd||||dS)a^Parses a text representation of a protocol message into a message. Like Parse(), but allows repeated values for a non-repeated field, and uses the last one. Args: text: Message text representation. message: A protocol buffer message to merge into. allow_unknown_extension: if True, skip over missing extensions and keep parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. Returns: The same message passed as argument. Raises: ParseError: On text parsing problems. zutf-8rM)r9)r+rr$rurvr- MergeLinesrx)ryrrzr{r9rrrr s   cCst|||d}|j||S)aParses a text representation of a protocol message into a message. Args: lines: An iterable of lines of a message's text representation. message: A protocol buffer message to merge into. allow_unknown_extension: if True, skip over missing extensions and keep parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. Returns: The same message passed as argument. Raises: ParseError: On text parsing problems. )r9)_Parserrw)linesrrzr{r9parserrrrrwsrwcCst|||d}|j||S)aParses a text representation of a protocol message into a message. Args: lines: An iterable of lines of a message's text representation. message: A protocol buffer message to merge into. allow_unknown_extension: if True, skip over missing extensions and keep parsing allow_field_number: if True, both field number and field name are allowed. descriptor_pool: A DescriptorPool used to resolve Any types. Returns: The same message passed as argument. Raises: ParseError: On text parsing problems. )r9)r~r})rrrzr{r9rrrrr}3sr}c@sneZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ e ddZddZdS)r~z(Text format parser for protocol message.FNcCs||_||_||_dS)N)rzr{r9)rrzr{r9rrrrQsz_Parser.__init__cCs&t|ts|jd}|j|jd|S)zBParses a text representation of a protocol message into a message.zutf-8rM)r+rrvrwrx)rryrrrrParseFromStringYs  z_Parser.ParseFromStringcCsd|_|j|||S)zBParses a text representation of a protocol message into a message.F)_allow_multiple_scalars _ParseOrMerge)rrrrrrrw_s z_Parser.ParseLinescCs|j|jd|S)zBMerges a text representation of a protocol message into a message.rM)Z _MergeLinesrx)rryrrrrrNesz_Parser.MergeFromStringcCsd|_|j|||S)zBMerges a text representation of a protocol message into a message.T)rr)rrrrrrr}is z_Parser.MergeLinescCs&t|}x|js |j||q WdS)zConverts a text representation of a protocol message into a message. Args: lines: Lines of a message's text representation. message: A protocol buffer message to merge into. Raises: ParseError: On text parsing problems. N) TokenizerAtEnd _MergeField)rrr tokenizerrrrros  z_Parser._ParseOrMergec Cs|j}|jdr|jg}x|jdr6|j|jqWdj|}|jsX|jd|j|jj |}|s|j rtd}q|jd|n||j kr|jd||jf|j dn|j }|jr|jrt|dd}|jj|d}| r|jr|jj|}nh|jj|d}|s8|jj|jd}|r8|jtjjkr8d}|r`|jtjjkr`|jj|kr`d}|sz|jd |j|f|r^|j r|jr|j|jj}|dk r||jkr|jd |j||jj|jf|jtjj kr|jd |j!}n|j d |j"}|j#tjj$krP|jdrPx8|||||jdr>P|j d q$Wn ||||nt%||jd s||jd dS)zMerges a single protocol message field into a message. Args: tokenizer: A tokenizer to parse the field name and values. message: A protocol message to record the data. Raises: ParseError: In case of text parsing problems. r_.z+Message type "%s" does not have extensions.NzExtension "%s" not registered. Did you import the _pb2 module which defines it? If you are trying to place the extension in the MessageSet field of another message that is in an Any or MessageSet field, that message's _pb2 module must be imported as wellz1Extension "%s" does not extend message type "%s".r`Tz*Message type "%s" has no field named "%s".zbField "%s" is specified along with field "%s", another member of oneof "%s" for message type "%s".:,;)&rV TryConsumeConsumeIdentifierappendjoinZ is_extendableParseErrorPreviousTokenrW ExtensionsZ_FindExtensionByNamerzrcConsumeConsumeIdentifierOrNumberr{isdigit ParseIntegerZfields_by_numberrqZ_FindExtensionByNumberfields_by_namelowerr?rr@rdrBrerZcontaining_oneofZ WhichOneofrfrg_MergeMessageField_MergeScalarFieldr\r]_SkipFieldContents) rrrrJrerDraZ which_oneofZmergerrrrr}s                    z_Parser._MergeFieldcCs|jg}|jd|j|j|jd|j|j|jd|jg}x|jdrj|j|jqPWdj|dj|fS)zBConsumes a google.protobuf.Any type URL and returns the type name.r/)rrrrr)rrprefixrerrr_ConsumeAnyTypeUrls      z_Parser._ConsumeAnyTypeUrlcCst|}|jdrd}n|jdd}|jjtko:|jdr |j|\}}|jd|jd|jdrpd}n|jdd}t||j} | st d|x2|j|s|j r|j d |f|j || qW|j tjjkrt||jj} n t||j} | j| |d n||j tjjkr^|jr2|j|j} n*|rLt||jj} nt||jj} n(|jrr|j|} n t||j} | jx8|j|s|j r|j d |f|j || qW|r|jjd j} | tjjkrt||j| j} | j| jn| jt||j| j<d S) a"Merges a single scalar field into a message. Args: tokenizer: A tokenizer to parse the field value. message: The message of which field is a member. field: The descriptor of the field to be merged. Raises: ParseError: In case of text parsing problems. rhrirjrkr_r`rz$Type %s not found in descriptor poolzExpected "%s".)type_url_prefixrFN)rErrrBrWrXrrKr9rrrrr\rr@r]getattrreaddZPackrbrr[Z SetInParentrrfrgrUZ MergeFromrF)rrrrDZ is_map_entryZ end_tokenrZpacked_type_nameZexpanded_any_end_tokenZexpanded_any_sub_messageZ any_messageZ sub_messageZ value_cpptyperFrrrrsb                z_Parser._MergeMessageFieldcCs|j}t|do|jdkS)Nsyntaxproto3)rVhasattrr)rrJrrr_IsProto3SyntaxAs z_Parser._IsProto3SyntaxcCs|j}d}|jtjjtjjtjjfkr0t|}n|jtjjtjj tjj fkrVt |}n|jtjj tjj fkrvt|}n|jtjjtjjfkrt|}n|jtjjtjjfkr|j}nr|jtjjkr|j}nZ|jtjjkr|j}nB|jtjjkr|j}n*|jtjjkr|j|}ntd|j|jtjjkrf|j rR|j!|j"|nt#||j$j"|n|j%| }|j r|j& r|r|j'|r|j(d|j)j*|j*fn ||j!|<nF|j& r|r|j+|j$r|j(d|j)j*|j$fnt,||j$|dS)aHMerges a single scalar field into a message. Args: tokenizer: A tokenizer to parse the field value. message: A protocol message to record the data. field: The descriptor of the field to be merged. Raises: ParseError: In case of text parsing problems. RuntimeError: On runtime errors. NzUnknown field type %dz;Message type "%s" should not have multiple "%s" extensions.z7Message type "%s" should not have multiple "%s" fields.)-rzr?rr@Z TYPE_INT32Z TYPE_SINT32Z TYPE_SFIXED32 _ConsumeInt32Z TYPE_INT64Z TYPE_SINT64Z TYPE_SFIXED64 _ConsumeInt64Z TYPE_UINT32Z TYPE_FIXED32_ConsumeUint32Z TYPE_UINT64Z TYPE_FIXED64_ConsumeUint64Z TYPE_FLOATZ TYPE_DOUBLE ConsumeFloatZ TYPE_BOOL ConsumeBoolZ TYPE_STRING ConsumeStringrrConsumeByteStringZ TYPE_ENUM ConsumeEnum RuntimeErrorr\r]rbrrrrerrZ HasExtensionrrVrWZHasFieldsetattr)rrrrD_rFZcan_check_presencerrrrGs\                       z_Parser._MergeScalarField)FFN)r rrrrrrwrNr}rrrr staticmethodrrrrrrr~Ns mH r~cCs8|jdr,|jd r,|jd r,t|nt|dS)z}Skips over contents (value or message) of a field. Args: tokenizer: A tokenizer to parse the field name and values. rrjrhN)r LookingAt_SkipFieldValue_SkipFieldMessage)rrrrrs  rcCs^|jdr6|jx|jdr(|jqW|jdn|jt||jdsZ|jddS)zSkips over a complete field (name and value/message). Args: tokenizer: A tokenizer to parse the field name and values. r_rr`rrN)rrrr)rrrr _SkipFields     rcCsR|jdrd}n|jdd}x$|jd rB|jd rBt|q W|j|dS)zfSkips over a field message. Args: tokenizer: A tokenizer to parse the field name and values. rhrirjrkN)rrrr)rZ delimiterrrrrs   rcCsT|jrx |jrq WdS|j rPt| rPt| rP|j rPtd|jdS)zSkips over a field value. Args: tokenizer: A tokenizer to parse the field name and values. Raises: ParseError: In case an invalid field value is found. NzInvalid field value: )TryConsumeByteStringTryConsumeIdentifier_TryConsumeInt64_TryConsumeUint64TryConsumeFloatrtoken)rrrrrs    rc@sHeZdZdZejdZejdejZejdejZ ejdj ddgdd e DZ ejd Z ejd ZdEd dZddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(ZdFd*d+Zd,d-Zd.d/Zd0d1Z d2d3Z!d4d5Z"d6d7Z#d8d9Z$d:d;Z%dd?Z'd@dAZ(dBdCZ)dDS)GrzProtocol buffer text representation tokenizer. This class handles the lower level string parsing by splitting it into meaningful tokens. It was directly ported from the Java protocol buffer API. z\s+z (\s*#.*$)z (\s|(#.*$))+|z[a-zA-Z_][0-9a-zA-Z_+-]*z#([0-9+-]|(\.[0-9]))[0-9a-zA-Z_.+-]*cCsg|]}dj|dqS)z!{qt}([^{qt}\n\\]|\\.)*({qt}|\\?$))Zqt)r).0Zmarkrrr szTokenizer.z [^\d\W]\w*z\w+TcCsld|_d|_d|_d|_d|_t||_d|_d|_d|_ d|_ ||_ |rP|j pT|j |_|j|jdS)NrT)Z _positionrrZ _token_startriter_lines _current_line_previous_line_previous_column _more_lines_skip_comments_WHITESPACE_OR_COMMENT _WHITESPACE_whitespace_pattern_SkipWhitespace NextToken)rrZ skip_commentsrrrrs  zTokenizer.__init__cCs |j|kS)N)r)rrrrrrszTokenizer.LookingAtcCs|j S)z^Checks the end of the text was reached. Returns: True iff the end was reached. )r)rrrrr szTokenizer.AtEndc Cs`xZt|j|jkrZyt|j|_Wn tk rBd|_d|_dSX|jd7_d|_qWdS)NrFrr)lenrrnextr StopIterationrr)rrrr_PopLineszTokenizer._PopLinecCsFx@|j|jj|j|j}|s"Pt|jd}|j|7_qWdS)Nr)rrmatchrrrgroup)rrZlengthrrrrszTokenizer._SkipWhitespacecCs|j|kr|jdSdS)zTries to consume a given piece of text. Args: token: Text to consume. Returns: True iff the text was consumed. TF)rr)rrrrrr's zTokenizer.TryConsumecCs|j|s|jd|dS)zConsumes a piece of text. Args: token: Text to consume. Raises: ParseError: If the text couldn't be consumed. zExpected "%s".N)rr)rrrrrr5s zTokenizer.ConsumecCs(|j}|jj|s|jd|j|S)NzExpected comment.)r_COMMENTrrr)rr>rrrConsumeCommentAs   zTokenizer.ConsumeCommentcCs:|jdko|jdk}|j}|j}|j|ko0| }||fS)zCConsumes a comment, returns a 2-tuple (trailing bool, comment str).r)rrrr)rZ just_startedZbefore_parsingZcommentZtrailingrrrConsumeCommentOrTrailingCommentHs  z)Tokenizer.ConsumeCommentOrTrailingCommentc Cs&y |jdStk r dSXdS)NTF)rr)rrrrrXs zTokenizer.TryConsumeIdentifiercCs(|j}|jj|s|jd|j|S)zConsumes protocol message field identifier. Returns: Identifier string. Raises: ParseError: If an identifier couldn't be consumed. zExpected identifier.)r _IDENTIFIERrrr)rr>rrrr_s   zTokenizer.ConsumeIdentifierc Cs&y |jdStk r dSXdS)NTF)rr)rrrrTryConsumeIdentifierOrNumberns z&Tokenizer.TryConsumeIdentifierOrNumbercCs,|j}|jj|s |jd||j|S)zConsumes protocol message field identifier. Returns: Identifier string. Raises: ParseError: If an identifier couldn't be consumed. z&Expected identifier or number, got %s.)r_IDENTIFIER_OR_NUMBERrrr)rr>rrrrus  z#Tokenizer.ConsumeIdentifierOrNumberc Cs&y |jdStk r dSXdS)NTF)ConsumeIntegerr)rrrrTryConsumeIntegers zTokenizer.TryConsumeIntegerFcCsPyt|j|d}Wn0tk rB}z|jt|WYdd}~XnX|j|S)zConsumes an integer number. Args: is_long: True if the value should be returned as a long integer. Returns: The integer parsed. Raises: ParseError: If an integer couldn't be consumed. )is_longN)_ParseAbstractIntegerr ValueErrorrrr)rrr>errrrs  zTokenizer.ConsumeIntegerc Cs&y |jdStk r dSXdS)NTF)rr)rrrrrs zTokenizer.TryConsumeFloatcCsLyt|j}Wn0tk r>}z|jt|WYdd}~XnX|j|S)zConsumes an floating point number. Returns: The number parsed. Raises: ParseError: If a floating point number couldn't be consumed. N) ParseFloatrrrrr)rr>rrrrrs  zTokenizer.ConsumeFloatcCsLyt|j}Wn0tk r>}z|jt|WYdd}~XnX|j|S)zConsumes a boolean value. Returns: The bool parsed. Raises: ParseError: If a boolean value couldn't be consumed. N) ParseBoolrrrrr)rr>rrrrrs  zTokenizer.ConsumeBoolc Cs&y |jdStk r dSXdS)NTF)rr)rrrrrs zTokenizer.TryConsumeByteStringcCsF|j}y tj|dStk r@}z|j|WYdd}~XnXdS)zConsumes a string value. Returns: The string parsed. Raises: ParseError: If a string value couldn't be consumed. zutf-8N)rr$r,UnicodeDecodeError_StringParseError)rZ the_bytesrrrrrs  zTokenizer.ConsumeStringcCs<|jg}x&|jr0|jdtkr0|j|jq Wdj|S)zConsumes a byte array value. Returns: The array parsed (as a string). Raises: ParseError: If a byte array value couldn't be consumed. r)_ConsumeSingleByteStringr_QUOTESrr)rZthe_listrrrrs zTokenizer.ConsumeByteStringcCs|j}t|dks|dtkr.|jd|ft|dksJ|d|dkrZ|jd|fytj|dd}Wn0tk r}z|jt|WYdd}~XnX|j|S) aOConsume one token of a string literal. String literals (whether bytes or text) can come in multiple adjacent tokens which are automatically concatenated, like in C or Python. This method only consumes one token. Returns: The token parsed. Raises: ParseError: When the wrong format data is found. rrzExpected string but found: %rrlzString missing ending quote: %rNrr) rrrrrZ CUnescaperrr)rryr>rrrrrs  z"Tokenizer._ConsumeSingleByteStringcCsNyt||j}Wn0tk r@}z|jt|WYdd}~XnX|j|S)N) ParseEnumrrrrr)rrDr>rrrrrs  zTokenizer.ConsumeEnumcCst||jd|jdS)zCreates and *returns* a ParseError for the previously read token. Args: message: A message to set for the exception. Returns: A ParseError instance. r)rrr)rrrrrr s z!Tokenizer.ParseErrorPreviousTokencCst||jd|jdS)z9Creates and *returns* a ParseError for the current token.r)rrr)rrrrrrszTokenizer.ParseErrorcCs|jdt|S)NzCouldn't parse string: )rr)rrrrrrszTokenizer._StringParseErrorcCs|j|_|j|_|jt|j7_|j|jsrrrrrs  rcCs2t||d}tdt|t|}|j||S)aParses an integer. Args: text: The text to parse. is_signed: True if a signed integer must be parsed. is_long: True if a long integer must be parsed. Returns: The integer value. Raises: ValueError: Thrown Iff the text is not a valid integer. )rrl)r_INTEGER_CHECKERSintZ CheckValue)ryrrr>Zcheckerrrrrs  rc CsBy|rt|dSt|dSWn tk r<td|YnXdS)a Parses an integer without checking size/signedness. Args: text: The text to parse. is_long: True if the value should be returned as a long integer. Returns: The integer value. Raises: ValueError: Thrown Iff the text is not a valid integer. rzCouldn't parse integer: %sN)longrr)ryrrrrrs  rcCsyt|Stk rtj|r@|ddkr6tdStdSnBtj|rRtdSyt|jdStk rtd|YnXYnXdS) zParse a floating point number. Args: text: Text to parse. Returns: The number parsed. Raises: ValueError: If a floating point number couldn't be parsed. r-z-infinfnanfzCouldn't parse float: %sN)floatr_FLOAT_INFINITYr _FLOAT_NANr3)ryrrrrs     rcCs$|d kr dS|dkrd Std d S)zParse a boolean value. Args: text: Text to parse. Returns: Boolean values parsed Raises: ValueError: If text is not a valid boolean. rmt1TrueTrnr0FalseFzExpected "true" or "false".N)rmrrr)rnrrr)r)ryrrrrs rc Cs|j}yt|d}Wn<tk rP|jj|d}|dkrLtd|j|fYnFXt|jdrn|jjdkrn|S|j j|d}|dkrtd|j|f|j S)a&Parse an enum value. The value can be specified by a number (the enum value), or by a string literal (the enum name). Args: field: Enum field descriptor. value: String value. Returns: Enum value number. Raises: ValueError: If the enum value could not be parsed. rNz%Enum type "%s" has no value named %s.rrz+Enum type "%s" has no value with number %d.) rorrZvalues_by_namerqrWrfilerrpra)rDrFZenum_descriptorrartrrrrs   r)r r ) FFFFNFNrN) rFFFFNFNN)rFFFFNN)rFFFFNN)FFN)FFN)FFN)FFN)FF)FF)FF)F)Br __author__r&rr$rurrZgoogle.protobuf.internalrrHrr__all__ZUint32ValueCheckerZInt32ValueCheckerZUint64ValueCheckerZInt64ValueCheckerrr IGNORECASErr frozensetr@Z CPPTYPE_FLOATZCPPTYPE_DOUBLErsrrX Exceptionr robjectr#rrErrrrKr2r|r rwr}r~rrrrrZ _Tokenizerrrrrrrrrrrrrrrrrr)s      )   J / #  AW