
    1\              
          S 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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  SS
KJr  SSKJr  SSKJr  SSKrS r " S S\R0                  5      r " S S\R0                  5      r " S S\R0                  5      r " S S\R0                  5      rS rSMS jrSrS r S r! " S S\5      r" " S S\5      r# " S S\RH                  5      r$ " S  S!\5      r% " S" S#\5      r& " S$ S%\5      r' " S& S'\5      r( " S( S)\5      r) " S* S+\5      r* " S, S-\5      r+0 S.\RX                  _S/\$_S0\RX                  _S1\RX                  _S2\RX                  _S3\RZ                  _S4S5 _S6S7 _S8\RX                  _S9\RX                  _S:\RX                  _S;\RX                  _S<\RX                  _S=\RX                  _S>\RZ                  _S?\RX                  _S@\RX                  _\RX                  \RX                  \RX                  \RX                  \RX                  \RX                  \RX                  \RX                  SA.Er.SB r/\0" 1 SCk5      r1SD r2SE r3SNSF jr4 " SG SH\55      r6SI r7SJ r8SK r9SL r:g)Oa&  Exceptions that can be thrown by calliope tools.

The exceptions in this file, and those that extend them, can be thrown by
the Run() function in calliope tools without worrying about stack traces
littering the screen in CLI mode. In interpreter mode, they are not caught
from within calliope.
    )absolute_import)division)unicode_literalsNwraps)
exceptions)log)
properties)console_attr)console_attr_osc                     [         R                  " 5       u  p#nU(       a2  [        R                  nUR	                  S5        UR                  U5        U(       a  U " U6 $ U(       a  U " UR                  6 $ U " S5      $ )a>  Creates a new error based on the current exception being handled.

If no exception is being handled, a new error with the given args
is created.  If there is a current exception, the original exception is
first logged (to file only).  A new error is then created with the
same args as the current one.

Args:
  error: The new error to create.
  *args: The standard args taken by the constructor of Exception for the new
    exception that is created.  If None, the args from the exception currently
    being handled will be used.

Returns:
  The generated error exception.
zAHandling the source of a tool exception, original details follow.zAn unknown error has occurred)sysexc_infor	   file_only_loggererror	exceptionargs)r   r   _current_exceptionfile_loggers        )lib/googlecloudsdk/calliope/exceptions.pyNewErrorFromCurrentExceptionr   +   st    " "lln1 &&KK +,	$<#(())	.	//    c                   (    \ rS rSrSr\S 5       rSrg)ToolExceptionO   zToolException is for Run methods to throw for non-code-bug errors.

Attributes:
  command_name: The dotted group and command name for the command that threw
    this exception. This value is set by calliope.
c                  "    [        [        /U Q76 $ N)r   r   )r   s    r   FromCurrentToolException.FromCurrentW   s    '===r    N)__name__
__module____qualname____firstlineno____doc__staticmethodr   __static_attributes__r!   r   r   r   r   O   s     > >r   r   c                       \ rS rSrSrSrg)ExitCodeNoError\   zA special exception for exit codes without error messages.

If this exception is raised, it's identical in behavior to returning from
the command code, except the overall exit code will be different.
r!   Nr"   r#   r$   r%   r&   r(   r!   r   r   r*   r*   \   s    r   r*   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )FailedSubCommandd   z:Exception capturing a subcommand which did sys.exit(code).c                 h   > [         [        U ]  SR                  SR	                  U5      U5      US9  g )Nz*Failed command: [{0}] with exit code [{1}] )	exit_code)superr.   __init__formatjoin)selfcmdcode	__class__s      r   r4   FailedSubCommand.__init__g   s8    	
D*4;;HHSM4	
 	 + r   r!   r"   r#   r$   r%   r&   r4   r(   __classcell__r:   s   @r   r.   r.   d   s    B r   r.   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )DryRunErrorp   z&Raised when a dry run request is made.c                 .   > [         TU ]  5         Xl        g r   )r3   r4   request)r7   rC   r:   s     r   r4   DryRunError.__init__s   s    	GLr   )rC   r<   r>   s   @r   r@   r@   p   s    . r   r@   c                    ^ ^ U U4S jnU$ )aM  A decorator that re-raises as an error.

If any of the error_types are raised in the decorated function, this decorator
will re-raise as an error.

Args:
  error: Exception, The new exception to raise.
  *error_types: [Exception], A list of exception types that this decorator
    will watch for.

Returns:
  The decorated function.
c                 6   >^  [        T 5      UUU 4S j5       nU$ )z#Wrapper function for the decorator.c                  l   >  T" U 0 UD6$ ! T a"    [         R                  " [        T5      5         g f = fr   )core_exceptionsreraiser   )r   kwargsr   error_typesfuncs     r   TryFunc2RaiseErrorInsteadOf.<locals>.Wrap.<locals>.TryFunc   s>    ET$V$$ E <U CDEs    %33r   )rL   rM   r   rK   s   ` r   Wrap!RaiseErrorInsteadOf.<locals>.Wrap   s$     4[E E Nr   r!   )r   rK   rO   s   `` r   RaiseErrorInsteadOfrQ   x   s    
 
+r   c                     [        U5      US-  :  a  [        R                  " S5        U $ [        U 5      U::  a  U $ X:  a  X0X-
  [        U5      -   S -   n [        U 5      U::  a  U $ U SU[        U5      -
   U-   n U $ )a  Truncate string to line width, right aligning at align.

Examples (assuming a screen width of 10):

>>> _TruncateToLineWidth('foo', 0)
'foo'
>>> # Align to the beginning. Should truncate the end.
... _TruncateToLineWidth('0123456789abcdef', 0)
'0123456789'
>>> _TruncateToLineWidth('0123456789abcdef', 0, fill='...')
'0123456...'
>>> # Align to the end. Should truncate the beginning.
... _TruncateToLineWidth('0123456789abcdef', 16)
'6789abcdef'
>>> _TruncateToLineWidth('0123456789abcdef', 16, fill='...')
'...9abcdef'
>>> # Align to the middle (note: the index is toward the end of the string,
... # because this function right-aligns to the given index).
... # Should truncate the begnining and end.
... _TruncateToLineWidth('0123456789abcdef', 12)
'23456789ab'
>>> _TruncateToLineWidth('0123456789abcdef', 12, fill='...')
'...5678...'

Args:
  string: string to truncate
  align: index to right-align to
  width: maximum length for the resulting string
  fill: if given, indicate truncation with this string. Must be shorter than
    terminal width / 2.

Returns:
  str, the truncated string

Raises:
  ValueError, if provided fill is too long for the terminal.
   z8Screen not wide enough to display correct error message.N)lenr	   warning)stringalignwidthfills       r   _TruncateToLineWidthrZ      s    L 	Y%1*
 KKJKM[EM
]5=3t94677F[EM%ECI%&-&	-r   z^ invalid characterc                 D   [        U [        R                  5      (       a'  [        U 5       H  u  p UR	                  S5        M     g[        U 5       H  u  p UR                  S5        M     g! [
        [        4 a    Us s  $ f = f! [
        [        4 a    Us s  $ f = f)zDReturns the index of the first non-ascii char in s, -1 if all ascii.ascii)
isinstancesix	text_type	enumerateencodeAttributeErrorUnicodeErrordecode)sicbs       r   _NonAsciiIndexrj      s    3==!!!	  
 !	 
 
 l+  l+ s#   A.B.BBBBc                 B   SnU  H+  n[        U5      nUS:  a  X-  n  O U[        U5      S-   -  nM-     [        S5      eSU-  [        -   n[        U5      nSR	                  U  Vs/ s H  n[
        R                  " U5      PM     sn5      n[        R                  " 5       u  pxSn	U[        [        5      [        U	5      -   :  a  SR	                  Xd45      $ [        UR                  U5      XWU	S9R                  5       n
[        XEU5      nSR	                  X45      $ s  snf )a  Format a string that will mark the first non-ASCII character it contains.

Example:

>>> args = ['command.py', '--foo=\xce\x94']
>>> _FormatNonAsciiMarkerString(args) == (
...     'command.py --foo=\u0394\n'
...     '                 ^ invalid character'
... )
True

Args:
  args: The arg list for the command executed

Returns:
  unicode, a properly formatted string with two lines, the second of which
    indicates the non-ASCII character in the first.

Raises:
  ValueError: if the given string is all ASCII characters
r      z:The command line is composed entirely of ASCII characters.r1   z...
)rY   )rj   rT   
ValueError_MARKERr6   r   SafeTextr   GetTermSizerZ   ljustrstrip)r   posargfirst_non_ascii_indexmarker_stringrW   args_stringrX   r   rY   formatted_args_stringformatted_marker_strings               r   _FormatNonAsciiMarkerStringr{      s'   . 	
#c*3/!	"c3s8a<C  D  )g%- m
%E,//4EF+((*(%	$
S\CI%% 99k122$ /4
FH  1uM	)C	DD9 Fs   % Dc                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidCharacterInArgExceptioni.  z>InvalidCharacterInArgException is for non-ASCII CLI arguments.c                 2  > X l         [        R                  R                  US   5      nUR	                  S5      (       a  US S nU/USS  -   n[
        [        U ]  SR                  [        R                  " U R                   5      [        U5      5      5        g )Nr   z.pyrl   zcFailed to read command line argument [{0}] because it does not appear to be valid 7-bit ASCII.

{1})invalid_argospathbasenameendswithr3   r}   r4   r5   r   rp   r{   )r7   r   r   r8   r:   s       r   r4   'InvalidCharacterInArgException.__init__1  s    "
''

47
#C
||EHc548D	
($8	f!!$"2"23'-
r   )r   r<   r>   s   @r   r}   r}   .  s    F r   r}   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )BadArgumentExceptioniB  z:For arguments that are wrong for reason hard to summarize.c                 V   > [         [        U ]  SR                  X5      5        Xl        g NzInvalid value for [{0}]: {1})r3   r   r4   r5   argument_name)r7   r   messager:   s      r   r4   BadArgumentException.__init__E  s)    	
.&--mE 'r   )r   r<   r>   s   @r   r   r   B  s    B' 'r   r   c                       \ rS rSrSrSrg)HttpExceptioniM  zHttpException is raised whenever the Http response status code != 200.

See api_lib.util.exceptions.HttpException for full documentation.
r!   Nr,   r!   r   r   r   r   M  s    r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidArgumentExceptioniT  z4InvalidArgumentException is for malformed arguments.c                 V   > [         [        U ]  SR                  X5      5        Xl        g r   )r3   r   r4   r5   parameter_namer7   r   r   r:   s      r   r4   !InvalidArgumentException.__init__W  )    	
"D2&--nF )r   r   r<   r>   s   @r   r   r   T  s    <) )r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ConflictingArgumentsExceptioni^  zDConflictingArgumentsException arguments that are mutually exclusive.c                 \   > [         [        U ]  SSR                  U5      -   5        Xl        g )Nz&arguments not allowed simultaneously: , )r3   r   r4   r6   parameter_names)r7   r   r:   s     r   r4   &ConflictingArgumentsException.__init__a  s,    	
'70499_3MM +r   )r   r<   r>   s   @r   r   r   ^  s    L+ +r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )UnknownArgumentExceptionih  zAUnknownArgumentException is for arguments with unexpected values.c                 V   > [         [        U ]  SR                  X5      5        Xl        g )NzUnknown value for [{0}]: {1})r3   r   r4   r5   r   r   s      r   r4   !UnknownArgumentException.__init__k  r   r   r   r<   r>   s   @r   r   r   h  s    I) )r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )RequiredArgumentExceptionir  zKAn exception for when a usually optional argument is required in this case.c                 V   > [         [        U ]  SR                  X5      5        Xl        g )Nz$Missing required argument [{0}]: {1})r3   r   r4   r5   r   r   s      r   r4   "RequiredArgumentException.__init__u  s)    	
#T3.55nN )r   r   r<   r>   s   @r   r   r   r  s    S) )r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )OneOfArgumentsRequiredExceptioni|  zDAn exception for when one of usually optional arguments is required.c                 v   > [         [        U ]  SR                  SR	                  U5      U5      5        Xl        g )Nz'One of arguments [{0}] is required: {1}r   )r3   r   r4   r5   r6   
parameters)r7   r   r   r:   s      r   r4   (OneOfArgumentsRequiredException.__init__  s6    	
)49188IIj!7	

 !Or   )r   r<   r>   s   @r   r   r   |  s    L! !r   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )MinimumArgumentExceptioni  z;An exception for when one of several arguments is required.c                    > U(       a  SR                  U5      nOSn[        [        U ]  SR                  SR	                  U Vs/ s H  nSR                  U5      PM     sn5      U5      5        g s  snf )Nz: {} z!One of [{0}] must be supplied{1}.r   z{0})r5   r3   r   r4   r6   )r7   r   r   pr:   s       r   r4   !MinimumArgumentException.__init__  s_    g&gg	
"D2+22II@1u||A@A7	
@s   A0r!   r   r<   r>   s   @r   r   r     s    C	 	r   r   c                       \ rS rSrSrSrg)BadFileExceptioni  z;BadFileException is for problems reading or writing a file.r!   Nr,   r!   r   r   r   r     s    Cr   r   z.apitools.base.py.exceptions.BadStatusCodeErrorz%apitools.base.py.exceptions.HttpErrorz(apitools.base.py.exceptions.RequestErrorz+apitools.base.py.exceptions.RetryAfterErrorz.apitools.base.py.exceptions.TransferRetryErrorz&google.auth.exceptions.GoogleAuthErrorz3googlecloudsdk.calliope.parser_errors.ArgumentErrorc                     g r   r!   xs    r   <lambda>r     s    Tr   z$googlecloudsdk.core.util.files.Errorc                     g r   r!   r   s    r   r   r     s    dr   zhttplib.ResponseNotReadyzhttplib.BadStatusLinezhttplib.IncompleteReadzhttp.client.ResponseNotReadyzhttp.client.BadStatusLinezhttp.client.IncompleteReadz+oauth2client.client.AccessTokenRefreshErrorzssl.SSLErrorzsocket.error)zsocket.timeoutzurllib3.exceptions.PoolErrorz urllib3.exceptions.ProtocolErrorzurllib3.exceptions.SSLErrorzurllib3.exceptions.TimeoutErrorzbuiltins.ConnectionAbortedErrorzbuiltins.ConnectionRefusedErrorzbuiltins.ConnectionResetErrorc                 :    U R                   S-   U R                  -   $ )zFReturns the exception name used as index into _KNOWN_ERRORS from type..)r#   r"   )clss    r   _GetExceptionNamer     s    	#		,,r   >!   ECOMMEPROTOEBADMSGEISCONNENOBUFSEREMCHGEMSGSIZEENETDOWNENOTCONNENOTSOCKENOTUNIQ	EHOSTDOWN	EMULTIHOP	ENETRESET	EREMOTEIO	ESHUTDOWN	ETIMEDOUT
EADDRINUSE
ECONNRESET
EOPNOTSUPP
EPROTOTYPEENETUNREACHENOPROTOOPTEAFNOSUPPORTECONNABORTEDECONNREFUSEDEDESTADDRREQEHOSTUNREACHEPFNOSUPPORTETOOMANYREFSEADDRNOTAVAILEPROTONOSUPPORTESOCKTNOSUPPORTc                 6   ^  [        U 4S j[         5       5      $ )z0Returns True if exc is a socket error exception.c              3   `   >#    U  H#  n[        [        US 5      TR                  :H  v   M%     g7f)r]   N)getattrerrno).0nameexcs     r   	<genexpr>!_IsSocketError.<locals>.<genexpr>  s'      8KgeT2#))+8Ks   +.)any_SOCKET_ERRNO_NAMESr   s   `r   _IsSocketErrorr     s     
 8K 
 r   c                    [        U [        5      (       a  U S4$ [        U [        R                  5      (       a  U S4$ Sn[	        U 5      /n[        / 5      nU(       a  UR                  S5      nUR                  U5        [        U5      nUS:X  a!  [        U 5      (       a  [        R                  nO[        R                  U5      nU(       a  OoUR                   Vs/ s H#  nXc;  d  M
  [        U[        5      (       d  M!  UPM%     nnUR!                  U Vs/ s H  o[        Ld  M  UPM     sn5        U(       a  M  U(       d  gU" U 5      n	U	(       a  U	S4$ U S4$ s  snf s  snf )a  Convert the given exception into an alternate type if it is known.

Searches backwards through Exception type hierarchy until it finds a match.

Args:
  exc: Exception, the exception to convert.

Returns:
  (exception, bool), exception is None if this is not a known type, otherwise
  a new exception that should be logged. The boolean is True if the error
  should be printed, or False to just exit without printing.
FTNr   zbuiltins.OSError)NT)r^   r*   rH   Errortypesetpopaddr   r   NetworkIssueError_KNOWN_ERRORSget	__bases__
issubclass	Exceptionextend)
r   	known_errclasses	processedr   r   bcbasesbasenew_excs
             r   ConvertKnownErrorr     sB    _%%:#,,--9)#YK'"g)
++a.CMM#S!D!!nS&9&9!33i##D)i --B 	#-b)#< 	 
 
 NNUDUT).CDUDE! 	$ 
 cN'#'44#t4
 Es   	E)E EE!'E!c                    [        U 5      u  p4U(       as  [        X1U5        U(       a  U" 5         [        R                  R                  R
                  R                  5       (       a  [        R                  " U 5        [        U5        g[        R                  " [        R                  " U 5      [        R                  " 5       S9  [        R                  " U 5        g)a  Handles an error that occurs during command execution.

It calls ConvertKnownError to convert exceptions to known types before
processing. If it is a known type, it is printed nicely as as error. If not,
it is raised as a crash.

Args:
  exc: Exception, The original exception that occurred.
  command_path: str, The name of the command that failed (for error
    reporting).
  known_error_handler: f(): A function to report the current exception as a
    known error.
r   N)r   _LogKnownErrorr
   VALUEScoreprint_handled_tracebacksGetBoolrH   rI   _Exitr	   debugr   rp   r   r   )r   command_pathknown_error_handler	known_excprint_errors        r   HandleErrorr  8  s     -S1)9K866>>@@c"	) IIl##C(3<<>BC r   c                   $    \ rS rSrSrS rS rSrg)HttpExceptionAdditionalHelpiU  a  Additional help text generator when specific HttpException was raised.

Attributes:
   known_exc: googlecloudsdk.api_lib.util.exceptions.HttpException, The
     exception to handle.
  error_msg_signature: string, The signature message to determine the nature
    of the error.
  additional_help: string, The additional help to print if error_msg_signature
    appears in the exception error message.
c                 (    Xl         X l        X0l        g r   )r	  error_msg_signatureadditional_help)r7   r	  r  r  s       r   r4   $HttpExceptionAdditionalHelp.__init__a  s    N2*r   c                     U R                   U R                  R                  ;   a0  SR                  U[        R
                  " U R                  5      5      $ U$ )z-Appends the additional help to the given msg.z{0}

{1})r  r	  r   r5   r   rp   r  )r7   msgs     r   Extend"HttpExceptionAdditionalHelp.Extendf  sJ    4>>#9#99  
|$$T%9%9:  jr   )r  r  r	  N)r"   r#   r$   r%   r&   r4   r  r(   r!   r   r   r  r  U  s    	+
r   r  c                     SnSR                  [        R                  R                  R                  5      n[        XU5      $ )zAdditional help when missing the 'serviceusage.services.use' permission.

Args:
  known_exc: googlecloudsdk.api_lib.util.exceptions.HttpException, The
    exception to handle.

Returns:
  A HttpExceptionAdditionalHelp object.
ziGrant the caller the Owner or Editor role, or a custom role with the serviceusage.services.use permissionzIf you want to invoke the command from a project different from the target resource project, use `--billing-project` or `{}` property.)r5   r
   r  billingquota_projectr  r	  error_message_signaturehelp_messages      r   /_BuildMissingServiceUsePermissionAdditionalHelpr  p  sG    B 
 &!2!2!:!:!H!HI 
 
%,
 r   c                 "    SnSn[        XU5      $ )aL  Additional help when missing authentication scopes.

When authenticated using user credentials and service account credentials
locally, the requested scopes (googlecloudsdk.core.config.CLOUDSDK_SCOPES)
should be enough to run gcloud commands. If users run gcloud from a GCE VM,
the scopes of the default service account is customizable during vm creation.
It is possible that the default service account does not have required scopes.

Args:
  known_exc: googlecloudsdk.api_lib.util.exceptions.HttpException, The
    exception to handle.

Returns:
  A HttpExceptionAdditionalHelp object.
z.Request had insufficient authentication scopesa  If you are in a compute engine VM, it is likely that the specified scopes during VM creation are not enough to run this command.
See https://cloud.google.com/compute/docs/access/service-accounts#accesscopesiam for more information about access scopes.
See https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes for how to update access scopes of the VM.)r  r  s      r   %_BuildMissingAuthScopesAdditionalHelpr    s)      M4  
%,
 r   c                    SR                  [        R                  " U5      [        R                  " U 5      5      n[        U [        R
                  5      (       a8  [        U 5      n[        U 5      nUR                  U5      nUR                  U5      n[        R                  " U[        R                  " 5       S9  U(       a  [        R                  " U5        gg)z.Logs the error message of the known exception.z	({0}) {1}r   N)r5   r   rp   r^   api_exceptionsr   r  r  r  r	   r  r   r   r   )r	  r  r
  r  service_use_helpauth_scopes_helps         r   r   r     s    L)<+@+@+K	# 	>7788F =YG

!
!#
&C

!
!#
&C))C#,,.)IIcN r   c                 F    [         R                  " [        U SS5      5        g)zFThis method exists so we can mock this out during testing to not exit.r2   rl   N)r   exitr   r   s    r   r  r    s     ((73Q'(r   )r   r   );r&   
__future__r   r   r   r   	functoolsr   r   r   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.corerH   r	   r
   googlecloudsdk.core.consoler   r   googlecloudsdk.core.credentialscreds_exceptionsr_   r   r   r   r*   r.   r@   rQ   rZ   ro   rj   r{   r}   r   r   r   r   r   r   r   r   r   r   TokenRefreshErrorr   r   	frozensetr   r   r   r  objectr  r  r  r   r  r!   r   r   <module>r/     s    '  '   	 
 D = # * 4 7 J 
 0H
>O)) 
>o++ 	,, 	/'' <7t  $FER] ('= 'N00 )} )+M +)} )) )	!m 	!} D} D(4))(
 ,]( /))( 2))( 5))( -**!($ :>%(& +N'((  A A)(* _>>+(, o??-(0 #O$E$E1(2  !B!B3(4 !/"C"C5(6 2**9(< O55=(> O55?(@ &77$3$E$E(7(I(I#2#D#D'6'H'H'6'H'H'6'H'H%4%F%FO(V-
   "! " J	.5b!:& 60<")r   