
                             d 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d
l
mZ dZdZ G d dej                         Z G d dej$                        Zy)z)This module manages the survey prompting.    )absolute_import)division)unicode_literalsN)config)log)yaml)files)prompt_helperi u i v c                   b     e Zd ZdZ fdZd Zd Zed        Zej                  d        Z xZ
S )PromptRecordaa  The survey prompt record.

  Attributes:
    _cache_file_path: cache file path.
    last_answer_survey_time: the time user most recently answered the survey
      (epoch time).
    last_prompt_time: the time when user is most recently prompted (epoch time).
    dirty: bool, True if record in the cache file should be updated. Otherwise,
      False.
  c                     t         t        |   t        j                         j
                         | j                         \  | _        | _        y )N)cache_file_path)	superr   __init__r   Pathssurvey_prompting_cache_pathReadPromptRecordFromFile_last_prompt_time_last_answer_survey_time)self	__class__s    .lib/googlecloudsdk/core/survey/survey_check.pyr   zPromptRecord.__init__-   sD    	,&BB ' D 	%%' :DD9    c                 H   | j                         sy	 t        j                  | j                        5 }t	        j
                  |      }ddd       j                  dd      |j                  dd      fS # 1 sw Y   -xY w# t        $ r t        j                  d       Y yw xY w)zLoads the prompt record from the cache file.

    Returns:
       Two-value tuple (last_prompt_time, last_answer_survey_time)
    )NNNlast_prompt_timelast_answer_survey_timez?Failed to parse survey prompt cache. Using empty cache instead.)
CacheFileExists
file_utils
FileReader_cache_file_pathr   loadget	Exceptionr   debug)r   fdatas      r   r   z%PromptRecord.ReadPromptRecordFromFile3   s     !  !6!671yy| 8hh)40hh0$79 9 87  	ii - .s(   B  A4+B  4A=9B   B! B!c                 v    i }| j                   | j                   |d<   | j                  | j                  |d<   |S )Nr   r   )r   r   )r   ress     r   _ToDictionaryzPromptRecord._ToDictionaryF   sF    
C) $ 6 6c
$$0'+'D'Dc
#$Jr   c                     | j                   S N)r   r   s    r   r   z$PromptRecord.last_answer_survey_timeN   s    (((r   c                      || _         d| _        y )NT)r   _dirty)r   values     r   r   z$PromptRecord.last_answer_survey_timeR   s    $)D!DKr   )__name__
__module____qualname____doc__r   r   r)   propertyr   setter__classcell__)r   s   @r   r   r   !   sE    	)& ) ) !! "r   r   c                   0    e Zd ZdZdZefdZd Zd Zd Zy)SurveyPrompterzManages prompting user for survey.

  Attributes:
     _prompt_record: PromptRecord, the record of the survey prompt history.
     _prompt_message: str, the prompting message.
  z8To take a quick anonymous survey, run:
  $ gcloud surveyc                 0    t               | _        || _        y r+   )r   _prompt_record_prompt_message)r   msgs     r   r   zSurveyPrompter.__init__b   s    &.DDr   c                 b    t         j                  j                  d| j                  z   dz          y )Nz

)r   statuswriter;   r,   s    r   PrintPromptMsgzSurveyPrompter.PrintPromptMsgf   s$    JJVd222V;<r   c                 :   t         j                  j                         rt         j                  j                         sy| j                  j
                  }| j                  j                  }t        j                         }|r||z
  t        k  ry|r||z
  t        k  ryy)z%Check if the user should be prompted.FT)
r   outisattyerrr:   r   r   timeSURVEY_PROMPT_INTERVAL%SURVEY_PROMPT_INTERVAL_AFTER_ANSWERED)r   r   r   nows       r   ShouldPromptzSurveyPrompter.ShouldPrompti   s}    GGNN!1**;;"11II
))+CS#337MMC*A$A$I%Jr   c                 d   | j                   j                         s/| j                   5 }t        j                         |_        ddd       y| j	                         r?| j                          | j                   5 }t        j                         |_        ddd       yy# 1 sw Y   yxY w# 1 sw Y   yxY w)z3Prompts user for survey if user should be prompted.N)r:   r   rE   r   rI   r@   )r   prs     r   PromptzSurveyPrompter.Promptx   s     ..0""iik 
""iik  	  s   B6B&B#&B/N)	r0   r1   r2   r3   _DEFAULT_SURVEY_PROMPT_MSGr   r@   rI   rL    r   r   r8   r8   X   s(    !4 4 =*r   r8   )r3   
__future__r   r   r   rE   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr	   r   r
   rF   rG   PromptRecordBaser   BasePrompterr8   rN   r   r   <module>rT      sV    0 &  '  & # $ 8 2# (6 %4=11 4n+*]// +*r   