
    D                        S r SSKJr  SSKJr  SSKJr  SSK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Jr  SSKrSrS r " S S\R*                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\R8                  " \R:                  \5      5      rg)z@Completer extensions for the core.cache.completion_cache module.    )absolute_import)division)unicode_literalsN)resource_search)parameter_info_lib)log)
properties)	resources)completion_cache)resource_cachez	cloud.sdkc                 0    SR                  [        U /5      $ )zReturns the pseudo collection name for name.

Pseudo collection completion entities have no resource parser and/or URI.

Args:
  name: The pseudo collection entity name.

Returns:
  The pseudo collection name for name.
.)join_PSEUDO_COLLECTION_PREFIXnames    1lib/googlecloudsdk/command_lib/util/completers.pyPseudoCollectionNamer   '   s     
,d3	44    c                      ^  \ rS rSrSr     SU 4S jjrSS jrSS jrS rS r	\
S 5       rSS	 jrSS
 jrSS jrSS jrSrU =r$ )	Converter5   a  Converter mixin, based on core/resource_completion_style at instantiation.

Attributes:
    _additional_params: A list of additional parameter names not int the
      parsed resource.
    _parse_all: If True, attempt to parse any string, otherwise, just parse
      strings beginning with 'http[s]://'.
    qualified_parameter_names: The list of parameter names that must be fully
      qualified.  Use the name 'collection' to qualify collections.
c                   > [         [        U ]
  " S0 UD6  U(       a  X l        O<U R                  (       a$  U R                  R                  S5      S   U l        OS U l        Xl        [        U=(       d    / 5      U l        Uc2  [        R                  R                  R                  R                  5       nUS:X  d7  [        R                  R                  R                  R                  5       (       a  U R                   U l        OU R$                  U l        US:X  a  U R&                  U l        OU R*                  U l        XPl        g )Nr   r   gri )superr   __init__api
collectionsplit_additional_paramssetqualified_parameter_namesr	   VALUEScoreresource_completion_styleGet
enable_griGetBool_GRI_StringToRow_string_to_row_StringToRow_GRI_RowToString_row_to_string_FLAGS_RowToString
_parse_all)selfadditional_paramsr   r#   style	parse_allkwargs	__class__s          r   r   Converter.__init__A   s     
)T#-f-
h	&&s+A.dhdh/%()B)Hb%ID"}$$>>BBDe~**//::BBDD 11d --d~ 11d 33dOr   c                 $    U R                  X5      $ )z)Returns the row representation of string.)r+   )r1   stringparameter_infos      r   StringToRowConverter.StringToRow_   s    v66r   c                      U R                  XS9$ ))Returns the string representation of row.)r:   )r.   r1   rowr:   s      r   RowToStringConverter.RowToStringc   s    sBBr   c                 .    U =R                   U-  sl         g)zBAdds qualified_parameter_names to the set of qualified parameters.N)r#   )r1   r#   s     r   AddQualifiedParameterNames$Converter.AddQualifiedParameterNamesg   s    ""&??"r   c                 D    [         R                  " XU R                  5      $ )a  Returns the parameter info object.

This is the default method that returns the parameter info by name
convention object.  Resource argument completers should override this
method to provide the exact object, not the "best guess" of the default.

Args:
  parsed_args: The command line parsed args object.
  argument: The argparse argument object attached to this completer.

Returns:
  The parameter info object.
)r   ParameterInfoByConventionr   )r1   parsed_argsarguments      r   ParameterInfoConverter.ParameterInfok   s"     778<B Br   c                     U  Vs/ s H  o"S;   d  M
  UPM     nnU(       a  US   OSnU(       a+  X   R                  5       (       a  UR                  USS9X'   gggs  snf )a  Convert project number into ID, if it's not one already.

Get the project ID from command parameters and compare it to project IDs
returned by list commands. If a project number is found instead, replace it
with the project ID before storing it in completion cache.
Idempotent. Does nothing if there's no project parameter, which is the case
for resources without a parent project, e.g. organization resources.

Args:
  row: a dict containing the values necessary for tab completion of resource
  args.
  parameter_info: Program state, contains the available information on
  the CLI command executed, such as param values, etc.

Returns:
  None, modifies the provided dict in-place.
)project	projectId
projectsIdr   NTcheck_properties)	isnumericGetValue)r@   r:   kproject_keys       r   _ConvertProjectNumberToID#Converter._ConvertProjectNumberToID|   sr    ( aEE3   %0+a.TKs'1133'00
 1 .c 4{	s
   	AAc                     U R                  U=(       d    S 5      nU(       a  U R                  X25        [        UR                  5       5      nU$ ! [        R
                   a    [        R                  R                  XR                  5      R                  n[        U5      U R                  :  a  US/U R                  [        U5      -
  -  -  n[        [        U5      5      s $ f = f)N )parserV   listvaluesr
   RequiredFieldOmittedExceptionGRI
FromStringr   path_fieldslencolumnsreversed)r1   r9   r:   r@   fieldss        r   r*   Converter._GRI_StringToRow   s    $JJv~&c	&&s;cj22 $}}''@LLf	Vt||	#2$$,,V455(6"##	$s   AA BC%$C%c                    U(       a  UR                  S5      (       d'  UR                  S5      (       d  U R                  (       aN   U R                  U=(       d    S 5      nU(       a  U R                  X25        [	        UR                  5       5      nU$ S/U R                  S-
  -  U/-   $ ! [        R                   a     N-f = f)Nzhttps://zhttp://rY      )	
startswithr0   rZ   rV   r[   r\   r
   r]   rb   )r1   r9   r:   r@   s       r   r,   Converter._StringToRow   s    6$$Z00$$Y//??jj4(

(
(
=3::< 
 44<<!#$x// 44 s   AB* *C Cc                    [        U5      n[        U R                  5       HJ  u  pEUR                  U R                  ;   a  M!  UR                  UR                  5      nX4   U:w  a    OSX4'   ML     SU R                  ;   a  U R                  nSnOS nSn[        R                  " [        R                  " [        U5      UUS95      $ )NrY   r   T)r   is_fully_qualified)r[   	enumerate
parametersr   r#   rS   r   six	text_typer
   r^   rc   )	r1   r@   r:   partscolumn	parametervaluer   rk   s	            r   r-   Converter._GRI_RowToString   s     IE&t7	499	9%%inn5e	%	em 8 t555??jj==UO!1	34 4r   c                    XR                   S-
     /nU R                  nSnXPR                  ;   a#  [        R                  " US9n[        U5      U/-   nU H|  nUR                  U R                   S-
  :X  a  M"  UR                  U R                  ;  nUR                  UR                  XR                     US9n	U	(       d  Mk  UR                  U	5        M~     [        U R                  =(       d    / UR                  5       -   =(       d    / 5       H/  n
UR                  U
S5      n	U	(       d  M  UR                  U	5        M1     SR                  U5      $ )Nrg   r   r   rP   T )rb   rm   r#   r   	Parameterr[   rq   r   GetFlagappendr"   r!   GetAdditionalParamsr   )r1   r@   r:   rp   rm   r   collection_parameterrr   rQ   flag	flag_names              r   r/   Converter._FLAGS_RowToString   s;   !"#EJD---+554@
#';&<<j				T\\A-	-"t/M/MM##
..

+ $ -d 
T   00 DB';;=5> DACE	##It4d	T	E
 88E?r   )r!   r0   r.   r+   r   r#   )NNNNFN)__name__
__module____qualname____firstlineno____doc__r   r;   rA   rD   rJ   staticmethodrV   r*   r,   r-   r/   __static_attributes____classcell__r6   s   @r   r   r   5   sb    	 "&)- <7C@B" . .4$040 r   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )ResourceCompleter   zA parsed resource parameter initializer.

Attributes:
  collection_info: The resource registry collection info.
  parse: The resource URI parse function. Converts a URI string into a list
    of parsed parameters.
c           	      F  >^^
 X l         T(       a  [        R                  R                  TUS9U l        U R                  R                  S5      n[        R                  " ST< SU R                  R                   < SU< 35        [        U5       VVs/ s H  u  pg[        R                  " XvS9PM     nnn[        R                  R                  m
UU
4S jn	Xl        O/ n/ n[        [        U ]>  " ST[!        U5      U(       a  UR#                  U5      OSUS	.UD6  g
s  snnf )zConstructor.

Args:
  collection: The resource collection name.
  api_version: The API version for collection, None for the default version.
  param: The updated parameter column name.
  **kwargs: Base class kwargs.
)api_versionrY   zcache collection=z api_version=z params=)r   rq   c                 2   > T" U TSSS9R                  5       $ )NF)r   enforce_collectionvalidate)AsDict)r9   r   rZ   s    r   _Parse*ResourceCompleter.__init__.<locals>._Parse  s%    !$	 #FH		%r   r   )r   rb   rq   rm   Nr   )r   r
   REGISTRYGetCollectionInfocollection_info	GetParamsr   inforl   r   rw   ParserZ   r   r   r   ra   index)r1   r   r   paramr5   paramsrq   r   rm   r   rZ   r6   s    `        @r   r   ResourceCompleter.__init__   s
    #&//AA
+ B /d##--b1f	hh
d**66@ A )2&(9;(9 #,,$F(9  ;  &&e% jfj	
T+ F&+v||E"	
 !;s    D)r   r   rZ   )NNN)r   r   r   r   r   r   r   r   r   s   @r   r   r      s    % %r   r   c                   H   ^  \ rS rSrSr   SU 4S jjrS rS rS rSr	U =r
$ )	ListCommandCompleteri  a  A parameterized completer that uses a gcloud list command for updates.

Attributes:
  list_command: The gcloud list command that returns the list of current
    resource URIs.
  flags: The resource parameter flags that are referenced by list_command.
  parse_output: The completion items are written to the list_command standard
    output, one per line, if True. Otherwise the list_command return value is
    the list of items.
c                 f   > Xl         U=(       d    / U l        X0l        [        [        U ]  " S0 UD6  g Nr   )_list_command_flags_parse_outputr   r   r   )r1   list_commandflagsparse_outputr5   r6   s        r   r   ListCommandCompleter.__init__"  s/    
 &+2DK%	
.88r   c                    S nU R                   R                  5       nU Vs1 s H"  oDR                  S5      (       d  M  U" U5      iM$     nnSU;  a"  UR                  S5        UR	                  S5        SU;   a(  SU;  a"  UR                  S5        UR	                  S5        U R
                  U R                   Vs/ s H  ofR                  PM     sn-   UR                  5       -    HN  nUR                  USSS	9nU(       d  M  U" U5      n	X;  d  M,  UR                  U	5        UR	                  U5        MP     U$ s  snf s  snf )
z3Returns the list command argv given parameter_info.c                 *    U R                  S5      S   $ )N=r   )r    )r|   s    r   	_FlagName6ListCommandCompleter.GetListCommand.<locals>._FlagName/  s    ZZ_Qr   z--z--quietz--uriz--formatz--format=disableFT)rQ   
for_update)
r   r    rh   addry   r   rm   r   rz   rx   )
r1   r:   r   r   fr   rr   r   r|   r}   s
             r   GetListCommand#ListCommandCompleter.GetListCommand,  s)     %%++-L#/F<a<<3E\Yq\<EFii	)$%Je3ii
,-26//B/Y../BC3356 ##
4 $ 9d	dO	!
))I



d
#6 # G Cs   D;D;<E c                     U R                   (       d.  UR                  U5      U R                   (       a  W[        l        $ $ [        R                  n[        R
                  " 5       nU[        l        UR                  U5        UR                  5       R                  S5      R                  S5      U R                   (       a  U[        l        $ $ ! U R                   (       a  W[        l        f f = f)z6Runs command and returns the list of completion items.
)	r   Executer   outioStringIOgetvaluerstripr    )r1   commandr:   log_outr   s        r   GetAllItems ListCommandCompleter.GetAllItemsF  s    
%%g. 
		 
 gKKMccgW%\\^""4(..t4			 
		 
s   !C A.C C,c           	      |   U R                  U5      nU HI  nUR                  UR                  UR                  SS9nU(       d  M1  XS;  d  M8  UR	                  U5        MK     [
        R                  " SSR                  U5      -  5         [        U R                  X15      =(       d    / 5      nU Vs/ s H  oR/                  X5      PM     sn$ ! [        [        4 a  n[        R                  R                  R                  R!                  5       (       a  e [
        R                  " ["        R$                  " U5      R'                  5       5         [)        U5      " SR+                  SR                  U5      ["        R$                  " U5      R'                  5       5      5      e! [,         a    Uef = fSnAff = fs  snf )z?Returns the current list of parsed resources from list_command.T)r   zcache update command: %srv   zUpdate command [{}]: {}N)r   rx   r   rs   ry   r   r   r   r[   r   	Exception
SystemExitr	   r$   r%   print_completion_tracebacksr)   rn   ro   r   typeformat	TypeErrorr;   )	r1   r:   aggregationsr   rr   r|   itemseitems	            r   UpdateListCommandCompleter.UpdateT  si   !!.1G!	##
..)//d $ <d	$%t	 "
 HH'#((7*;;<
4##G<BCe @EEutT2uEE z" 						;	;	C	C	E	E	hhs}}Q&&()Aw188HHWs}}Q/668: ; 	;  Fs2   
#C 1F9F6A0F1AF!!F..F11F6)r   r   r   )NNF)r   r   r   r   r   r   r   r   r   r   r   r   s   @r   r   r     s.    	 !!94F Fr   r   c                       \ rS rSrSrS rSrg)ResourceSearchCompleterik  zFA parameterized completer that uses Cloud Resource Search for updates.c           	      l   SR                  U R                  5      n[        R                  " SU-  5         [        R
                  " USS9nU Vs/ s H  o`R!                  Xa5      PM     sn$ ! [         a  n[        R                  R                  R                  R                  5       (       a  e [        R                  " [        R                  " U5      R                  5       5        [        U5      " SR                  U[        R                  " U5      R                  5       5      5      eSnAff = fs  snf )z-Returns the current list of parsed resources.z@type:{}zcloud resource search query: %sT)queryurizUpdate resource query [{}]: {}N)r   r   r   r   r   Listr   r	   r$   r%   r   r)   rn   ro   r   r   r;   )r1   r:   r   r   r   r   r   s          r   r   ResourceSearchCompleter.Updaten  s    doo.EHH.67-""D9e @EEutT2uEE  -						;	;	C	C	E	E	hhs}}Q&&()!W6==
q!((*, - -	- Fs   A, D1,
D.6B3D))D.r   N)r   r   r   r   r   r   r   r   r   r   r   r   k  s    NFr   r   c                   :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )ResourceParamCompleteri}  zAA completer that produces a resource list for one resource param.c                 4   > [         [        U ]
  " SUUS.UD6  g )N)r   r   r   )r   r   r   )r1   r   r   r5   r6   s       r   r   ResourceParamCompleter.__init__  s(    	
 $0  r   c                     XR                      $ )r>   )rq   r?   s      r   rA   "ResourceParamCompleter.RowToString  s    {{r   r   NNr   )	r   r   r   r   r   r   rA   r   r   r   s   @r   r   r   }  s    I r   r   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )MultiResourceCompleteri  zkA completer that composes multiple resource completers.

Attributes:
  completers: The list of completers.
c                 l  > U Vs/ s H  nU" S0 UD6PM     snU l         0 nU(       a  U H  nSXV'   M	     U R                    H_  nUR                  (       d  M  UR                   H9  nUR                  U;   a  XXR                  ==   S-  ss'   M+  SXXR                  '   M;     Ma     [        R                  " U5       VV	s1 s H"  u  piU	[        U R                   5      :w  d  M   UiM$     nnn	/ n
[        5       nU R                    H\  nUR                  U5        UR                  UR                  R                  S5      S   5        U
R                  UR                  5        M^     SR                  U
5      n[        U5      S:X  a  UR                  5       OSn[        [        U ]B  " SXS.UD6  gs  snf s  sn	nf )zConstructor.

Args:
  completers: The list of completers.
  qualified_parameter_names: The set of parameter names that must be
    qualified.
  **kwargs: Base class kwargs.
rg   r   r   ,N)r   r   r   )
completersrm   r   rn   	iteritemsra   r"   rD   r   r   r    ry   r   popr   r   r   )r1   r   r#   r5   completer_class
name_countr   	completerrr   countcollectionsapisr   r   r6   s                 r   r   MultiResourceCompleter.__init__  s    /9:.8? '00.8:DOJ +$
 ,__					"--I^^z)~~&!+&)*J~~&	 . % !$j 9!* 9C(( 	 9  !* K5D__	**+DE
hhy##))#.q12--. % +&JD	Q$((*DC	
 $0 22*02?:!*s   F+>F0!F0c           
          [        U R                   VVs1 s H  nUR                  X5        H  nUiM     M      snn5      $ s  snnf )z5Returns the union of completions from all completers.)sortedr   Complete)r1   prefixr:   r   completionss        r   r   MultiResourceCompleter.Complete  sP    //	H)Y%..vF[ 
F 
)	HI I	Hs   %A
c                     AAg)z"Update handled by self.completers.Nr   r1   r:   r   s      r   r   MultiResourceCompleter.Update  s    r   )r   r   )
r   r   r   r   r   r   r   r   r   r   r   s   @r   r   r     s    )2VI r   r   c                   ^   ^  \ rS rSrSrSU 4S jjr\R                  S 5       rSS jr	Sr
U =r$ )	NoCacheCompleteri  z,A completer that does not cache completions.c                 0   > A[         [        U ]
  " S0 UD6  g r   )r   r   r   )r1   cacher5   r6   s      r   r   NoCacheCompleter.__init__  s    	
D*4V4r   c                     AAg)a_  Returns the list of strings matching prefix.

This method is normally provided by the cache, but must be specified here
in order to bypass the cache.

Args:
  prefix: The resource prefix string to match.
  parameter_info: A ParamaterInfo object for accessing parameter values in
    the program state.

Returns:
  The list of strings matching prefix.
Nr   )r1   r   r:   s      r   r   NoCacheCompleter.Complete  s
     	r   c                     AAg)z2Satisfies abc resolution and will never be called.Nr   r   s      r   r   NoCacheCompleter.Update  s    r   r   r   r   )r   r   r   r   r   r   abcabstractmethodr   r   r   r   r   s   @r   r   r     s/    45  "% %r   r   )r   
__future__r   r   r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.utilr   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.cacher   r   rn   r   r   	Completerr   r   r   r   r   r   with_metaclassABCMetar   r   r   r   <module>r     s     G &  ' 
 	 7 > # * ) 6 4 
 ( 5m ** m`.	 .bRF, RFjF/ F$1 <Y <~%s))#++yA %r   