
    G                     
   S r SSK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
  \R                  " \
R                  \
R                  S9rS\S	\4S
 jrS\S	\4S jr  SS\S\S\\\4   S\\\4   S\\   S\S	\4S jjrg)z?An adapter that takes a bq command and executes it with gcloud.    N)DictOptional)bq_to_gcloud_config_classes)gcloud_runner)supported_gcloud_commands)global_flag_mappingscommand_mappingsgcloud_outputreturnc                 f   U R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  SS5      R                  S	S5      R                  S
S5      R                  SS5      R                  SS5      $ )Nu   ┌+u   ┐u   └u   ┘u   ├u   ┤u   ┬u   ┼u   ┴u   │|   ─-)replace)r
   s    2platform/bq/gcloud_wrapper/bq_to_gcloud_adapter.py_swap_gcloud_box_to_bq_prettyr      s     E3'wucwucwucwucwucwucwucwucwucwuc    c                 "   [         R                  " SSU 5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " S	S
U5      $ )z1Converts gcloud table output to bq sparse output.u   ┌.*┐ u   └.*┘u
   │(.*)│z \1 u   [├┤] u   [│┼]r   r   )resub)r
   stripped_upper_borderstripped_lower_bordermostly_stripped_side_bordersstripped_side_bordersno_vertical_barss         r   _swap_gcloud_box_to_bq_sparser   %   s~    &&b-@&&b2GH!#W3" &&d4PQVVK/DE	-	..r   resource
bq_commandbq_global_flagsbq_command_flags
identifierdry_runc                 8   [         R                  U UUUUS9nU(       a3  [        SR                  S/[        R
                  " U5      -   5      5        g[        R                  " U[        R                  S9nUR                  SS5      n[         R                  XS9n	UR                  (       d  UR                  $ S	n
[        UR                  R                  S	5       H  nS	n[!        U5      R#                  5       nU	R%                  U5      nU(       a  UnOdU	R&                  (       d"  U	R)                  XUR                  S
5      5      nO1SU;   a  X-  n
O&US:X  a  [+        U5      nOUS:X  a  [-        U5      nOUnU(       d  M  [        U5        M     U
(       a   [.        R0                  " U
5      n[3        U[4        5      (       a-  / nU H$  nUR7                  U	R9                  UU5      5        M&     OU	R9                  X5      nSU:X  a*  [        [.        R:                  " USS95        UR                  $ SU:X  a  [        [.        R:                  " USSS95        UR                  $ UR                  $ ! [.        R<                   a    [        U
5         UR                  $ f = f)ad  Takes a bq command and executes it with gcloud returning the exit code.

Args:
  resource: The resource the command is being run on, named to align with
    `gcloud` commands. For example, 'jobs' or 'datasets'.
  bq_command: The bq command to run. For example, 'ls' or 'show'.
  bq_global_flags: The BQ CLI global flags to use when running the command.
  bq_command_flags: The BQ CLI command flags to use when running the command.
  identifier: The identifier of the resource to act on.
  dry_run: If true, the gcloud command will be printed instead of executed.

Returns:
  The exit code of the gcloud command.
)r    r!   r"   r#   r$   r   gcloudr   )stderrformatsparse)r    r!   r   
project_idjsonpretty),:)
separators
prettyjson   T)indent	sort_keys)GCLOUD_COMMAND_GENERATORget_gcloud_commandprintjoinr   quote_flag_valuesr   run_gcloud_command
subprocessSTDOUTgetget_command_mappingstdout
returncodeiterreadlinestrstrip$synchronous_progress_message_matcherprint_resourcestatus_mappingr   r   r,   loads
isinstancelistappendjson_mappingdumpsJSONDecodeError)r    r!   r"   r#   r$   r%   gcloud_commandproc	bq_formatcommand_mappingjson_outputraw_lineline_to_printoutputis_progress_messageparsed_jsonjson_object	item_dicts                     r   run_bq_command_using_gcloudr[   1   sl   , ,>>%' ? . 	J);;NKL	
 		)	) 	
$ !!(H5),@@ A / 
?? +t{{++R0hM]  "F)NN m++%44
o11,?m 
9	k	h	3F;m	h	3F;mm}M/ 10 JJ{+k	K	&	&$I


_99)YO
P % &22;J	9	djj<= 
 9$djjQ$?@
 
	   K		s   BI, /%I, ,JJ)NF)__doc__r,   r   r;   typingr   r   gcloud_wrapperr   r   r   GcloudCommandGeneratorSUPPORTED_GLOBAL_FLAGSSUPPORTED_COMMANDSr5   rC   r   r   boolintr[    r   r   <module>re      s    E  	  ! 6 ( 46MM2II.AA   $	/ 	/ 	/" !%]]] #s(^] 38n	]
 ] ] 	]r   