
    u:                     J   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Jr  SSKJr  SSKrSSKJr  S rS r\\S.r " S S\R4                  5      r " S S\5      r " S S\R8                  " \R:                  \5      5      rg)z2Facilities for user prompting for request context.    )absolute_import)division)unicode_literalsN)
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)Error5   z"Exceptions for the scope prompter.r   N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   r!   r!   5   s    *r   r!   c                       \ rS rSrSrSrg)_InvalidPromptInvocation:   z6Exception for invoking prompt with invalid parameters.r   Nr#   r   r   r   r+   r+   :   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_typeB        	r   c                     g)z2Specifies the http client to be used for requests.Nr   r1   s    r   httpScopePrompter.httpG   r5   r   c                     g)zSpecifies the user's project.Nr   r1   s    r   projectScopePrompter.projectL   r5   r   c                     g)zSpecifies the API batch URL.Nr   r1   s    r   	batch_urlScopePrompter.batch_urlQ   r5   r   c                     g)zSpecifies the compute client.Nr   r1   s    r   computeScopePrompter.computeV   r5   r   c                     g)z5Specifies the resources parser for compute resources.Nr   r1   s    r   r
   ScopePrompter.resources[   r5   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)servicer:   filter_exprr7   r=   errors:.zFUnable to fetch a list of {0}s. Specifying [{1}] may fix this issue{2}z, or )	formatr   GetGlobalResourcesr:   r7   r=   r   RaiseToolExceptionjoin)r2   	attributerE   
flag_namesprefix_filterrF   rG   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FailedPromptErrorrJ   rM   )phrasesrO   s    r   RaiseOnPromptFailure;ScopePrompter._PromptForScope.<locals>.RaiseOnPromptFailure   sO    (+J!(;$g00
7
>
>aj$))J/=> >r   NrY   r   c                      g r   r   r   r   r   <lambda>/ScopePrompter._PromptForScope.<locals>.<lambda>   s    r   params
collection)r^   r+   GCE_SUGGESTION_SOURCESget_PromptDidYouMeanScoper   rU   _PromptForScopeListcopyr
   ParsehasattrappendRequiredFieldOmittedExceptionr	   RequiredPropertyError)r2   ambiguous_names
attributesservicesr3   rO   rP   rb   selected_resource_nameselected_attributegce_suggestorgce_suggested_resourcechoice_resourcesrE   rN   resultambigous_namerh   ri   
new_paramsresource_refs        `               r   _PromptForScopeScopePrompter._PromptForScope{   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}]?r]   T)messagedefaultthrow_if_unattendedN)r   CamelCaseToOutputFriendlyrJ   rM   r   PromptContinueUnattendedPromptError)r2   ambiguous_refsrN   r3   suggested_resourceraise_on_prompt_failureresource_namename_namesr   s              r   rl   $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})rY   z{0}: {1}zFor the following {0}:z{0}choose a {1}:z or )optionsr   N)r   r   rJ   
deprecatedr   stater^   rq   ConstructListr   PromptChoicerM   )r2   r   ru   r3   r{   r   r   r   r   r   choice_nameschoice_mappingrN   choice_resourcer   choice_nametitleidxs                     r   rm   !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.r:   r   r   rg   r   Nrt   ru   rv   r3   rO   rP   )r^   r+   r   GetApiCollectionr	   r   r   r:   	GetOrFailr
   GetCollectionInforh   r@   r   r   ro   rq    WrongResourceCollectionExceptionrr   rs   r   )r2   resource_namesscope_namesscope_servicesresource_typesrO   resource_refsrt   r   r3   ri   rh   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.r:   )ri   rh   r   )r3   r   r   r	   r   r   r:   r   getattrr@   r
   ro   rq   rr   rs   r   )r2   r   
scope_name	scope_argscope_servicer3   rO   rP   r   rt   ri   r   rh   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--zoner:   zcompute.zonesrg   r   )r   r   r   r3   rO   rP   )r
   ro   r	   r   r   r:   r   Namer   r@   zones)r2   r   zone_argr3   rO   region_filterzone_ref	zone_names           r   CreateZonalReferences#ScopePrompter.CreateZonalReferencesS  s     :j%%
**//77AA % & &h --/ii&&ll((## ' % %r   c                 0    U R                  U/X#XE5      S   $ Nr   )r   )r2   r   r   r3   rO   r   s         r   CreateZonalReference"ScopePrompter.CreateZonalReferencem  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--regionr:   zcompute.regionsrg   r   )r   r   r   rO   r3   )r
   ro   r	   r   r   r:   r   r   r   r@   regions)r2   r   
region_argrO   r3   
region_refregion_names          r   CreateRegionalReferences&ScopePrompter.CreateRegionalReferencesr  s     <j>>''
**//77AA ' ( (j OO%kk&&ll**# ' % %r   c                 0    U R                  U/X#U5      S   $ r   )r   )r2   r   r   rO   r3   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.r:   rg   )rq   r
   ro   r	   r   r   r:   r   r   r   r3   )r2   r   r3   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   )r2   r   r3   s      r   CreateGlobalReference#ScopePrompter.CreateGlobalReference  s    &&FqIIr   r   r   )NNN)NN)r$   r%   r&   r'   r(   abcabstractpropertyr3   r7   r:   r=   r@   r
   rU   r   rl   rm   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(   
__future__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   rj   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   