o
    {j6h                     @   s  d Z dZddlZddlZddl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dlmZ ddlmZ g d	Ze
 e
 e
 e
 fZed
ejZedejZedZdZdZdZG dd deZG dd deZ G dd de!Z"eddddddddddddfde#fddZ$de%fddZ&dd Z'dd Z(dedddddddddddfd d!Z)dedddddddddfd"d#Z*dedddddddddfd$d%Z+d&d' Z,d(Z-d)Z.G d*d+ d+e!Z/				dPd,d-Z0				dPd.d/Z1				dPd0d1Z2				dPd2d3Z3G d4d5 d5e!Z4G d6d7 d7e!Z5e5Z6d8d9 Z7d:d; Z8d<d= Z9d>d? Z:d@dA Z;dBdC Z<dQdDdEZ=dQdFdGZ>dHdI Z?dJdK Z@dLdM ZAdNdO ZBdS )Rac  Contains 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)decoder)type_checkers)
descriptor)text_encoding)unknown_fields)MessageToStringParsePrintMessage
PrintFieldPrintFieldValueMergeMessageToBytesz-?inf(?:inity)?f?$znanf?$)'"zgoogle.protobuf.Anyz	 Tc                   @   s   e Zd ZdZdS )Errorz'Top-level module error for text_format.N)__name__
__module____qualname____doc__ r   r   j/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/protobuf/text_format.pyr   4   s    r   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )

ParseErrorz3Thrown in case of text parsing or tokenizing error.Nc                    sr   |d ur|d urt |}|d ur|d|7 }d||}|d ur*tt| | ntt|   || _|| _d S )Nz:{0}z	{0} : {1})strformatsuperr   __init___line_column)selfmessagelinecolumnloc	__class__r   r   r   ;   s   
zParseError.__init__c                 C      | j S N)r   r   r   r   r   GetLineH      zParseError.GetLinec                 C   r%   r&   )r   r'   r   r   r   	GetColumnK   r)   zParseError.GetColumn)NNN)r   r   r   r   r   r(   r*   __classcell__r   r   r#   r   r   8   s
    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )

TextWriterc                 C   s   t  | _d S r&   )ioStringIO_writer)r   as_utf8r   r   r   r   Q   s   zTextWriter.__init__c                 C   s   | j |S r&   )r/   write)r   valr   r   r   r1   T   s   zTextWriter.writec                 C   
   | j  S r&   )r/   closer'   r   r   r   r4   W      
zTextWriter.closec                 C   r3   r&   )r/   getvaluer'   r   r   r   r6   Z   r5   zTextWriter.getvalueN)r   r   r   r   r1   r4   r6   r   r   r   r   r,   O   s
    r,   Freturnc                 C   sV   t |}t||
|||||||||	|||d}||  | }|  |r)| S |S )a  Convert protobuf message to text format.

  Double values can be formatted compactly with 15 digits of
  precision (which is the most that IEEE 754 "double" can guarantee)
  using double_format='.15g'. To ensure that converting to text and back to a
  proto will result in an identical value, double_format='.17g' should be used.

  Args:
    message: The protocol buffers message.
    as_utf8: Return unescaped Unicode for non-ASCII characters.
    as_one_line: Don't introduce newlines between fields.
    use_short_repeated_primitives: Use short repeated format for primitives.
    pointy_brackets: If True, use angle brackets instead of curly braces for
      nesting.
    use_index_order: If True, fields of a proto message will be printed using
      the order defined in source code instead of the field number, extensions
      will be printed at the end of the message and their relative order is
      determined by the extension number. By default, use the field number
      order.
    float_format (str): If set, use this to specify float field formatting
      (per the "Format Specification Mini-Language"); otherwise, shortest float
      that has same value in wire will be printed. Also affect double field
      if double_format is not set but float_format is set.
    double_format (str): If set, use this to specify double field formatting
      (per the "Format Specification Mini-Language"); if it is not set but
      float_format is set, use float_format. Otherwise, use ``str()``
    use_field_number: If True, print field numbers instead of names.
    descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
    indent (int): The initial indent level, in terms of spaces, for pretty
      print.
    message_formatter (function(message, indent, as_one_line) -> unicode|None):
      Custom formatter for selected sub-messages (usually based on message
      type). Use to pretty print parts of the protobuf for easier diffing.
    print_unknown_fields: If True, unknown fields will be printed.
    force_colon: If set, a colon will be added after the field name even if the
      field is a proto message.

  Returns:
    str: A string of the text formatted protocol buffer message.
  )print_unknown_fieldsforce_colon)r,   _Printerr	   r6   r4   rstrip)r   r0   as_one_lineuse_short_repeated_primitivespointy_bracketsuse_index_orderfloat_formatdouble_formatuse_field_numberdescriptor_poolindentmessage_formatterr8   r9   outprinterresultr   r   r   r   ^   s.   7
r   c                 K   s:   t | fi |}t|tr|S |drdnd}||S )zFConvert protobuf message to encoded text format.  See MessageToString.r0   utf-8ascii)r   
isinstancebytesgetencode)r   kwargstextcodecr   r   r   r      s
   

r   c                 C   s"   | j tjjko| jjo| j jS r&   )typer   FieldDescriptorTYPE_MESSAGEmessage_typehas_options
GetOptions	map_entryfieldr   r   r   _IsMapEntry   s
   
r[   c                 C   s\   | j tjjkr	dS | j| jj krdS | jj| jkrdS | jr'| jj	| j
kS | jj	| j	kS )zDetermines if a field is consistent with a proto2 group.

  Args:
    field: The field descriptor.

  Returns:
    True if this field is group-like, false otherwise.
  F)rR   r   rS   
TYPE_GROUPnamerU   lowerfileis_extensioncontaining_typeextension_scoperY   r   r   r   _IsGroupLike   s   
rc   c                 C   s2   t |||||||||	|
||||d}||  dS )a  Convert the message to text format and write it to the out stream.

  Args:
    message: The Message object to convert to text format.
    out: A file handle to write the message to.
    indent: The initial indent level for pretty print.
    as_utf8: Return unescaped Unicode for non-ASCII characters.
    as_one_line: Don't introduce newlines between fields.
    use_short_repeated_primitives: Use short repeated format for primitives.
    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 float field formatting
      (per the "Format Specification Mini-Language"); otherwise, shortest
      float that has same value in wire will be printed. Also affect double
      field if double_format is not set but float_format is set.
    double_format: If set, use this to specify double field formatting
      (per the "Format Specification Mini-Language"); if it is not set but
      float_format is set, use float_format. 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.
    print_unknown_fields: If True, unknown fields will be printed.
    force_colon: If set, a colon will be added after the field name even if
      the field is a proto message.
  rF   rD   r0   r<   r=   r>   r?   r@   rA   rB   rC   rE   r8   r9   N)r:   r	   )r   rF   rD   r0   r<   r=   r>   r?   r@   rA   rB   rC   rE   r8   r9   rG   r   r   r   r	      s   -r	   c                 C   0   t ||||||||	|
|||d}|| | dS )%Print a single field name/value pair.rE   r8   r9   N)r:   r
   rZ   valuerF   rD   r0   r<   r=   r>   r?   r@   rA   rE   r8   r9   rG   r   r   r   r
        
r
   c                 C   re   )z0Print a single field value (not including name).rg   N)r:   r   rh   r   r   r   r   /  rj   r   c                 C   s\   |du rddl m} | }ddl m} z|| }W n
 ty%   Y dS w ||}| 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   )rC   )message_factory)google.protobufrC   Defaultrk   FindMessageTypeByNameKeyErrorGetMessageClass)	type_namerC   pool_modrk   message_descriptorrU   r   r   r   _BuildMessageFromTypeNameG  s   
rt         c                   @   s|   e Zd ZdZdedddddddddddf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)Text format printer for protocol message.r   FNc                 C   sh   || _ || _|| _|| _|| _|| _|| _|| _|	dur |	| _n|| _|
| _	|| _
|| _|| _|| _dS )a  Initialize the Printer.

    Double values can be formatted compactly with 15 digits of precision
    (which is the most that IEEE 754 "double" can guarantee) using
    double_format='.15g'. To ensure that converting to text and back to a proto
    will result in an identical value, double_format='.17g' should be used.

    Args:
      out: To record the text format result.
      indent: The initial indent level for pretty print.
      as_utf8: Return unescaped Unicode for non-ASCII characters.
      as_one_line: Don't introduce newlines between fields.
      use_short_repeated_primitives: Use short repeated format for primitives.
      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 float field formatting
        (per the "Format Specification Mini-Language"); otherwise, shortest
        float that has same value in wire will be printed. Also affect double
        field if double_format is not set but float_format is set.
      double_format: If set, use this to specify double field formatting
        (per the "Format Specification Mini-Language"); if it is not set but
        float_format is set, use float_format. 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.
      print_unknown_fields: If True, unknown fields will be printed.
      force_colon: If set, a colon will be added after the field name even if
        the field is a proto message.
    Nrd   )r   rF   rD   r0   r<   r=   r>   r?   r@   rA   rB   rC   rE   r8   r9   r   r   r   r   g  s    2
z_Printer.__init__c                 C   s   d|j vrdS t| | j}|rB||j | jrdnd}| jd| j	d |j |f  | 
| | j| jr=d dS d dS dS )	z5Serializes if message is a google.protobuf.Any field./F: z	%s[%s]%s  
T)type_urlrt   TypeNamerC   MergeFromStringri   r9   rF   r1   rD   _PrintMessageFieldValuer<   )r   r   packed_messagecolonr   r   r   _TryPrintAsAnyMessage  s   

z_Printer._TryPrintAsAnyMessagec                 C   s\   |  || j| j}|d u rdS | j}|d| j  || || jr)d dS d dS )NFrz   r{   T)rE   rD   r<   rF   r1   )r   r   	formattedrF   r   r   r   _TryCustomFormatMessage  s   
z _Printer._TryCustomFormatMessagec                 C   s  | j r
| |r
dS |jjtkr| |rdS | }| jr&|jdd d |D ]Q\}}t	|rHt
|D ]}| ||| d}| || q4q(|jtjjkrs| jrg|jtjjkrg|jtjjkrg| || q(|D ]}| || qiq(| || q(| jr| t| dS dS )zeConvert protobuf message to text format.

    Args:
      message: The protocol buffers message.
    Nc                 S   s   | d j r
| d jS | d jS )Nr   )r`   numberindex)xr   r   r   <lambda>  s    z'_Printer.PrintMessage.<locals>.<lambda>)key)r   ri   )rE   r   
DESCRIPTOR	full_name_ANY_FULL_TYPE_NAMEr   
ListFieldsr?   sortr[   sortedGetEntryClassr
   labelr   rS   LABEL_REPEATEDr=   cpp_typeCPPTYPE_MESSAGECPPTYPE_STRING"_PrintShortRepeatedPrimitivesValuer8   _PrintUnknownFieldsr   UnknownFieldSet)r   r   fieldsrZ   ri   r   entry_submsgelementr   r   r   r	     s:   z_Printer.PrintMessagec              	   C   s  | j }|D ]}|d| j  |t|j |jtkrR| jr%|d n|d |  jd7  _| |j	 | jr@|d q|  jd8  _|d| j d  q|jt
krztt|j	dt|j	\}}W n tys   d}Y nw |t|j	kr| jr|d n|d |  jd7  _| | | jr|d q|  jd8  _|d| j d  q|d |t|j	d	 || jrd
nd q|d |t|j	 || jrdnd qdS )zPrint unknown fields.rz   z { z {
ru   z} z}
r   z: "Fz" z"
z: r{   N)rF   r1   rD   r   field_number	wire_typeWIRETYPE_START_GROUPr<   r   dataWIRETYPE_LENGTH_DELIMITEDr   _DecodeUnknownFieldSet
memoryviewlen	Exceptionr   CEscape)r   unknown_field_setrF   rZ   embedded_unknown_messageposr   r   r   r     sN   






z_Printer._PrintUnknownFieldsc                 C   s   | j }|d| j  | jr|t|j nB|jrG|d |j j	r;|j
tjjkr;|jtjjkr;||jj n||j |d nt|rS||jj n||j | jsc|jtjjkrj|d dS dS )zPrint field name.rz   []rx   N)rF   r1   rD   rB   r   r   r`   ra   rW   message_set_wire_formatrR   r   rS   rT   r   LABEL_OPTIONALrU   r   rc   r]   r9   r   r   )r   rZ   rF   r   r   r   _PrintFieldName$  s&   
z_Printer._PrintFieldNamec                 C   sB   |  | | jd | || | j| jrd dS d dS )rf   rz   r{   N)r   rF   r1   r   r<   )r   rZ   ri   r   r   r   r
   A  s   
 z_Printer.PrintFieldc                 C   s   |  | | jd tt|d D ]}| |||  | jd q| ||d  | jd | j| jr=d dS d dS )	z("Prints short repeated primitives value.z [   z, r   rz   r{   N)r   rF   r1   ranger   r   r<   )r   rZ   ri   ir   r   r   r   H  s   
 z+_Printer._PrintShortRepeatedPrimitivesValuec                 C   s   | j rd}d}nd}d}| jr$| jd|  | | | j| d S | jd|  |  jd7  _| | |  jd8  _| jd| j |  d S )	N<>{}z%s z%s
ru   rz   )r>   r<   rF   r1   r	   rD   )r   ri   openbclosebr   r   r   r   T  s   

z _Printer._PrintMessageFieldValuec                 C   s  | j }|jtjjkr| | dS |jtjjkr5|jj	|d}|dur,|
|j dS |
t| dS |jtjjkrn|
d t|trO| jsO|d}n|}|jtjjkr[d}n| j}|
t|| |
d dS |jtjjkr|r~|
d dS |
d dS |jtjjkr| jdur|
d| j| dS t|r|
t| dS |
tt| dS |jtjjkr| jdur|
d| j| dS |
t| dS )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   rI   Ftruefalsez{1:{0}})rF   r   r   rS   r   r   CPPTYPE_ENUM	enum_typevalues_by_numberrM   r1   r]   r   r   rK   r0   rN   rR   
TYPE_BYTESr   r   CPPTYPE_BOOLCPPTYPE_FLOATr@   r   mathisnanr   ToShortestFloatCPPTYPE_DOUBLErA   )r   rZ   ri   rF   
enum_value	out_valueout_as_utf8r   r   r   r   g  s@   	



z_Printer.PrintFieldValue)r   r   r   r   _as_utf8_defaultr   r   r   r	   r   r   r
   r   r   r   r   r   r   r   r:   d  s2    
D'7r:   c                 C   (   t | t| tr
dnd|||||dS )a  Parses 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.
  If text contains a field already set in message, the value is appended if the
  field is repeated. Otherwise, an error is raised.

  Example::

    a = MyProto()
    a.repeated_field.append('test')
    b = MyProto()

    # Repeated fields are combined
    text_format.Parse(repr(a), b)
    text_format.Parse(repr(a), b) # repeated_field contains ["test", "test"]

    # Non-repeated fields cannot be overwritten
    a.singular_field = 1
    b.singular_field = 2
    text_format.Parse(repr(a), b) # ParseError

    # Binary version:
    b.ParseFromString(a.SerializeToString()) # repeated_field is now "test"

  Caller is responsible for clearing the message as needed.

  Args:
    text (str): Message text representation.
    message (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 (DescriptorPool): Descriptor pool used to resolve Any types.
    allow_unknown_field: if True, skip over unknown field and keep
      parsing. Avoid to use this option if possible. It may hide some
      errors (e.g. spelling error on field name)

  Returns:
    Message: The same message passed as argument.

  Raises:
    ParseError: On text parsing problems.
     
r{   rC   allow_unknown_field)
ParseLinessplitrK   rL   rP   r   allow_unknown_extensionallow_field_numberrC   r   r   r   r   r     s   2r   c                 C   r   )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. This means any non-repeated, top-level fields specified in text
  replace those in the message.

  Args:
    text (str): Message text representation.
    message (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 (DescriptorPool): Descriptor pool used to resolve Any types.
    allow_unknown_field: if True, skip over unknown field and keep
      parsing. Avoid to use this option if possible. It may hide some
      errors (e.g. spelling error on field name)

  Returns:
    Message: The same message passed as argument.

  Raises:
    ParseError: On text parsing problems.
  r   r{   r   )
MergeLinesr   rK   rL   r   r   r   r   r     s   r   c                 C      t ||||d}|| |S )a  Parses a text representation of a protocol message into a message.

  See Parse() for caveats.

  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.
    allow_unknown_field: if True, skip over unknown field and keep
      parsing. Avoid to use this option if possible. It may hide some
      errors (e.g. spelling error on field name)

  Returns:
    The same message passed as argument.

  Raises:
    ParseError: On text parsing problems.
  r   )_Parserr   linesr   r   r   rC   r   parserr   r   r   r        r   c                 C   r   )a  Parses a text representation of a protocol message into a message.

  See Merge() for more details.

  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.
    allow_unknown_field: if True, skip over unknown field and keep
      parsing. Avoid to use this option if possible. It may hide some
      errors (e.g. spelling error on field name)

  Returns:
    The same message passed as argument.

  Raises:
    ParseError: On text parsing problems.
  r   )r   r   r   r   r   r   r     r   r   c                   @   s   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d Zdd Zdd Zd d! ZdS )#r   z(Text format parser for protocol message.FNc                 C   s   || _ || _|| _|| _d S r&   )r   r   rC   r   )r   r   r   rC   r   r   r   r   r   A  s   
z_Parser.__init__c                 C      d| _ | || |S )zBParses a text representation of a protocol message into a message.F_allow_multiple_scalars_ParseOrMerger   r   r   r   r   r   r   K     z_Parser.ParseLinesc                 C   r   )zBMerges a text representation of a protocol message into a message.Tr   r   r   r   r   r   Q  r   z_Parser.MergeLinesc              
   C   sl   zdd |D }t |}W n ty } zt|d}~ww |r$|jj| _| s4| || | r(dS dS )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.
    c                 s   s(    | ]}t |tr|n|d V  qdS )rI   N)rK   r   decode).0r    r   r   r   	<genexpr>c  s
    
z(_Parser._ParseOrMerge.<locals>.<genexpr>N)	TokenizerUnicodeDecodeErrorr   r   r   	root_typeAtEnd_MergeField)r   r   r   	str_lines	tokenizerer   r   r   r   W  s   
z_Parser._ParseOrMergec                 C   s  |j }|jtkrq|drq| |\}}|d |d | ||j|d |  |dr2d}n|d d}t|| j}|d	u rIt	d
| ||se|
 rZ|d|f | || ||rNd}|j|||d d	S |dr| g}	|dr|	|  |dsd|	}	|js|d|j |j|	}
|
s| jrd	}
n|d|	 ||
jkr|d|	|jf |d n^| }	| jr|	 rt|	dd}|j|d	}
|
s|jr|j|}
n)|j|	d	}
|
s|j|	 d	}
|
rt|
sd	}
|
r|
j j!|	krd	}
|
s$| j"s$|d|j|	f |
r| j#sP|
j$rP|%|
j$j!}|d	urP||
j!krP|d|
j!||
j$j!|jf |
j&t'j(j)krj|d | ||j|
j | j*}n|d | ||j|
j | j+}|
j,t'j(j-kr|dr|ds	 ||||
 |drn|d qn||||
 n| js| j"sJ | .||	|j |ds|d d	S d	S )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   r   rx   rw   r   r   r   r   Nz$Type %s not found in descriptor poolExpected "%s".F)type_url_prefixdeterministic.z+Message type "%s" does not have extensions.zExtension "%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".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".,;)/r   r   r   
TryConsume_ConsumeAnyTypeUrlConsume_DetectSilentMarkerrt   rC   r   r   ParseErrorPreviousTokenr   PackConsumeIdentifierappendjoinis_extendable
Extensions_FindExtensionByNamer   ra   ConsumeIdentifierOrNumberr   isdigitParseIntegerfields_by_numberrM   _FindExtensionByNumberfields_by_namer^   rc   rU   r]   r   r   containing_oneof
WhichOneofr   r   rS   r   _MergeMessageField_MergeScalarFieldr   r   _SkipFieldContents)r   r   r   rs   r   packed_type_nameexpanded_any_end_tokenexpanded_any_sub_messager   r]   rZ   r   which_oneofmergerr   r   r   r   n  s   





















z_Parser._MergeFieldc                 C   s   d S r&   r   )r   immediate_message_type
field_namer   r   r   _LogSilentMarker  s   z_Parser._LogSilentMarkerc                 C   s   |j r| || d S d S r&   )+contains_silent_marker_before_current_tokenr  )r   r   r  r  r   r   r   r     s   z_Parser._DetectSilentMarkerc                 C   s   |  g}|d ||   |d ||   |d |  g}|dr8||   |ds,d|d|fS )zBConsumes a google.protobuf.Any type URL and returns the type name.r   rw   )r   r   r   r   r   )r   r   prefixr]   r   r   r   r     s   






z_Parser._ConsumeAnyTypeUrlc           	      C   s  t |}|drd}n|d d}|jtjjkr:|jr%|j| 	 }nQ|r1t
||j  }nEt
||j	 }n<|jrW| jsQ||rQ|d|jj|jf |j| }n| jsl||jrl|d|jj|jf t
||j}|  ||s| r|d|f | || ||r{|r|jjd j}|tjjkrt
||j|j }||j d	S |jt
||j|j< d	S 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.
    r   r   r   r   ;Message type "%s" should not have multiple "%s" extensions.7Message type "%s" should not have multiple "%s" fields.r   ri   N)r[   r   r   r   r   rS   r   r`   r   addgetattrr]   r   r   HasExtensionr   r   r   HasFieldSetInParentr   r   rU   r   r   r   r   CopyFromri   )	r   r   r   rZ   is_map_entry	end_tokensub_messagevalue_cpptyperi   r   r   r   r    sV   




z_Parser._MergeMessageFieldc                 C   s  | j }d}|jtjjtjjtjjfv rt|}n{|jtjjtjj	tjj
fv r+t|}nh|jtjjtjjfv r;t|}nX|jtjjtjjfv rKt|}nH|jtjjtjjfv r[| }n8|jtjjkrg| }n,|jtjjkrs| }n |jtjjkr| }n|jtjjkr||}ntd|j |jtjjkr|j r|j!| "| dS t#||j$"| dS |j r| j%s|j&r|'|r|(d|j)j*|j*f ||j!|< dS d}| j%s|j&r|+|j$}nt,t#||j$}|r|(d|j)j*|j$f t-||j$| dS )aH  Merges 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.
    NzUnknown field type %dr  Fr  ).r   rR   r   rS   
TYPE_INT32TYPE_SINT32TYPE_SFIXED32_ConsumeInt32
TYPE_INT64TYPE_SINT64TYPE_SFIXED64_ConsumeInt64TYPE_UINT32TYPE_FIXED32_ConsumeUint32TYPE_UINT64TYPE_FIXED64_ConsumeUint64
TYPE_FLOATTYPE_DOUBLEConsumeFloat	TYPE_BOOLConsumeBoolTYPE_STRINGConsumeStringr   ConsumeByteString	TYPE_ENUMConsumeEnumRuntimeErrorr   r   r`   r   r   r  r]   r   has_presencer  r   r   r   r  boolsetattr)r   r   r   rZ   _ri   duplicate_errorr   r   r   r  N  sz   












z_Parser._MergeScalarFieldc                 C   sp   | dr)|ds)|ds)| ||| |dr"| | dS | | dS | ||| | || dS )a$  Skips over contents (value or message) of a field.

    Args:
      tokenizer: A tokenizer to parse the field name and values.
      field_name: The field name currently being parsed.
      immediate_message_type: The type of the message immediately containing
        the silent marker.
    rx   r   r   r   N)r   	LookingAtr   _SkipRepeatedFieldValue_SkipFieldValue_SkipFieldMessage)r   r   r  r  r   r   r   r    s   
z_Parser._SkipFieldContentsc                 C   s   d}| drT|d|  7 }d}| dr'|d|  7 }|d7 }| ds|dkrJ| drJ|d|  7 }| drJ|d|  7 }| ds=|d |d7 }n|| 7 }| ||| | dsm| d	 d
S d
S )zSkips over a complete field (name and value/message).

    Args:
      tokenizer: A tokenizer to parse the field name and values.
      immediate_message_type: The type of the message immediately containing
        the silent marker.
    ry   r   r   r   rv   rw   r   r   r   N)r   r   r   r   r  )r   r   r  r  num_identifiersr   r   r   
_SkipField  s(   







z_Parser._SkipFieldc                 C   s`   | drd}n|d d}|ds)|ds)| || |ds)|dr|| dS )zSkips over a field message.

    Args:
      tokenizer: A tokenizer to parse the field name and values.
      immediate_message_type: The type of the message immediately containing
        the silent marker
    r   r   r   r   N)r   r   r:  r?  )r   r   r  	delimiterr   r   r   r=    s   

z_Parser._SkipFieldMessagec                 C   sJ   |  s| st|st|s!| s#td|j dS dS dS dS dS )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.
    zInvalid field value: N)TryConsumeByteStringTryConsumeIdentifier_TryConsumeInt64_TryConsumeUint64TryConsumeFloatr   tokenr   r   r   r   r   r<    s   	z_Parser._SkipFieldValuec                 C   sJ   | d |ds| | |dr| | |ds| d dS )ziSkips over a repeated field value.

    Args:
      tokenizer: A tokenizer to parse the field value.
    r   r   r   N)r   r:  r<  r   rG  r   r   r   r;    s   





z_Parser._SkipRepeatedFieldValueFFNF)r   r   r   r   r   r   r   r   r   r  r   r   r  r  r  r?  r=  r<  r;  r   r   r   r   r   >  s,    

 :K!r   c                   @   sF  e Zd ZdZedZedejZedejZ	ed
ddgdd	 eD  Zed
ZedZdDd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#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Z d1d2 Z!d3d4 Z"d5d6 Z#d7d8 Z$d9d: Z%d;d< Z&d=d> Z'd?d@ Z(dAdB Z)dCS )Er   zProtocol 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_.+-]*c                 C   s   g | ]}d j |dqS )z0{qt}[^{qt}\n\\]*((\\.)+[^{qt}\n\\]*)*({qt}|\\?$))qt)r   )r   markr   r   r   
<listcomp>  s    
zTokenizer.<listcomp>z
[^\d\W]\w*z\w+Tc                 C   sr   d| _ d| _d| _d | _d| _t|| _d| _d| _d| _	d| _
|| _|r(| jp*| j| _d| _|   |   d S )Nr   r   ry   TF)	_positionr   r   _token_startrF  iter_lines_current_line_previous_line_previous_column_more_lines_skip_comments_WHITESPACE_OR_COMMENT_WHITESPACE_whitespace_patternr  _SkipWhitespace	NextToken)r   r   skip_commentsr   r   r   r     s"   

zTokenizer.__init__c                 C   s
   | j |kS r&   rF  r   rF  r   r   r   r:  0  r5   zTokenizer.LookingAtc                 C   s   | j  S )z^Checks the end of the text was reached.

    Returns:
      True iff the end was reached.
    r\  r'   r   r   r   r   3  s   zTokenizer.AtEndc                 C   sn   t | j| jkr5zt| j| _W n ty    d| _d| _Y d S w |  jd7  _d| _t | j| jksd S d S )Nry   Fr   r   )r   rQ  r   nextrP  StopIterationrT  r   r'   r   r   r   _PopLine;  s   zTokenizer._PopLinec                 C   sV   	 |    | j| j| j}|sd S |ddt k| _t|d}|  j|7  _q)NTr   rz   )	r`  rX  matchrQ  r   group_DEBUG_STRING_SILENT_MARKERr  r   )r   ra  lengthr   r   r   rY  G  s   zTokenizer._SkipWhitespacec                 C   s   | j |kr|   dS dS )zTries to consume a given piece of text.

    Args:
      token: Text to consume.

    Returns:
      True iff the text was consumed.
    TF)rF  rZ  r]  r   r   r   r   R  s   
	zTokenizer.TryConsumec                 C   s   |  |s| d| dS )zConsumes a piece of text.

    Args:
      token: Text to consume.

    Raises:
      ParseError: If the text couldn't be consumed.
    r   N)r   r   r]  r   r   r   r   `  s   
	zTokenizer.Consumec                 C   (   | j }| j|s| d|   |S )NzExpected comment.)rF  _COMMENTra  r   rZ  r   rH   r   r   r   ConsumeCommentl  s
   
zTokenizer.ConsumeCommentc                 C   s:   | j dko	| jdk}| j}|  }| j|ko| }||fS )zCConsumes a comment, returns a 2-tuple (trailing bool, comment str).r   )r   r   rR  rh  )r   just_startedbefore_parsingcommenttrailingr   r   r   ConsumeCommentOrTrailingComments  s   
z)Tokenizer.ConsumeCommentOrTrailingCommentc                 C   $   z|    W dS  ty   Y dS w NTF)r   r   r'   r   r   r   rB       zTokenizer.TryConsumeIdentifierc                 C   re  )Consumes protocol message field identifier.

    Returns:
      Identifier string.

    Raises:
      ParseError: If an identifier couldn't be consumed.
    zExpected identifier.)rF  _IDENTIFIERra  r   rZ  rg  r   r   r   r     s
   	
zTokenizer.ConsumeIdentifierc                 C   rn  ro  )r   r   r'   r   r   r   TryConsumeIdentifierOrNumber  rp  z&Tokenizer.TryConsumeIdentifierOrNumberc                 C   s,   | j }| j|s| d| |   |S )rq  z&Expected identifier or number, got %s.)rF  _IDENTIFIER_OR_NUMBERra  r   rZ  rg  r   r   r   r     s
   	z#Tokenizer.ConsumeIdentifierOrNumberc                 C   rn  ro  )ConsumeIntegerr   r'   r   r   r   TryConsumeInteger  rp  zTokenizer.TryConsumeIntegerc              
   C   B   zt | j}W n ty } z| t|d}~ww |   |S )zConsumes an integer number.

    Returns:
      The integer parsed.

    Raises:
      ParseError: If an integer couldn't be consumed.
    N)_ParseAbstractIntegerrF  
ValueErrorr   r   rZ  r   rH   r   r   r   r   ru       	zTokenizer.ConsumeIntegerc                 C   rn  ro  )r,  r   r'   r   r   r   rE    rp  zTokenizer.TryConsumeFloatc              
   C   rw  )zConsumes an floating point number.

    Returns:
      The number parsed.

    Raises:
      ParseError: If a floating point number couldn't be consumed.
    N)
ParseFloatrF  ry  r   r   rZ  rz  r   r   r   r,    r{  zTokenizer.ConsumeFloatc              
   C   rw  )zConsumes a boolean value.

    Returns:
      The bool parsed.

    Raises:
      ParseError: If a boolean value couldn't be consumed.
    N)	ParseBoolrF  ry  r   r   rZ  rz  r   r   r   r.    r{  zTokenizer.ConsumeBoolc                 C   rn  ro  )r1  r   r'   r   r   r   rA    rp  zTokenizer.TryConsumeByteStringc              
   C   s8   |   }zt|dW S  ty } z| |d}~ww )zConsumes a string value.

    Returns:
      The string parsed.

    Raises:
      ParseError: If a string value couldn't be consumed.
    rI   N)r1  r   r   _StringParseError)r   	the_bytesr   r   r   r   r0    s   	
zTokenizer.ConsumeStringc                 C   sJ   |   g}| jr | jd tv r ||    | jr | jd tv sd|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       )_ConsumeSingleByteStringrF  _QUOTESr   r   )r   the_listr   r   r   r1    s
   
	
zTokenizer.ConsumeByteStringc              
   C   s   | j }t|dk s|d tvr| d|f t|dk s%|d |d kr-| d|f zt|dd }W n tyK } z| t|d}~ww |   |S )aO  Consume 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.
    r   r   zExpected string but found: %rru   r   zString missing ending quote: %rN)	rF  r   r  r   r   	CUnescapery  r   rZ  )r   rP   rH   r   r   r   r   r    s   z"Tokenizer._ConsumeSingleByteStringc              
   C   sD   zt || j}W n ty } z| t|d }~ww |   |S r&   )	ParseEnumrF  ry  r   r   rZ  )r   rZ   rH   r   r   r   r   r3  +  s   zTokenizer.ConsumeEnumc                 C   s   t || jd | jd S )zCreates and *returns* a ParseError for the previously read token.

    Args:
      message: A message to set for the exception.

    Returns:
      A ParseError instance.
    r   )r   rR  rS  r   r   r   r   r   r   3  s   	z!Tokenizer.ParseErrorPreviousTokenc                 C   s&   t d| j d | | jd | jd S )z9Creates and *returns* a ParseError for the current token.r   z': r   )r   rQ  r   r   r  r   r   r   r   ?  s   zTokenizer.ParseErrorc                 C   s   |  dt| S )NzCouldn't parse string: )r   r   )r   r   r   r   r   r~  D  s   zTokenizer._StringParseErrorc                 C   s   | j | _| j| _d| _|  jt| j7  _|   | js!d| _dS | j	
| j| j}|s8| js8| j
| j| j}|rD|d}|| _dS | j| j | _dS )z Reads the next meaningful token.Fry   Nr   )r   rR  r   rS  r  r   rF  rY  rT  _TOKENra  rQ  rU  rf  rb  )r   ra  rF  r   r   r   rZ  G  s   


zTokenizer.NextTokenN)T)*r   r   r   r   recompilerW  	MULTILINErf  rV  r   r  r  rr  rt  r   r:  r   r`  rY  r   r   rh  rm  rB  r   rs  r   rv  ru  rE  r,  r.  rA  r0  r1  r  r3  r   r   r~  rZ  r   r   r   r   r     sT    

	

r   c                 C      t | dddS )Consumes a signed 32bit integer number from tokenizer.

  Args:
    tokenizer: A tokenizer used to parse the number.

  Returns:
    The integer parsed.

  Raises:
    ParseError: If a signed 32bit integer couldn't be consumed.
  TF	is_signedis_long_ConsumeIntegerr   r   r   r   r  b     r  c                 C      t | dddS )zConsumes an unsigned 32bit integer number from tokenizer.

  Args:
    tokenizer: A tokenizer used to parse the number.

  Returns:
    The integer parsed.

  Raises:
    ParseError: If an unsigned 32bit integer couldn't be consumed.
  Fr  r  r  r   r   r   r&  q  r  r&  c                 C   $   zt |  W dS  ty   Y dS w ro  )r#  r   r  r   r   r   rC    rp  rC  c                 C   r  )r  Tr  r  r  r   r   r   r#    r  r#  c                 C   r  ro  )r)  r   r  r   r   r   rD    rp  rD  c                 C   r  )zConsumes an unsigned 64bit integer number from tokenizer.

  Args:
    tokenizer: A tokenizer used to parse the number.

  Returns:
    The integer parsed.

  Raises:
    ParseError: If an unsigned 64bit integer couldn't be consumed.
  FTr  r  r  r   r   r   r)    r  r)  c              
   C   sH   z
t | j||d}W n ty } z| t|d}~ww |   |S )aU  Consumes an integer number from tokenizer.

  Args:
    tokenizer: A tokenizer used to parse the number.
    is_signed: True if a signed integer must be parsed.
    is_long: True if a long integer must be parsed.

  Returns:
    The integer parsed.

  Raises:
    ParseError: If an integer with given characteristics couldn't be consumed.
  r  N)r   rF  ry  r   r   rZ  )r   r  r  rH   r   r   r   r   r    s   r  c                 C   s.   t | }tdt| t|  }|| |S )a  Parses 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.
  ru   )rx  _INTEGER_CHECKERSint
CheckValue)rP   r  r  rH   checkerr   r   r   r     s   
r   c                 C   sT   | }t d| }|r|dd |d } zt| dW S  ty)   td| w )zParses an integer without checking size/signedness.

  Args:
    text: The text to parse.

  Returns:
    The integer value.

  Raises:
    ValueError: Thrown Iff the text is not a valid integer.
  z(-?)0(\d+)$r   0oru   r   zCouldn't parse integer: %s)r  ra  rb  r  ry  )rP   	orig_textc_octal_matchr   r   r   rx    s   rx  c                 C   s   zt | W S  tyF   t| r#| d dkrt d Y S t d Y S t| r.t d Y S z
t | dW  Y S  tyE   td|  w w )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-infinfnanfzCouldn't parse float: %s)floatry  _FLOAT_INFINITYra  
_FLOAT_NANr;   rP   r   r   r   r|    s   


r|  c                 C   s    | dv rdS | dv rdS t d)zParse a boolean value.

  Args:
    text: Text to parse.

  Returns:
    Boolean values parsed

  Raises:
    ValueError: If text is not a valid boolean.
  )r   t1TrueT)r   r  0FalseFzExpected "true" or "false".)ry  r  r   r   r   r}    s
   r}  c                 C   s   | j }zt|d}W n ty)   |j|d}|du r%td|j|f Y |jS w | j js0|S |j|d}|du rDtd|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.
  r   Nz%Enum type "%s" has no value named %s.z+Enum type "%s" has no value with number %d.)	r   r  ry  values_by_namerM   r   	is_closedr   r   )rZ   ri   enum_descriptorr   r   r   r   r   r  (  s(   
r  rH  )FF)Cr   
__author__encodings.raw_unicode_escape	encodingsencodings.unicode_escaper-   r   r  google.protobuf.internalr   r   rl   r   r   r   __all__Uint32ValueCheckerInt32ValueCheckerUint64ValueCheckerInt64ValueCheckerr  r  
IGNORECASEr  r  	frozensetr  r   rc  r   r   r   r   objectr,   r   r   rL   r   r[   rc   r	   r
   r   rt   r   r   r:   r   r   r   r   r   r   
_Tokenizerr  r&  rC  r#  rD  r)  r  r   rx  r|  r}  r  r   r   r   r   <module>   s  
O	 
@

  :
<
(
$
"   J  \

 