
    -                     H   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 dd	lm
Z
 dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZ G d de      Zd Z	 	 ddZ G d de
j:                        Z G d de	j>                        Z y)z&The meta cache completers run command.    )absolute_import)division)print_function)unicode_literalsN)arg_parsers)base)parser_extensions)
cache_util)parameter_info_lib)concept_parsers)presentation_specs)
exceptions)log)module_util)
console_ioc                   "    e Zd ZdZd Zd Zd Zy)_FunctionCompleterzEConvert an argparse function completer to a resource_cache completer.c                      || _         d | _        y N)
_completer
parameters)self	completers     (lib/surface/meta/cache/completers/run.py__init__z_FunctionCompleter.__init__+   s    DODO    c                     ~|S r    )r   parsed_argsarguments      r   ParameterInfoz _FunctionCompleter.ParameterInfo/   s    r   c                 (    | j                  ||      S )N)r   )r   )r   prefixparameter_infos      r   Completez_FunctionCompleter.Complete3   s    ??6~?>>r   N)__name__
__module____qualname____doc__r   r!   r%   r   r   r   r   r   (   s    M?r   r   c                    t        j                  |       }t        |      r |       }|j                  dd      }|j	                  d      D ci c]1  }|r-|j	                  d      d   d|v r|j	                  d      d   nd3 }}|j                  dd      }t        j                  |j                  d	|j                        |d
f||d|S c c}w )zBuild a presentation spec.flag_name_overrides ;:r      prefixesFnamez	help text)r+   r0   )r   ImportModulecallablepopsplitr   ResourcePresentationSpecr1   )resource_spec_pathkwargsresource_specr+   or0   s         r   _GetPresentationSpecr;   7   s    **+=>-m!OM

#8"= #((--!	
 ggcl1o#(qwws|A:-   ZZ
E*(		4	4jj++,
 .
 	
 s   6Cc                 .   |xs i }|r3t        |fi |} t        j                  |       |j                  |      }n0t        j                  |       }t	        |t
              st        |      S 	  |d||d|S # t        $ r t         |             cY S w xY w)z2Returns an instantiated completer for module_path.)cachequalified_parameter_namesr   )r;   r   r2   concept_spec
isinstancetyper   	TypeError)	module_pathr=   qualifyr9   presentation_kwargs	attributer8   presentation_specr   s	            r   _GetCompleterrH   K   s     ,1r,] D/BD5((5&&I
 ((5Ii&	**+ ")   
 +ik**+s   -
A8 8BBc                   .     e Zd ZdZ fdZd Zd Z xZS )AddCompleterResourceFlagsz4Adds resource argument flags based on the completer.c                 H    t        t        | 
  |i | d | _        d | _        y r   )superrJ   r   $_AddCompleterResourceFlags__argument%_AddCompleterResourceFlags__completer)r   argsr8   	__class__s      r   r   z"AddCompleterResourceFlags.__init__g   s'    	
#T3TDVDDODr   c           
      H   g }|j                   xs i }|j                  rt        |j                  fi |}t        j                  |g      j                  |j                        }|j                         D ][  }|j                  j                  d      rd|j                  d<   n|j                  sdnd|j                  d<   |j                  |       ] |j                  xs i }t        |f|j                  |j                  ||j                  d|| _        | j                  j                   r| j                  j                   D ]  }	t#        j$                  |	j                        }
t'        ||
      r/t#        j(                  |
      }t+        j,                  ||
dd	j/                  | j                  j0                  j2                  |	j                        
      }|j                  |        t+        j,                  ddd      | _        |j                  | j4                         |S )Nz--Frequired?*nargs)rD   r9   rE   rF   zRESOURCE COMPLETERz{} `{}` parameter value.)destcategoryhelpresource_to_completezThe partial resource name to complete. Omit to enter an interactive loop that reads a partial resource name from the input and lists the possible prefix matches on the output or displays an ERROR message.)rU   rX   )resource_presentation_kwargsr7   r;   r   ConceptParserGetInfor1   GetAttributeArgs
startswithr8   pluralappendrH   rD   rF   rN   r   r   GetDestFromParamhasattrGetFlagFromDestr   ArgumentformatrP   r&   rM   )r   	namespacerC   rO   rE   specinfoargr8   	parameterrV   flags               r   GenerateArgsz&AddCompleterResourceFlags.GenerateArgsl   s   D#@@FB ##!)">"> 9$79d**D62::499Ed&&(#88t$#(#**Z
 +/;;C#**W
C ) #F$&..22/%%	
 D ""''22)!229>>B9d#
!11$7mm)+22  **33Y^^E	F 	C 3 mm.0DO 	KK Kr   c                     | j                   j                  || j                        }| j                   j                  ||      S r   )rN   r!   rM   r%   )r   r#   r   r8   r$   s        r   Completionsz%AddCompleterResourceFlags.Completions   s:    %%33T__&N$$V^<<r   )r&   r'   r(   r)   r   rl   rn   __classcell__)rP   s   @r   rJ   rJ   d   s    <
,\=r   rJ   c                   &    e Zd ZdZed        Zd Zy)RunaK  Cloud SDK completer module tester.

  *{command}* is an ideal way to debug completer modules without interference
  from the shell.  Shells typically ignore completer errors by disabling all
  standard output, standard error and exception messaging.  Specify
  `--verbosity=INFO` to enable completion and resource cache tracing.
  c                 <   t        j                  g       j                  |        | j                  dd       | j                  dd       | j                  dt	        j
                  t        t        t        t        t        d      d	
       t        j                  |        | j                  ddt	        j                         d       | j                  ddt	        j
                         d       | j                  dddd       | j                  dt        d       y )Nz--resource-spec-pathzHThe resource spec path for a resource argument auto-generated completer.)rX   z--attributezTThe name of the resource attribute for a resource argument auto-generated completer.z--resource-presentation-kwargs)r1   r+   r_   r0   rR   )rg   a-  Dict of kwargs to be passed to the presentation spec for the resource argument for which a completer is being tested, such as name, prefixes, plural, flag name overrides (format as a list of semicolon-separated key:value pairs). Prefixes is False by default. Name is the resource spec name by default.)rA   rX   z	--qualifyNAMEzA list of resource parameter names that must always be qualified. This is a manual setting for testing. The CLI sets this automatically.)metavarrA   rX   z--kwargsz
NAME=VALUEzKeyword arg dict passed to the completer constructor. For example, use this to set the resource collection and list command for `DeprecatedListCommandCompleter`:

  --kwargs=collection=...,foo="..."z--stack-trace
store_trueTzGEnable all exception stack traces, including Cloud SDK core exceptions.)actiondefaultrX   rC   a  The completer module path. Run $ gcloud meta completers list` to list the module paths of the available completers. A completer module may declare additional flags. Specify `--help` after _MODULE_PATH_ for details on the module specific flags.

NOTE: To test resource argument completers, use the module path "googlecloudsdk.command_lib.util.completers:CompleterForAttribute". The flags `--resource-spec-path`, `--attribute`, and (if desired) `--resource-presentation-kwargs` must be provided BEFORE the positional. Unlike with most gcloud commands, the arguments are generated on the fly using the completer you provide, so all the information to create a resource completer needs to be provided up-front. For example:

  $ {command} --resource-spec-path MODULE_PATH:SPEC_OBJECT --attribute ATTRIBUTE_NAME --resource-presentation-kwargs flag_name_overrides=ATTRIBUTE1:FLAG1;ATTRIBUTE2:FLAG2 googlecloudsdk.command_lib.util.completers:CompleterForAttribute)rv   rX   )r   r[   AddToParseradd_argumentr   ArgDictstrboolr
   AddCacheFlagArgListAddDynamicPositionalrJ   )parsers    r   ArgszRun.Args   s@    !!"%11&9
   *  , (  '*  "#G  I F#
  "$	  &   "4	  6 	   (Q   r   c           
         |j                   xs i }t        j                  |j                  d      5 }t	        j
                  dj                  |j                               |j                  si |_        |j                  rbt        |j                  fi |}d|_        t        j                  |g      j                  |j                        fd}||j                  _        t#        |j$                  f||j&                  |j                  ||j(                  d|j                  }|j+                  ||j-                  d            }|j.                  (|j1                  |j.                  |      }|gcddd       S 	 t3        j4                  d	      }	|	n0	 |j1                  |	|      }
|
rtG        d
jI                  |
             HtJ        jL                  jO                  d
       	 ddd       y# t6        t8        f$ r]}|j:                  rt=        j>                  t7        |             n(t	        j@                  tC        jD                  |             Y d}~d}~ww xY w# 1 sw Y   yxY w)z'Returns the results for one completion.T)createzcache name {}Fc                      ~ ~S r   r   )rO   r8   resource_infos     r   ResourceInfoMonkeyPatchz(Run.Run.<locals>.ResourceInfoMonkeyPatch   s    F
r   )r=   rD   r9   rE   rF   rY   Nz
COMPLETE> 
)(rZ   r
   GetCacher=   r   rh   re   r1   r8   r7   r;   rR   r   r[   r\   CONCEPTSArgNameToConceptInforH   rC   rD   rF   r!   GetPositionalArgumentrY   r%   r   PromptResponse	Exception
SystemExitstack_tracer   reraiseerrorsix	text_typeprintjoinsysstderrwrite)r   rO   rE   r=   rg   r   r   r$   matchesr1   completionser   s               @r   rq   zRun.Run   s   ;;Ar			TZZ	5	hh%%ejj12[[ 
	 	 ####!# '55tf=EEdiiP	 .E*


"'//1NN	
 KKi !..
**+ABDn		"	"	.$$T%>%>Oy= 
6	5> ((6<
	!**4@+ 
		+&
'  
jjt] 
6	5J :& 	y|,IIcmmA&'
	K 
6	5s=   D?I<IG.(=I.I=AIIIII&N)r&   r'   r(   r)   staticmethodr   rq   r   r   r   rq   rq      s#     B BH1r   rq   )NNNNN)!r)   
__future__r   r   r   r   r   googlecloudsdk.callioper   r   r	   googlecloudsdk.command_lib.metar
   googlecloudsdk.command_lib.utilr   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler   r   objectr   r;   rH   DynamicPositionalActionrJ   Commandrq   r   r   r   <module>r      s     - &  % ' 
 / ( 5 6 > D G * # + 2 
? ?( 48JN+29= 1 I I 9=x$,, r   