
    #(                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ	 ddlm
Z dd	lmZ dd
lmZ dZdZdZd ZdedefdZ G d dej0                        Z G d dej0                        Z G d dej0                        Zy)zJob-specific printer.    )absolute_import)division)print_function)unicode_literalsN)!container_and_volume_printer_util)k8s_object_printer_util)	time_util)custom_printer_base	executionjobtaskc                 J    dj                  |xs d| |dk(  rd      S d      S )Nz{count} {word}{plural}r       s)countwordplural)format)r   r   s     :lib/googlecloudsdk/command_lib/run/printers/job_printer.py_PluralizedWordr   "   s=    	!	(	(JQT
" 
) 
 @C 
) 
     duration_secondsreturnc                 Z   | dk(  ryt        j                  |       }|j                  }|dz  }|dz  }|dz  }|dz  }d}|j                  r|dj	                  |j                        z  }|r|dj	                  |      z  }|r|d	j	                  |      z  }|r|d
j	                  |      z  }|S )a  Format duration from seconds into shorthand string.

  Duration will be represented of the form `#d#h#m$s` for days, hours, minutes
  and seconds. Any field that's 0 will be excluded. So 3660 seconds (1 hour and
  1 minute) will be represented as "1h1m" with no days or seconds listed.

  Args:
    duration_seconds: the total time in seconds to format

  Returns:
    a string representing the duration in more human-friendly units.
  r   0sseconds  <   r   z{}dz{}hz{}mz{}s)datetime	timedeltar   daysr   )r   duration	remaininghoursminutesr   ress          r   FormatDurationShortr)   (   s     (89()
t
%$)O'N'
#]]5<<&&C
5<<C5<<  C5<<  C	*r   c                   F    e Zd ZdZed        Zed        Zed        Zd Zy)
JobPrinterzPrints the run Job in a custom human-readable format.

  Format specific to Cloud Run jobs. Only available on Cloud Run commands
  that print jobs.
  c                 B    t         j                  | j                  |       S )N)ExecutionPrinterTransformSpecexecution_template)records    r   r.   zJobPrinter.TransformSpecQ   s    ))&*C*CVLLr   c                    | j                   ydj                  t        d| j                   j                              g}| j                   j                  ]|j                  dj                  | j                   j                  j                  | j                   j                  j                               |j                  t        j                  |              t        j                  |      S )Nr   zExecuted {}timez"Last executed {} with execution {})statusr   r   executionCountlatestCreatedExecutionappendcreationTimestampnamek8s_utilLastUpdatedMessageForJobcpLinesr0   liness     r   TransformStatuszJobPrinter.TransformStatusU   s    }}FFMM$@$@A	
E
 }}++7ll
.
5
5mm22DDmm2277 
LL226:;88E?r   c                    g }t        j                  |       }t        j                  |       }t        j                  | j
                        }t        j                  |       }t        j                  |       }|r|j                  |       |r|j                  |       |j                  d       |r"|j                  |       |j                  d       |r|j                  |       |r|j                  |       |S N )	r9   BuildHeaderr+   r?   	GetLabelslabelsr.   FormatReadyMessager6   r0   outputheaderr3   rE   specready_messages          r   _formatOutputzJobPrinter._formatOutputh   s    F!!&)F''/F.F##F+D//7MmmFmmF
MM#mmFmmCmmDmmM"Mr   c                 V    t        j                  t        j                  |            }|S <Transform a job into the output structure of marker classes.)r;   r<   r+   rL   selfr0   fmts      r   	TransformzJobPrinter.Transform   s     
((:++F3
4CJr   N)	__name__
__module____qualname____doc__staticmethodr.   r?   rL   rS    r   r   r+   r+   J   sJ     M M  $  ,r   r+   c                   6    e Zd ZdZed        Zed        Zd Zy)TaskPrinterzPrints the run execution Task in a custom human-readable format.

  Format specific to Cloud Run jobs. Only available on Cloud Run commands
  that print tasks.
  c           	      2   d| j                   j                  rt        | j                   j                        nd fd| j                   j                  %dj	                  | j                   j                        nd fd| j
                  fdt        j                  | j                        fdt        j                  | j                        fdt        j                  |       fg}t        j                  t        j                  |       t        j                  |      g      S )NzTask TimeoutzMax Retriesz{}zService accountz
VPC accesszSQL connectionsVolumes)rJ   timeoutSecondsr)   
maxRetriesr   service_accountr9   GetVpcNetworkannotationsGetCloudSqlInstancescontainer_util
GetVolumesr;   r<   GetContainersLabeled)r0   rE   s     r   r.   zTaskPrinter.TransformSpec   s     {{))   : :;		
 {{%%1 KK../		
 
F223	x--f.@.@AB	H99&:L:LMN%%f-	
!F* 88^11&92::f;MNOOr   c                     d| j                   fg}| j                  A|j                  dt        j                  d| j                  fd| j
                  fg      fg       t        j                  |      S )NzRunning statezLast Attempt Resultz	Exit CodeMessage)running_statelast_exit_codeextendr;   rg   last_exit_message)r0   r3   s     r   r?   zTaskPrinter.TransformStatus   s~     
&../F (mm#jj 5 56f667  	 ::fr   c           	          t        j                  t        j                  |      | j	                  |      d| j                  |      t        j                  |      g      S )rO   rB   )r;   r<   r9   rC   r?   r.   rF   )rQ   r0   s     r   rS   zTaskPrinter.Transform   sS    88V$V$6"##F+  r   N)rT   rU   rV   rW   rX   r.   r?   rS   rY   r   r   r[   r[      s6     P P0   r   r[   c                   V    e Zd ZdZed        Zed        Zed        Zed        Zd Z	y)r-   zPrints the run Execution in a custom human-readable format.

  Format specific to Cloud Run jobs. Only available on Cloud Run commands
  that print executions.
  c                    t        j                  |      }t        j                  t        j                  d| j
                  j                  fd| j                  r| j                  ndfg      t        j                  | j                        t        j                  dt        j                  |      fd|dk(  rdn|fdt        j                  |      fg      g      S )	ax  Transforms the execution spec into a custom human-readable format.

    Args:
      record: The execution or execution template to transform.
      record_for_annotations: The resource whose annotations should be used to
        extract Cloud Run feature settings. It should be an execution or a job.

    Returns:
      A custom printer Marker class for a list of lines.
    TasksParallelismzNo limitzBinary AuthorizationzBreakglass Justificationr   rB   zThreat Detection)r9   GetBinAuthzBreakglassr;   r<   rg   rJ   	taskCountparallelismr[   r.   templateGetBinAuthzPolicyGetThreatDetectionEnabled)r0   record_for_annotationsbreakglass_values      r   r.   zExecutionPrinter.TransformSpec   s      556LM88


fkk++,&,&8&8""j
 	 	!!&//2


&**+AB +'2-3C
 #223IJ
 	  r   c                    | j                   yg }| j                  d   >|j                  dj                  t	        d| j                   j
                                     |j                  dj                  t	        d| j                   j                                     | j                   j                  W| j                   j                  dkD  r>|j                  dj                  t	        d| j                   j                                     | j                   j                  W| j                   j                  dkD  r>|j                  dj                  t	        d| j                   j                                     | j                   j                  R| j                  F|j                  d	t        j                  | j                  | j                   j                        z          | j                   j                  E|j                  d
       |j                  dj                  | j                   j                               t        j                  |      S )Nr   r3   z{} currently runningr   z{} completed successfullyr   z{} failed to completez{} cancelledzElapsed time: rB   zLog URI: {})r3   ready_conditionr6   r   r   runningCountsucceededCountfailedCountcancelledCountcompletionTimecreation_timestampr-   _elapsedTimelogUrir;   r<   r=   s     r   r?   z ExecutionPrinter.TransformStatus   s   }}Eh'/ll
 
'
'ffmm&@&@A
 
LL#**FFMM$@$@A	

 }}  ,1J1JQ1Nll
!
(
(ffmm&?&?@ 	$$0MM((1,ll


ffmm&B&BC 	$$0%%1ll
))'')E)E }}'ll3ll=''(<(<=>88E?r   c                    t        j                  t        j                  |      t        j                  |       z
        j                  }|dz  }|dz  }|dz  }|dz  }|dkD  r&dj                  t        d|      t        d|            S |dkD  r&dj                  t        d|      t        d|            S t        d|      S )	Nr   r   r    r   z	{} and {}hourminutesecond)r!   r"   r	   Strptimer   r   r   )startendr$   r&   r'   r   s         r   r   zExecutionPrinter._elapsedTime   s    !!""3')*<*<U*CCg  E$H"nGmGqy
&%
(/(G*L  {
(G
,oh.P  8W--r   c                    g }t        j                  |       }t        j                  |       }t        j                  | j
                        }t        j                  | |       }t        j                  |       }|r|j                  |       |r|j                  |       |j                  d       |r"|j                  |       |j                  d       |r|j                  |       |r|j                  |       |S rA   )	r9   rC   r-   r?   rD   rE   r.   rF   r6   rG   s          r   rL   zExecutionPrinter._formatOutput4  s    F!!&)F--f5F.F))&&9D//7MmmFmmF
MM#mmFmmCmmDmmM"Mr   c                 V    t        j                  t        j                  |            }|S rN   )r;   r<   r-   rL   rP   s      r   rS   zExecutionPrinter.TransformK  s!    
((#11&9
:CJr   N)
rT   rU   rV   rW   rX   r.   r?   r   rL   rS   rY   r   r   r-   r-      s^     & &P , ,\ . .&  ,r   r-   )rW   
__future__r   r   r   r   r!   'googlecloudsdk.command_lib.run.printersr   rd   r   r9   googlecloudsdk.command_lib.utilr	   googlecloudsdk.core.resourcer
   r;   EXECUTION_PRINTER_FORMATJOB_PRINTER_FORMATTASK_PRINTER_FORMATr   intstrr)   CustomPrinterBaser+   r[   r-   rY   r   r   <module>r      s     &  % '  g W 5 B&   # # D8%% 8v9"&& 9xMr++ Mr   