
    =                        d Z ddlmZ ddlmZ ddlm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 ddlmZ ddlZddlmZ dZdZ G d d ej,                  ej.                  e            Z G d de      Z G d de      Zy)z2Classes for runtime handling of concept arguments.    )absolute_import)division)unicode_literalsN)arg_parsers)base)deps)util)
completers)text)filterzFID of the {resource} or fully qualified identifier for the {resource}.zHIDs of the {resource} or fully qualified identifiers for the {resource}.c                       e Zd ZdZed        Zed        Zej                  d        Z	d Z
d Zd Zej                  dd	       Zd
 Zy)ConceptInfoa  Holds information for a concept argument.

  The ConceptInfo object is responsible for holding information about the
  dependencies of a concept, and building a Deps object when it is time for
  lazy parsing of the concept.

  Attributes:
    concept_spec: The concept spec underlying the concept handler.
    attribute_to_args_map: A map of attributes to the names of their associated
      flags.
    fallthroughs_map: A map of attributes to non-argument fallthroughs.
  c                     t         )z1The concept spec associated with this info class.NotImplementedErrorselfs    <lib/googlecloudsdk/command_lib/util/concepts/info_holders.pyconcept_speczConceptInfo.concept_spec6   
         c                     t         )z5A map of attribute names to non-primary fallthroughs.r   r   s    r   fallthroughs_mapzConceptInfo.fallthroughs_map;   r   r   c                      y)zGet a list of string hints for how to specify a concept's attribute.

    Args:
      attribute_name: str, the name of the attribute to get hints for.

    Returns:
      [str], a list of string hints.
    N )r   attribute_names     r   GetHintszConceptInfo.GetHints@       r   c                     t         )zGet the group help for the group defined by the presentation spec.

    Must be overridden in subclasses.

    Returns:
      (str) the help text.
    r   r   s    r   GetGroupHelpzConceptInfo.GetGroupHelpK   
     r   c                     t         )zGenerate args to add to the argument group.

    Must be overridden in subclasses.

    Yields:
      (calliope.base.Argument), all arguments corresponding to concept
        attributes.
    r   r   s    r   GetAttributeArgszConceptInfo.GetAttributeArgsU   s
     r   c                     t         )zAdds all attribute args for the concept to argparse.

    Must be overridden in subclasses.

    Args:
      parser: the parser for the Calliope command.
    r   )r   parsers     r   AddToParserzConceptInfo.AddToParser`   r!   r   Nc                      y)zLazy parsing function to parse concept.

    Args:
      parsed_args: the argparse namespace from the runtime handler.

    Returns:
      the parsed concept.
    Nr   )r   parsed_argss     r   ParsezConceptInfo.Parsej   r   r   c                      y)z0Clear cache if it exists. Override where needed.Nr   r   s    r   
ClearCachezConceptInfo.ClearCacheu   s    r   N)__name__
__module____qualname____doc__propertyr   r   abcabstractmethodr   r    r#   r&   r)   r+   r   r   r   r   r   (   sw          	  	r   r   c                        e Zd ZdZ	 	 	 	 d fd	Zed        Zed        Zed        Zed        Z	d Z
d Zd	 Zd
 Zed        Zd Zd Zd Zd Zd Zd Zd Zd ZddZd Z xZS )ResourceInfoz*Holds information for a resource argument.c
                     t         t        |           || _        || _        || _        || _        || _        || _        | | _	        || _
        |	| _        d| _        d| _        d| _        y)a~  Initializes the ResourceInfo.

    Args:
      presentation_name: str, the name of the anchor argument of the
        presentation spec.
      concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, The underlying
        concept spec.
      group_help: str, the group help for the argument group.
      attribute_to_args_map: {str: str}, A map of attribute names to the names
        of their associated flags.
      fallthroughs_map: {str: [deps_lib.Fallthrough]} A map of attribute names
        to non-argument fallthroughs.
      required: bool, False if resource parsing is allowed to return no
        resource, otherwise True.
      plural: bool, True if multiple resources can be parsed, False otherwise.
      group: an argparse argument group parser to which the resource arg group
        should be added, if any.
      hidden: bool, True, if the resource should be hidden.
    NFr   )superr5   __init__presentation_name_concept_spec_fallthroughs_mapattribute_to_args_mapplural
group_helpallow_emptygrouphidden_result_result_computedsentinel)r   r9   r   r>   r<   r   requiredr=   r@   rA   	__class__s             r   r8   zResourceInfo.__init__}   sn    : 
,&(.D%D-D!6DDK DO#|DDJDKDL!DDMr   c                     | j                   S r,   )r:   r   s    r   r   zResourceInfo.concept_spec   s    r   c                     | j                   S r,   )r   r   s    r   resource_speczResourceInfo.resource_spec   s    r   c                     | j                   S r,   )r;   r   s    r   r   zResourceInfo.fallthroughs_map   s    !!!r   c                     | j                   j                  }|d   j                         |dd z   }|j                  dd      j                  dd      S )zAThe title of the arg group for the spec, in all caps with spaces.r      N_ -)r   nameupperreplace)r   rP   s     r   titlezResourceInfo.title   sM     !!D7==?T!"X%D<<S!))#s33r   c                 8    | j                   j                  |      S r,   r   IsAnchorr   	attributes     r   	_IsAnchorzResourceInfo._IsAnchor   s    %%i00r   c                 b    | j                   j                  | j                  | j                        S r,   )r   BuildFullFallthroughsMapr<   r   r   s    r   r[   z%ResourceInfo.BuildFullFallthroughsMap   s-    55"" r   c                 l    | j                         j                  |g       }t        j                  |      S )aA  Gets a list of string hints for how to set an attribute.

    Given the attribute name, gets a list of hints corresponding to the
    attribute's fallthroughs.

    Args:
      attribute_name: str, the name of the attribute.

    Returns:
      A list of hints for its fallthroughs, including its primary arg if any.
    )r[   getdeps_libr   )r   r   fallthroughss      r   r   zResourceInfo.GetHints   s0     00266~rJL\**r   c                    t        t        t        t        t        | j                  j                                                 dk(  rd}nd}dj                  | j                  | j                  |      g}| j                  j                  D cg c]3  }| j                  j                  |j                        s|j                  5 }}|rz|j                  d       |D ]d  }| j                  |      D cg c]  }dj                  |       }}|s3dj                  |dj                  |            }|j                  |       f d	j                  |      S c c}w c c}w )
z(Build group help for the argument group.rL   z!This represents a Cloud resource.zSThe arguments in this group can be used to specify the attributes of this resource.z{} resource - {} {}zZ(NOTE) Some attributes are not given arguments in this group but can be set in other ways.
* {}

To set the `{}` attribute:{}.;rN   )lenlistr   boolr<   valuesformatrS   r>   rI   
attributesr]   rP   appendr   join)r   generic_helpdescriptionrX   
skip_flags	attr_namehinthintss           r   r    zResourceInfo.GetGroupHelp   sQ   
4tT$"<"<"C"C"EFGHIQN8l5l(//

 K
 )-(:(:(E(E?(E9))--inn= 	(E  ?  D E!).2mmI.FH.FdHOOD!.F 	 H 299sxx
 	4  " 88K  #?Hs   8E1Ec                     | j                   ry| j                  j                  }| j                  j	                  |j
                  d      r'| j                  j	                  |j
                  g       syy)a8  True if the resource is required and any arguments have no fallthroughs.

    If fallthroughs can ever be configured in the ResourceInfo object,
    a more robust solution will be needed, e.g. a GetFallthroughsForAttribute
    method.

    Returns:
      bool, whether the argument group should be required.
    FNT)r?   rI   anchorr<   r]   rP   r   )r   rs   s     r   args_requiredzResourceInfo.args_required   s[     &&F""&&v{{D9%%))&++r:r   c           	         | j                  |      r| j                  st        nt        }n|j                  }t        j                  | j                  rdnd| j                  j                  t        | j                  dd            }| j                  |j                        D cg c]  }dj                  |       }}|r0dj                  |j                  dj                  |            }||z  }|j                  |	      S c c}w )
z2Helper to get the help text for the attribute arg.   rL   plural_nameNr=   ra   rb   rc   )resource)rY   r=   ANCHOR_HELPPLURAL_ANCHOR_HELP	help_textr   	PluralizerI   rP   getattrr   rh   rk   )r   rX   r|   expansion_namerp   rq   s         r   _GetHelpTextForAttributez%ResourceInfo._GetHelpTextForAttribute  s    ~~i %)[[+6Hi%%i^^[[at))=$?AN
 +/--	*GI*G$*G 
 I077
..#((5/d 4i^44Is   #Dc                     | j                    xr: | j                  |      xr' | j                  j                  |j                  g        S r,   )rA   rY   r   r]   rP   rW   s     r   _IsRequiredArgzResourceInfo._IsRequiredArg  sC    {{? ;y! 	:!!%%innb99;r   c                 @    | j                  |      xr | j                  S r,   )rY   r=   rW   s     r   _IsPluralArgzResourceInfo._IsPluralArg  s    >>)$44r   c                    | j                  |      }| j                  |      }| j                  |      }|j                  r|j                  }nC| j                  j
                  s+t        j                  | j                  |j                        }nd}||j                  || j                  d}t        j                  |      rI|r|r|j                  ddi       |S |r|s|j                  ddi       |S |s|j                  ddi       |S |j                  dt        j                  |      i       |r|j                  dd	i       |r%|j                  d
t        j                          i       |S )z:Constructs the kwargs for adding an attribute to argparse.N)helptype	completerrA   nargs+*?metavarrE   Tr   )r   r   r   r   rI   disable_auto_completersr
   CompleterForAttributerP   
value_typerA   r	   IsPositionalupdateMetavarFormatr   ArgList)r   rP   rX   rE   final_help_textr=   r   kwargs_dicts           r   _KwargsForAttributez ResourceInfo._KwargsForAttribute!  se    ""9-H33I>Oy)F%%i7722



..i i$$++	K 	HGS>*  (GS>*  GS>*  )T%7%7%=>?	J-.	FK$7$7$9:;r   c                     | j                   j                  |j                  d      }|syt        j                  |fi | j                  ||      S )z*Creates argument for a specific attribute.N)r<   r]   rP   r   Argumentr   )r   rX   rP   s      r   _GetAttributeArgzResourceInfo._GetAttributeArgI  sM    %%)))..$?D==5

"
"4
35 5r   c                     g }| j                   j                  D ]'  }| j                  |      }|s|j                  |       ) |S )z+Generate args to add to the argument group.)rI   ri   r   rj   )r   argsrX   args       r   r#   zResourceInfo.GetAttributeArgsS  sC    D''22	!!),c	C 3
 Kr   c                     | j                         }|sy| j                  xs |}t        d |D              }|j                  | j	                         | j
                  |      }|D ]  }|j                  |        y)a.  Adds all attributes of the concept to argparse.

    Creates a group to hold all the attributes and adds an argument for each
    attribute. If the presentation spec is required, then the anchor attribute
    argument will be required.

    Args:
      parser: the parser for the Calliope command.
    Nc              3   <   K   | ]  }|j                           y wr,   )IsHidden).0xs     r   	<genexpr>z+ResourceInfo.AddToParser.<locals>.<genexpr>o  s     ,t!ts   )r   rE   rA   )r#   r@   any	add_groupr    rt   r&   )r   r%   r   rA   resource_groupr   s         r   r&   zResourceInfo.AddToParser]  sz       "D ZZ!6F,t,,F%% 4+=+=f & NN	oon% r   c                 6   | j                         }g }|D ]  }|j                  j                  d      r*dj                  |j                  |j                  dd       }n)dj                  |j                  j	                               }|j                  |        |S )zCReturns a list of command line example arg strings for the concept.z--z{}=my-{}rv   Nzmy-{})r#   rP   
startswithrh   lowerrj   )r   r   examplesr   examples        r   GetExampleArgListzResourceInfo.GetExampleArgListv  s}      "DH			T	"##CHHchhqrl;..!12oog  Or   c                     | j                   sV| j                  j                  | j                  | j                  || j
                  | j                        }d| _         || _        | j                  S )zLazy, cached parsing function for resource.

    Args:
      parsed_args: the parsed Namespace.

    Returns:
      the initialized resource or a list of initialized resources if the
        resource argument was pluralized.
    )r(   r=   r?   T)rC   r   r)   r<   r   r=   r?   rB   )r   r(   results      r   r)   zResourceInfo.Parse  sh         &&t'A'A'+'<'<3>.2kk373C3C	 ' Ef
 #ddl<<r   c                      d | _         d| _        y )NF)rB   rC   r   s    r   r+   zResourceInfo.ClearCache  s    DL!Dr   )FFNFr,   )r-   r.   r/   r0   r8   r1   r   rI   r   rS   rY   r[   r   r    rt   r   r   r   r   r   r#   r&   r   r)   r+   __classcell__rF   s   @r   r5   r5   z   s    2 *X     " " 4 41
+!<  $5(;5&P5&2
("r   r5   c                   T     e Zd ZdZd Zd Zd Zd Zed        Z	 fdZ
 fdZ xZS )	MultitypeResourceInfoz9ResourceInfo object specifically for multitype resources.c                 8    | j                   j                  |      S )z+Returns true if the attribute is an anchor.rU   rW   s     r   rY   zMultitypeResourceInfo._IsAnchor  s    %%i00r   c                 x    | j                   j                  D cg c]  }| j                  |      s| c}S c c}w r,   )r   ri   rY   )r   as     r   _GetAnchorsz!MultitypeResourceInfo._GetAnchors  s3    ((33I3!t~~a7HA3IIIs   77c                     | j                         }||gk(  xr' | j                  j                  |j                  g        S )z5Returns True if the attribute arg should be required.)r   r   r]   rP   )r   rX   anchorss      r   r   z$MultitypeResourceInfo._IsRequiredArg  sE     Gyk! $*?*?*C*C+ ' r   c                 P    | j                   j                  || j                        S )Nrx   )r   r}   r=   rW   s     r   r   z"MultitypeResourceInfo._IsPluralArg  s"    &&y&EEr   c                     | j                   ry| j                         }t        |      dk7  ry|d   }| j                  j	                  |j
                  g       ryy)zTrue if resource is required & has a single anchor with no fallthroughs.

    Returns:
      bool, whether the argument group should be required.
    FrL   r   T)r?   r   rd   r   r]   rP   )r   r   rs   s      r   rt   z#MultitypeResourceInfo.args_required  sV      G
7|qQZF  b1r   c                     t         t        |          }| j                  j                  D cg c]  }|j
                   }}|dj                  dj                  |            z   S c c}w )Nz7 This resource can be one of the following types: [{}].z, )r7   r   r    r   	type_enumrP   rh   rk   )r   	base_texttype_	all_typesrF   s       r   r    z"MultitypeResourceInfo.GetGroupHelp  sj    +T?AI $ 1 1 ; ;= ;u

 ;  =   &tyy'; <> >=s   A$c                    t         t        |   |      }t        | j                  j
                  j                  |j                        D cg c]  }|j                   c}      }| j                  j                  D cg c]  }|j                   }}t        t        |            t        |      k(  r|S |dj                  dj                  |D cg c]  }dj                  |       c}            z   S c c}w c c}w c c}w )Nz+ Must be specified for resource of type {}.z or z[{}])r7   r   r   sortedr   _attribute_to_types_mapr]   rP   r   rd   setrh   rk   )r   rX   r   r   relevant_typesr   trF   s          r   r   z.MultitypeResourceInfo._GetHelpTextForAttribute  s    +TKI  $ 1 1 I I M MNN!!u

 ! N
 !% 1 1 ; ;= ;u

 ;  =
3~3y>15	~F~!V]]1-~FG	HJ J= Gs   C4=C9C>)r-   r.   r/   r0   rY   r   r   r   r1   rt   r    r   r   r   s   @r   r   r     sA    A1JF  $>J Jr   r   )r0   
__future__r   r   r   r2   googlecloudsdk.callioper   r    googlecloudsdk.calliope.conceptsr   r^   r	   (googlecloudsdk.command_lib.util.conceptsr
   googlecloudsdk.core.utilr   six	six.movesr   rz   r{   with_metaclassABCMetaobjectr   r5   r   r   r   r   <module>r      s}    9 &  ' 
 / ( = 1 ? ) 
 , O	$#$$S[[&9 O	d^"; ^"B	<JL <Jr   