
                             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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\R*                  5      r " S S\R.                  5      rg)z/The BigQuery CLI repl command and related code.    )absolute_import)division)print_functionN)ListOptional)flags)utils)bigquery_command)bq_cached_client)appcommands)bq_error_utilsc                       \ rS rSrSr " S S\5      rSS jr\S\	4S j5       r
S	 rSS
 jrSS jrS\4S jrS rS\S\4S jrS\S\4S jrS\\   4S jrS\S\	4S jrS\S\	4S jrS\4S jrS\S\4S jrSrg)CommandLoop   z?Instance of cmd.Cmd built to work with bigquery_command.NewCmd.c                       \ rS rSrSrSrg)CommandLoop.TerminateSignal   z2Exception type used for signaling loop completion. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       $platform/bq/frontend/command_repl.pyTerminateSignalr      s    <r   r   Nc                    [         R                  R                  U 5        SUS   0U l        / SQU l        UR                  5        Hs  u  p4X0R                  ;  d  M  [        U[        R                  5      (       d  M7  UR                  (       d  MJ  X@R                  U'   [        U SU< 3UR                  5        Mu     U=(       d    SU l        U R                  5         SU l        g )Nhelp)r   replEOFdo_z
BigQuery> r   )cmdCmd__init__	_commands_special_command_namesitems
isinstancer
   NewCmdsurface_in_shellsetattr
RunCmdLoop_default_prompt_set_prompt_last_return_code)selfcommandspromptnamecommands        r   r%   CommandLoop.__init__#   s    GGThv./DN"9D!)
11
1"2"9"9::&&&&t'););< * "1\DDr   returnc                     U R                   $ N)r0   r1   s    r   last_return_codeCommandLoop.last_return_code3   s    !!!r   c                     [         R                  " 5       R                  5       nUR                  (       a)  [	        [
        R                  " US95      nU< S3U l        g U R                  U l        g )N)id_fallbacksz> )	r   ClientGet
project_idstrbq_client_utilsGetReferencer3   r.   )r1   clientpaths      r   r/   CommandLoop._set_prompt7   sN    $$&**,F--6BCd"$dk((dkr   c                 *    [         R                  5       e)zTerminate the running command loop.

This function raises an exception to avoid the need to do
potentially-error-prone string parsing inside onecmd.

Returns:
  Never returns.

Raises:
  CommandLoop.TerminateSignal: always.
)r   r   )r1   unused_argss     r   do_EOFCommandLoop.do_EOF?   s     
%
%
''r   c                     [        S5        g )NzGoodbye.)printr:   s    r   postloopCommandLoop.postloopM   s
    	*r   linec                 @   U(       d  / $ UR                  S5      S   R                  5       nSnXPR                  ;   a  U R                  U   R                  nOUS:X  a  SnOUS:X  a  SnU(       a/  [	        5         [	        U5        [	        U R
                  < U< 3SS9  / $ )	N r    set"set (project_id|dataset_id) <name>unsetunset (project_id|dataset_id)end)	partitionlowerr&   usagerM   r3   )r1   unused_textrP   unused_begidxunused_endidxcommand_namer\   s          r   completedefaultCommandLoop.completedefaultP   s     i^^C(+113le		'|,225 47"/	eT*4ir   c                 r    [        SSS9  [        SR                  [        U R                  5      5      5        g )NzAvailable commands:rR   rX   )rM   joinlistr&   r:   s    r   	emptylineCommandLoop.emptylined   s'    	
S)	#((4'
()r   c                    US:X  a  U$ UR                  S5      (       d  UR                  S5      (       a  gUR                  5       R                  5       n[        U5      S:  a1  US   R	                  5       S:X  a  S[
        R                  " U5      < 3$ [        U5      S:X  a  US   S;  a  S	UR                  5       < 3$ U$ )
zPreprocess the shell input.r!   exitquit   r   selectzquery )r   lsversionzhelp )
startswithstripsplitlenr[   shlexquote)r1   rP   wordss      r   precmdCommandLoop.precmdh   s    u}kv$//&"9"9JJL E
5zA~%(..*h6 ;;t,..
5zQ58+DD**,((Kr   c                     [         R                  R                  X5      U l        g! [        R
                   a     g[         a9  nUR                  S5      S   n[        R                  " X#S9  SU l         SnAgSnAff = f)a  Process a single command.

Runs a single command, and stores the return code in
self._last_return_code. Always returns False unless the command
was EOF.

Args:
  line: (str) Command line to process.

Returns:
  A bool signaling whether or not the command loop should terminate.
TrR   r   )r4   rk   NF)
r#   r$   onecmdr0   r   r   BaseExceptionrq   r   process_error)r1   rP   er4   s       r   ry   CommandLoop.onecmdu   sr    !"ww~~d9d  &&  !ZZ_Qd""10 d	!s   $' A?	A?/A::A?c                    ^  [        T 5      nU 4S jT R                   5       nUR                  S U 5       5        UR                  S5        UR	                  S5        U$ )Nc              3   L   >#    U  H  nUTR                   ;  d  M  Uv   M     g 7fr9   r'   ).0r4   r1   s     r   	<genexpr>(CommandLoop.get_names.<locals>.<genexpr>   s(      "Dt222 	"s   $	$c              3   .   #    U  H  nS U< 3v   M     g7f)r"   Nr   )r   r4   s     r   r   r      s     8xtD"xs   	do_selectrJ   )dirr&   extendappendremove)r1   namesr2   s   `  r   	get_namesCommandLoop.get_names   sP    IENNH
 
LL8x88	LL	LLLr   c                 p   [         R                  " 5       R                  5       nUR                  S5      SS/-   SS u  p4US;  d'  S[	        UR                  S5      5      s=::  a  S::  d  O  [        S5        g
US:X  a  UR                  (       d  [        S	5        g
[        X#U5        U R                  5         g
)z3Set the value of the project_id or dataset_id flag.rR   rS   N   rA   
dataset_idrk   rU   r   z+Cannot set dataset_id with project_id unsetr   )	r   r?   r@   rq   rr   rM   rA   r,   r/   )r1   rP   rE   r4   values        r   do_setCommandLoop.do_set   s    $$&**,F::c?b"X-r2KD00C

3(-A-01  
	f&7&79:  fE"
r   c                     UR                  5       n[        R                  R                  5       nUS;  a  [	        S5        g[        X2S5        US:X  a  SUl        U R                  5         g)z5Unset the value of the project_id or dataset_id flag.r   rW   rS   rA   r   )rp   r   r?   r@   rM   r,   r   r/   )r1   rP   r4   rE   s       r   do_unsetCommandLoop.do_unset   sc    ::<D$$((*F//+, 	 fB		
r   r`   c                 N  ^ ^^ S mU(       di  [        S5        [        T R                  5      m[        SR                  UUU 4S jT R                  R	                  5        5       5      5        [        5         gUT R                  ;   a  [        T" UT R                  U   U/S95        g)z=Print the help for command_name (if present) or general help.c                 Z   [         R                  " 5       S-   n[        U5      S:  aY  SU-  n[        R                  " UR                  SUS9USS9nUR                  S5      u  pgnSX0S	-   U4-  n	SR                  X45      $ S
n
S[        R                  " UR                  SUS9U
U
S9-   S-   $ )N   rk   rR   rS   )	cmd_names)indentfirstline_indent
z%-*s%s:z  )r   GetMaxCommandLengthrr   r   	text_wrapCommandGetHelprZ   rd   )r4   r5   command_namesindent_sizer   command_helpfirst_help_line_rest
first_linedefault_indents              r   FormatOneCmd)CommandLoop.do_help.<locals>.FormatOneCmd   s    3359k	]	a	{"""2"?

 $0#9#9$#? DSj/ JJ
yy*+,,oo&&r]&C%!/ 	
r   z
Help for Bigquery commands:
z

c              3   ^   >#    U  H"  u  pUTR                   ;  d  M  T" XT5      v   M$     g 7fr9   r   )r   r4   r5   r   r   r1   s      r   r   &CommandLoop.do_help.<locals>.<genexpr>   s4      #9-$T888 9l4-88#9s   --)r   r   )rM   re   r&   rd   r(   )r1   r`   r   r   s   ` @@r   do_helpCommandLoop.do_help   s    
0 -.4>>*m
++ #'>>#7#7#9  g  
	'
nn\*)N r   c                 0    [        U5      =(       d    US:H  $ )Nr!   )bool)r1   stoprP   s      r   postcmdCommandLoop.postcmd   s    :&&r   )r&   r.   r0   r'   r3   r9   )r7   N)r   r   r   r   r   	Exceptionr   r%   propertyintr;   r/   rJ   rN   rB   ra   rf   rv   r   ry   r   r   r   r   r   r   r   r   r   r   r   r      s    G		 	
  " " ")("(*    .
c 
   3 3 /# /b' ' 'r   r   c                   `   ^  \ rS rSrSrS\S\R                  4U 4S jjrS\	\
   4S jrSrU =r$ )	Repl    Start an interactive bq session.r4   fvc                    > [         [        U ]  X5        SU l        [        R
                  " SSSUS9  U R                  U5        g )NFr3   rS   z!Prompt to use for BigQuery shell.)flag_values)superr   r%   r+   r   DEFINE_string_ProcessCommandRc)r1   r4   r   	__class__s      r   r%   Repl.__init__   sB    	$t(!D	"9r 	2r   r7   c                     [        [        R                  " 5       U R                  S9n[	        S5          UR                  5          UR                  $ ! [         a    [	        5          Of = fM:  )r   )r3   z6Welcome to BigQuery! (Type help for more information.))r   r   GetCommandListr3   rM   cmdloopKeyboardInterruptr;   )r1   r    s     r   RunWithArgsRepl.RunWithArgs   sa    {113DKKHD	
BC
      	 s   A A)(A))r+   )r   r   r   r   r   rB   r   
FlagValuesr%   r   r   r   r   __classcell__)r   s   @r   r   r      s4    (3 E$4$4 
!8C= 
! 
!r   r   )r   
__future__r   r   r   r#   rs   typingr   r   abslr   clientsr	   rC   frontendr
   r   pyglibr   r   r$   r   BigqueryCmdr   r   r   r   <module>r      sR    5 &  % 
  !  , % %   L'#'' L'd!'' !r   