
    g                         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	  G d d	e	j                        Zdd
Zd Zd Zd Zd Zy)z?Utilities for handling YAML schemas for gcloud update commands.    )absolute_import)division)unicode_literals)	arg_utils)yaml_arg_schema)
exceptionsc                       e Zd ZdZy)NoFieldsSpecifiedErrorz7Raises when no arguments specified for update commands.N)__name__
__module____qualname____doc__     2lib/googlecloudsdk/command_lib/util/apis/update.pyr
   r
      s    ?r   r
   c           	         | j                         st        d      t               }t        |j                  j
                        D ]  }|j                  t        || ||             ! dj                  t        |            S )a  Gets the fieldMask that is required for update api calls.

  Args:
    args: The argparse parser.
    spec: The CommandData class.
    mask_path: string, the dotted path of mask in the api method
    is_dotted: Boolean, True if the dotted path of the name is returned.

  Returns:
    A String, represents a mask specifying which fields in the resource should
    be updated.

  Raises:
    NoFieldsSpecifiedError: this error would happen when no args are specified.
  z4Must specify at least one valid parameter to update.,)
GetSpecifiedArgsr
   set_GetSpecParams	argumentsparamsupdate_GetMaskFieldsjoinsorted)argsspec	mask_path	is_dotted	field_setparams         r   GetMaskStringr#      ss      
			 
 >@ @ e)dnn334e^E4IFG 5 
&#	$$r   c                     t               }| j                  |      s|S | j                  D ]#  }t        |||      }|s|j	                  |       % |S )a  Gets the fieldMask based on the yaml arg and the arguments specified.

  Args:
    param: yaml_arg_schema.YAMLArgument, the yaml argument added to parser
    args: parser_extensions.Namespace, user specified arguments
    mask_path: str, path to where update mask applies
    is_dotted: bool, True if the dotted path of the name is returned

  Returns:
    Set of fields (str) to add to the update mask
  )r   IsApiFieldSpecified
api_fields_ExtractMaskFieldadd)r"   r   r   r    r!   	api_field
mask_fields          r   r   r   :   sT     e)		"	"4	(##i"9iCJmmJ $ 
r   c              #      K   | D ]?  }t        |t        j                        rt        |j                        D ]  }|  <| A yw)zRecursively yields all the params in the spec.

  Args:
    params: List of Argument or ArgumentGroup objects.

  Yields:
    All the Argument objects in the command spec.
  N)
isinstancer   ArgumentGroupr   r   )r   r"   ps      r   r   r   Q   sA      e%667eoo.! / k s   AAc                     t        | j                  d            }|j                  d      }|r.d|v r|j                  d      }|d| }dj                  ||d       S ||   S )a  Extracts the api field name which constructs the mask used for request.

  For most update requests, you have to specify which fields in the resource
  should be updated. This information is stored as updateMask or fieldMask.
  Because resource and mask are in the same path level in a request, this
  function uses the mask_path as the guideline to extract the fields need to be
  parsed in the mask.

  Args:
    mask_path: string, the dotted path of mask in an api method, e.g. updateMask
      or updateRequest.fieldMask. The mask and the resource would always be in
      the same level in a request.
    api_field: string, the api field name in the resource to be updated and it
      is specified in the YAML files, e.g. displayName or
      updateRequest.instance.displayName.
    is_dotted: Boolean, True if the dotted path of the name is returned.

  Returns:
    String, the field name of the resource to be updated..

  .additionalPropertiesN)lensplitindexr   )r   r)   r    levelapi_field_listrepeated_indexs         r   r'   r'   b   sn    , iooc"
#%??3'./%++,BCn%o~6n88N56*++%  r   c                 :   d} | j                                }|D ]  }t        ||      s|c S  | j                  ra| j                  }d}t        ||      r t        j                  ||      j
                  }|D ]"  }t        ||      sdj                  ||      c S  y)zGets the dotted path of mask in the api method.

  Args:
    method: APIMethod, The method specification.

  Returns:
    String or None.
  )
updateMask	fieldMaskNz{}.{})GetRequestTypehasattrrequest_fieldr   GetFieldFromMessagetypeformat)methodpossible_mask_fieldsmessagemaskr=   request_messages         r   GetMaskFieldPathrF      s     5#F!!#%' #dwk # ((MOw&!55g6CEEIT  %	$	'~~mT22 % 
r   N)T)r   
__future__r   r   r   $googlecloudsdk.command_lib.util.apisr   r   googlecloudsdk.corer   Errorr
   r#   r   r   r'   rF   r   r   r   <module>rK      sK    F &  ' : @ *@Z-- @%8."!Dr   