
    =                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJr  SSK	J
r  SS	K	Jr  SS
KJr  SSKJr  SSKrSSKJr  SrSr " S S\R,                  " \R.                  \5      5      r " S S\5      r " S S\5      rg)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                       \ rS rSrSr\S 5       r\S 5       r\R                  S 5       r
S rS rS r\R                  SS
 j5       rS rSrg	)ConceptInfo(   a  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                     [         e)z1The concept spec associated with this info class.NotImplementedErrorselfs    <lib/googlecloudsdk/command_lib/util/concepts/info_holders.pyconcept_specConceptInfo.concept_spec6   
         c                     [         e)z5A map of attribute names to non-primary fallthroughs.r   r   s    r   fallthroughs_mapConceptInfo.fallthroughs_map;   r   r   c                     g)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   GetHintsConceptInfo.GetHints@       r   c                     [         e)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   GetGroupHelpConceptInfo.GetGroupHelpK   
     r   c                     [         e)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   GetAttributeArgsConceptInfo.GetAttributeArgsU   s
     r   c                     [         e)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   AddToParserConceptInfo.AddToParser`   r&   r   Nc                     g)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   ParseConceptInfo.Parsej   r"   r   c                     g)z0Clear cache if it exists. Override where needed.Nr   r   s    r   
ClearCacheConceptInfo.ClearCacheu   s    r   r   N)__name__
__module____qualname____firstlineno____doc__propertyr   r   abcabstractmethodr    r$   r(   r,   r0   r3   __static_attributes__r   r   r   r   r   (   sw          	  	r   r   c                      ^  \ rS rSrSr    SU 4S jjr\S 5       r\S 5       r\S 5       r	\S 5       r
S rS	 rS
 rS r\S 5       rS rS rS rS rS rS rS rS rSS jrS rSrU =r$ )ResourceInfoz   z*Holds information for a resource argument.c
                    > [         [        U ]  5         Xl        X l        XPl        X@l        Xpl        X0l        U(       + U l	        Xl
        Xl        SU l        SU l        SU l        g)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   )superr@   __init__presentation_name_concept_spec_fallthroughs_mapattribute_to_args_mapplural
group_helpallow_emptygrouphidden_result_result_computedsentinel)r   rE   r   rJ   rH   r   requiredrI   rL   rM   	__class__s             r   rD   ResourceInfo.__init__}   s^    : 
,&(.%-!6K O#|DJKDL!DDMr   c                     U R                   $ r5   )rF   r   s    r   r   ResourceInfo.concept_spec   s    r   c                     U R                   $ r5   )r   r   s    r   resource_specResourceInfo.resource_spec   s    r   c                     U R                   $ r5   )rG   r   s    r   r   ResourceInfo.fallthroughs_map   s    !!!r   c                     U R                   R                  nUS   R                  5       USS -   nUR                  SS5      R                  SS5      $ )zAThe title of the arg group for the spec, in all caps with spaces.r      N_ -)r   nameupperreplace)r   r`   s     r   titleResourceInfo.title   sM     !!D7==?T!"X%D<<S!))#s33r   c                 8    U R                   R                  U5      $ r5   r   IsAnchorr   	attributes     r   	_IsAnchorResourceInfo._IsAnchor   s    %%i00r   c                 b    U R                   R                  U R                  U R                  5      $ r5   )r   BuildFullFallthroughsMaprH   r   r   s    r   rm   %ResourceInfo.BuildFullFallthroughsMap   s-    55"" r   c                 n    U R                  5       R                  U/ 5      n[        R                  " U5      $ )a%  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.
)rm   getdeps_libr    )r   r   fallthroughss      r   r    ResourceInfo.GetHints   s0     00266~rJL\**r   c                    [        [        [        [        [        U R                  R                  5       5      5      5      5      S:X  a  SnOSnSR                  U R                  U R                  U5      /nU R                  R                   Vs/ s H;  nU R                  R                  UR                  5      (       a  M/  UR                  PM=     nnU(       a  UR                  S5        U Hn  nU R                  U5       Vs/ s H  nSR                  U5      PM     nnU(       d  M<  SR                  USR                  U5      5      nUR                  U5        Mp     S	R                  U5      $ s  snf s  snf )
z(Build group help for the argument group.r\   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:{}.;r^   )lenlistr   boolrH   valuesformatrc   rJ   rW   
attributesrp   r`   appendr    join)r   generic_helpdescriptionri   
skip_flags	attr_namehinthintss           r   r$   ResourceInfo.GetGroupHelp   sS   
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   .E.?E.E3c                    U R                   (       a  gU R                  R                  nU R                  R	                  UR
                  S5      (       a,  U R                  R	                  UR
                  / 5      (       d  gg)a   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)rK   rW   anchorrH   rp   r`   r   )r   r   s     r   args_requiredResourceInfo.args_required   sa     &&F""&&v{{D99%%))&++r::r   c           	      .   U R                  U5      (       a  U R                  (       d  [        O[        nOUR                  n[
        R                  " U R                  (       a  SOSU R                  R                  [        U R                  SS5      S9nU R                  UR                  5       Vs/ s H  nSR                  U5      PM     nnU(       a/  SR                  UR                  SR                  U5      5      nX$-  nUR                  US	9$ s  snf )
z2Helper to get the help text for the attribute arg.   r\   plural_nameNrI   ru   rv   rw   )resource)rj   rI   ANCHOR_HELPPLURAL_ANCHOR_HELP	help_textr   	PluralizerW   r`   getattrr    r|   r   )r   ri   r   expansion_namer   r   s         r   _GetHelpTextForAttribute%ResourceInfo._GetHelpTextForAttribute  s    ~~i  %)[[+6Hi%%i^^[[at))=$?AN
 +/--	*GI*G$*G 
 I077
..#((5/d i^44Is   0Dc                     U R                   (       + =(       aC    U R                  U5      =(       a+    U R                  R                  UR                  / 5      (       + $ r5   )rM   rj   r   rp   r`   rh   s     r   _IsRequiredArgResourceInfo._IsRequiredArg  sC    {{? ;y! 	:!!%%innb99;r   c                 J    U R                  U5      =(       a    U R                  $ r5   )rj   rI   rh   s     r   _IsPluralArgResourceInfo._IsPluralArg  s    >>)$44r   c                 F   U R                  U5      nU R                  U5      nU R                  U5      nUR                  (       a  UR                  nOIU R                  R
                  (       d,  [        R                  " U R                  UR                  5      nOSnUUR                  UU R                  S.n[        R                  " U5      (       ab  U(       a  U(       a  UR                  SS05        U$ U(       a  U(       d  UR                  SS05        U$ U(       d  UR                  SS05        U$ UR                  S[        R                  " U5      05        U(       a  UR                  SS	05        U(       a&  UR                  S
[        R                   " 5       05        U$ )z:Constructs the kwargs for adding an attribute to argparse.N)helptype	completerrM   nargs+*?metavarrQ   Tr   )r   r   r   r   rW   disable_auto_completersr
   CompleterForAttributer`   
value_typerM   r	   IsPositionalupdateMetavarFormatr   ArgList)r   r`   ri   rQ   final_help_textrI   r   kwargs_dicts           r   _KwargsForAttribute ResourceInfo._KwargsForAttribute!  sh    ""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                     U R                   R                  UR                  S5      nU(       d  g[        R                  " U40 U R                  X!5      D6$ )z*Creates argument for a specific attribute.N)rH   rp   r`   r   Argumentr   )r   ri   r`   s      r   _GetAttributeArgResourceInfo._GetAttributeArgI  sK    %%)))..$?D==5

"
"4
35 5r   c                     / nU R                   R                   H.  nU R                  U5      nU(       d  M  UR                  U5        M0     U$ )z+Generate args to add to the argument group.)rW   r}   r   r~   )r   argsri   args       r   r(   ResourceInfo.GetAttributeArgsS  sE    D''22	!!),c	C 3
 Kr   c                    U R                  5       nU(       d  gU R                  =(       d    Un[        S U 5       5      nUR                  U R	                  5       U R
                  US9nU H  nUR                  U5        M     g)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   @   #    U  H  oR                  5       v   M     g 7fr5   )IsHidden).0xs     r   	<genexpr>+ResourceInfo.AddToParser.<locals>.<genexpr>o  s     ,t!ts   )r   rQ   rM   )r(   rL   any	add_groupr$   r   r,   )r   r+   r   rM   resource_groupr   s         r   r,   ResourceInfo.AddToParser]  sz       "D ZZ!6F,t,,F%% 4+=+=f & NN	oon% r   c                 D   U R                  5       n/ nU H  nUR                  R                  S5      (       a*  SR                  UR                  UR                  SS 5      nO)SR                  UR                  R	                  5       5      nUR                  U5        M     U$ )zCReturns a list of command line example arg strings for the concept.z--z{}=my-{}r   Nzmy-{})r(   r`   
startswithr|   lowerr~   )r   r   examplesr   examples        r   GetExampleArgListResourceInfo.GetExampleArgListv  s      "DH			T	"	"##CHHchhqrl;..!12oog  Or   c                     U R                   (       dR  U R                  R                  U R                  U R                  UU R
                  U R                  S9nSU l         X l        U R                  $ )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/   rI   rK   T)rO   r   r0   rH   r   rI   rK   rN   )r   r/   results      r   r0   ResourceInfo.Parse  sf         &&t'A'A'+'<'<3>.2kk373C3C	 ' Ef
 #dl<<r   c                      S U l         SU l        g )NF)rN   rO   r   s    r   r3   ResourceInfo.ClearCache  s    DL!Dr   )rF   rG   rN   rO   rK   rH   rL   rJ   rM   rI   rE   rP   )FFNFr5   )r6   r7   r8   r9   r:   rD   r;   r   rW   r   rc   rj   rm   r    r$   r   r   r   r   r   r   r(   r,   r   r0   r3   r>   __classcell__rR   s   @r   r@   r@   z   s    2 *X     " " 4 41
+!<  $5(;5&P5&2
(" "r   r@   c                   `   ^  \ rS rSrSrS rS rS rS r\	S 5       r
U 4S jrU 4S	 jrS
rU =r$ )MultitypeResourceInfoi  z9ResourceInfo object specifically for multitype resources.c                 8    U R                   R                  U5      $ )z+Returns true if the attribute is an anchor.rf   rh   s     r   rj   MultitypeResourceInfo._IsAnchor  s    %%i00r   c                     U R                   R                   Vs/ s H  oR                  U5      (       d  M  UPM     sn$ s  snf r5   )r   r}   rj   )r   as     r   _GetAnchors!MultitypeResourceInfo._GetAnchors  s1    ((33I3!~~a7HA3IIIs   ??c                     U R                  5       nX!/:H  =(       a+    U R                  R                  UR                  / 5      (       + $ )z5Returns True if the attribute arg should be required.)r   r   rp   r`   )r   ri   anchorss      r   r   $MultitypeResourceInfo._IsRequiredArg  sC     Gk! $*?*?*C*C+ ' r   c                 H    U R                   R                  XR                  S9$ )Nr   )r   r   rI   rh   s     r   r   "MultitypeResourceInfo._IsPluralArg  s     &&y&EEr   c                     U R                   (       a  gU R                  5       n[        U5      S:w  a  gUS   nU R                  R	                  UR
                  / 5      (       a  gg)zTrue if resource is required & has a single anchor with no fallthroughs.

Returns:
  bool, whether the argument group should be required.
Fr\   r   T)rK   r   rx   r   rp   r`   )r   r   r   s      r   r   #MultitypeResourceInfo.args_required  sY      G
7|qQZF  b11r   c                    > [         [        U ]  5       nU R                  R                   Vs/ s H  o"R
                  PM     nnUSR                  SR                  U5      5      -   $ s  snf )Nz7 This resource can be one of the following types: [{}].z, )rC   r   r$   r   	type_enumr`   r|   r   )r   	base_texttype_	all_typesrR   s       r   r$   "MultitypeResourceInfo.GetGroupHelp  sh    +T?AI $ 1 1 ; ;= ;u

 ;  =   &tyy'; <> >=s   A&c                   > [         [        U ]  U5      n[        U R                  R
                  R                  UR                  5       Vs/ s H  o3R                  PM     sn5      nU R                  R                   Vs/ s H  o3R                  PM     nn[        [        U5      5      [        U5      :X  a  U$ USR                  SR                  U Vs/ s H  nSR                  U5      PM     sn5      5      -   $ s  snf s  snf s  snf )Nz+ Must be specified for resource of type {}.z or z[{}])rC   r   r   sortedr   _attribute_to_types_maprp   r`   r   rx   setr|   r   )r   ri   r   r   relevant_typesr   trR   s          r   r   .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   C<?DDr   )r6   r7   r8   r9   r:   rj   r   r   r   r;   r   r$   r   r>   r   r   s   @r   r   r     sA    A1JF  $>J Jr   r   )r:   
__future__r   r   r   r<   googlecloudsdk.callioper   r    googlecloudsdk.calliope.conceptsr   rq   r	   (googlecloudsdk.command_lib.util.conceptsr
   googlecloudsdk.core.utilr   six	six.movesr   r   r   with_metaclassABCMetaobjectr   r@   r   r   r   r   <module>r      sz    9 &  ' 
 / ( = 1 ? ) 
 , O	#$$S[[&9 O	d^"; ^"B	<JL <Jr   