
    K0                         S 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	J
r
  SS	Kr " S
 S\5      rS rg	)zConceptParsers manage the adding of all concept arguments to argparse parser.

The ConceptParser is created with a list of all resources needed for the
command, and they should be added all at once during calliope's Args method.
    )absolute_import)division)unicode_literals)deps)handlers)util)presentation_specsNc                   ~    \ rS rSrSrSS jr\    SS j5       rS rS r	S r
S	 r\S
 5       rS rS rS rS rSrg)ConceptParser!   z4Class that handles adding concept specs to argparse.Nc                     0 U l         / U l        U H  nU R                  U5        M     U R                  U=(       d    0 5      U l        g)a  Initializes a concept holder.

Args:
  specs: [presentation_specs.PresentationSpec], a list of the
    specs for concepts to be added to the parser.
  command_level_fallthroughs: {str: str}, a map of attributes to argument
    fallthroughs for those attributes. The format of the key should be FOO.a
    (the resource presentation name is "FOO" and the attribute name is "a").
    The format of the value should either be "BAR.b" (where the argument
    depended upon is the main argument generated for attribute "b" of
    the resource presentation spec that is named "BAR"), or "--baz", where
    "--baz" is a non-resource argument that is added separately to the
    parser.

Raises:
  ValueError: if two presentation specs have the same name or two specs
    contain positional arguments.
N)_specs	_all_args_AddSpec!_ValidateAndFormatFallthroughsMap_command_level_fallthroughs)selfspecscommand_level_fallthroughsspecs       ?lib/googlecloudsdk/command_lib/util/concepts/concept_parsers.py__init__ConceptParser.__init__$   sC    & DKDN
mmD '+'M'M"(b(*D$    c                    [         R                  " UUUUU=(       d    0 UUU	US9	n0 n[        XU
5        [        R                  " U
=(       d    0 5       H%  u  pSR                  UR                  U5      nXU'   M'     U " U/U5      $ )a  Constructs a ConceptParser for a single resource argument.

Automatically sets prefixes to False.

Args:
  name: str, the name of the main arg for the resource.
  resource_spec: googlecloudsdk.calliope.concepts.ResourceSpec, The spec
    that specifies the resource.
  group_help: str, the help text for the entire arg group.
  required: bool, whether the main argument should be required for the
    command.
  hidden: bool, whether or not the resource is hidden.
  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.
  prefixes: bool, True if flag names will be prefixed with the resource
    name, False otherwise. Should be False for all typical use cases.
  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.
  command_level_fallthroughs: a map of attribute names to lists of command-
    specific fallthroughs. These will be prioritized over the default
    fallthroughs for the attribute.

Returns:
  (googlecloudsdk.calliope.concepts.concept_parsers.ConceptParser) The fully
    initialized ConceptParser.
)requiredflag_name_overridespluralprefixesgrouphidden{}.{})r	   ResourcePresentationSpecUpdateFallthroughsMapsix	iteritemsformatname)clsr(   resource_spec
group_helpr   r!   r   r   r   r    r   presentation_specfallthroughs_mapattribute_namefallthroughskeys                   r   ForResourceConceptParser.ForResource>   s    D +CC/52	 *2LM(+"(b)*$NN,11>Bc*s)* !"$455r   c                 b    [         R                  " U5      [         R                  " U5      :X  a  gg)a  Checks if two argument names match in the namespace.

RESOURCE_ARG and --resource-arg will match with each other, as well as exact
matches.

Args:
  name: the first argument name.
  other_name: the second argument name.

Returns:
  (bool) True if the names match.
TF)r   NormalizeFormat)r   r(   
other_names      r   _ArgNameMatchesConceptParser._ArgNameMatchesr   s(     D!T%9%9*%EEr   c                    U R                    H  nU R                  X!R                  5      (       a$  [        SR	                  X!R                  5      5      e[
        R                  " U5      (       d  Md  [
        R                  " UR                  5      (       d  M  [        SR	                  UUR                  5      5      e   [        R                  " UR                  5       H`  u  p4A[
        R                  " U5      nXPR                  ;   a  [        SR	                  U5      5      eU R                  R                  U5        Mb     XR                   UR                  '   g)a+  Adds a given presentation spec to the concept holder's spec registry.

Args:
  presentation_spec: PresentationSpec, the spec to be added.

Raises:
  ValueError: if two presentation specs have the same name, if two
    presentation specs are both positional, or if two args are going to
    overlap.
z:Attempted to add two concepts with the same name: [{}, {}]zFAttempted to add multiple concepts with positional arguments: [{}, {}]z0Attempted to add a duplicate argument name: [{}]N)r   r6   r(   
ValueErrorr'   r   IsPositionalr%   r&   attribute_to_args_mapr4   r   append)r   r,   	spec_nameaarg_namer(   s         r   r   ConceptParser._AddSpec   s    [[				i)?)?	@	@ $$*F96L6L$MO 	O


I
&
&


-22
3
3 //5vi6G6L6L0NO 	O ! }}%6%L%LM
!!(+d		K &*, 	,
nnD! N +<KK!&&'r   c                    0 n[         R                  " U5       H  u  p4UR                  S5      n[        U5      S:w  a  [	        SR                  U5      5      eUu  pgU R                  SXg5        U He  nUR                  S5      n	[        U	5      S;  a  [	        SR                  U5      5      e[        U	5      S:X  d  MN  U	u  pU R                  SU
U5        Mg     XBR                  U0 5      U'   M     U$ )zEValidate formatting of fallthroughs and build map keyed to spec name..   z}invalid fallthrough key: [{}]. Must be in format "FOO.a" where FOO is the presentation spec name and a is the attribute name.r0   )   rC   zCinvalid fallthrough value: [{}]. Must be in the form BAR.b or --bazvalue)r%   r&   splitlenr9   r'   _ValidateSpecAndAttributeExist
setdefault)r   r   spec_mapr0   fallthroughs_listkeysr=   r.   fallthrough_stringvaluesvalue_spec_namevalue_attribute_names               r   r   /ConceptParser._ValidateAndFormatFallthroughsMap   s    H"%--0J"KYYs^d	Ta 44:F3KA 	A #'i
))%K 1
#))#.v;f$ 1178J1KM Mv;!28
//

-
-g.=.BD !2 <M)R(8% #L& Or   c                    X R                   ;  ad  [        SR                  USR                  X#5      SR                  [	        [        U R                   R                  5       5      5      5      5      5      eU R                   R                  U5      nUUR                  R                   Vs/ s H  oUR                  PM     sn;  a,  [        SR                  USR                  X#5      UU5      5      egs  snf )zFRaises if a formatted string refers to non-existent spec or attribute.zginvalid fallthrough {}: [{}]. Spec name is not present in the presentation specs. Available names: [{}]r"   z, zIinvalid fallthrough {}: [{}]. spec named [{}] has no attribute named [{}]N)r   r9   r'   joinsortedlistrL   getconcept_spec
attributesr(   )r   locationr=   r.   r   	attributes         r   rH   ,ConceptParser._ValidateSpecAndAttributeExist   s    

" $f#">>)D99VD1B,C%DEGH H ::>>)$D(,(9(9(D(DF(D9(DF F ..4f#">>)D$)	/+, ,F Fs   (C0c                     U R                   $ N)r   )r   s    r   r   ConceptParser.specs   s    ;;r   c                 ~   UR                   R                  nU(       d&  [        R                  " 5       nUR	                  U5        [
        R                  " U R                  5       HV  u  p4U R                  U5      nUR                  U5        UR                  [        R                  " U5      UUR                  S9  MX     g)zpAdds attribute args for all presentation specs to argparse.

Args:
  parser: the parser for a Calliope command.
)r   N)dataconcept_handlerr   RuntimeHandleradd_conceptsr%   r&   r   GetInfoAddToParser
AddConceptr   r4   r   )r   parserruntime_handlerr=   r   concept_infos         r   re   ConceptParser.AddToParser   s     kk11O //1o/*==5	\\),lv&  


y
)
== ! " 6r   c                     / nU R                    H>  nU R                  U5      nUR                  5       nU(       d  M-  UR                  U5        M@     S nSR	                  [        XS95      $ )z:Returns a command line example arg string for the concept.c                 >    U R                  S5      (       a  SOSnX-   $ )Nz--ZA)
startswith)argprefixs     r   _PositionalsFirst<ConceptParser.GetExampleArgString.<locals>._PositionalsFirst   s    nnT**sf\r    )r0   )r   rd   GetExampleArgListextendrS   rT   )r   examplesr=   infoargsrr   s         r   GetExampleArgString!ConceptParser.GetExampleArgString   s]    H[[	\\)$d##%d		 ! 88F8;<<r   c                    UR                  S5      n[        U5      S:X  a  Un[        R                  " US   5      $ [        U5      S:X  at  Uu  pEU R                  R                  U5      nUR                  R                  US5      nU(       d  [        SR                  UUU5      5      e[        R                  " U5      $ [        SR                  U5      5      e)z/Make an ArgFallthrough from a formatted string.rB   rD   r   rC   NziInvalid fallthrough value [{}]: No argument associated with attribute [{}] in concept argument named [{}]zbad fallthrough string [{}])	rF   rG   r   ArgFallthroughr   rV   r;   r9   r'   )r   rM   rN   r?   r=   r.   r   s          r   _MakeFallthroughConceptParser._MakeFallthrough   s    %%c*F
6{ah  ++	V	"(iZZ^^I&d++//Eh<<BF"= 	   ** 4;;<NOPPr   c                    XR                   ;  a  [        SR                  U5      5      eU R                   U   n0 nUR                  R                   H~  nU R
                  R                  UR                  0 5      R                  UR                  / 5      nU Vs/ s H  nU R                  U5      PM     nnXtR                  -   X4R                  '   M     UR                  U5      $ s  snf )z:Build ConceptInfo object for the spec with the given name.z`Presentation spec with name [{}] has not been added to the concept parser, cannot generate info.)r   r9   r'   rW   rX   r   rV   r(   r~   r/   _GenerateInfo)r   presentation_spec_namer,   r-   rZ   fallthrough_stringsrM   r/   s           r   rd   ConceptParser.GetInfo
  s    ZZ/ FFLf1G34 4 

#9:&33>>	 <<@@

 
 "&&)c).."&=  1DE0C, ++,>?0C  E)58N8N)N~~& ? **+;<<	Es   C$)r   r   r   r]   )FFNFFNN)__name__
__module____qualname____firstlineno____doc__r   classmethodr1   r6   r   r   rH   propertyr   re   rz   r~   rd   __static_attributes__ r   r   r   r   !   si    <*4 AF48).9=16 16f"<B0,&  "$=Q,=r   r   c                 ~    [         R                  " U=(       d    0 5       H  u  p4SR                  X5      nX@U'   M     g)z=Helper to add a single resource's command level fallthroughs.r"   N)r%   r&   r'   )r-   resource_arg_namer   r.   r/   r0   s         r   r$   r$     s9     '*mm &B'("n
..*
;C(S'(r   )r   
__future__r   r   r    googlecloudsdk.calliope.conceptsr   r   r   (googlecloudsdk.command_lib.util.conceptsr	   r%   objectr   r$   r   r   r   <module>r      s8    '  ' 1 5 1 G 
x=F x=v)r   