K idZdZddlZddlmZddlZddlZddlmZddl Z ddl Z ddl m Z ddl m Z ddl mZdd lmZee j$j&e j$j(e j$j*e j$j,gZee j$j*e j$j,gZee j$j2e j$j4gZd Zd Zd Ze j>d Z e j>dZ!Gdde"Z#Gdde#Z$Gdde#Z%Gdde%Z& d2dZ' d3dZ(dZ)Gdde*Z+dZ,dZ-dZ. d4dZ/ d4d Z0e1e2fZ3e4e5fZ6Gd!d"e*Z7d5d#Z8d$Z9d%Z:d&Z;d'd(gd)d*gd)d*gd+d,gd-d.gd)d*gd/d0gd1Zs/rr ceZdZdZy)SerializeToJsonErrorz&Thrown if serialization to JSON fails.Nr rrrrrBs.rrceZdZdZy) ParseErrorz Thrown in case of parsing error.Nr rrrrrFs(rrceZdZdZy)EnumStringValueParseErrorzwThrown if unknown string enum value is encountered. This exception is suppressed if ignore_unknown_fields is set. Nr rrrrrJsrrc Ht|||||} | j||||S)a8Converts protobuf message to JSON format. Args: message: The protocol buffers message instance to serialize. always_print_fields_with_no_presence: If True, fields without presence (implicit presence scalars, repeated fields, and map fields) will always be serialized. Any field that supports presence is not affected by this option (including singular message fields and oneof fields). preserving_proto_field_name: If True, use the original proto field names as defined in the .proto file. If False, convert the field names to lowerCamelCase. indent: The JSON object will be pretty-printed with this indent level. An indent level of 0 or negative will only insert newlines. If the indent level is None, no newlines will be inserted. sort_keys: If True, then the output will be sorted by field names. use_integers_for_enums: If true, print integers instead of enum names. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. float_precision: Deprecated. If set, use this to specify float field valid digits. ensure_ascii: If True, strings with non-ASCII characters are escaped. If False, Unicode strings are returned unchanged. Returns: A string containing the JSON formatted protocol buffer message. )_Printer ToJsonString) messagepreserving_proto_field_nameindent sort_keysuse_integers_for_enumsdescriptor_poolfloat_precision ensure_ascii$always_print_fields_with_no_presenceprinters r MessageToJsonr(Qs7J !*  '   gvy, GGrcBt|||||}|j|S)aConverts protobuf message to a dictionary. When the dictionary is encoded to JSON, it conforms to proto3 JSON spec. Args: message: The protocol buffers message instance to serialize. always_print_fields_with_no_presence: If True, fields without presence (implicit presence scalars, repeated fields, and map fields) will always be serialized. Any field that supports presence is not affected by this option (including singular message fields and oneof fields). preserving_proto_field_name: If True, use the original proto field names as defined in the .proto file. If False, convert the field names to lowerCamelCase. use_integers_for_enums: If true, print integers instead of enum names. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. float_precision: Deprecated. If set, use this to specify float field valid digits. Returns: A dict representation of the protocol buffer message. )r_MessageToJsonObject)rr&rr"r#r$r's r MessageToDictr+s0< !*  '  % %g ..rc|jtjjk(xr<|jj xr$|jj jSN)typerFieldDescriptor TYPE_MESSAGE message_type has_options GetOptions map_entry)fields r _IsMapEntryr6sP jjJ..;;;4    ( (4    ' ' ) 3 3rc^eZdZdZ ddZdZdZdZdZdZ d Z d Z d Z d Z d Zy)rz)JSON format printer for protocol message.Nc||_||_||_||_|r,t j ddj ||_yd|_y)Nzxfloat_precision option is deprecated for json_format. This will turn into error in 7.34.0, please remove it before that.z.{}g)r&rr"r#warningswarnformat float_format)selfrr"r#r$r&s r__init__z_Printer.__init__sY - -(CD$"8D*Dmm  !--8ddrcV|j|}tj||||S)N)r r!r%)r*jsondumps)r=rr r!r%jss rrz_Printer.ToJsonStrings, " "7 +B :: 6Y\ rc|j}|j}t|r|j|S|tvrt t|d||Si}|j ||S)zEConverts message to an object according to Proto3 JSON Specification.r) DESCRIPTOR full_name_IsWrapperMessage_WrapperMessageToJsonObject_WKTJSONMETHODSr_RegularMessageToJsonObject)r=rmessage_descriptorrErBs rr*z_Printer._MessageToJsonObjectsq ++",,I+,  - -g 66O# A\/)4Q7 A$ GG B  + +GR 88rc|j} |D]\}}|jrd|jz}n%|jr |j}n |j }t |rc|jjd}i}|D]=} t| tr| rd} nd} n t| } |j||| || <?|||<|jr$|D cgc]} |j|| c} ||<|j||||<|jr|j} | j D]}|jr |j"r|jr |j}n |j }||vrFt |ri||<W|jrg||<i|j||j$||<|Scc} w#t&$r+} t)dj+j| | d} ~ wwxYw)z?Converts normal message according to Proto3 JSON Specification.z[%s]valuetruefalsez#Failed to serialize {0} field: {1}.N) ListFields is_extensionrErname json_namer6r1fields_by_name isinstanceboolstr_FieldToJsonObject is_repeatedr&rDfields has_presence default_value ValueErrorrr;)r=rrBrYr5rLrQv_fieldjs_mapkey recorded_keykrJes rrIz$_Printer._RegularMessageToJsonObjects    !F: ;,%   %//)$  - -$$ u &&55g>'& Pc#t$% &  Xl#'#:#:7E#J#OF<  P"T(   AFGAd--eQ7G"T(,,UE:"T(5;<  3 3$//'.. KE 6 65;M;M   - -::D??D RZ   BtH  BtH..ue6I6IJBtH' K4 IGH<  / 6 6uzz1 E  s+CGG2C GG G9&G44G9c|jtjjk(r|j |S|jtjj k(r|j r|S|jjdk(ry|jjj|d}| |jS|jjr td|S|jtjjk(rV|jtjj k(r$t#j$|j'dSt)|S|jtjj*k(r t-|S|jt.vr t)|S|jt0vrt3j4|r|dkrt6St8St3j:|rt<S|j>rtAtC||j>S|jtjjDk(rtGjH|S|S)z>Z77EE E,,U33 Lrc|jsiSt}|j}||d<t||j}|j |j |j}|j}t|r|j||d<|S|tvr!tt|d|||d<|S|j||S)z L bki  + +K <Converts Value message according to Proto3 JSON Specification.kindN null_value list_value number_valuezTFail to serialize Infinity for Value.number_value, which would parse as string_valuezOFail to serialize NaN for Value.number_value, which would parse as string_value) WhichOneof_ListValueMessageToJsonObjectrrrurvr\rygetattrrDrSrW)r=rwhichrLoneof_descriptors r_ValueMessageToJsonObjectz"_Printer._ValueMessageToJsonObjecths   v &E }-    / /0B0B CC ""e E  0   E  0  gu%e))88?  " "#3U ;;rc^|jDcgc]}|j|c}Scc}w)zBConverts ListValue message according to Proto3 JSON Specification.)valuesr)r=rrLs rrz&_Printer._ListValueMessageToJsonObjects%?F~~ NeD * *5 1 NN Ns*c^|j}i}|D]}|j||||<|S)z?Converts Struct message according to Proto3 JSON Specification.)rYr)r=rrYretr_s r_StructMessageToJsonObjectz#_Printer._StructMessageToJsonObjects< ^^F C=//s rr*rIrWrrrrrrGrrrrrsQ1#("+0 0 9@D+Z=," <4Orrc4|jjdk(S)Nzgoogle/protobuf/wrappers.proto)filerQ)rJs rrFrFs  % %)I IIrcdi}|D](\}}||vrtdj||||<*|S)Nz'Failed to load JSON: duplicate key {0}.)rr;)rBresultrQrLs r_DuplicateCheckerrsH &kdE v~ @GGM NNF4L -rc&tj}| |jn|}|jdd} |j |}tj|}|S#t $r }t dj||d}~wwxYw)z"Creates a message from a type URL.N/z0Can not find message descriptor by type_url: {0}) rDefaultpoolsplitFindMessageTypeByNameKeyError TypeErrorr;rGetMessageClass)rr#dbr type_namerJrb message_classs rrrs "#+$nnS!"%) 33I> "112DE-   :AA(K    sA'' B0B  Bc t|ts|jd} tj|t } t|||||S#t $r)}tdjt||d}~wwxYw#t$r}|d}~wt $r=}tdjt|jt||d}~wwxYw)alParses a JSON representation of a protocol message into a message. Args: text: Message JSON representation. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. max_recursion_depth: max recursion depth of JSON message to be deserialized. JSON messages over this depth will fail to be deserialized. Default value is 100. Returns: The same message passed as argument. Raises:: ParseError: On JSON parsing problems. re)object_pairs_hookzFailed to load JSON: {0}.NzFailed to parse JSON: {0}: {1}.) rTrVrqr@loadsr Exceptionrr; ParseDictr.r)textrignore_unknown_fieldsr#max_recursion_depthrBrbs rParsers2 D#  ;;w DH D,= >B   G*O=P  H 077A? @aGH  G  )00a1A1A3q6J    s:AB B$A;;B C B C8CCcFt|||}|j||d|S)a8Parses a JSON dictionary representation into a message. Args: js_dict: Dict representation of a JSON message. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. max_recursion_depth: max recursion depth of JSON message to be deserialized. JSON messages over this depth will fail to be deserialized. Default value is 100. Returns: The same message passed as argument. )_ParserConvertMessage)js_dictrrr#rparsers rrrs+, (/;N O&"- .rc^eZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZy)rz(JSON format parser for protocol message.c<||_||_||_d|_y)Nr)rr#rrecursion_depth)r=rr#rs rr>z_Parser.__init__s$"7D*D2DDrc|xjdz c_|j|jkDr$tdj|j|j}|j }|s |j }t|r|j|||n:|tvrtt|d||||n|j||||xjdzc_y)aConvert a JSON object into a message. Args: value: A JSON object. message: A WKT or regular protocol message to record the data. path: parent path to log parse error info. Raises: ParseError: In case of convert problems. z,Message too deep. Max recursion depth is {0}N) rrrr;rDrErQrF_ConvertWrapperMessagerHr_ConvertFieldValuePair)r=rLrpathrJrEs rrz_Parser.ConvertMessages A d666  8 ? ?&&   !++",,I   $ $d+, !!%$7 o %Gl?9-a0%$GM !!%$7Arc  g}|j}td|jD}d}d}d} |D]} |j| d} | s|jj| d} | st j | r|js%tdj|j|| dd} |jj| } | s>d j| jd dd} |jj| } | sV|jrtd j|j| ||jD cgc]} | j c} | |vr0td j|jj| ||j#| || }| j$]|[| j$j&}||vr0td j|jj|||j#||| j(t*j,j.k(r*| j0jd k(r||| }d|_nT| j(t*j,j4k(r$| j6jdk(r | || dn ||| t9| r/||| |j;||| dj|| n| j<r9||| t?|t@stdj| ||| j(t*j,j.k(rtC|D]w\}}||| jE}|5|jjd k7rtdj|| ||jG||dj|| |yntC|D]H\}}|tdj|| ||jI|| |dj|| |Jn| j(t*j,j.k(r=||| }|jK|jG||dj|| n$|jM|| |dj|| ycc} w#t$rD} r(| j$tdj| ||ttO||d}~wtP$r!}tdj| ||d}~wtR$r!}tdj| ||d}~wwxYw)a'Convert field value pairs into regular message. Args: js: A JSON object to convert the field value pairs. message: A regular protocol message to record the data. path: parent path to log parse error info. Raises: ParseError: In case of problems converting. c38K|]}|j|fywr-)rR).0fs r z1_Parser._ConvertFieldValuePair..2sascv|jr|j|y|j|jyr-)rPClearExtension ClearFieldrQrr5s r_ClearFieldOrExtensionz>_Parser._ConvertFieldValuePair.._ClearFieldOrExtension6s+   u%5::&rcd|jr|j|St||jSr-)rP ExtensionsrrQrs r_GetFieldOrExtensionz<_Parser._ConvertFieldValuePair.._GetFieldOrExtension<s-   !!%((w ++rcj|jr||j|<yt||j|yr-)rPrsetattrrQ)rr5rLs r_SetFieldOrExtensionz<_Parser._ConvertFieldValuePair.._SetFieldOrExtensionBs+   $)5!U+rNz0Message type {0} does not have extensions at {1}rr.zaMessage type "{0}" has no field named "{1}" at "{2}". Available Fields(except extensions): "{3}"zBMessage type "{0}" should not have multiple "{1}" fields at "{2}".zHMessage type "{0}" should not have multiple "{1}" oneof fields at "{2}".google.protobuf.Valuerrd{0}.{1}z4repeated field {0} must be in [] which is {1} at {2}zPnull is not allowed to be used as an element in a repeated field at {0}.{1}[{2}]z {0}.{1}[{2}]zFailed to parse {0} field: {1}.)*rDdictrYrkrS_VALID_EXTENSION_NAMEmatch is_extendablerr;rEr_FindExtensionByNamejoinrrrRappendcontaining_oneofrQrfrr/rgr1rrhrir6_ConvertMapFieldValuerXrT _LIST_LIKE enumerateaddr_ConvertAndAppendScalar SetInParent_ConvertAndSetScalarrVr\r)r=rBrrnamesrJfields_by_json_namerrrrQr5 identifierrrL oneof_namerindexitemrbs rrz_Parser._ConvertFieldValuePair%sr E ++"4";";' , , NM#''d3$3377dC%.44T:#11BII&00$  Abz*$$99*E%*"2"23"7"<=J&&;;JGE  ' ' @$..(:(A(AB11;;B    5=''-v$$..d(   T4  ! ! -%2C--22* 5 //5v&&00*d0  ,,z " =nn : : J JJ$$..2II.w>K%&K "nn : : G GG//++/JJ % 3 "7E 2  u  % 0  $ $WeY%5%5dD%A    % 0E:.FMM%  ^^z99II I(/  t0%@DDFk,!,,66,-!;;A6dE< !! ^%:%:4u%M & )/  t ;;A6dE< **5$(=(=dD%(P ^^z99II I,We<+  ! ! #   e[)2B2B42N O  # #ueY%5%5dD%A {NDC|* U++3/66tQ?  3q6" )  - 4 4T1 =   - 4 4T1 =  sPC&S"+S S E%SHSS U5!?T  U5,U U5U00U5c t|tr|sy |d} t ||j }|j}|j}t|r&|j|d|dj |nU|tvr1tt|d|d|dj ||n|d= |j|||||d<|j|_||_y#t$r }tdj ||d}~wwxYw#t$r!}tdj |||d}~wwxYw#||d<wxYw)z/Convert a JSON representation into Any message.Nrz0@type is missing when parsing any message at {0} {0} at {1}rL {0}.valuer)rTrrrr;rr#rrDrErFrrHrrSerializeToStringrLr) r=rLrrrrbrrJrEs r_ConvertAnyMessagez_Parser._ConvertAnyMessagesi%u wh <-h8L8LMk%//",,I+, !! .+{'9'9$'? o %l ) $Q ' .    T "   ." ##E;=!g113GMG=   < C CD I   < |**1d3 4!;<("gs:C,D7E, D5DD E!D==EE c |j|y#t$r!}tdj|||d}~wwxYw)z?Convert a JSON representation into message with FromJsonString.rN)FromJsonStringr\rr;)r=rLrrrbs r_ConvertGenericMessagez_Parser._ConvertGenericMessages?< U# < |**1d3 4!;9>ct|tr|j||j|yt|tr|j ||j |y|d|_yt|tr||_ yt|tr||_ yt|tr||_ ytdj|t!||)z1Convert a JSON representation into Value message.Nrz(Value {0} has unexpected type {1} at {2})rTr_ConvertStructMessage struct_valuer_ConvertListOrTupleValueMessagerrrU bool_valuerV string_value _INT_OR_FLOATrrr;r.)r=rLrrs r_ConvertValueMessagez_Parser._ConvertValueMessages%   (<( >ud K  x  ' t  ""$j&7&7e&Drc t|tstdj|||j |D]5}|j |||j |dj||7y)z2Convert a JSON representation into Struct message.z,Struct must be in a dict which is {0} at {1}rN)rTrrr;ClearrrY)r=rLrrr_s rrz_Parser._ConvertStructMessage(sw eT "  8 ? ?t L   MMO  *gnnS)9+;+;D#+F rc~|jjd}|j|||dj|y)z3Convert a JSON representation into Wrapper message.rLrrN)rDrSrr;)r=rLrrr5s rrz_Parser._ConvertWrapperMessage7s?    - -g 6EK$6$6t$<rc 8t|ts&tdj|j|||j j d}|j j d}|D]}t||dj|d}|jtjjk(r>|j||t||j|dj|||j|||||dj||y) afConvert map field value for a message map field. Args: value: A JSON object to convert the map field value. message: A protocol message to record the converted data. field: The descriptor of the map field to be converted. path: parent path to log parse error info. Raises: ParseError: In case of convert problems. z3Map field {0} must be in a dict which is {1} at {2}r_rLz{0}.keyTrrN)rTrrr;rQr1rS_ConvertScalarFieldValuerfrr/rgrr_ConvertAndSetScalarToMapKey) r=rLrr5r key_field value_fieldr_ key_values rrz_Parser._ConvertMapFieldValue>s eT "  ? F Fjj%   ""11%8I$$33GAAN)rrrrr>rrrrrrrrrrrrrrrrrsJ0r/t)dj;||j,j<|S|j@Sy#t6$r5}t9dj;||j,j<|d}~wwxYw#t8$r!}t9dj;|||d}~wt($r!}t)dj;|||d}~wwxYw)aConvert a single scalar field value. Args: value: A scalar value to convert the scalar field value. field: The descriptor of the field to convert. path: parent path to log parse error info. require_str: If True, the field value must be a str. Returns: The converted scalar field value Raises: ParseError: In case of convert problems. EnumStringValueParseError: In case of unknown enum string value. re=zUnpaired surrogateNz(Invalid enum value {0} for enum type {1}r)!rf _INT_TYPES_ConvertIntegerrt _ConvertFloatrr/rr _ConvertBoolrmr.rnrTrVencodelenrourlsafe_b64decode_UNPAIRED_SURROGATE_PATTERNsearchrrhrivalues_by_namerkintrjr\rr;rErlnumber) rLr5r require_strencoded padded_valuer~rrbs rrrsF 2: ~~# U ## < ' 5% (( :55BB B % -- :55DD D z11<< < eS !LL)''S\A-=)=!>> '' 55 ' - -e 4/0 0 :55BB B??1155eTBj   u:&77;;FDI*   __ & &:AA5??44  M   1 C *8??22    $ #I #L$7$74$@ AqH : \((D1 29:slII2I0BI!I0AI1H1AI: I I0III JI-- J9JJc.t|tr*|jstdj |t|t r.|j ddk7rtdj |t|trtdj | t|S#t$r\} t|}n#t$r|wxYw|jstdj ||t|cYd}~Sd}~wwxYw)zConvert an integer. Args: value: A scalar value to convert. Returns: The integer value. Raises: ParseError: If an integer couldn't be consumed. zCouldn't parse integer: {0} rzCouldn't parse integer: "{0}"z2Bool value {0} is not acceptable for integer fieldz(Couldn't parse non-integer string: "{0}"N) rTr{ is_integerrr;rVfindrUrr\)rLrbrs rrrsue&6&6&8 299%@ AAs 32 5 5<  5 < r=s_ + # ! &++2,,--,,--  ,,-- ,,--     (bjjL# #:;0I0/5/)) !&  ).,Hb*/ %  &/R`v`FJ $  * `  6e E] PfPf B:J$N%IP667KL% ! & " ()" % & " $-r