
    	2                       S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJr	  SSKJ
r
  SSKJr  SS	KJr  \R                  " S
5      r\R                  " S5      r " S S\R$                  5      r " S S\R$                  5      r " S S\R$                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\\	R6                  5      r " S S\\5      r " S  S!\\5      r " S" S#\\5      r " S$ S%\	R@                  5      r! " S& S'\\	R6                  5      r" S*S( jr# S*S) jr$g)+z;API interface for interacting with cloud storage providers.    )absolute_import)annotations)division)unicode_literalsN)
exceptions)resource)urllibz(b/(?P<bucket>.*)/o/(?P<object>.*?)(\?|$)z`^b/(?P<bucket>[a-z0-9\-_\.]+)(/)?(iam|channels|lockRetentionPolicy|iam/testPermissions|o)?(\?|$)c                      \ rS rSrSrg)CloudApiError/    N__name__
__module____qualname____firstlineno____static_attributes__r       ,lib/googlecloudsdk/api_lib/storage/errors.pyr   r   /       r   r   c                      \ rS rSrSrg)InsightApiError3   r   Nr   r   r   r   r   r   3   r   r   r   c                      \ rS rSrSrg)StorageBatchOperationsApiError7   r   Nr   r   r   r   r   r   7   r   r   r   c                      \ rS rSrSrSrg)RetryableApiError;   z3Error raised to indicate a transient network error.r   Nr   r   r   r   __doc__r   r   r   r   r   r   ;   s    ;r   r   c                      \ rS rSrSrSrg)NotFoundError@   zError raised when the requested resource does not exist.

Both GCS and S3 APIs should raise this error if a resource
does not exist so that the caller can handle the error in an API agnostic
manner.
r   Nr    r   r   r   r#   r#   @   s     r   r#   c                      \ rS rSrSrSrg)PreconditionFailedErrorJ   ,Raised when a precondition is not satisfied.r   Nr    r   r   r   r&   r&   J       4r   r&   c                      \ rS rSrSrSrg)ConflictErrorN   DRaised when a resource cannot be created because one already exists.r   Nr    r   r   r   r+   r+   N       Lr   r+   c                      \ rS rSrSrSrg)ResumableUploadAbortErrorR   z5Raised when a resumable upload needs to be restarted.r   Nr    r   r   r   r0   r0   R   s    =r   r0   c                  ,    \ rS rSrSr\SS j5       rSrg)GcsApiErrorW   zBase class for GCS API errors.c                    [        U S5      (       a*  U R                  (       a   [        U R                  SS5      $ gg! [         a     gf = f)zEReturns the status code of the error, or None if it is not available.errorstatus_codeN)hasattrr6   getattrKeyError)selfs    r   r7   GcsApiError.status_codeZ   sK     tW$**tzz=$77	 #-
  s   = 
A
	A
r   N)returnz
int | None)r   r   r   r   r!   propertyr7   r   r   r   r   r3   r3   W   s    & r   r3   c                  8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )GcsNotFoundErrorf   zsError raised when the requested GCS resource does not exist.

Implements custom formatting to avoid messy default.
c                X  > AA[         [        U ]  USS9  UR                  (       d  g Sn[        R
                  " UR                  5      u    pV[        R                  U5      nU(       a  U R                  UU5        g [        R                  U5      nU(       a  U R                  UU5        g g )Nz)HTTPError {status_code}: {status_message}error_formatz.gs://{instance_name} not found: {status_code}.)superr@   __init__urlr   SplitEndpointUrlOBJECT_RESOURCE_PATH_PATTERNsearch_custom_format_object_errorBUCKET_RESOURCE_PATH_PATTERN_custom_format_bucket_error)
r;   r6   argskwargs+custom_error_format_for_buckets_and_objects_resource_pathmatch_object_resource_pathmatch_bucket_resource_path	__class__s
            r   rF   GcsNotFoundError.__init__l   s    f	
D* A + 	B 99 	9 0 #33EII>Aq!=!D!D"!
&&
$
57 !=!D!D"!
&&
$
57 "r   c                P    X l         UR                  S5      U R                  l        g)a  Sets custom error formatting for buckets resource paths.

Args:
  match_bucket_resource_path (re.Match): Match object that contains the
    result of searching regex BUCKET_RESOURCE_PATH_PATTERN in a resource
    path.
  error_format (str): Custom error format for buckets.
bucketN)rD   grouppayloadinstance_name)r;   rT   rD   s      r   rM   ,GcsNotFoundError._custom_format_bucket_error   s!     %!;!A!A(!KDLLr   c                   UR                   n[        R                  R                  U5      nSU;   a  SUS   S   -   nOSnX l        SR                  UR                  S5      UR                  S5      U5      U R                  l        g)	a  Sets custom error formatting for object resource paths.

Args:
  match_object_resource_path (re.Match): Match object
    that contains the result of searching regex OBJECT_RESOURCE_PATH_PATTERN
    in a resource path.
  error_format (str): Custom error format for objects.

generation#r    z{}/{}{}rX   objectN)	stringr	   parseparse_qsrD   formatrY   rZ   r[   )r;   rS   rD   rR   paramsgeneration_strings         r   rK   ,GcsNotFoundError._custom_format_object_error   s     /55M\\""=1Fv| 4Q 77 $!*!1!1"((2"((24E"GDLLr   rC   )
r   r   r   r   r!   rF   rM   rK   r   __classcell__rU   s   @r   r@   r@   f   s     
"7HLG Gr   r@   c                      \ rS rSrSrSrg)GcsPreconditionFailedError   r(   r   Nr    r   r   r   rl   rl      r)   r   rl   c                      \ rS rSrSrSrg)GcsConflictError   r-   r   Nr    r   r   r   ro   ro      r.   r   ro   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )S3ErrorPayload   aH  Allows using format strings to create strings from botocore ClientErrors.

Format strings of the form '{field_name}' will be populated from class
attributes. Strings of the form '{.field_name}' will be populated from the
self.content JSON dump. See api_lib.util.HttpErrorPayload for more detail and
sample usage.

Attributes:
  content (dict): The dumped JSON content.
  message (str): The human readable error message.
  status_code (int): The HTTP status code number.
  status_description (str): The status_code description.
  status_message (str): Context specific status message.
c                  > [         [        U ]  U5        [        U5      U l        [        US5      (       a  UR                  U l        SUR                  ;   a$  UR                  S   R                  SS5      U l	        SUR                  ;   a=  UR                  S   nUR                  SS5      U l
        UR                  SS5      U l        U R                  5       U l        g	g	)
zInitializes an S3ErrorPayload instance.

Args:
  client_error (Union[botocore.exceptions.ClientError, str]): The error
    thrown by botocore, or a string that will be displayed as the error
    message.
responseResponseMetadataHttpStatusCoder   ErrorCoder`   MessageN)rE   rr   rF   strbotocore_error_stringr8   ru   contentgetr7   status_descriptionstatus_message_MakeGenericMessagemessage)r;   client_errorr6   rU   s      r   rF   S3ErrorPayload.__init__   s     
.$(6 "%\!2D|Z((!**dl	|44	4'001CDHHa!	L))	)%%g."'))FB"7#ii	26--/dl )r   )r|   r}   r   r7   r   r   r   r   r   r   r!   rF   r   ri   rj   s   @r   rr   rr      s    0 0r   rr   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )XmlApiError   zTranslates a botocore ClientError and allows formatting.

Attributes:
  error: The original ClientError.
  error_format: An S3ErrorPayload format string.
  payload: The S3ErrorPayload object.
c                2   > [         [        U ]  X[        S9  g )N)rD   payload_class)rE   r   rF   rr   )r;   r6   rD   rU   s      r   rF   XmlApiError.__init__   s    	+t% & Hr   r   )z{botocore_error_string}r   rj   s   @r   r   r      s    H Hr   r   c                    Uc  S nU H5  u  nnn[        X5      (       d  M  Ub  Ub  U" U 5      U:X  d  M-  U" X5      s  $    U $ )a{  Translates error or returns original error if no matches.

Note, an error will be translated if it is a child class of a value in
translation_list. Also, translations earlier in the list take priority.

Args:
  error (Exception): Error to translate.
  translation_list (list): List of (Exception, int|None, Exception) tuples.
    Translates errors that are instances of first error type to second if the
    status code of the first exception matches the integer value. If the
    status code argument is None, the entry will translate errors of any
    status code.
  format_str (str|None): An api_lib.util.exceptions.FormattableErrorPayload
    format string. Note that any properties that are accessed here are on the
    FormattableErrorPayload object, not the object returned from the server.
  status_code_getter (Exception -> int|None): Function that gets a status code
    from the exception type raised by the underlying client, e.g.
    apitools_exceptions.HttpError. If None, only entries with a null status
    code in `translation_list` will translate errors.

Returns:
  Error (Exception). Translated if match. Else, original error.
c                    g )Nr   )rQ   s    r   <lambda>!translate_error.<locals>.<lambda>  s    4r   )
isinstance)r6   translation_list
format_strstatus_code_getteruntranslated_erroruntranslated_status_codetranslated_errors          r   translate_errorr      s_    4 ' 	%,, (%e$(@@e00  
,r   c                   ^ ^^ UUU 4S jnU$ )a  Decorator catches an error and raises CloudApiError with a custom message.

Args:
  translation_list (list): List of (Exception, int|None, Exception) tuples.
    Translates errors that are instances of first error type to second if the
    status code of the first exception matches the integer value. If the
    status code argument is None, the entry will translate errors of any
    status code.
  format_str (str|None): An api_lib.util.exceptions.FormattableErrorPayload
    format string. Note that any properties that are accessed here are on the
    FormattableErrorPayload object, not the object returned from the server.
  status_code_getter (Exception -> int|None): Function that gets a status code
    from the exception type raised by the underlying client, e.g.
    apitools_exceptions.HttpError. If None, only entries with a null status
    code in `translation_list` will translate errors.

Returns:
  A decorator that catches errors and raises a CloudApiError with a
    customizable error message.

Example:
  @catch_error_raise_cloud_api_error(
      [(apitools_exceptions.HttpError, GcsApiError)],
      'Error [{status_code}]')
  def some_func_that_might_throw_an_error():
c                   >^  UU UU4S jnU$ )Nc            
        >  T" U 0 UD6$ ! [          a*  n[        R                  " [        UTTTS95         S nAg S nAff = f)N)r   r   )	Exceptioncore_exceptionsreraiser   )rN   rO   er   functionr   r   s      r   wrapperYcatch_error_raise_cloud_api_error.<locals>.translate_api_error_decorator.<locals>.wrapperE  sO    
((( 	
 %#5		
 	
	
s    
? :?r   )r   r   r   r   r   s   ` r   translate_api_error_decoratorHcatch_error_raise_cloud_api_error.<locals>.translate_api_error_decoratorB  s    
 
 Nr   r   )r   r   r   r   s   ``` r   !catch_error_raise_cloud_api_errorr   %  s    :( 
'&r   )NN)%r!   
__future__r   r   r   r   regooglecloudsdk.api_lib.utilr   api_exceptionsr   googlecloudsdk.corer   	six.movesr	   compilerI   rL   rx   r   r   r   r   r#   r&   r+   r0   HttpExceptionr3   r@   rl   ro   FormattableErrorPayloadrr   r   r   r   r   r   r   <module>r      sJ   B & "  ' 	 D 0 = 
  "zz/ 1   "zz% & 
O)) o++ _%:%:  
M 5m 5MM M 
-!=!= NG{M NGb5.E 5M{M M%0^;; %0PH-!=!= H( BF(X ;?1'r   