
                             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	  SSKJ
r
  SSKJr   " S	 S
\5      r " S S\5      r " S S\5      rS rS rS rg)a$  The edit module allows you to edit a text blob without leaving the shell.

When a user needs to edit a blob of text and you don't want to save to
some location, tell them about it, and have the user re-upload the file, this
module can be used to do a quick inline edit.

It will inspect the environment variable EDITOR to see what tool to use
for editing, defaulting to vi. Then, the EDITOR will be opened in the current
terminal; when it exits, the file will be reread and returned with any edits
that the user may have saved while in the EDITOR.
    )absolute_import)division)unicode_literalsN)encoding)files)	platformsc                       \ rS rSrSrSrg)Error)   zExceptions for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       $lib/googlecloudsdk/core/util/edit.pyr
   r
   )   s    #r   r
   c                       \ rS rSrSrSrg)NoSaveException-   z>NoSaveException is thrown when the user did not save the file.r   Nr   r   r   r   r   r   -   s    Fr   r   c                       \ rS rSrSrSrg)EditorException1   zGEditorException is thrown when the editor returns a non-zero exit code.r   Nr   r   r   r   r   r   1   s    Or   r   c                 B    [         R                  " U 5      R                  $ z!Enables mocking in the unit test.)osstatst_mtime)	file_names    r   FileModifiedTimer"   5   s    			$	$$r   c                  .    [         R                  " U 0 UD6$ r   )
subprocess
check_call)argskargss     r   SubprocessCheckCallr(   :   s    				.	..r   c                    [         R                  " SS9R                  n[        R                  " X5        [        U5      n[        R                  R                  5       [        R                  R                  L a   [        U/SS9  O> [         R"                  " [$        R&                  SS5      n[        S	R                  XAS
9SS9  [        U5      nX%:X  a  [)        S5      e[        R*                  " U5      $ ! [        R                   a)  n[        SR                  UR                  5      5      eSnAff = f! [        R                   a)  n[        SR                  UR                  5      5      eSnAff = f)a  Edit will edit the provided text.

Args:
  text: The initial text blob to provide for editing.

Returns:
  The edited text blob.

Raises:
  NoSaveException: If the user did not save the temporary file.
  EditorException: If the process running the editor has a
      problem.
z.txt)suffixT)shellz:Your editor exited with return code {0}; please try again.NEDITORviz{editor} {file})editorfilez}Your editor exited with return code {0}; please try again. You may set the EDITOR environment to use a different text editor.zedit aborted by user)tempfileNamedTemporaryFilenamer   WriteFileContentsr"   r   OperatingSystemCurrentWINDOWSr(   r$   CalledProcessErrorr   format
returncoder   GetEncodedValuer   environr   ReadFileContents)textfnamestart_mtimeerrorr.   	end_mtimes         r   
OnlineEditrB   ?   sY    
%
%V
4
9
9%%& !'+'')''(J5'.
@''

HdCf
 +22 3 %,02 u%)
0
11				&&+ (( J 006u7G7G0HJ JJ (( @ & '-fU-=-=&>@ @@s0   :C5 =D5 5D2	$D--D25E2	$E--E2)r   
__future__r   r   r   r   r$   r0   googlecloudsdk.core.utilr   r   r   	Exceptionr
   r   r   r"   r(   rB   r   r   r   <module>rF      s^    
 '  ' 	   - * .$I $Ge GPe P%
/
,'r   