
    71                         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  G d de	      Z
 G d	 d
e
      Z G d de      Z G d de
      Zy)zClasses to define how concept args are added to argparse.

A PresentationSpec is used to define how a concept spec is presented in an
individual command, such as its help text. ResourcePresentationSpecs are
used for resource specs.
    )absolute_import)division)unicode_literals)util)info_holdersc                   P    e Zd ZdZ	 	 	 	 	 	 ddZed        Zed        Zd Zd Z	y)	PresentationSpeca5  Class that defines how concept arguments are presented in a command.

  Attributes:
    name: str, the name of the main arg for the concept. Can be positional or
      flag style (UPPER_SNAKE_CASE or --lower-train-case).
    concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, The spec that
      specifies the concept.
    group_help: str, the help text for the entire arg group.
    prefixes: bool, whether to use prefixes before the attribute flags, such as
      `--myresource-project`.
    required: bool, whether the anchor argument should be required. If True, the
      command will fail at argparse time if the anchor argument isn't given.
    plural: bool, True if the resource will be parsed as a list, False
      otherwise.
    group: the parser or subparser for a Calliope command that the resource
      arguments should be added to. If not provided, will be added to the main
      parser.
    attribute_to_args_map: {str: str}, dict of attribute names to names of
      associated arguments.
    hidden: bool, True if the arguments should be hidden.
  Nc
                     || _         || _        || _        || _        || _        || _        || _        | j                  |      | _        |	| _	        y)a  Initializes a ResourcePresentationSpec.

    Args:
      name: str, the name of the main arg for the concept.
      concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, The spec that
        specifies the concept.
      group_help: str, the help text for the entire arg group.
      prefixes: bool, whether to use prefixes before the attribute flags, such
        as `--myresource-project`. This will match the "name" (in flag format).
      required: bool, whether the anchor argument should be required.
      flag_name_overrides: {str: str}, dict of attribute names to the desired
        flag name. To remove a flag altogether, use '' as its rename value.
      plural: bool, True if the resource will be parsed as a list, False
        otherwise.
      group: the parser or subparser for a Calliope command that the resource
        arguments should be added to. If not provided, will be added to the main
        parser.
      hidden: bool, True if the arguments should be hidden.
    N)
name_concept_spec
group_helpprefixesrequiredpluralgroup_GetAttributeToArgsMap_attribute_to_args_maphidden)
selfr   concept_specr   r   r   flag_name_overridesr   r   r   s
             Blib/googlecloudsdk/command_lib/util/concepts/presentation_specs.py__init__zPresentationSpec.__init__5   sS    : DI%D DODMDMDKDJ"&"="=#DDK    c                     | j                   S )zThe ConceptSpec associated with the PresentationSpec.

    Returns:
      (googlecloudsdk.calliope.concepts.ConceptSpec) the concept spec.
    )r   r   s    r   r   zPresentationSpec.concept_spec]   s     r   c                     | j                   S )z\The map of attribute names to associated args.

    Returns:
      {str: str}, the map.
    )r   r   s    r   attribute_to_args_mapz&PresentationSpec.attribute_to_args_mapf   s     &&&r   c                     t         )aR  Generate a ConceptInfo object for the ConceptParser.

    Must be overridden in subclasses.

    Args:
      fallthroughs_map: {str: [googlecloudsdk.calliope.concepts.deps.
        _FallthroughBase]}, dict keyed by attribute name to lists of
        fallthroughs.

    Returns:
      info_holders.ConceptInfo, the ConceptInfo object.
    NotImplementedErrorr   fallthroughs_maps     r   _GenerateInfozPresentationSpec._GenerateInfoo   s
     r   c                     t         )zGenerate a map of attributes to primary arg names.

    Must be overridden in subclasses.

    Args:
      flag_name_overrides: {str: str}, the dict of flags to overridden names.

    Returns:
      {str: str}, dict from attribute names to arg names.
    r    )r   r   s     r   r   z'PresentationSpec._GetAttributeToArgsMap~   s
     r   )FFNFNF)
__name__
__module____qualname____doc__r   propertyr   r   r$   r    r   r   r	   r	      sS    4 #'&P   ' 'r   r	   c                   >    e Zd ZdZd Zd Ze	 	 dd       Zd Zd Z	y)	ResourcePresentationSpeczGClass that specifies how resource arguments are presented in a command.c                 @   |sy |j                         D ]  }| j                  j                  D ]  }|j                  |k(  s . t	        dj                  |dj                  | j                  j                  D cg c]  }|j                   c}                   y c c}w )NzmAttempting to override the name for an attribute not present in the concept: [{}]. Available attributes: [{}], )keysr   
attributesr   
ValueErrorformatjoin)r   r   attribute_name	attributes       r   _ValidateFlagNameOverridesz3ResourcePresentationSpec._ValidateFlagNameOverrides   s    -224((33)>>^+
 4 <<BF		,0,=,=,H,HJ,Hy %>>,HJ K=LM 	M 5Js   7Bc                 P   | j                  |       i }t        | j                  j                        D ]p  \  }}|t	        | j                  j                        dz
  k(  }| j                  |j                  | j                  || j                  |      }|sb|||j                  <   r |S )N   )	is_anchor)r7   	enumerater   r1   lenGetFlagNamer   r   )r   r   r   ir6   r:   r   s          r   r   z/ResourcePresentationSpec._GetAttributeToArgsMap   s    ##$78!$"4"4"?"?@9s4--889A==i
..$))%8$--  d 
04inn- A ! r   Nc                 R   |xs i }| |v r|j                  |       S | dk(  ry|r|S t        j                  }|rl|j                  t        j                        r'||t	        t        j                        d dz   z  }|| z   S ||j                         j                  dd      dz   z  }|| z   S )a  Gets the flag name for a given attribute name.

    Returns a flag name for an attribute, adding prefixes as necessary or using
    overrides if an override map is provided.

    Args:
      attribute_name: str, the name of the attribute to base the flag name on.
      presentation_name: str, the anchor argument name of the resource the
        attribute belongs to (e.g. '--foo').
      flag_name_overrides: {str: str}, a dict of attribute names to exact string
        of the flag name to use for the attribute. None if no overrides.
      prefixes: bool, whether to use the resource name as a prefix for the flag.
      is_anchor: bool, True if this it he anchor flag, False otherwise.

    Returns:
      (str) the name of the flag.
    project N-_)getr   PREFIX
startswithr<   lowerreplace)r5   presentation_namer   r   r:   prefixs         r   r=   z$ResourcePresentationSpec.GetFlagName   s    ( .3,, $$^44"[[F		%	%dkk	2#C$4$56<< N"" 	#))+33C=CCN""r   c                     t        j                  | j                  | j                  | j                  | j
                  || j                  | j                  | j                  | j                  	      S )a,  Gets the ResourceInfo object for the ConceptParser.

    Args:
      fallthroughs_map: {str: [googlecloudsdk.calliope.concepts.deps.
        _FallthroughBase]}, dict keyed by attribute name to lists of
        fallthroughs.

    Returns:
      info_holders.ResourceInfo, the ResourceInfo object.
    )r   r   r   r   )
r   ResourceInfor   r   r   r   r   r   r   r   r"   s     r   r$   z&ResourcePresentationSpec._GenerateInfo   sV     $$		""{{jj{{	 	r   c                    t        |t        |             sy| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j
                  |j
                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S NF

isinstancetyper   r   r   r   r   r   r   r   r   others     r   __eq__zResourcePresentationSpec.__eq__       eT$Z(II# (!3!33(OOu///( MMU^^+( 15u||0K( MMU^^+	( 15

ekk0I	(
 KK5<<')r   NFF)
r&   r'   r(   r)   r7   r   staticmethodr=   r$   rT   r+   r   r   r-   r-      s5    OM! IM,1 #  #D,)r   r-   c                       e Zd ZdZy)InvalidPresentationSpecErrorz$Error for invalid presentation spec.N)r&   r'   r(   r)   r+   r   r   rY   rY      s    ,r   rY   c                   8    e Zd ZdZd Ze	 	 dd       Zd Zd Zy)!MultitypeResourcePresentationSpecz&A resource-specific presentation spec.c           
      `   i }| j                   j                  D cg c]  }| j                   j                  |      r|! }}t        |      dkD  rst	        j
                  | j                        rTdj                  d |D              }t        d| d| j                   dt	        j                  | j                         d      | j                   j                  D ]N  }|g|k(  }| j                  |j                  | j                  || j                  |      }|s@|||j                  <   P |S c c}w )	Nr9   r/   c              3   4   K   | ]  }|j                     y w)N)r   ).0as     r   	<genexpr>zKMultitypeResourcePresentationSpec._GetAttributeToArgsMap.<locals>.<genexpr>   s     <|!qvv|s   z Multitype resource has anchors [z] and positional name [z]. Multitype resource can only be non-positional or have a single anchor. Update multitype collections or change the presentation name to .)r   r   r:   )r   r1   IsLeafAnchorr<   r   IsPositionalr   r4   rY   FlagNameFormatr=   r   )	r   r   r   r_   leaf_anchorsanchor_namesr6   r:   r   s	            r   r   z8MultitypeResourcePresentationSpec._GetAttributeToArgsMap   s2   #11<< ;<!))66q9 <L ; <1!2!2499!=YY<|<<l(,\N ;99+ &&*&9&9$))&D%EQHI I ''22	+-i
..$))9L==I  7d 
04inn- 3 ! %;s   $D+Nc                     |xs i }| |v r|j                  |       S |r|S | dk(  ry|r&t        j                  dj                  || g            S t        j                  |       S )a  Gets the flag name for a given attribute name.

    Returns a flag name for an attribute, adding prefixes as necessary or using
    overrides if an override map is provided.

    Args:
      attribute_name: str, the name of the attribute to base the flag name on.
      presentation_name: str, the anchor argument name of the resource the
        attribute belongs to (e.g. '--foo').
      flag_name_overrides: {str: str}, a dict of attribute names to exact string
        of the flag name to use for the attribute. None if no overrides.
      prefixes: bool, whether to use the resource name as a prefix for the flag.
      is_anchor: bool, True if this is the anchor flag, False otherwise.

    Returns:
      (str) the name of the flag.
    r@   rA   rB   )rD   r   rd   r4   )r5   rI   r   r   r:   s        r   r=   z-MultitypeResourcePresentationSpec.GetFlagName  sq    ( .3,, $$^44"  +<n*M!NOO  00r   c           
          t        j                  | j                  | j                  | j                  | j
                  || j                  | j                  | j                        S )a>  Gets the MultitypeResourceInfo object for the ConceptParser.

    Args:
      fallthroughs_map: {str: [googlecloudsdk.calliope.concepts.deps.
        _FallthroughBase]}, dict keyed by attribute name to lists of
        fallthroughs.

    Returns:
      info_holders.MultitypeResourceInfo, the ResourceInfo object.
    )r   r   r   )	r   MultitypeResourceInfor   r   r   r   r   r   r   r"   s     r   r$   z/MultitypeResourcePresentationSpec._GenerateInfo/  sO     --		""{{jj r   c                    t        |t        |             sy| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xr | j
                  |j
                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S rN   rO   rR   s     r   rT   z(MultitypeResourcePresentationSpec.__eq__D  rU   r   rV   )	r&   r'   r(   r)   r   rW   r=   r$   rT   r+   r   r   r[   r[      s/    .!. IM,11 1@*)r   r[   N)r)   
__future__r   r   r    googlecloudsdk.calliope.conceptsr   (googlecloudsdk.command_lib.util.conceptsr   objectr	   r-   	ExceptionrY   r[   r+   r   r   <module>rp      sU    '  ' 1 Akv k\`)/ `)F-9 -
X)(8 X)r   