
                             d 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
 ddlmZ ddlZd	 Zd
 Z G d dej                         Z G d de      Z G d de      Z	 ddZy)z=Utilities for interacting with message classes and instances.    )absolute_import)division)unicode_literals)messages)encoding)
exceptionsNc                 "    |rt        | |      S | S )ax  Updates given message from diff object recursively.

  The function recurses down through the properties of the diff object,
  checking, for each key in the diff, if the equivalent property exists on the
  message at the same depth. If the property exists, it is set to value from the
  diff. If it does not exist, that diff key is silently ignored. All diff keys
  are assumed to be strings.

  Args:
    message: An apitools.base.protorpclite.messages.Message instance.
    diff: A dict of changes to apply to the message
      e.g. {'settings': {'availabilityType': 'REGIONAL'}}.

  Returns:
    The modified message instance.
  )_UpdateMessageHelper)messagediffs     +lib/googlecloudsdk/api_lib/util/messages.pyUpdateMessager      s    " 
..	.    c                     t        j                  |      D ]I  \  }}t        | |      st        |t              rt        t        | |      ||          =t        | ||       K | S )N)six	iteritemshasattr
isinstancedictr
   getattrsetattr)r   r   keyvals       r   r
   r
   2   sU    --%hc3w	C	WWc2DI>c" & 
.r   c                       e Zd ZdZy)Errorz8Indicates an error with an encoded protorpclite message.N__name__
__module____qualname____doc__ r   r   r   r   <   s    @r   r   c                   0    e Zd ZdZed        Zed        Zy)DecodeErrorz6Indicates an error in decoding a protorpclite message.c           	      ,   |D cg c]  }t        j                  |       }}t        |      dkD  r9|j                  dj	                  dj                  t        |                         n|r|j                  |d          dj                  |      S c c}w )a'  Returns a string representation of a path to a proto field.

    The return value represents one or more fields in a python dictionary
    representation of a message (json/yaml) that could not be decoded into the
    message as a string. The format is a dot separated list of python like sub
    field references (name, name[index], name[name]). The final element of the
    returned dot separated path may be a comma separated list of names enclosed
    in curly braces to represent multiple subfields (see examples)

    Examples:
      o Reference to a single field that could not be decoded:
        'a.b[1].c[x].d'

      o Reference to two subfields
        'a.b[1].c[x].{d,e}'

    Args:
      edges: List of objects representing python field references
             (__str__ suitably defined.)
      field_names: List of names for subfields of the message
         that could not be decoded.

    Returns:
      A string representation of a python reference to a filed or
      fields in a message that could not be decoded as described
      above.
       z{{{}}},r   .)r   	text_typelenappendformatjoinsorted)clsedgesfield_namesedgepaths        r   _FormatProtoPathzDecodeError._FormatProtoPathC   s{    < -22EDCMM$ED2
;! kk(//#((6++>"?@A	 kk+a.!88D> 3s   Bc                     t        |      j                  }dj                  |      }|D cg c](  \  }}dj                  || j                  ||            * }}} | dj	                  |g|z               S c c}}w )a
  Returns a DecodeError from a list of locations of errors.

    Args:
      message: The protorpc Message in which a parsing error occurred.
      errors: List[(edges, field_names)], A list of locations of errors
          encountered while decoding the message.
    z/Failed to parse value(s) in protobuf [{type_}]:)type_z  {type_}.{path})r5   r2   
)typer   r+   r3   r,   )r.   r   errorsr5   base_msgr/   r0   error_pathss           r   FromErrorPathszDecodeError.FromErrorPathsn   s     M""E@GG H H .45-3)uk &,,#..ukB - D-3  5 tyy(k12335s   -A8N)r   r   r   r    classmethodr3   r;   r!   r   r   r#   r#   @   s,    >( (T 4 4r   r#   c                       e Zd ZdZy)ScalarTypeMismatchErrorzGIncicates a scalar property was provided a value of an unexpected type.Nr   r!   r   r   r>   r>      s    Or   r>   c           	      ^   	 t        j                  | |      }t        t        j                  |            }|r|rt        j                  ||      |S # t        j                  $ r>}t        dj                  |j                  t        j                  |                  d}~wt        $ r  w xY w)a)  Convert "dict_" to a message of type message_type and check for errors.

  A common use case is to define the dictionary by deserializing yaml or json.

  Args:
    dict_: The dict to parse into a protorpc Message.
    message_type: The protorpc Message type.
    throw_on_unexpected_fields: If this flag is set, an error will be raised if
    the dictionary contains unrecognized fields.

  Returns:
    A message of type "message_type" parsed from "dict_".

  Raises:
    DecodeError: One or more unparsable values were found in the parsed message.
  zBFailed to parse value in protobuf [{type_}]:
  {type_}.??: "{msg}")r5   msgN)	_encodingDictToMessagelistUnrecognizedFieldIterr#   r;   	_messagesValidationErrorr>   r+   r   r   r(   AttributeError)dict_message_typethrow_on_unexpected_fieldsr   r8   es         r   DictToMessageWithErrorCheckrL      s    &%%e\:G )11':;F,&&w77N# 
	"	" @ "	  &''S]]1-= !' !?@ @ 
 
 
	
s   A B,%9BB,)T)r    
__future__r   r   r   apitools.base.protorpcliter   rE   apitools.base.pyr   rA   googlecloudsdk.corer   r   r   r
   r   r#   r>   rL   r!   r   r   <module>rQ      sd    D &  ' < 2 * 
,AJ A=4% =4@Pk P <@&r   