
    T@                         U d Z ddlmZ ddlZddlmZmZmZmZ ddl	m
Z
 eeeef   Ze
ed<   eeeedf   f   Ze
ed<   deeef   d	ee   fd
Zdee   d	ee   fdZ G d de      Zej*                  dk\  r;eegef   Ze
ed<   eeee   gef   Ze
ed<   eeeegef   Ze
ed<   eegef   Ze
ed<   neZe
ed<   eZe
ed<   eZe
ed<   eZe
ed<    G d d      Z G d de      Zdeeef   deeef   d	eeef   fdZ G d d      Z G d d      Zy)zIThe classes used to define config used to delegate BQ commands to gcloud.    )CallableN)DictListOptionalUnion)	TypeAliasPrimitiveFlagValueNestedStrDictmapped_flagsreturnc           	          g }| j                         D ]a  \  }}t        |t              s!|j                  d| dt	        |              7|r|j                  d|        N|j                  d|        c |S )z8Returns the gcloud command flags as an array of strings.--=z--no-)items
isinstanceboolappendstr)r   
flag_arraynamevalues       9platform/bq/gcloud_wrapper/bq_to_gcloud_config_classes.py_flatten_flag_dictionaryr      s|     *!'')kdEeT""TF!CJ<01	"TF$%v' * 
    command_arrayc                     g }| D ]U  }|j                  d      r1d|v r-|j                  d      \  }}}|j                  | d| d       E|j                  |       W |S )z?Returns the gcloud command flags after quoting the flag values.r   r   z='')
startswith	partitionr   )r   resultcommand_or_flagr   _r   s         r   quote_flag_valuesr#      si    &&o!!$'C?,B(2237tQmmtfBugQ'(mmO$ ' 
-r   c                       e Zd ZdZy)!BigqueryGcloudDelegationUserErrorz9Class to represent a user error during gcloud delegation.N)__name__
__module____qualname____doc__ r   r   r%   r%   (   s    Ar   r%   )   	      ConvertFlagValuesFunctionConvertJsonFunctionConvertStatusFunctionMatchOutputFunctionc                   >    e Zd ZdZ	 d
dededee   fdZdedefd	Z	y)FlagMappingzDefines how to create a gcloud command flag from a bq flag.

  For example this would return True:

  FlagMapping(
      bq_name='httplib2_debuglevel',
      gcloud_name='log-http',
      bq_to_gcloud_mapper=lambda x: x > 0,
  ).bq_to_gcloud_mapper(1)
  Nbq_namegcloud_namebq_to_gcloud_mapperc                 V    || _         || _        |r|| _        y | j                  | _        y N)r4   r5   r6   $default_map_bq_value_to_gcloud_value)selfr4   r5   r6   s       r   __init__zFlagMapping.__init__J   s,     DL"D!4d!%!J!Jdr   bq_flag_valuer   c                 h    t        |t              r|xs dS t        |t              r|S t        |      S )zCTakes a bq flag value and returns the equivalent gcloud flag value.F)r   r   intr   )r:   r<   s     r   r9   z0FlagMapping.default_map_bq_value_to_gcloud_valueX   s4     -&#e#	M3	'r   r8   )
r&   r'   r(   r)   r   r   r.   r;   r	   r9   r*   r   r   r3   r3   >   sN    	 BF	KK K $$=>	K	 -	 	 r   r3   c                   ,     e Zd ZdZdedef fdZ xZS )UnsupportedFlagMappingz9Defines a bq global flag that is not supported in gcloud.r4   error_messagec                     dt         t        t        f   dt         t        t        f   ffd}t        |   |d|       y )Nxr   c                     t              r8   )r%   )rC   rA   s    r   raise_unsupported_flag_errorzEUnsupportedFlagMapping.__init__.<locals>.raise_unsupported_flag_errorl   s    -m<<r   unsupported_flag)r   r   r   superr;   )r:   r4   rA   rE   	__class__s     ` r   r;   zUnsupportedFlagMapping.__init__g   s=    
=c4i(8 =U39=M = 
GW02NOr   )r&   r'   r(   r)   r   r;   __classcell__)rH   s   @r   r@   r@   d   s&    APP P Pr   r@   flag_mappingsbq_flagsc                     i }|j                         D ]:  \  }}|| vrt        d|       | |   }|j                  |      ||j                  <   < |S )as  Returns the equivalent gcloud flags for a set of bq flags.

  Args:
    flag_mappings: The flag mappings to use. For example, {'project_id':
      FlagMapping('project_id', 'project')}
    bq_flags: The bq flags that will be mapped. For example, {'project_id':
      'my_project'}

  Returns:
    The equivalent gcloud flags. For example,
    {'project': 'my_project'}
  zUnsupported bq flag: )r   
ValueErrorr6   r5   )rJ   rK   gcloud_flagsbq_flagr<   flag_mappers         r   _convert_to_gcloud_flagsrQ   r   sj      , ( 0g}m#.wi899(K,7,K,K-L(()	 !1 
r   c                   6   e Zd ZdZ	 	 	 	 	 	 	 	 ddededee   deee      dee   dee   d	ee   d
ee	   dee
   dedefdZedeeef   fd       Z	 ddedee   defdZdee   defdZdeeef   deeef   fdZ	 ddee   deeef   dee   dee   fdZy)CommandMappinga  Stores the configuration to map a BQ CLI command to gcloud.

  This class does not include the global flags. These are handled at a higher
  level in the system.

  Example usage:

  CommandMapping(
      resource='datasets',
      bq_command='ls',
      gcloud_command=['alpha', 'bq', 'datasets', 'list'],
      flag_mapping_list=[
          FlagMapping(
              bq_name='max_results',
              gcloud_name='limit',
          ),
      ],
  ).get_gcloud_command_minus_global_flags(
      bq_format='pretty',
      bq_command_flags={'max_results': 5},
  )

  Results in:
  ['alpha', 'bq', 'datasets', 'list', '--format=table[box]', '--limit=5']
  Nresource
bq_commandgcloud_commandflag_mapping_listtable_projectioncsv_projectionjson_mappingstatus_mapping$synchronous_progress_message_matcherprint_resource
no_promptsc                     || _         || _        || _        |xs g | _        d| _        || _        || _        |r|nd | _        |r|| _        nd | _        |	r|	| _	        nd | _	        |
| _
        || _        y)a  Initializes the CommandMapping.

    Args:
      resource: The resource this command targets. For example, 'datasets'.
      bq_command: The bq command to map. For example, 'ls'.
      gcloud_command: The gcloud command that will be mapped to. For example,
        ['alpha', 'bq', 'datasets', 'list'].
      flag_mapping_list: The flag mappings for this command. For example,
        [FlagMapping('max_results', 'limit')]
      table_projection: An optional projection to use for the command when a
        table is displayed. For example:
        'datasetReference.datasetId:label=datasetId'.
      csv_projection: An optional projection to use for the command when the
        output is in csv format. For example:
        'datasetReference.datasetId:label=datasetId'.
      json_mapping: A function to map the json output from gcloud to bq. For
        example, lambda x: {'kind': 'bigquery#project', 'id': x['projectId']}
      status_mapping: A function to map the status output from gcloud to bq. For
        example, lambda orig, id, project: f'Dataset {project}:{id} deleted.'
      synchronous_progress_message_matcher: A function to match a progress
        message from gcloud when running a synchronous command. For example,
        lambda message: 'Waiting for job' in message.
      print_resource: If the command also prints the resource it is operating
        on. For example, 'ls' will list resources but 'rm' usually prints status
        and not the resource.
      no_prompts: Some commands need a prompt to be disabled when they're run
        and usually, the BQ CLI code flow will have done this already. For
        example, the when `bq rm -d` is run, the BQ CLI will prompt the user
        before deleting the dataset, so the gcloud prompt is not needed.
    Nc                     | S r8   r*   )rC   r"   s     r   <lambda>z)CommandMapping.__init__.<locals>.<lambda>   s    qr   c                     | S r8   r*   )original_statusr"   __s      r   ra   z)CommandMapping.__init__.<locals>.<lambda>   s    ?r   c                      y)NFr*   )r"   s    r   ra   z)CommandMapping.__init__.<locals>.<lambda>   s    Er   )rT   rU   rV   rW   _flag_mappingsrX   rY   rZ   r[   r\   r]   r^   )r:   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   s               r   r;   zCommandMapping.__init__   s    \ DM DO(D.4"D26D,D(D(4.D*dJd+
. / 3Bd/(D DOr   r   c                     | j                   s1i | _         | j                  D ]  }|| j                   |j                  <    | j                   S )z2Returns the command flag mappings as a dictionary.)rf   rW   r4   )r:   flag_mappings     r   rJ   zCommandMapping.flag_mappings   sG     d00,4@L001 1r   prefixlabelsc                     |r| d| dS |S )z Returns the format from the map.()r*   )r:   ri   rj   s      r   _add_fields_to_formatz$CommandMapping._add_fields_to_format   s     xq""mr   	bq_formatc                     |r
|dk(  s|dk(  r| j                  d| j                        S d|v ryd|v r| j                  d| j                        S t        d|       )z2Returns the gcloud format for the given bq format.prettysparsez
table[box]jsoncsvzUnsupported format: )rn   rX   rY   rM   )r:   ro   s     r   get_gcloud_formatz CommandMapping.get_gcloud_format   sm     	X-h1F''d6K6KLL	9		)	''t/B/BCC-i[9::r   rK   c                 .    t        | j                  |      S )z0Returns the gcloud flags for the given bq flags.)rQ   rJ   )r:   rK   s     r   _get_gcloud_flagsz CommandMapping._get_gcloud_flags  s    
 $D$6$6AAr   bq_command_flags
identifierc                 N   | j                   j                         }| j                  r%| j                  |      }|j	                  d|        |j                  t        | j                  |                   | j                  r|j	                  d       |r|j	                  |       |S )aP  Returns the gcloud command to use for the given bq command.

    Args:
      bq_format: The `format` flag from the BQ CLI (eg. 'json').
      bq_command_flags: The flags for this BQ command that will be mapped. For
        example, {'max_results': 5}
      identifier: An optional identifier of the resource this command will
        operate on.

    Returns:
      The equivalent gcloud command array with the leading 'gcloud' removed,
      with the format flag and command flags but no global flags. For example,
      ['alpha', 'bq', 'datasets', 'list', '--format=json', '--limit=5']
    z	--format=z--quiet)	rV   copyr]   ru   r   extendr   rw   r^   )r:   ro   rx   ry   rV   gcloud_formats         r   %get_gcloud_command_minus_global_flagsz4CommandMapping.get_gcloud_command_minus_global_flags  s    ( !% 3 3 8 8 :N,,Y7mm_
%  !7!78H!IJ I&J'r   )NNNNNNTFr8   )r&   r'   r(   r)   r   r   r   r3   r/   r0   r1   r   r;   propertyr   rJ   rn   ru   r	   rw   r~   r*   r   r   rS   rS      s   > 8<(,&*488< !A!A! A! 3i	A!
 "${"34A! !A! smA! 01A! 45A! -5
-A! A! A!F T#{"23   #		 sm	 		;# ;3 ;BS,,-B C##$B #'	"#" S#X" 3-	"
 Cy"r   rS   c                       e Zd ZdZdee   dee   fdZede	e
e	e
ef   f   fd       Zede	e
ef   fd       Zde	e
ef   de	e
ef   fd	Zd
e
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e
   fdZy)GcloudCommandGeneratorz-Generates a gcloud command from a bq command.command_mappingsglobal_flag_mappingsc                 <    || _         || _        d | _        d | _        y r8   )_command_mapping_list_global_flag_mapping_list_command_dict_global_flag_dict)r:   r   r   s      r   r;   zGcloudCommandGenerator.__init__;  s$    
 "2D%9D"IMD?CDr   r   c                 `   | j                   si | _         | j                  D ]  }|j                  | j                   vri | j                   |j                  <   | j                   |j                     }|j                  |v rt	        d|j                         |||j                  <    | j                   S )zEReturns the commands as a map of resource to bq command to delegator.zDuplicate bq command: )r   r   rT   rU   rM   )r:   command_mappingresource_to_commandss      r   command_dictz#GcloudCommandGenerator.command_dictE  s     d!77/##4+=+==9;$

_55
6#11/2J2JK%%)==&'A'A&BC  <K_778 8 r   c                     | j                   sOi | _         | j                  D ]9  }|j                  }|| j                   v rt        d|       || j                   |<   ; | j                   S )NzDuplicate bq flag: )r   r   r4   rM   )r:   rh   rO   s      r   global_flag_dictz'GcloudCommandGenerator.global_flag_dictU  sm    !!!d88,&&d,,,0	:;
;*6w'	 9
 !!!r   bq_global_flagsc                 .    t        | j                  |      S )a  Returns the equivalent gcloud global flags for a set of bq flags.

    In the Args and Returns below, this `GcloudCommandGenerator` is used:

    GcloudCommandGenerator(
      command_mappings=[],
      global_flag_mappings=[
        FlagMapping(
            bq_name='project_id',
            gcloud_name='project'),
        FlagMapping(
            bq_name='httplib2_debuglevel',
            gcloud_name='log-http', lambda x: x > 0)
    ])

    Args:
      bq_global_flags: The bq flags that will be mapped. For example,
        {'project_id': 'my_project', 'httplib2_debuglevel': 1}

    Returns:
      The equivalent gcloud flags. For example,
      {'project': 'my_project', 'log-http': True}
    )rQ   r   )r:   r   s     r   map_to_gcloud_global_flagsz1GcloudCommandGenerator.map_to_gcloud_global_flags`  s    4 $D$9$9?KKr   rT   rU   c                 &    | j                   |   |   S )zCReturns the gcloud delegator for the given resource and bq command.)r   )r:   rT   rU   s      r   get_command_mappingz*GcloudCommandGenerator.get_command_mapping|  s    
 X&z22r   Nrx   ry   c                     | j                  ||      }|st        d|       |j                         }|j                  dd      }t	        | j                  |            }	|	|j                  |||      z   S )a  Returns the gcloud command to use for the given bq command.

    As an example usage:

    GcloudCommandGenerator(
      command_mappings=[CommandMapping(
        resource='datasets',
        bq_command='ls',
        gcloud_command=['alpha', 'bq', 'datasets', 'list'],
        flag_mapping_list=[
            FlagMapping(
                bq_name='max_results',
                gcloud_name='limit',
            ),
      ],
      flag_mappings=[
        FlagMapping(
            bq_name='project_id',
            gcloud_name='project'),
    ]).get_gcloud_command(
        resource='datasets',
        bq_command='ls',
        bq_global_flags={'project_id': 'bigquery-cli-e2e', 'format': 'pretty'},
        bq_command_flags={'max_results': 5},
    )

    Will return:

    ['--project=bigquery-cli-e2e', 'alpha', 'bq', 'datasets', 'list',
    '--format=json', '--limit=5']

    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 for the command.
      bq_command_flags: The BQ CLI command flags for the command.
      identifier: The identifier of the resource to act on.

    Returns:
      The gcloud command to run as an array of strings, minus the leading
      'gcloud'. This can be parsed directly into
      `gcloud_runner.run_gcloud_command`.
    zUnsupported bq command: formatrr   )ro   rx   ry   )r   rM   r{   popr   r   r~   )
r:   rT   rU   r   rx   ry   	delegatorfiltered_global_flagsro   gcloud_global_flagss
             r   get_gcloud_commandz)GcloudCommandGenerator.get_gcloud_command  s    h ((:>I1*>?? ,002%))(H=I%=''(=>&
 	

9
9-! : 
	
r   r8   )r&   r'   r(   r)   r   rS   r3   r;   r   r   r   r   r   r	   r   r   r   r   r*   r   r   r   r   8  s+   5D^,D !-D Dd3+>&?!?@   "S+%5 6 " "L!#'9"9:LC##$L833'*33 #'HH H CH~	H
 S#XH 3-H CyHr   r   )r)   collections.abcr   systypingr   r   r   r   typing_extensionsr   r   r   r>   r	   __annotations__r
   r   r#   	Exceptionr%   version_infor.   r/   r0   r1   r3   r@   rQ   rS   r   r*   r   r   <module>r      s   O $ 
 . . ' %c4n 5 I 5U3+?%@ @Ay As../	#Y	T#Y 	49 	B	 B y )1..*Y  $,hsm$m3$y  &.sCos.B%CC#+SE4K#8y8)1Y1#+y+%--#+y+#  # LP[ P[()3**+ 
#!
!"6h hVS Sr   