o
                         @   s   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 dejZG dd dejZdS )z)This module manages the survey prompting.    )absolute_import)division)unicode_literalsN)config)log)yaml)files)prompt_helperi u i v c                       sJ   e Zd ZdZ fddZdd Zdd Zedd	 Zej	d
d	 Z  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                    s,   t t| jt jd |  \| _| _d S )N)cache_file_path)	superr
   __init__r   Pathssurvey_prompting_cache_pathReadPromptRecordFromFile_last_prompt_time_last_answer_survey_timeself	__class__ D/tmp/google-cloud-sdk/lib/googlecloudsdk/core/survey/survey_check.pyr   -   s
   
zPromptRecord.__init__c                 C   s|   |   sdS z(t| j}t|}W d   n1 sw   Y  |dd|ddfW S  ty=   t	d Y dS 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datar   r   r   r   3   s   


z%PromptRecord.ReadPromptRecordFromFilec                 C   s0   i }| j d ur| j |d< | jd ur| j|d< |S )Nr   r   )r   r   )r   resr   r   r   _ToDictionaryF   s   



zPromptRecord._ToDictionaryc                 C   s   | j S N)r   r   r   r   r   r   N   s   z$PromptRecord.last_answer_survey_timec                 C   s   || _ d| _d S )NT)r   _dirty)r   valuer   r   r   r   R   s   
)__name__
__module____qualname____doc__r   r   r&   propertyr   setter__classcell__r   r   r   r   r
   !   s    
r
   c                   @   s8   e Zd ZdZdZefddZdd Zdd Zd	d
 ZdS )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                 C   s   t  | _|| _d S r'   )r
   _prompt_record_prompt_message)r   msgr   r   r   r   b   s   
zSurveyPrompter.__init__c                 C   s   t jd| j d  d S )Nz

)r   statuswriter3   r   r   r   r   PrintPromptMsgf   s   zSurveyPrompter.PrintPromptMsgc                 C   s\   t j r
t j sdS | jj}| jj}t }|r"|| tk r"dS |r,|| t	k r,dS dS )z%Check if the user should be prompted.FT)
r   outisattyerrr2   r   r   timeSURVEY_PROMPT_INTERVAL%SURVEY_PROMPT_INTERVAL_AFTER_ANSWERED)r   r   r   nowr   r   r   ShouldPrompti   s   
zSurveyPrompter.ShouldPromptc                 C   s   | j  s | j }t |_W d   dS 1 sw   Y  dS |  rC|   | j }t |_W d   dS 1 s<w   Y  dS dS )z3Prompts user for survey if user should be prompted.N)r2   r   r;   r   r?   r7   )r   prr   r   r   Promptx   s   

"zSurveyPrompter.PromptN)	r*   r+   r,   r-   _DEFAULT_SURVEY_PROMPT_MSGr   r7   r?   rA   r   r   r   r   r1   X   s    r1   )r-   
__future__r   r   r   r;   googlecloudsdk.corer   r   r   googlecloudsdk.core.utilr   r   r	   r<   r=   PromptRecordBaser
   BasePrompterr1   r   r   r   r   <module>   s   7