
    G                        d Z ddlZddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddlm
Z
  ej                  e
j                  e
j                        Zded	efd
Zded	efdZ	 	 ddededeeef   deeef   dee   ded	efdZy)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   | j                  dd      j                  dd      j                  dd      j                  dd      j                  dd      j                  dd      j                  dd      j                  d	d      j                  d
d      j                  dd      j                  dd      S )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                    t        j                  dd|       }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  d	d
|      S )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                    t         j                  | ||||      }|r2t        dj                  dgt	        j
                  |      z                yt        j                  |t        j                        }|j                  dd      }t         j                  | |      }	|j                  s|j                  S d	}
t        |j                  j                  d	      D ]  }d	}t!        |      j#                         }|	j%                  |      }|r|}n]|	j&                  s#|	j)                  |||j                  d
            }n.d|v r|
|z  }
n$|dk(  rt+        |      }n|dk(  rt-        |      }n|}|st        |        |
r	 t/        j0                  |
      }t3        |t4              r+g }|D ]#  }|j7                  |	j9                  ||             % n|	j9                  ||      }d|k(  r,t        t/        j:                  |d             |j                  S d|k(  r!t        t/        j:                  |dd             |j                  S |j                  S # t.        j<                  $ r t        |
       Y |j                  S w xY w)az  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   so   , ,>>%' ? . 	J);;NKL	
 		)	) 	
$ !!(H5),@@J A / 
?? +t{{++R0hM]  "F)NN m++%44
*o11,?m 
9	V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 I87I8)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M6MM2II.AA   $	/ 	/ 	/" !%]]] #s(^] 38n	]
 ] ] 	]r   