
    8                        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rSSKJ	r	J
r
JrJrJrJr  SSKJr  SSKJr  S	rS
rSSSSSSSS.r\R+                  5        V Vs0 s H  u  pX_M	     snn r\R/                  5       rS\S\4S jrS\S\4S jrS\S\4S jrS\S\S\4S jrS\S\4S jrSGS jrSSS \S\4S! jr S"\\   S\
\\
\\
\\	4   4   4   4S# jr! " S$ S%\5      r"S&\\   S"\S\"4S' jr#S( r$S) r%S*\S\\   4S+ jr&S, r'S- r(S. r)S/ r*   SHS0\\+   S1\\   S2\\   4S3 jjr, " S4 S5\5      r-   SHS6\R\                  R^                  S \S7\\+   S1\\   S8\\   S\-4S9 jjr0 SIS2\\   S\\\      4S: jjr1   SHS6\S;\\   S0\\+   S1\\   S<\\   4
S= jjr2   SHS6\S0\\+   S1\\   S>\\   4S? jjr3S@\SA\
\\	4   4SB jr4SC\SD\
\\	4   SA\
\\	4   SE\4SF jr5gs  snn f )Jz#Bigquery Client library for Python.    )absolute_import)division)print_functionN)AnyDictList
NamedTupleOptional	TypedDict)bq_error)bq_id_utilsi zgs://cloudSqlawsazuresqlDataSourcecloudSpannercloudResourcespark)	CLOUD_SQLAWSAzureSQL_DATA_SOURCECLOUD_SPANNERCLOUD_RESOURCESPARKiam_role_idreturnc                     SU -  $ )zReturns properties for a connection with IAM role id.

Args:
  iam_role_id: IAM role id.

Returns:
  JSON string with properties to create a connection with IAM role id.
z#{"accessRole": {"iamRoleId": "%s"}} )r   s    'platform/bq/utils/bq_processor_utils.pyMakeAccessRolePropertiesJsonr!   )   s     
/	<<    	tenant_idc                     SU -  $ )zReturns properties for a connection with tenant id.

Args:
  tenant_id: tenant id.

Returns:
  JSON string with properties to create a connection with customer's tenant
  id.
z{"customerTenantId": "%s"}r   )r#   s    r    MakeTenantIdPropertiesJsonr%   6   s     
&		11r"   federated_app_client_idc                     SU -  $ )zReturns properties for a connection with a federated app (client) id.

Args:
  federated_app_client_id: federated application (client) id.

Returns:
  JSON string with properties to create a connection with customer's federated
  application (client) id.
z&{"federatedApplicationClientId": "%s"}r   )r&   s    r    +MakeAzureFederatedAppClientIdPropertiesJsonr(   D   s     
24K	KKr"   c                     SU < SU< S3$ )a#  Returns properties for a connection with tenant and federated app ids.

Args:
  tenant_id: tenant id
  federated_app_client_id: federated application (client) id.

Returns:
  JSON string with properties to create a connection with customer's tenant
  and federated application (client) ids.
z{"customerTenantId": "z%", "federatedApplicationClientId" : "z"}r   )r#   r&   s     r    4MakeAzureFederatedAppClientAndTenantIdPropertiesJsonr*   T   s     
 r"   namec                 4    [         R                  " SS U 5      $ )z-Convert a name with underscores to camelcase.z_[a-z]c                 F    U R                  S5      S   R                  5       $ )Nr      )groupupper)matchs    r    <lambda>ToLowerCamel.<locals>.<lambda>l   s    Aq(9(?(?(Ar"   )resub)r+   s    r    ToLowerCamelr6   j   s    	A4	HHr"   c                 P    U R                  S UR                  5        5       5        g)zAdds all kwds to config dict, adjusting keys to camelcase.

Note this does not remove entries that are set to None, however.

Args:
  config: A configuration dict.
  **kwds: A dict of keys and values to set in the config.
c              3   J   #    U  H  u  pUc  M
  [        U5      U4v   M     g 7fN)r6   ).0kvs      r    	<genexpr>"ApplyParameters.<locals>.<genexpr>x   s      O$a!$s   	##N)updateitems)configkwdss     r    ApplyParametersrC   o   s     	--OOOr"   project_referencez,bq_id_utils.ApiClientHelper.ProjectReferencelocationc                 ,    SU R                   -   S-   U-   $ )a  Formats a project identifier for data transfers.

Data transfer API calls take in the format projects/(projectName), so because
by default project IDs take the format (projectName), add the beginning format
to perform data transfer commands

Args:
  project_reference: The project id to format for data transfer commands.
  location: The location id, e.g. 'us' or 'eu'.

Returns:
  The formatted project name for transfers.
z	projects/z/locations/)	projectId)rD   rE   s     r    #FormatProjectIdentifierForTransfersrH   {   s     $ 
(22	2]	BX	MMr"   json_stringc                      [         R                  " U 5      $ ! [         a#  n[        R                  " SU < SU< 35      eSnAff = f)zAWrapper for standard json parsing, may throw BigQueryClientError.z Error decoding JSON from string : N)jsonloads
ValueErrorr   BigqueryClientError)rI   es     r    	ParseJsonrQ      sA    ::k""	 

&
&4?C s    
AA  Ac                   0    \ rS rSr% \\   \S'   \\S'   Srg)InsertEntry   	insert_idrecordr   N)	__name__
__module____qualname____firstlineno__r
   str__annotations__object__static_attributes__r   r"   r    rS   rS      s    c]
.r"   rS   rU   c                     [         R                  " U5      n[        U[        5      (       d  [        R
                  " S5      e[        X5      $ ! [         a(  n[        R
                  " S[        U5      < 35      eSnAff = f)zParses a JSON encoded record and returns an InsertEntry.

Arguments:
  insert_id: Id for the insert, can be None.
  json_string: The JSON encoded data to be converted.

Returns:
  InsertEntry object for adding to a table.
zValue is not a JSON objectzCould not parse object: N)	rL   rM   
isinstancedictr   rO   rS   rN   r[   )rU   rI   rowrP   s       r    JsonToInsertEntryrc      sj    Q
**[
!Cc4  (()EFFy&&	 Q

&
&s1v'O
PPQs   AA 
B #A;;B c                 V    U R                  S0 5      nSU;   a  SUS   ;   a  US   S   $ g)zHelper to return the session id if the job is part of one.

Args:
  job: a job resource to get statistics and sessionInfo from.

Returns:
  sessionId, if the job is part of a session.

statisticssessionInfo	sessionIdN)get)jobstatss     r    GetSessionIdrk      s;     '',
#%eu]/C C,,	r"   c                     [        S5      n [        U R                  S0 5      R                  5       5      R                  U5      R	                  5       $ ! [
         a     gf = f)zHelper for job printing code.)extractloadquerycopyconfigurationN)setrh   keysintersectionpopKeyError)job_info	job_namess     r    GetJobTypeNamery      sX    67)HLL"-2245	i	 	
 
 s   AA 
A A source_stringc                    U R                  S5       Vs/ s H  oR                  5       PM     nnU Vs/ s H   oR                  [        5      (       d  M  UPM"     nnU(       d  [        R
                  " S5      eU(       aC  [        U5      [        U5      :w  a)  [        R
                  " SR                  [        5      5      eU$ US   n[        U5      S:  a#  [        R
                  " S[        U5      4-  5      e[        R                  R                  U5      (       d  [        R
                  " SU< 35      e[        R                  R                  U5      (       d  [        R
                  " SU< 35      eU$ s  snf s  snf )	at  Take a source string and return a list of URIs.

The list will consist of either a single local filename, which
we check exists and is a file, or a list of gs:// uris.

Args:
  source_string: A comma-separated list of URIs.

Returns:
  List of one or more valid URIs, as strings.

Raises:
  bq_error.BigqueryClientError: if no valid list of sources can be
    determined.
,zNo sources specifiedz(All URIs must begin with "{}" if any do.r   r.   z7Local upload currently supports only one file, found %dzSource file not found: zSource path is not a file: )splitstrip
startswithGCS_SCHEME_PREFIXr   rO   lenformatospathexistsisfile)rz   sourcesourcesgs_uriss       r    ProcessSourcesr      sK     +8*=*=c*BC*B\\^*B'C""&7&78I&Jf7 
  


&
&'=
>>
7|s7|#((
4
;
;<M
N  NQZF
7|a((
C\O  77>>&!!(()/
1  77>>&!!((-3
5  
.7 Ds   E8E=E=c                 *    U R                  S5      S   $ )z#Convert a kind to just a type name.#   )	partition)kinds    r    
KindToNamer     s    		Q	r"   c                 P    [         R                  5        H  u  pX ;   d  M  Us  $    g r9   )CONNECTION_TYPE_TO_PROPERTY_MAPr@   )
connectiontps      r    GetConnectionTyper     s&    -335dah 6 
r"   c                    SU ;   a0  [        U S   5      nUS-   nX ;  a  [        SU< SU< SU < 35      eO[SnU  Vs/ s H  o3R                  S5      (       d  M  UPM     nn[        U5      S:w  a  [        S[        U5      < SU< 35      eUS	   nUS	   R	                  5       USS
 -   n[        [        R                  US
5      nUc  [        SU< 35      eUR                  " S0 X   D6$ s  snf )z-Construct a Reference from a server response.r   	ReferencezCannot find z in object of type rK    r.   zExpected one Reference, found r   NzUnknown reference type: r   )	r   rN   endswithr   r0   getattrr   ApiClientHelperCreate)object_infotypenamelower_camelr;   rs   upper_camelreference_types          r    ConstructObjectReferencer     s    {+f-.H[(K%(K1  & H">{!jj&=A{D>
4yA~47It
D  q'KA$$&QR8+;66TJ.
X?
@@				:!9	:: ?s   C,C,c                 z    U R                   R                  nUS   R                  5       USS -   nU[        U 5      0$ )z,Construct an Object from an ObjectReference.r   r.   N)	__class__rW   lowerra   )	referencer   r   s      r    ConstructObjectInfor   &  s?      ))(!!#hqrl2+
tI	''r"   max_results
page_tokenfilter_expressionc                 F    [        U 5      nUb  XS'   Ub  X4S'   Ub  X$S'   U$ )#Create and populate a list request.
maxResultsfilter	pageToken)ra   )r   r   r   r   requests        r    PrepareListRequestr   -  s<     O''L")H%K	.r"   c                   V    \ rS rSr% \\S'   \\   \S'   \\   \S'   \\\      \S'   Sr	g)TransferListRequestiC  parentpageSizer   dataSourceIdsr   N)
rW   rX   rY   rZ   r[   r\   r
   intr   r^   r   r"   r    r   r   C  s*    
+SMc]$s)$$r"   r   r   	page_sizedata_source_idsc                     [        [        X5      S9nUb  X%S'   Ub  X5S'   UbP  UR                  S5      nUS   S:X  a  US   R                  S5      nXES'   U$ [        R                  " S	US   -  5      eU$ )
r   r   r   r   :r   r   r.   r|   zSInvalid filter flag values: '%s'. Expected format: '--filter=dataSourceIds:id1,id2')ra   rH   r}   r   BigqueryError)r   rE   r   r   r   r   s         r    PrepareTransferListRequestr   M  s     0E' #J%K %++C0Oq_,'*005o!0o 
. "">A  
.r"   c                    U c  gU R                  S5      (       a$   U R                  S5      S   R                  S5      $ [        R                  " SU -   S-   5      e! [         a"  n[        R                  " SU -   S-   5      UeSnAff = f)	a5  Parses the state filter for list jobs.

Args:
  filter_expression: A string containing the state filter, e.g., 'state:done'.

Returns:
  A single state filter or a list of filters to apply. Returns None if no
  filter is provided.

Raises:
  bq_error.BigqueryClientError: if the filter expression is invalid.
Nzstates:r   r.   r|   Invalid flag argument ""z*, the expression must start with "states:")r   r}   
IndexErrorr   r   )r   rP   s     r    ParseStateFilterExpressionr   k  s     !!),,$$S)!,22377 
 
 !
	
6	7   ""
#&7
7#
=s   "A 
B%BBrun_attemptstatesc                 ~    [        U S9nXS'   Ub  U[        :  a  [        nX%S'   Ub  [        U5      US'   Ub  X5S'   U$ )z0Create and populate a transfer run list request.r   
runAttemptr   r   r   )ra   MAX_RESULTSr   )r   r   r   r   r   r   s         r    PrepareTransferRunListRequestr     sY     	"'%,[ k%J26:GH%K	.r"   message_typec                 P   [        U S9nUb  U[        :  a  [        nXS'   Ub  X$S'   UbL  SU;   a*   UR                  S5      S   R                  S5      nX4S	'   U$ [        R
                  " S
U-   S-   5      eU$ ! [         a"  n[        R
                  " S
U-   S-   5      UeSnAff = f)z0Create and populate a transfer log list request.r   Nr   r   zmessageTypes:r   r.   r|   messageTypesr   r   )ra   r   r}   r   r   r   )r   r   r   r   r   rP   s         r    PrepareListTransferLogRequestr     s     	"'[ k%J%K,&#))#.q177<". 
. ""
#l
2S
8  
.  $$%4s:
	s   'A9 9
B%B  B%paramsr@   c                      [         R                  " U 5      nX!S'   U$ ! [         a  n[        R                  " S5      UeSnAff = f)a  Processes the params flag.

Args:
  params: The user specified parameters. The parameters should be in JSON
    format given as a string. Ex: --params="{'param':'value'}".
  items: The body that contains information of all the flags set.

Returns:
  items: The body after it has been updated with the params flag.

Raises:
  bq_error.BigqueryError: If there is an error with the given params.
zWParameters should be specified in JSON format when creating the transfer configuration.Nr   )rL   rM   	Exceptionr   r   )r   r@   parsed_paramsrP   s       r    ProcessParamsFlagr     sR    JJv&M "/	, 
 

 
 	# s    
A?Arefresh_window_daysdata_source_infodata_sourcec                     SU;   a(  US   S:X  a  XS'   U$ [         R                  " SU-  5      e[         R                  " SU-  5      e)a  Processes the Refresh Window Days flag.

Args:
  refresh_window_days: The user specified refresh window days.
  data_source_info: The data source of the transfer config.
  items: The body that contains information of all the flags set.
  data_source: The data source of the transfer config.

Returns:
  items: The body after it has been updated with the
  refresh window days flag.
Raises:
  bq_error.BigqueryError: If the data source does not support (custom)
    window days.
dataRefreshTypeCUSTOM_SLIDING_WINDOWdata_refresh_window_daysz=Data source '%s' does not support custom refresh window days.z6Data source '%s' does not support refresh window days.)r   r   )r   r   r@   r   s       r    ProcessRefreshWindowDaysFlagr     se    * **)*.EE*=&'l""
I 
 
 
 @;N r"   )r   N)NNNr9   )6__doc__
__future__r   r   r   rL   r   r4   typingr   r   r   r	   r
   r   utilsr   r   r   r   r   r@   CONNECTION_PROPERTY_TO_TYPE_MAPrs   CONNECTION_TYPESr[   r!   r%   r(   r*   r6   rC   rH   rQ   rS   rc   rk   ry   r   r   r   r   r   r   r   r   r   ProjectReferencer   r   r   r   r   r   )r   r   s   00r    <module>r      s{   * &  %  	 	 C C     &#%#  5::<#<TQAD<#  3779 
=c 
=c 
=2# 2# 2L LL -0,Is Is I
	PNENN 	N*	#		#tCc3h'(
()	* 
Q}QQ Q,
+# +$s) +\ 
;2( "& $'+	#   }	,%) %  $ $%)**;; } 	
 c] > (,}d3iF "& $ # # 	
 SM. "& $"&	#  3-	<c $sCx. 2!!38n! S>! 	!i#s   G