a
    Sic                     @   s  d Z dZddlZddlmZ ddlZddlZddlmZ ddl	Z	ddl
Z
ddlmZ ddlmZ ddlmZ d	ZeejjejjejjejjgZeejjejjgZeejjejjgZd
ZdZdZe	dZ e	dZ!G dd de"Z#G dd de#Z$G dd de#Z%d>ddZ&d?ddZ'dd Z(G dd de)Z*dd  Z+d!d" Z,d#d$ Z-d@d%d&Z.dAd'd(Z/e0e1fZ2G d)d* d*e)Z3dBd+d,Z4d-d. Z5d/d0 Z6d1d2 Z7d3d4gd5d6gd5d6gd7d8gd9d:gd5d6gd;d<gd=Z8dS )Cat  Contains routines for printing protocol messages in JSON format.

Simple usage example:

  # Create a proto object and serialize it to a json format string.
  message = my_proto_pb2.MyMessage(foo='bar')
  json_string = json_format.MessageToJson(message)

  # Parse a json format string to proto object.
  message = json_format.Parse(json_string, my_proto_pb2.MyMessage())
zjieluo@google.com (Jie Luo)    N)OrderedDict)methodcaller)type_checkers)
descriptor)symbol_databasez%Y-%m-%dT%H:%M:%SInfinityz	-InfinityNaNu.   [-](?![-])|(?<![-])[-]z\[[a-zA-Z0-9\._]*\]$c                   @   s   e Zd ZdZdS )Errorz'Top-level module error for json_format.N__name__
__module____qualname____doc__ r   r   W/var/www/html/django/DPS/env/lib/python3.9/site-packages/google/protobuf/json_format.pyr	   N   s   r	   c                   @   s   e Zd ZdZdS )SerializeToJsonErrorz&Thrown if serialization to JSON fails.Nr
   r   r   r   r   r   R   s   r   c                   @   s   e Zd ZdZdS )
ParseErrorz Thrown in case of parsing error.Nr
   r   r   r   r   r   V   s   r   F   c           	      C   s    t |||||d}|| ||S )aR  Converts protobuf message to JSON format.

  Args:
    message: The protocol buffers message instance to serialize.
    including_default_value_fields: If True, singular primitive fields,
        repeated fields, and map fields will always be serialized.  If
        False, only serialize non-empty fields.  Singular message fields
        and oneof fields are not affected by this option.
    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.
    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: If set, use this to specify float field valid digits.

  Returns:
    A string containing the JSON formatted protocol buffer message.
  float_precision)_PrinterToJsonString)	messageincluding_default_value_fieldspreserving_proto_field_nameindent	sort_keysuse_integers_for_enumsdescriptor_poolr   printerr   r   r   MessageToJsonZ   s    r    c                 C   s   t |||||d}|| S )a  Converts 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.
    including_default_value_fields: If True, singular primitive fields,
        repeated fields, and map fields will always be serialized.  If
        False, only serialize non-empty fields.  Singular message fields
        and oneof fields are not affected by this option.
    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: If set, use this to specify float field valid digits.

  Returns:
    A dict representation of the protocol buffer message.
  r   )r   _MessageToJsonObject)r   r   r   r   r   r   r   r   r   r   MessageToDict   s    r"   c                 C   s"   | j tjjko | jjo | j jS N)typer   FieldDescriptorTYPE_MESSAGEmessage_typehas_options
GetOptions	map_entry)fieldr   r   r   _IsMapEntry   s
    
r,   c                   @   sj   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )r   z)JSON format printer for protocol message.FNc                 C   s4   || _ || _|| _|| _|r*d|| _nd | _d S )Nz.{}g)r   r   r   r   formatfloat_format)selfr   r   r   r   r   r   r   r   __init__   s    z_Printer.__init__c                 C   s   |  |}tj|||dS )N)r   r   )r!   jsondumps)r/   r   r   r   jsr   r   r   r      s    
z_Printer.ToJsonStringc                 C   sL   |j }|j}t|r| |S |tv r<tt| d || S i }| ||S )zEConverts message to an object according to Proto3 JSON Specification.r   )
DESCRIPTOR	full_name_IsWrapperMessage_WrapperMessageToJsonObject_WKTJSONMETHODSr   _RegularMessageToJsonObject)r/   r   message_descriptorr5   r3   r   r   r   r!      s    
z_Printer._MessageToJsonObjectc              
      s  |  }z|D ]\ }jr& j}n j}t r jjd }i }|D ]:}t|trf|r`d}	qnd}	nt	|}	
||| ||	< qH|||< q jtjjkr fdd|D ||< q jrd j }
 |||< q
 |||< qjr|j}
|
jD ]  jtjjkr  jtjjks jr*qjr: j}n j}||v rLqt r`i ||< q jtjjkrzg ||< q
  j||< qW n8 ty } ztd j|W Y d}~n
d}~0 0 |S )	z?Converts normal message according to Proto3 JSON Specification.valuetruefalsec                    s   g | ]}  |qS r   )_FieldToJsonObject).0kr+   r/   r   r   
<listcomp>   s   z8_Printer._RegularMessageToJsonObject.<locals>.<listcomp>z[%s]z#Failed to serialize {0} field: {1}.N)
ListFieldsr   name	json_namer,   r'   fields_by_name
isinstanceboolstrr>   labelr   r%   LABEL_REPEATEDis_extensionr5   r   r4   fieldscpp_typeCPPTYPE_MESSAGEcontaining_oneofdefault_value
ValueErrorr   r-   )r/   r   r3   rM   r;   rD   Zv_fieldZjs_mapkeyZrecorded_keyr:   er   rA   r   r9      sf    








z$_Printer._RegularMessageToJsonObjectc                 C   sB  |j tjjkr| |S |j tjjkrx| jr0|S |jjdkr@dS |jj	
|d}|dur^|jS |jjdkrn|S tdn|j tjjkr|jtjjkrt|dS |S n|j tjjkrt|S |j tv rt|S |j tv r>t| r|dk  rtS tS t|rtS |j tjj kr>| j!r4t"t#|| j!S t$%|S |S )z<Converts field value according to Proto3 JSON Specification.google.protobuf.NullValueNproto3zKEnum field contains an integer value which can not mapped to an enum value.utf-8g        )&rN   r   r%   rO   r!   CPPTYPE_ENUMr   	enum_typer5   values_by_numbergetrD   filesyntaxr   CPPTYPE_STRINGr$   
TYPE_BYTESbase64	b64encodedecodeCPPTYPE_BOOLrH   _INT64_TYPESrI   _FLOAT_TYPESmathisinf_NEG_INFINITY	_INFINITYisnan_NANCPPTYPE_FLOATr.   floatr-   r   ToShortestFloat)r/   r+   r;   
enum_valuer   r   r   r>     sB    




z_Printer._FieldToJsonObjectc                 C   s   |  si S t }|j}||d< t|| j}||j |j}|j}t	|r^| 
||d< |S |tv rtt| d || |d< |S | ||S )z<Converts Any message according to Proto3 JSON Specification.@typer;   r   )rC   r   type_url_CreateMessageFromTypeUrlr   ParseFromStringr;   r4   r5   r6   r7   r8   r   r9   )r/   r   r3   rq   sub_messager:   r5   r   r   r   _AnyMessageToJsonObject;  s(    z _Printer._AnyMessageToJsonObjectc                 C   s   |  S )z8Converts message according to Proto3 JSON Specification.)r   r/   r   r   r   r   _GenericMessageToJsonObjectP  s    z$_Printer._GenericMessageToJsonObjectc                 C   sd   | d}|du s|dkrdS |dkr2| |jS |dkrB|j}n
t||}|jj| }| ||S )z>Converts Value message according to Proto3 JSON Specification.kindN
null_value
list_valuestruct_value)
WhichOneof_ListValueMessageToJsonObjectrz   r{   getattrr4   rF   r>   )r/   r   whichr;   Zoneof_descriptorr   r   r   _ValueMessageToJsonObjectV  s    

z"_Printer._ValueMessageToJsonObjectc                    s    fdd|j D S )zBConverts ListValue message according to Proto3 JSON Specification.c                    s   g | ]}  |qS r   )r   )r?   r;   r/   r   r   rB   h  s   z:_Printer._ListValueMessageToJsonObject.<locals>.<listcomp>)valuesrv   r   r   r   r}   f  s    
z&_Printer._ListValueMessageToJsonObjectc                 C   s*   |j }i }|D ]}| || ||< q|S )z?Converts Struct message according to Proto3 JSON Specification.)rM   r   )r/   r   rM   retrS   r   r   r   _StructMessageToJsonObjectk  s
    z#_Printer._StructMessageToJsonObjectc                 C   s   |  |jjd |jS )Nr;   )r>   r4   rF   r;   rv   r   r   r   r7   s  s    z$_Printer._WrapperMessageToJsonObject)FFFNN)r   r   r   r   r0   r   r!   r9   r>   ru   rw   r   r}   r   r7   r   r   r   r   r      s"        
@+r   c                 C   s   | j jdkS )Nzgoogle/protobuf/wrappers.proto)r\   rD   )r:   r   r   r   r6   x  s    r6   c                 C   s4   i }| D ]&\}}||v r&t d||||< q|S )Nz'Failed to load JSON: duplicate key {0}.)r   r-   )r3   resultrD   r;   r   r   r   _DuplicateChecker|  s    
r   c                 C   sh   t  }|du r|jn|}| dd }z||}W n  tyV   td| Y n0 ||}| S )z"Creates a message from a type URL.N/z1Can not find message descriptor by type_url: {0}.)	r   DefaultpoolsplitFindMessageTypeByNameKeyError	TypeErrorr-   GetPrototype)rq   r   dbr   	type_namer:   message_classr   r   r   rr     s    

rr   c              
   C   sl   t | ts| d} ztj| td}W n6 ty\ } ztdt|W Y d}~n
d}~0 0 t	||||S )a  Parses 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.

  Returns:
    The same message passed as argument.

  Raises::
    ParseError: On JSON parsing problems.
  rW   )object_pairs_hookzFailed to load JSON: {0}.N)
rG   rI   rb   r1   loadsr   rR   r   r-   	ParseDict)textr   ignore_unknown_fieldsr   r3   rT   r   r   r   Parse  s    

(r   c                 C   s   t ||}|| | |S )a  Parses 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.

  Returns:
    The same message passed as argument.
  )_ParserConvertMessage)Zjs_dictr   r   r   parserr   r   r   r     s    
r   c                   @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )r   z(JSON format parser for protocol message.c                 C   s   || _ || _d S r#   )r   r   )r/   r   r   r   r   r   r0     s    z_Parser.__init__c                 C   sT   |j }|j}t|r"| || n.|tv rDtt| d |||  n| || dS )zConvert a JSON object into a message.

    Args:
      value: A JSON object.
      message: A WKT or regular protocol message to record the data.

    Raises:
      ParseError: In case of convert problems.
       N)r4   r5   r6   _ConvertWrapperMessager8   r   _ConvertFieldValuePair)r/   r;   r   r:   r5   r   r   r   r     s    
z_Parser.ConvertMessagec                 C   s  g }|j }tdd |jD }|D ]}z||d}|sJ|j|d}|st|r|jsntd	|j
|dd }|j|}|sd|ddd }|j|}|s| jrW q"td	|j
|d	d
 |jD ||v rtd	|j j
||| || }	|jdurP|	durP|jj}
|
|v rFtd	|j j
|
||
 |	du r|jtjjkr|jj
dkrt||j}d|_n:|jtjjkr|jj
dkrt||jd n||j W q"t|r||j | |	|| n>|j tjj!kr||j t"|	t#s*td	||	|jtjjkr|	D ]B}t||j$ }|du rr|j j
dkrrtd| %|| q>n6|	D ]0}|du rtdt||jt&|| qnt|jtjjkr|j'r|j| }nt||j}|(  | %|	| n.|j'rt&|	||j|< nt||jt&|	| W q" ty } z<|rh|jdu rhtd	||ntt)|W Y d}~q"d}~0  t*y } ztd	||W Y d}~q"d}~0  t+y } ztd	||W Y d}~q"d}~0 0 q"dS )zConvert 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.

    Raises:
      ParseError: In case of problems converting.
    c                 s   s   | ]}|j |fV  qd S r#   rE   r?   fr   r   r   	<genexpr>  s   z1_Parser._ConvertFieldValuePair.<locals>.<genexpr>Nz)Message type {0} does not have extensionsr   r   .zVMessage type "{0}" has no field named "{1}".
 Available Fields(except extensions): {2}c                 S   s   g | ]
}|j qS r   r   r   r   r   r   rB         z2_Parser._ConvertFieldValuePair.<locals>.<listcomp>z9Message type "{0}" should not have multiple "{1}" fields.z?Message type "{0}" should not have multiple "{1}" oneof fields.google.protobuf.Valuer   rU   z.repeated field {0} must be in [] which is {1}.zAnull is not allowed to be used as an element in a repeated field.zFailed to parse {0} field: {1}.),r4   dictrM   r[   rF   _VALID_EXTENSION_NAMEmatchis_extendabler   r-   r5   
Extensions_FindExtensionByNamejoinr   r   appendrP   rD   rN   r   r%   rO   r'   r~   ry   rX   rY   setattr
ClearFieldr,   _ConvertMapFieldValuerJ   rK   rG   listaddr   _ConvertScalarFieldValuerL   SetInParentrI   rR   r   )r/   r3   r   namesr:   Zfields_by_json_namerD   r+   
identifierr;   
oneof_namert   itemrT   r   r   r   r     s    












 $z_Parser._ConvertFieldValuePairc                 C   s   t |tr|sdS z|d }W n ty8   tdY n0 t|| j}|j}|j}t|rl| 	|d | n@|t
v rtt
| d |d ||  n|d= | || ||d< | |_||_dS )z/Convert a JSON representation into Any message.Nrp   z*@type is missing when parsing any message.r;   r   )rG   r   r   r   rr   r   r4   r5   r6   r   r8   r   r   SerializeToStringr;   rq   )r/   r;   r   rq   rt   r:   r5   r   r   r   _ConvertAnyMessageT  s,    
z_Parser._ConvertAnyMessagec              
   C   s@   z| | W n, ty: } zt|W Y d}~n
d}~0 0 dS )z?Convert a JSON representation into message with FromJsonString.N)FromJsonStringrR   r   )r/   r;   r   rT   r   r   r   _ConvertGenericMessagem  s    z_Parser._ConvertGenericMessagec                 C   s   t |tr| ||j ntt |tr4| ||j nZ|du rDd|_nJt |trV||_	n8t |t
rh||_n&t |trz||_ntd|t|dS )z1Convert a JSON representation into Value message.Nr   z"Value {0} has unexpected type {1}.)rG   r   _ConvertStructMessager{   r   _ConvertListValueMessagerz   ry   rH   
bool_valuerI   string_value_INT_OR_FLOATnumber_valuer   r-   r$   )r/   r;   r   r   r   r   _ConvertValueMessagev  s    




z_Parser._ConvertValueMessagec                 C   sB   t |tstd||d |D ]}| ||j  q&dS )z5Convert a JSON representation into ListValue message.z%ListValue must be in [] which is {0}.r   N)rG   r   r   r-   r   r   r   r   )r/   r;   r   r   r   r   r   r     s    

z _Parser._ConvertListValueMessagec                 C   sD   t |tstd||  |D ]}| || |j|  q$dS )z2Convert a JSON representation into Struct message.z&Struct must be in a dict which is {0}.N)rG   r   r   r-   Clearr   rM   )r/   r;   r   rS   r   r   r   r     s    
z_Parser._ConvertStructMessagec                 C   s"   |j jd }t|dt|| dS )z3Convert a JSON representation into Wrapper message.r;   N)r4   rF   r   r   )r/   r;   r   r+   r   r   r   r     s    z_Parser._ConvertWrapperMessagec                 C   s   t |tstd|j||jjd }|jjd }|D ]V}t||d}|jt	j
jkrt| || t||j|  q8t|| |t||j|< q8dS )a5  Convert 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.

    Raises:
      ParseError: In case of convert problems.
    z-Map field {0} must be in a dict which is {1}.rS   r;   TN)rG   r   r   r-   rD   r'   rF   r   rN   r   r%   rO   r   r~   )r/   r;   r   r+   Z	key_fieldZvalue_fieldrS   	key_valuer   r   r   r     s&    
z_Parser._ConvertMapFieldValueN)r   r   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   v		r   c                 C   sX  |j tv rt| S |j tv r&t| |S |j tjjkr>t| |S |j tjj	kr|j
tjjkrt| trp| d}n| }|ddt|d    }t|S t| rtd| S n|j tjjkrT|jj| d}|du rNzt| }|jj|d}W n( ty   td| |jjY n0 |du rN|jjdkr:|S td| |jj|j S dS )aB  Convert a single scalar field value.

  Args:
    value: A scalar value to convert the scalar field value.
    field: The descriptor of the field to convert.
    require_str: If True, the field value must be a str.

  Returns:
    The converted scalar field value

  Raises:
    ParseError: In case of convert problems.
  rW      =   zUnpaired surrogateNz)Invalid enum value {0} for enum type {1}.rV   )!rN   
_INT_TYPES_ConvertIntegerre   _ConvertFloatr   r%   rc   _ConvertBoolr^   r$   r_   rG   rI   encodelenr`   urlsafe_b64decode_UNPAIRED_SURROGATE_PATTERNsearchr   rX   rY   values_by_namer[   intrZ   rR   r-   r5   r\   r]   number)r;   r+   require_strencodedZpadded_valuero   r   r   r   r   r     sB    








r   c                 C   sf   t | tr |  s td| t | trF| ddkrFtd| t | tr^td| t| S )zConvert an integer.

  Args:
    value: A scalar value to convert.

  Returns:
    The integer value.

  Raises:
    ParseError: If an integer couldn't be consumed.
  zCouldn't parse integer: {0}. r   zCouldn't parse integer: "{0}".z3Bool value {0} is not acceptable for integer field.)	rG   rm   
is_integerr   r-   rI   findrH   r   )r;   r   r   r   r     s    
r   c                 C   s   t | trrt| rtdt| r@| dkr8tdntd|jtjj	krr| t
jkr`td| t
jk rrtd| dkrtdz
t| W S  ty   | tkrtd	 Y S | tkrtd
 Y S | tkrtd Y S td| Y n0 dS )z!Convert an floating point number.z-Couldn't parse NaN, use quoted "NaN" instead.r   zJCouldn't parse Infinity or value too large, use quoted "Infinity" instead.zLCouldn't parse -Infinity or value too small, use quoted "-Infinity" instead.zFloat value too largezFloat value too smallnanz.Couldn't parse float "nan", use "NaN" instead.z-infinfzCouldn't parse float: {0}.N)rG   rm   rf   rj   r   rg   rN   r   r%   rl   r   
_FLOAT_MAX
_FLOAT_MINrR   rh   ri   rk   r-   )r;   r+   r   r   r   r     s0    






r   c                 C   s@   |r*| dkrdS | dkrdS t d| t| ts<t d| S )zConvert a boolean value.

  Args:
    value: A scalar value to convert.
    require_str: If True, value must be a str.

  Returns:
    The bool parsed.

  Raises:
    ParseError: If a boolean value couldn't be consumed.
  r<   Tr=   Fz$Expected "true" or "false", not {0}.z&Expected true or false without quotes.)r   r-   rG   rH   )r;   r   r   r   r   r   0  s    
r   ru   r   rw   r   r}   r   r   r   r   r   )zgoogle.protobuf.Anyzgoogle.protobuf.Durationzgoogle.protobuf.FieldMaskzgoogle.protobuf.ListValuezgoogle.protobuf.Structzgoogle.protobuf.Timestampr   )FFr   FFNN)FFFNN)FN)FN)F)9r   
__author__r`   collectionsr   r1   rf   operatorr   resysgoogle.protobuf.internalr   google.protobufr   r   _TIMESTAMPFOMAT	frozensetr%   CPPTYPE_INT32CPPTYPE_UINT32CPPTYPE_INT64CPPTYPE_UINT64r   rd   rl   CPPTYPE_DOUBLEre   ri   rh   rk   compiler   r   	Exceptionr	   r   r   r    r"   r,   objectr   r6   r   rr   r   r   r   rm   r   r   r   r   r   r   r8   r   r   r   r   <module>   s   
       
*     
& K	
  
 z
6$