
    u                     Z    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mZ  G d de	      Z
y	)
zBase classes for diagnostics.    )absolute_import)division)unicode_literals)log)progress_trackerc                   6    e Zd ZdZdZd Zd Zd	dZd
dZd Z	y)
DiagnosticzBase class for diagnostics.

  Attributes:
    intro: A message to introduce the objectives and tasks of the diagnostic.
    title: The name of the diagnostic.
    checklist: An iterator of checkbase.Check objects to be run by the
       diagnostic.
     c                 .    || _         || _        || _        y)a  Initializes Diagnostic with necessary attributes.

    Args:
      intro: A message to introduce the objectives and tasks of the diagnostic.
      title: The name of the diagnostic.
      checklist: An iterable of checkbase.Check objects to be run by the
         diagnostic.
    N)introtitle	checklist)selfr   r   r   s       6lib/googlecloudsdk/core/diagnostics/diagnostic_base.py__init__zDiagnostic.__init__&   s     DJDJDN    c                    | j                  | j                         d}| j                  D ]  }| j                  |      \  }}d}|j                  sV|rT|| j
                  k  rE|dz  } |       }|r| j                  |d      \  }}nd}|j                  s|r|| j
                  k  rE|j                  s@|r>|| j
                  k(  r/t        j                  dj                  | j                  |             |j                  s|dz  } t        | j                        }||k(  }dj                  | j                  |||rdnd	
      }	| j                  |	|        |S )z}Runs one or more checks, tries fixes, and outputs results.

    Returns:
      True if the diagnostic ultimately passed.
    r      F	first_runNz-Unable to fix {0} failure after {1} attempts.z<{check} {status} ({num_passed}/{num_checks} checks passed).
passedfailed)check
num_passed
num_checksstatus)as_error)_Printr   r   	_RunCheckr   _MAX_RETRIESr   warningformatr   len)
r   num_checks_passedr   resultfixernum_retriesshould_check_againr   r   summarys
             r   	RunCheckszDiagnostic.RunChecks3   sI    	KK

nnU+mfe k%K$:K:K,Kq"W..%.@-&%% %K$:K:K,K ]]u8I8I)ICJJJJ% 	&	Q%  ( T^^$J*,FNvDJJ!2!+)/XX  ? 
 	KKf*K-Mr   c                     t        j                  dj                  |rdnd|j                              5  |j	                  |      \  }}d d d        | j                         |fS # 1 sw Y   xY w)Nz{0} {1}Checking
Recheckingr   )r   ProgressTrackerr"   issueCheck_PrintResult)r   r   r   r%   r&   s        r   r   zDiagnostic._RunCheckZ   sm    		)	))*:*:
\5;;+@ 
AkkIk6mfe
A 	f5=	
A 
As   A''A0c                 l    |st         j                  j                  nt         j                  } ||       y N)r   r   Printerror)r   messager   loggers       r   r   zDiagnostic._Printa   s     %-SZZ399F
7Or   c                 R    | j                  |j                  |j                          y r3   )r   r6   r   )r   r%   s     r   r1   zDiagnostic._PrintResulte   s    KKFMM 12r   N)T)F)
__name__
__module____qualname____doc__r    r   r*   r   r   r1    r   r   r	   r	      s(     ,%N3r   r	   N)r<   
__future__r   r   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   objectr	   r=   r   r   <module>rB      s'     $ &  ' # 8L3 L3r   