
    T@                     (   % S r SSKJr  SSKrSSKJrJrJrJr  SSK	J
r
  \\\\4   r\
\S'   \\\\S4   4   r\
\S'   S\\\4   S	\\   4S
 jrS\\   S	\\   4S jr " S S\5      r\R*                  S:  a;  \\/\4   r\
\S'   \\\\   /\4   r\
\S'   \\\\/\4   r\
\S'   \\/\4   r\
\S'   O\r\
\S'   \r\
\S'   \r\
\S'   \r\
\S'    " S S5      r " S S\5      rS\\\4   S\\\4   S	\\\4   4S jr " S S5      r " S S5      rg)zIThe classes used to define config used to delegate BQ commands to gcloud.    )CallableN)DictListOptionalUnion)	TypeAliasPrimitiveFlagValueNestedStrDictmapped_flagsreturnc           	         / nU R                  5        Hm  u  p#[        U[        5      (       d"  UR                  SU S[	        U5       35        M<  U(       a  UR                  SU 35        MY  UR                  SU 35        Mo     U$ )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}     *!'')kdeT"""TF!CJ<01	"TF$%v' * 
    command_arrayc                     / nU  H]  nUR                  S5      (       a3  SU;   a-  UR                  S5      u  p4nUR                  U SU S35        ML  UR                  U5        M_     U$ )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#      sj    &&o!!$''C?,B(2237tmmtfBugQ'(mmO$ ' 
-r   c                       \ rS rSrSrSrg)!BigqueryGcloudDelegationUserError(   z9Class to represent a user error during gcloud delegation. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r   r   r%   r%   (   s    Ar   r%   )   	      ConvertFlagValuesFunctionConvertJsonFunctionConvertStatusFunctionMatchOutputFunctionc                   L    \ rS rSrSr SS\S\S\\   4S jjrS\	S	\	4S
 jr
Srg)FlagMapping>   zDefines 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                 Z    Xl         X l        U(       a  X0l        g U R                  U l        g N)r8   r9   r:   $default_map_bq_value_to_gcloud_value)selfr8   r9   r:   s       r   __init__FlagMapping.__init__J   s&     L"!4!%!J!Jdr   bq_flag_valuer   c                     [        U[        5      (       a  U=(       d    S$ [        U[        5      (       a  U$ [        U5      $ )zCTakes a bq flag value and returns the equivalent gcloud flag value.F)r   r   intr   )r>   rA   s     r   r=   0FlagMapping.default_map_bq_value_to_gcloud_valueX   s:     -&&#e#	M3	'	'r   )r8   r:   r9   r<   )r(   r)   r*   r+   r,   r   r   r1   r?   r	   r=   r-   r'   r   r   r6   r6   >   sN    	 BF	KK K $$=>	K	 -	 	 r   r6   c                   8   ^  \ rS rSrSrS\S\4U 4S jjrSrU =r$ )UnsupportedFlagMappingd   z9Defines a bq global flag that is not supported in gcloud.r8   error_messagec                    >^ S[         [        [        4   S[         [        [        4   4U4S jjn[        TU ]  USU5        g )Nxr   c                    > [        T5      er<   )r%   )rJ   rH   s    r   raise_unsupported_flag_errorEUnsupportedFlagMapping.__init__.<locals>.raise_unsupported_flag_errorl   s    -m<<r   unsupported_flag)r   r   r   superr?   )r>   r8   rH   rL   	__class__s     ` r   r?   UnsupportedFlagMapping.__init__g   s=    
=c4i(8 =U39=M = 
GW02NOr   r'   )	r(   r)   r*   r+   r,   r   r?   r-   __classcell__)rP   s   @r   rF   rF   d   s&    APP P Pr   rF   flag_mappingsbq_flagsc                     0 nUR                  5        H9  u  p4X0;  a  [        SU 35      eX   nUR                  U5      X%R                  '   M;     U$ )aa  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   
ValueErrorr:   r9   )rS   rT   gcloud_flagsbq_flagrA   flag_mappers         r   _convert_to_gcloud_flagsrZ   r   sb      , ( 0g#.wi899(K,7,K,K-L(()	 !1 
r   c                   P   \ rS rSrSr        SS\S\S\\   S\\\      S\\   S	\\   S
\\	   S\\
   S\\   S\S\4S jjr\S\\\4   4S j5       r SS\S\\   S\4S jjrS\\   S\4S jrS\\\4   S\\\4   4S jr SS\\   S\\\4   S\\   S\\   4S jjrSrg)CommandMapping   ai  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l         X l        X0l        U=(       d    / U l        SU l        XPl        X`l        U(       a  UOS U l        U(       a  Xl        OS U l        U	(       a  U	U l	        OS U l	        Xl
        Xl        g)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                     U $ r<   r'   )rJ   r"   s     r   <lambda>)CommandMapping.__init__.<locals>.<lambda>   s    qr   c                     U $ r<   r'   )original_statusr"   __s      r   rk   rl      s    ?r   c                     g)NFr'   )r"   s    r   rk   rl      s    Er   )r^   r_   r`   ra   _flag_mappingsrb   rc   rd   re   rf   rg   rh   )r>   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   s               r   r?   CommandMapping.__init__   sw    \ M O(.4"D26D,((4.D*Jd+
. / 3Bd/( Or   r   c                     U R                   (       d2  0 U l         U R                   H  nXR                   UR                  '   M     U R                   $ )z2Returns the command flag mappings as a dictionary.)rq   ra   r8   )r>   flag_mappings     r   rS   CommandMapping.flag_mappings   sE     d00,4@L001 1r   prefixlabelsc                 $    U(       a  U SU S3$ U$ )z Returns the format from the map.()r'   )r>   rv   rw   s      r   _add_fields_to_format$CommandMapping._add_fields_to_format   s     xq""mr   	bq_formatc                     U(       a  US:X  d  US:X  a  U R                  SU R                  5      $ SU;   a  gSU;   a  U R                  SU R                  5      $ [        SU 35      e)z2Returns the gcloud format for the given bq format.prettysparsez
table[box]jsoncsvzUnsupported format: )r{   rb   rc   rV   )r>   r}   s     r   get_gcloud_format CommandMapping.get_gcloud_format   sm     	X-h1F''d6K6KLL	9		)	''t/B/BCC-i[9::r   rT   c                 .    [        U R                  U5      $ )z0Returns the gcloud flags for the given bq flags.)rZ   rS   )r>   rT   s     r   _get_gcloud_flags CommandMapping._get_gcloud_flags  s    
 $D$6$6AAr   bq_command_flags
identifierc                 l   U R                   R                  5       nU R                  (       a%  U R                  U5      nUR	                  SU 35        UR                  [        U R                  U5      5      5        U R                  (       a  UR	                  S5        U(       a  UR	                  U5        U$ )a$  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)	r`   copyrg   r   r   extendr   r   rh   )r>   r}   r   r   r`   gcloud_formats         r   %get_gcloud_command_minus_global_flags4CommandMapping.get_gcloud_command_minus_global_flags  s    ( !% 3 3 8 8 :N,,Y7mm_
%  !7!78H!IJ I&J'r   )rq   r_   rc   ra   r`   rd   rh   rg   r^   re   rf   rb   )NNNNNNTFr<   )r(   r)   r*   r+   r,   r   r   r   r6   r2   r3   r4   r   r?   propertyr   rS   r{   r   r	   r   r   r-   r'   r   r   r\   r\      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   r\   c                      \ rS rSrSrS\\   S\\   4S jr\	S\
\\
\\4   4   4S j5       r\	S\
\\4   4S j5       rS	\
\\4   S\
\\4   4S
 jrS\S\S\4S jr SS\S\S	\
\\4   S\
\\4   S\\   S\\   4S jjrSrg)GcloudCommandGeneratori8  z-Generates a gcloud command from a bq command.command_mappingsglobal_flag_mappingsc                 8    Xl         X l        S U l        S U l        g r<   )_command_mapping_list_global_flag_mapping_list_command_dict_global_flag_dict)r>   r   r   s      r   r?   GcloudCommandGenerator.__init__;  s     
 "2%9"IMD?CDr   r   c                 t   U R                   (       d  0 U l         U R                   H  nUR                  U R                   ;  a  0 U R                   UR                  '   U R                   UR                     nUR                  U;   a  [	        SUR                   35      eXUR                  '   M     U R                   $ )zEReturns the commands as a map of resource to bq command to delegator.zDuplicate bq command: )r   r   r^   r_   rV   )r>   command_mappingresource_to_commandss      r   command_dict#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                     U R                   (       dQ  0 U l         U R                   H:  nUR                  nX R                   ;   a  [        SU 35      eXR                   U'   M<     U R                   $ )NzDuplicate bq flag: )r   r   r8   rV   )r>   rt   rX   s      r   global_flag_dict'GcloudCommandGenerator.global_flag_dictU  si    !!!d88,&&,,,0	:;
;*6w'	 9
 !!!r   bq_global_flagsc                 .    [        U R                  U5      $ )au  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}
)rZ   r   )r>   r   s     r   map_to_gcloud_global_flags1GcloudCommandGenerator.map_to_gcloud_global_flags`  s    4 $D$9$9?KKr   r^   r_   c                 &    U R                   U   U   $ )zCReturns the gcloud delegator for the given resource and bq command.)r   )r>   r^   r_   s      r   get_command_mapping*GcloudCommandGenerator.get_command_mapping|  s    
 X&z22r   Nr   r   c                     U R                  X5      nU(       d  [        SU 35      eUR                  5       nUR                  SS5      n[	        U R                  U5      5      n	U	UR                  UUUS9-   $ )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: formatr   )r}   r   r   )r   rV   r   popr   r   r   )
r>   r^   r_   r   r   r   	delegatorfiltered_global_flagsr}   gcloud_global_flagss
             r   get_gcloud_command)GcloudCommandGenerator.get_gcloud_command  s    h ((>I1*>?? ,002%))(H=I%=''(=>&
 	

9
9-! : 
	
r   )r   r   r   r   r<   )r(   r)   r*   r+   r,   r   r\   r6   r?   r   r   r   r   r   r	   r   r   r   r   r-   r'   r   r   r   r   8  s1   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 Hr   r   )r,   collections.abcr   systypingr   r   r   r   typing_extensionsr   r   r   rC   r	   __annotations__r
   r   r#   	Exceptionr%   version_infor1   r2   r3   r4   r6   rF   rZ   r\   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   