
    :                     &   S 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Jr  SSKrSS
KJr  S rS r\\S.r " S S\R,                  5      r " S S\5      r " S S\R0                  " \R2                  \5      5      rg)z2Facilities for user prompting for request context.    N)
exceptions)lister)utils)
properties)	resources)
console_io)gce)zipc                      [         R                  R                  R                  R	                  5       (       a#  [
        R                  " 5       R                  5       $ g N)r   VALUEScorecheck_gce_metadataGetBoolc_gceMetadataRegion     4lib/googlecloudsdk/api_lib/compute/scope_prompter.py_GetGCERegionr       s<    ..6688>>""$$	r   c                      [         R                  R                  R                  R	                  5       (       a#  [
        R                  " 5       R                  5       $ g r   )r   r   r   r   r   r   r   Zoner   r   r   _GetGCEZoner   &   s<    ..6688>>  ""	r   )zoneregionc                       \ rS rSrSrSrg)Error2   z"Exceptions for the scope prompter.r   N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   r   r   2   s    *r   r   c                       \ rS rSrSrSrg)_InvalidPromptInvocation7   z6Exception for invoking prompt with invalid parameters.r   Nr    r   r   r   r(   r(   7   s    >r   r(   c                   h   \ rS rSrSr\R                  S 5       r\R                  S 5       r\R                  S 5       r	\R                  S 5       r
\R                  S 5       r\R                  S 5       r SS
 jrS rS rS rS r SS jr  SS jr  SS jr SS jr SS jrSS jrSS jrSrg	)ScopePrompter<   zBA mixin class prompting in the case of ambiguous resource context.c                     g)z<Specifies the name of the collection that should be printed.Nr   selfs    r   resource_typeScopePrompter.resource_type?        	r   c                     g)z2Specifies the http client to be used for requests.Nr   r.   s    r   httpScopePrompter.httpD   r2   r   c                     g)zSpecifies the user's project.Nr   r.   s    r   projectScopePrompter.projectI   r2   r   c                     g)zSpecifies the API batch URL.Nr   r.   s    r   	batch_urlScopePrompter.batch_urlN   r2   r   c                     g)zSpecifies the compute client.Nr   r.   s    r   computeScopePrompter.computeS   r2   r   c                     g)z5Specifies the resources parser for compute resources.Nr   r.   s    r   r   ScopePrompter.resourcesX   r2   r   Nc           	      z   U(       a  SR                  U5      nOSn/ n[        R                  " UU R                  UU R                  U R
                  US9nU Vs/ s H  oPM     n	nU(       d  U	(       dB  U(       a  SOSn
[        R                  " USR                  USR                  U5      U
5      5        U	$ s  snf )z.Returns a list of choices used to prompt with.zname eq {0}.*N)servicer7   filter_exprr4   r:   errors:.zFUnable to fetch a list of {0}s. Specifying [{1}] may fix this issue{2}z, or )	formatr   GetGlobalResourcesr7   r4   r:   r   RaiseToolExceptionjoin)r/   	attributerB   
flag_namesprefix_filterrC   rD   global_resourcesresourcechoicespunctuations              r   FetchChoiceResources"ScopePrompter.FetchChoiceResources]   s     #**=9kkF00YY.. )99(8Hx(8G9W!Csk
VIw||J'?MO
 N :s   B8c                   ^ U4S jn[        U5      [        U5      :w  a
  [        5       eSnSn	[        U5      S:X  aK  [        R                  US   5      =(       d    S n
U
" 5       nU(       a  US   n	U R	                  XS   UX5      nUc@  0 n[        X25       H  u  pU R                  XTU5      X'   M     U R                  XXLU5      u  pUc   eU	c   e/ nU Hb  u  nnnUR                  5       nU	U;   a  UUU	'    U R                  R                  UUUS9n[        UU	5      (       a  UR                  U5        Mb  Md     U$ ! [        R                  [        R                  4 a     M  f = f)a  Prompts user to specify a scope for ambiguous resources.

Args:
  ambiguous_names: list(tuple(name, params, collection)),
    list of parameters which can be fed into resources.Parse.
  attributes: list(str), list of scopes to prompt over.
  services: list(apitool.base.py.base_api.BaseApiService), service for each
    attribute/scope.
  resource_type: str, collection name without api name.
  flag_names: list(str), flag names which can be used to specify scopes.
  prefix_filter: str, used to filter retrieved resources on backend.
Returns:
  List of fully resolved names for provided ambiguous_names parameter.
Raises:
  _InvalidPromptInvocation: if number of attributes does not match number of
    services.
c                     > [        T5      S:  a  SOSn [        R                  " SR                  U S   SR	                  T5      U S   5      5      e)zDCall this to raise an exn when prompt cannot read from input stream.   )zone of flags) flagz+Unable to prompt. Specify {0}the [{1}] {2}.r   , )lencompute_exceptionsFailedPromptErrorrG   rJ   )phrasesrL   s    r   RaiseOnPromptFailure;ScopePrompter._PromptForScope.<locals>.RaiseOnPromptFailure   sO    (+J!(;$g00
7
>
>aj$))J/=> >r   NrV   r   c                      g r   r   r   r   r   <lambda>/ScopePrompter._PromptForScope.<locals>.<lambda>   s    r   params
collection)r[   r(   GCE_SUGGESTION_SOURCESget_PromptDidYouMeanScoper
   rR   _PromptForScopeListcopyr   ParsehasattrappendRequiredFieldOmittedExceptionr   RequiredPropertyError)r/   ambiguous_names
attributesservicesr0   rL   rM   r_   selected_resource_nameselected_attributegce_suggestorgce_suggested_resourcechoice_resourcesrB   rK   resultambigous_namere   rf   
new_paramsresource_refs        `               r   _PromptForScopeScopePrompter._PromptForScopex   s   *> :#h-'$&& " :!
 
$
$Z]
3
E ,	']!%!<!<]M"":
 % #H 9
'%%J? 	# !: 483K3K
}
4 0
 "---)))F-<)vz;;=j	z	))?
%&&~~++* , E <!344
--
% 5 .= M 55..0 s   <E$E)(E)c                 ,   [         R                  " U5      nU VVs/ s H  u  n  oPM
     n	nnSR                  X$USR                  U	5      5      n
 [        R
                  " U
SSS9(       a  U$ gs  snnf ! [        R                   a
    U" 5          gf = f)z@Prompts "did you mean <scope>".  Returns str or None, or raises.z&Did you mean {0} [{1}] for {2}: [{3}]?rZ   T)messagedefaultthrow_if_unattendedN)r   CamelCaseToOutputFriendlyrG   rJ   r   PromptContinueUnattendedPromptError)r/   ambiguous_refsrK   r0   suggested_resourceraise_on_prompt_failureresource_namename_namesr   s              r   ri   $ScopePrompter._PromptDidYouMeanScope   s    
 33MBM$23NjdAqTNE36==}dii6FHG 		"	"7D7;
=!! 4 ++   s   A/A5 5BBc           	         [         R                  " U5      nU VVs/ s H  u  n  nSR                  U5      PM     n	nn/ n
/ nU H  nXL    H  nUR                  nU(       a'  SR                  UR                  UR
                  5      nOUR                  n[        U5      S:  a  SR                  X5      nUR                  XR                  45        U
R                  U5        M     M     [         R                  " SR                  U5      U	5      n[        R                  " U
SR                  USR                  U5      5      S9nUc  U" 5         g	UU   $ s  snnf )
zDPrompt to resolve abiguous resources.  Either returns str or throws.z[{0}]z	{0} ({1})rV   z{0}: {1}zFor the following {0}:z{0}choose a {1}:z or )optionsr   N)r   r   rG   
deprecatedr   stater[   rn   ConstructListr   PromptChoicerJ   )r/   r   rr   r0   rx   r   r   r   r   r   choice_nameschoice_mappingrK   choice_resourcer   choice_nametitleidxs                     r   rj   !ScopePrompter._PromptForScopeList   sB   
 33MBM4BCNjdAqW^^D!NECLN	-8/$//
#**""J$4$46+ (,,+z?Q")))A+y*>*>?@K( 9    ''6?E

!
!"))%Z1HIKC {C  7 Ds   Ec           
         [        U5      [        U5      :w  d  [        U5      [        U5      :w  a
  [        5       e/ n/ nU GH  nU GH  n	[        R                  " U	5      n
S[        R
                  R                  R                  R                  0nU R                  R                  U
5      nSUR                  ;   a2  [        R
                  R                  R                  R                  US'   OASUR                  ;   a1  [        R
                  R                  R                  R                  US'    U R                  R                  XU
S9nUR!                  U5        GM     GM"     U(       a  X`R)                  UUUUS   USS9-  nU$ ! [        R"                   a     GMX  [        R$                  [        R&                  4 a    UR!                  XU
45         GM  f = f)zFPrompt for resources, which can be placed in several different scopes.r7   r   r   rd   r   Nrq   rr   rs   r0   rL   rM   )r[   r(   r   GetApiCollectionr   r   r   r7   	GetOrFailr   GetCollectionInfore   r=   r   r   rl   rn    WrongResourceCollectionExceptionro   rp   r}   )r/   resource_namesscope_namesscope_servicesresource_typesrL   resource_refsrq   r   r0   rf   re   collection_infor|   s                 r   PromptForMultiScopedReferences,ScopePrompter.PromptForMultiScopedReferences   s    ;3~..KC//$&&MO')-++M:
z((--55??
 ..:::F_+++%,,4499CC&.///'..66==GG&
		---z . C, 

|
,' * (, ++) !&q) ,  m # 99 	
77002 	F

 
 -!D
E
E	Fs   0FG6GGc           
      d   / n/ n	U=(       d    U R                   n[        R                  " U5      n
U H  nS[        R                  R
                  R                  R                  X#=(       d.    [        [        R                  R                  U5      R                  0n U R                  R                  UU
US9nUR                  U5        M     U	(       a  U(       d  XR                  U	U/U/UUUS9-  nU$ ! [        R                  [        R                  4 a    U	R                  XU
45         M  f = f)zDReturns a list of resolved resource references for scoped resources.r7   )rf   re   r   )r0   r   r   r   r   r   r7   r   getattrr=   r   rl   rn   ro   rp   r}   )r/   r   
scope_name	scope_argscope_servicer0   rL   rM   r   rq   rf   r   re   r|   s                 r   CreateScopedReferences$ScopePrompter.CreateScopedReferences+  s1    MO!7T%7%7M''6J'
Z&&++33==
 Pz0088*EOOf
	+~~++! ,  	\* (" y++) \!?%% , ' 'm ! 55..0 DzBCDs   C447D/.D/c           
      &   Uc  S/nU(       aZ  U R                   R                  US[        R                  R                  R
                  R                  0SS9nUR                  5       nOSnU R                  USUU R                  R                  UUUS9$ )z5Returns a list of resolved zonal resource references.Nz--zoner7   zcompute.zonesrd   r   )r   r   r   r0   rL   rM   )r   rl   r   r   r   r7   r   Namer   r=   zones)r/   r   zone_argr0   rL   region_filterzone_ref	zone_names           r   CreateZonalReferences#ScopePrompter.CreateZonalReferencesP  s     :j%%
**//77AA % & &h --/ii&&ll((## ' % %r   c                 0    U R                  U/X#XE5      S   $ Nr   )r   )r/   r   r   r0   rL   r   s         r   CreateZonalReference"ScopePrompter.CreateZonalReferencej  s)    %%	*MMNP Pr   c           	      $   Uc  S/nU(       aZ  U R                   R                  US[        R                  R                  R
                  R                  0SS9nUR                  5       nOSnU R                  USUU R                  R                  UUS9$ )z8Returns a list of resolved regional resource references.Nz--regionr7   zcompute.regionsrd   r   )r   r   r   rL   r0   )r   rl   r   r   r   r7   r   r   r   r=   regions)r/   r   
region_argrL   r0   
region_refregion_names          r   CreateRegionalReferences&ScopePrompter.CreateRegionalReferenceso  s     <j>>''
**//77AA ' ( (j OO%kk&&ll**# ' % %r   c                 0    U R                  U/X#U5      S   $ r   )r   )r/   r   r   rL   r0   s        r   CreateRegionalReference%ScopePrompter.CreateRegionalReference  s)    ((	@@AC Cr   c                    / nU H  nUR                  U R                  R                  US[        R                  R
                  R                  R                  0[        R                  " U=(       d    U R                  5      S95        M     U$ )z6Returns a list of resolved global resource references.r7   rd   )rn   r   rl   r   r   r   r7   r   r   r   r0   )r/   r   r0   r   r   s        r   CreateGlobalReferences$ScopePrompter.CreateGlobalReferences  s    M'4>>//
**//77AA ++1t113 0 4 5 ( r   c                 .    U R                  U/U5      S   $ r   )r   )r/   r   r0   s      r   CreateGlobalReference#ScopePrompter.CreateGlobalReference  s    &&FqIIr   r   r   )NNN)NN)r!   r"   r#   r$   r%   abcabstractpropertyr0   r4   r7   r:   r=   r   rR   r}   ri   rj   r   r   r   r   r   r   r   r   r&   r   r   r   r+   r+   <   s   J	 	 	 	 	 	 	 	 	 	 	 	
 *.6M^ &"!H+^ ,0#J KO;?%4 IM:>P ?C%4 >BC
Jr   r+   )r%   r   googlecloudsdk.api_lib.computer   r   r   "googlecloudsdk.command_lib.computer\   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr	   r   six	six.movesr
   r   r   rg   r   r(   with_metaclassABCMetaobjectr+   r   r   r   <module>r      s    9  5 1 0 O * ) 2 8 
   J 
u 
_JC&&s{{F; _Jr   