o
    w                     @   s2  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 eddd	gZed
ddgZG dd deZG dd dedg dZi Zi Zi Zi Zdd Zdd Zdd Zdd Zdd ZdbddZdd Zd d! Zd"d# Z	$dcd%d&Zd'd( Z d)d* Z!dcd+d,Z"d-d. Z#d/d0 Z$d1d2 Z%d3d4 Z&G d5d6 d6e	j'Z(d7d8 Z)d9d: Z*d;d< Z+d=d> Z,d?d@ Z-dAdB Z.dCdD Z/dEdF Z0dGdH Z1ee.e/ej2 i Z3i Z4	dbdIdJZ5	dbdKdLZ6dddMdNZ7dddOdPZ8		dedQdRZ9dSdT Z:dUdV Z;dWdX Z<dYdZ Z=d[d\ Z>d]d^ Z?dfd`daZ@dS )gz@Common code for converting proto to other formats, such as JSON.    N)message_types)messages)	protojson)
exceptions_CodecencoderdecoderCodecResultvaluecompletec                   @   s   e Zd ZdZdZdZdZdS )EdgeTypez'The type of transition made by an edge.         N)__name__
__module____qualname____doc__SCALARREPEATEDMAP r   r   I/tmp/google-cloud-sdk/lib/third_party/apitools/base/py/encoding_helper.pyr   $   s
    r   c                   @   s   e Zd ZdZdZdd ZdS )	ProtoEdgea.  A description of a one-level transition from a message to a value.

    Protobuf messages can be arbitrarily nested as fields can be defined with
    any "message" type. This nesting property means that there are often many
    levels of proto messages within a single message instance. This class can
    unambiguously describe a single step from a message to some nested value.

    Properties:
      type_: EdgeType, The type of transition represented by this edge.
      field: str, The name of the message-typed field.
      index: Any, Additional data needed to make the transition. The semantics
          of the "index" property change based on the value of "type_":
            SCALAR: ignored.
            REPEATED: a numeric index into "field"'s list.
            MAP: a key into "field"'s mapping.
    r   c                 C   s"   | j tjkr	| jS d| j| jS )Nz{}[{}])type_r   r   fieldformatindexselfr   r   r   __str__?   s   zProtoEdge.__str__N)r   r   r   r   	__slots__r    r   r   r   r   r   +   s    r   )r   r   r   c                    s    fdd}|S )z;Register field_name as a container for unrecognized fields.c                    s    t | < | S N)_UNRECOGNIZED_FIELD_MAPPINGScls
field_namer   r   RegisterO   s   z'MapUnrecognizedFields.<locals>.Registerr   )r'   r(   r   r&   r   MapUnrecognizedFieldsM   s   r)   c                        fdd}|S )z9Register a custom encoder/decoder for this message class.c                       t  dt| < | S N)r   r   )r   _CUSTOM_MESSAGE_CODECSr$   r   r   r   r   r(   W      z,RegisterCustomMessageCodec.<locals>.Registerr   r   r   r(   r   r.   r   RegisterCustomMessageCodecU      r1   c                    r*   )z1Register a custom encoder/decoder for this field.c                    r+   r,   )r   _CUSTOM_FIELD_CODECSr   r.   r   r   r(   _   r/   z*RegisterCustomFieldCodec.<locals>.Registerr   r0   r   r.   r   RegisterCustomFieldCodec]   r2   r5   c                    r*   )z>Register a custom encoder/decoder for all fields of this type.c                    r+   r,   )r   _FIELD_TYPE_CODECS)
field_typer.   r   r   r(   g   s   
z(RegisterFieldTypeCodec.<locals>.Registerr   r0   r   r.   r   RegisterFieldTypeCodece   s   r8   c                 C   s   t t| t| S )zMake a deep copy of a message.)JsonToMessagetypeMessageToJsonmessager   r   r   CopyProtoMessagen   s   r>   c                 C   s   t  | }t|| |S )z"Convert the given message to JSON.)_ProtoJsonApiToolsGetencode_message_IncludeFields)r=   include_fieldsresultr   r   r   r;   s   s   r;   c                 C   s   t  | |S )z9Convert the given JSON to a message of type message_type.)r?   r@   decode_message)message_typer=   r   r   r   r9   y      r9   c                 C   s   t |t| S )z?Convert the given dictionary to a message of type message_type.r9   jsondumps)drF   r   r   r   DictToMessage   rG   rL   c                 C      t t| S )z*Convert the given message to a dictionary.rI   loadsr;   r<   r   r   r   MessageToDict      rP   Fc                 C   sD   |   }|r
t|}g }|D ]\}}||j||d q||dS )z>Convert the given dictionary to an AdditionalProperty message.keyr
   )additionalProperties)itemssortedappendAdditionalProperty)
propertiesadditional_property_type
sort_itemsrU   map_rS   r
   r   r   r   DictToAdditionalPropertyMessage   s   

r]   c                 C   s   t | t|S )zAConvert the given python value to a message of type message_type.rH   )rF   r
   r   r   r   PyValueToMessage   rG   r^   c                 C   rM   )z,Convert the given message to a python value.rN   r<   r   r   r   MessageToPyValue   rQ   r_   c                    s   dd}dd }t| trBd}| D ]}|r!|dd|d   7 }|t|fd	|i|d
 7 }q|r<|dd|  7 }|d7 }|S t| tjrt| jd } ds[| jd | }t	dd | 
 D }|D ].}| |}	|r{|dd|d   7 }t| |	j}
||	jd t|
fd	|i| d
 7 }qh|r|dd|  7 }|d7 }|S t| tjr drt| dkr| dd } t| tjrG  fdddtj t| j| j| j| j| j| j| j | jd} t| S )a7  Return a repr-style string for a protorpc message.

    protorpc.Message.__repr__ does not return anything that could be considered
    python code. Adding this function lets us print a protorpc message in such
    a way that it could be pasted into code later, and used to compare against
    other things.

    Args:
      msg: protorpc.Message, the message to be repr'd.
      multiline: bool, True if the returned string should have each field
          assignment on its own line.
      **kwargs: {str:str}, Additional flags for how to format the string.

    Known **kwargs:
      shortstrings: bool, True if all string values should be
          truncated at 100 characters, since when mocking the contents
          typically don't matter except for IDs, and IDs are usually
          less than 100 characters.
      no_modules: bool, True if the long module name should not be printed with
          each type.

    Returns:
      str, A string of valid python (assuming the right imports have been made)
      that recreates the message passed into this function.

    indentr   c                 S   s    t | } | ddd | d< | S )Nr`   r      )dictgetkwargsr   r   r   IndentKwargs   s   z#MessageToRepr.<locals>.IndentKwargs[
 ra   	multiline,](
no_modules.c                 S      g | ]}|j qS r   name).0r   r   r   r   
<listcomp>       z!MessageToRepr.<locals>.<listcomp>=)shortstringsd   Nc                       s*   e Zd Z fddZfddZ  ZS )z$MessageToRepr.<locals>.SpecialTZInfoc                    s   t  |   || _d S r"   )super__init__offset)r   r|   )SpecialTZInfo	__class__r   r   r{      s   
z-MessageToRepr.<locals>.SpecialTZInfo.__init__c                    s(   dt | j d } dsd| }|S )NzTimeZoneOffset(rw   rn   z apitools.base.protorpclite.util.)reprr|   rc   )r   srd   r   r   __repr__   s   
z-MessageToRepr.<locals>.SpecialTZInfo.__repr__)r   r   r   r{   r   __classcell__r   r}   re   r~   r   r}      s    r}   )rc   
isinstancelistMessageToReprr   Messager:   r   r   rV   
all_fieldsfield_by_namegetattrrr   sixstring_typeslendatetimetzinfoyearmonthdayhourminutesecondmicrosecond	utcoffsetr   )msgrj   re   r`   rf   r   itemnamesrr   r   r
   r   r   r   r      sj   


r   c                 C   s0   |D ]}|t | vrtd| t| |} q| S )Nzno field "%s")dirKeyErrorr   )r=   
field_pathr   r   r   r   	_GetField   s
   r   c                 C   s.   |d d D ]}|  |i } q|| |d < d S )N)
setdefault)dictblobr   r
   r   r   r   r   	_SetField  s   r   c              
   C   s   |du r| S t | }|D ]1}zt||d}d}t|tr!g }W n ty4   td|t	|f w t
||d| qt |S )z0Add the requested fields to the encoded message.Nro   z'No field named %s in message of type %s)rI   rO   r   splitr   r   r   r   InvalidDataErrorr:   r   rJ   )encoded_messager=   rC   rD   r'   r
   	nullvaluer   r   r   rB   	  s&   


rB   c                 C   s6   t t| |d t tt| |d g}dd |D S )Nc                 S   s   g | ]}|d ur|qS r"   r   rs   xr   r   r   rt   !  s    z#_GetFieldCodecs.<locals>.<listcomp>)r   r3   rc   r6   r:   )r   attrrD   r   r   r   _GetFieldCodecs  s   r   c                       sT   e Zd ZdZdZedd Z fddZ fddZ fd	d
Z	 fddZ
  ZS )r?   z&JSON encoder used by apitools clients.Nc                 C   s   | j d u r	|  | _ | j S r"   )	_INSTANCEr$   r   r   r   r@   )  s   
z_ProtoJsonApiTools.Getc                    sP   |t v rt | |S t||}tt| ||}t||}t||}t||S r"   )	r-   r   _DecodeCustomFieldNamesrz   r?   rE   _ProcessUnknownEnums_ProcessUnknownMessages_DecodeUnknownFields)r   rF   r   rD   r   r   r   rE   /  s   




z!_ProtoJsonApiTools.decode_messagec                    s   t |dD ]}|||}|j}|jr|  S qt|tjr)| |jt	|}|S t|tj
rYt|j|dp7|}ztt| ||}W |S  tjyX   t|tjsS d}Y |S w tt| ||}|S )zDecode the given JSON value.

        Args:
          field: a messages.Field for the field we're decoding.
          value: a python value we'd like to decode.

        Returns:
          A value suitable for assignment to field.
        r   	json_nameN)r   r
   r   r   r   MessageFieldrE   rF   rI   rJ   	EnumFieldGetCustomJsonEnumMappingr:   rz   r?   decode_fieldDecodeErrorr   r   )r   r   r
   r   rD   field_valuer   r   r   r   :  sH   

	z_ProtoJsonApiTools.decode_fieldc                    s|   t |tjrdd fdd|D  S t|tv r#tt| |S t|}tt	 
|}t||}tjt|ddS )Nz[%s]z, c                 3   s    | ]}  |V  qd S r"   )rA   r   r   r   r   	<genexpr>]  s    z4_ProtoJsonApiTools.encode_message.<locals>.<genexpr>T)	sort_keys)r   r   	FieldListjoinr:   r-   r   _EncodeUnknownFieldsrz   r?   rA   _EncodeCustomFieldNamesrI   rJ   rO   )r   r=   rD   r   r   r   rA   [  s   

z!_ProtoJsonApiTools.encode_messagec                    s   t  dD ]}| |}|j}|jr|  S qt tjr6 jr* fdd|D }nt j|j	d}|r6|S t tj
rJt tjsJt| |}tt|  |S )zEncode the given value as JSON.

        Args:
          field: a messages.Field for the field we're encoding.
          value: a value for field.

        Returns:
          A python value suitable for json.dumps.
        r   c                    s"   g | ]}t  j|jd p|jqS )python_name)r   r:   rr   )rs   er4   r   r   rt   z  s    z3_ProtoJsonApiTools.encode_field.<locals>.<listcomp>r   )r   r
   r   r   r   r   repeatedr   r:   rr   r   r   DateTimeFieldrI   rO   rA   rz   r?   encode_field)r   r   r
   r   rD   remapped_valuer   r4   r   r   i  s*   



z_ProtoJsonApiTools.encode_field)r   r   r   r   r   classmethodr@   rE   r   rA   r   r   r   r   r   r   r?   $  s    
!r?   c                 C   s   t t| }|du r| S | |}t|tjstd|j	}t|j
tjr1t| t||}nt| |}t| || t| di  | S )z;Rewrite unknown fields in message into message.destination.Nz>Unrecognized fields must be mapped to a compound message type._Message__unrecognized_fields)r#   rc   r:   r   r   r   r   r   InvalidDataFromServerErrorrF   r
   _DecodeUnknownMessagesrI   rO   _DecodeUnrecognizedFieldssetattr)r=   r   destination
pair_field	pair_type
new_valuesr   r   r   r     s"   

r   c           
      C   sp   |j j}g }dd |  D }t|D ]!\}}||v rqt||}|j jr*t|}|||d}	||	 q|S )z<Process unknown fields in encoded_message of a message type.c                 S   rp   r   rq   r   r   r   r   rt     ru   z*_DecodeUnknownMessages.<locals>.<listcomp>rR   )	r
   r:   r   r   	iteritemsr^   r   _AsMessageListrW   )
r=   r   r   r7   r   all_field_namesrr   
value_dictr
   new_pairr   r   r   r     s   
r   c              	   C   s   g }t  }|  D ]G}| |\}}|d}t|tjr&t||j	j
}n||j	|}zt|}	W n tyE   t |j|}	Y nw ||	|d}
||
 q
|S )z'Process unrecognized fields in message.r
   rR   )r?   r@   all_unrecognized_fieldsget_unrecognized_field_infor   r   r   r   rL   r
   rF   r   strUnicodeEncodeErrorr   	ProtoJsonrS   rW   )r=   r   r   codecunknown_fieldr
   _
value_typedecoded_valuenew_pair_keyr   r   r   r   r     s(   
r   c                 C   s   t  }|t| || S r"   )r   r   rE   r:   rA   )r=   r   r   r   r   !_CopyProtoMessageVanillaProtoJson  s   r   c                 C   s   t t| }|du r| S t| }| |}t|tjs#t	d| |j
}|d}|j}t| |}t }|D ]}	|||	j}
||	j|
| q9t||g  |S )z6Remap unknown fields in message out of message.source.NzInvalid pairs field %sr
   )r#   rc   r:   r   r   r   r   r   r   InvalidUserInputErrorrF   variantr   r?   r@   r   r
   set_unrecognized_fieldrS   r   )r=   sourcerD   pairs_field
pairs_typevalue_fieldvalue_variantpairsr   pairencoded_valuer   r   r   r     s&   


r   c                 C   sP   z| j rdd |D }nt|}d}W n ty!   |}d}Y nw t||dS )z,Encode the bytes in value as urlsafe base64.c                 S   s   g | ]}t |qS r   )base64urlsafe_b64encode)rs   byter   r   r   rt     s    z$_SafeEncodeBytes.<locals>.<listcomp>TFr
   r   )r   r   r   	TypeErrorr	   )r   r
   rD   r   r   r   r   _SafeEncodeBytes  s   
r   c                 C   s>   zt t|}d}W n ty   |}d}Y nw t||dS )z+Decode the urlsafe base64 value into bytes.TFr   )r   urlsafe_b64decoder   r   r	   )unused_fieldr
   rD   r   r   r   r   _SafeDecodeBytes  s   r   c                 C   s   |s| S t t|}|  D ]2}t|tjrB|j|v rB| 	|j}|j
r1t|t||j ks5|du rB| |j||j tjj q| S )af  Add unknown enum values from encoded_message as unknown fields.

    ProtoRPC diverges from the usual protocol buffer behavior here and
    doesn't allow unknown fields. Throwing on unknown fields makes it
    impossible to let servers add new enum values and stay compatible
    with older clients, which isn't reasonable for us. We simply store
    unrecognized enum values as unknown fields, and all is well.

    Args:
      message: Proto message we've decoded thus far.
      encoded_message: JSON string we're decoding.

    Returns:
      message, with any unknown enums stored as unrecognized fields.
    N)rI   rO   r   
ensure_strr   r   r   r   rr   get_assigned_valuer   r   r   VariantENUM)r=   r   decoded_messager   r
   r   r   r   r     s   
r   c                    sp   |s| S t t|}dd |  D t|     fdd| D }|D ]}| ||| t	j
j q(| S )a?  Store any remaining unknown fields as strings.

    ProtoRPC currently ignores unknown values for which no type can be
    determined (and logs a "No variant found" message). For the purposes
    of reserializing, this is quite harmful (since it throws away
    information). Here we simply add those as unknown fields of type
    string (so that they can easily be reserialized).

    Args:
      message: Proto message we've decoded thus far.
      encoded_message: JSON string we're decoding.

    Returns:
      message, with any remaining unrecognized fields saved.
    c                 S   rp   r   rq   r   r   r   r   rt   2  ru   z+_ProcessUnknownMessages.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   r   message_fieldsr   r   rt   4  s    )rI   rO   r   r   r   r   r   keysr   r   r   STRING)r=   r   r   missing_fieldsr'   r   r   r   r     s   r   c                 C   s^   t | tjstd|  ||  vrtd|| f t| i }t	d| || |||< dS )a  Add a custom wire encoding for a given enum value.

    This is primarily used in generated code, to handle enum values
    which happen to be Python keywords.

    Args:
      enum_type: (messages.Enum) An enum type
      python_name: (basestring) Python name for this value.
      json_name: (basestring) JSON name to be used on the wire.
      package: (NoneType, optional) No effect, exists for legacy compatibility.
    z.Cannot set JSON enum mapping for non-enum "%s"z%Enum value %s not a value for type %senumN)

issubclassr   Enumr   TypecheckErrorr   r   _JSON_ENUM_MAPPINGSr   _CheckForExistingMappings)	enum_typer   r   packagefield_mappingsr   r   r   AddCustomJsonEnumMappingE  s   
r  c                 C   sp   t | tjstd|  z| |}W n ty$   td|| f w t	| i }t
d| || |||< dS )a  Add a custom wire encoding for a given message field.

    This is primarily used in generated code, to handle enum values
    which happen to be Python keywords.

    Args:
      message_type: (messages.Message) A message type
      python_name: (basestring) Python name for this value.
      json_name: (basestring) JSON name to be used on the wire.
      package: (NoneType, optional) No effect, exists for legacy compatibility.
    z2Cannot set JSON field mapping for non-message "%s"z#Field %s not recognized for type %sr   N)r   r   r   r   r  r   r   r   _JSON_FIELD_MAPPINGSr   r  )rF   r   r   r  r   r  r   r   r   AddCustomJsonFieldMapping]  s$   r
  c                 C      t | d||tdS )z=Return the appropriate remapping for the given enum, or None.r   r   r   mappings)_FetchRemappingr  )r  r   r   r   r   r   r   y     r   c                 C   r  )z>Return the appropriate remapping for the given field, or None.r   r  )r  r	  )rF   r   r   r   r   r   GetCustomJsonFieldMapping  r  r  c                    s|   |rrt d| |sst d|f || i   r<|r& |S r<t  v r< fdd D d S dS )z>Common code for fetching a key or value from a remapping dict.z>Cannot specify both python_name and json_name for %s remappingz=Must specify either python_name or json_name for %s remappingc                    s   g | ]
} | kr|qS r   r   )rs   kfield_remappingsr   r   r   rt     s    z#_FetchRemapping.<locals>.<listcomp>r   N)r   r   rc   r   values)	type_namemapping_typer   r   r  r   r  r   r    s,   
r  c                 C   s   | dkrt }n| dkrt}|||d}|dur%||kr%td| ||f |||d}|dur=||kr?td| ||f dS dS )z5Validate that no mappings exist for the given values.r   r   r   Nz6Cannot add mapping for %s "%s", already mapped to "%s"r   )r  r   r   r   )r  rF   r   r   getter	remappingr   r   r   r    s&   r  c                 C   sX   t tt| i  }|r*t|}|D ]\}}||v r$||||< qt|}|S r"   )	r   r	  rc   r:   rU   rI   rO   poprJ   )r=   r   r  r   r   r   r   r   r   r     s   

r   c                 C   sT   t | i }|r(t|}t| D ]\}}||v r"||||< qt|}|S r"   )r	  rc   rI   rO   r   rU   r  rJ   )rF   r   r  r   r   r   r   r   r   r     s   

r   c                    sP   ddl m   fdd}|| stdt|  jr| j} t|  jr&| j} | S )z1Convert the provided list-as-JsonValue to a list.r   extra_typesc                    s*   t |  jrdS t |  jr| jrdS dS )z6Return True if msg is a repeated value as a JsonValue.TF)r   	JsonArray	JsonValuearray_value)r   r  r   r   _IsRepeatedJsonValue  s
   z,_AsMessageList.<locals>._IsRepeatedJsonValuez"invalid argument to _AsMessageList)apitools.base.pyr  
ValueErrorr   r  r  r  entries)r   r  r   r  r   r     s   r   c                 C   sD   |  |j}t|tjsdS z	|d}W |jS  ty!   Y dS w )z3Returns whether the "field" is actually a map-type.FrT   )r   rr   r   r   r   r   r   r   )r=   r   r
   additional_propertiesr   r   r   _IsMap  s   r$  c                 c   sB    t | |sJ | |j}|d}|D ]	}|j|jfV  qdS )z/Yields the (key, value) pair of the map values.rT   N)r$  r   rr   rS   r
   )r=   r   map_messager#  kv_pairr   r   r   	_MapItems  s   r'  r   c                 c   s,   t | tjs	dS |  }|r||fV  dS |  D ]y}| |j}|jrKt|D ] \}}t	t
j|j|}t|||f }|D ]	\}	}
|	|
fV  q?q)qt| |rwt| |D ] \}}t	t
j|j|}t|||f }|D ]	\}	}
|	|
fV  qkqUqt	t
j|jd}t|||f }|D ]	\}	}
|	|
fV  qqdS )a  Yields the locations of unrecognized fields within "message".

    If a sub-message is found to have unrecognized fields, that sub-message
    will not be searched any further. We prune the search of the sub-message
    because we assume it is malformed and further checks will not yield
    productive errors.

    Args:
      message: The Message instance to search.
      _edges: Internal arg for passing state.

    Yields:
      (edges_to_message, field_names):
        edges_to_message: List[ProtoEdge], The edges (relative to "message")
            describing the path to the sub-message where the unrecognized
            fields were found.
        field_names: List[Str], The names of the field(s) that were
            unrecognized in the sub-message.
    N)r   r   r   r   r   r   rr   r   	enumerater   r   r   UnrecognizedFieldIterr$  r'  r   r   )r=   _edgesfield_namesr   r
   ir   repeated_edgeiter_r   yrS   map_edgescalar_edger   r   r   r)    s>   

r)  r"   )F)NN)NNN)r   )Ar   r   collectionsr   rI   r   apitools.base.protorpcliter   r   r   r   r   
namedtupler   r	   objectr   r   r#   r-   r3   r6   r)   r1   r5   r8   r>   r;   r9   rL   rP   r]   r^   r_   r   r   r   rB   r   r   r?   r   r   r   r   r   r   r   r   r   
BytesFieldr  r	  r  r
  r   r  r  r  r   r   r   r$  r'  r)  r   r   r   r   <module>   s   	


[d





