o
    H                     @   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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dlmZ ddlZG dd deejeZG dd deZG dd deejeZG dd deZdS )z!This module helps with prompting.    )absolute_import)division)unicode_literalsN)config)log)
properties)yaml)
console_io)filesc                   @   s|   e Zd ZdZdddZdd Zdd Zejd	d
 Z	ejdd Z
dd Zdd Zedd Zedd Zejdd ZdS )PromptRecordBasezBase class to cache prompting results.

  Attributes:
    _cache_file_path: cache file path.
    dirty: bool, True if record in the cache file should be updated. Otherwise,
      False.
    last_prompt_time: Last time user was prompted.
  Nc                 C      || _ d| _d S )NF)_cache_file_path_dirty)selfcache_file_path r   C/tmp/google-cloud-sdk/lib/googlecloudsdk/core/util/prompt_helper.py__init__-   s   
zPromptRecordBase.__init__c                 C   s   t j| jS N)ospathisfiler   r   r   r   r   CacheFileExists1   s   z PromptRecordBase.CacheFileExistsc                 C   sR   | j sdS t| j}tj|  |d W d   n1 sw   Y  d| _ dS )z"Serializes data to the cache file.N)streamF)r   
file_utils
FileWriterr   r   dump_ToDictionary)r   fr   r   r   SavePromptRecordToFile4   s   
z'PromptRecordBase.SavePromptRecordToFilec                 C      d S r   r   r   r   r   r   r   <      zPromptRecordBase._ToDictionaryc                 C   r!   r   r   r   r   r   r   ReadPromptRecordFromFile@   r"   z)PromptRecordBase.ReadPromptRecordFromFilec                 C   s   | S r   r   r   r   r   r   	__enter__D   s   zPromptRecordBase.__enter__c                 C   s   |    d S r   )r    )r   exc_typeexc_valexc_tbr   r   r   __exit__G      zPromptRecordBase.__exit__c                 C      | j S r   )r   r   r   r   r   dirtyJ      zPromptRecordBase.dirtyc                 C   r*   r   _last_prompt_timer   r   r   r   last_prompt_timeN   r,   z!PromptRecordBase.last_prompt_timec                 C   r   )NT)r.   r   )r   valuer   r   r   r/   R   s   
r   )__name__
__module____qualname____doc__r   r   r    abcabstractmethodr   r#   r$   r(   propertyr+   r/   setterr   r   r   r   r   #   s"    
	



r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )OptInPromptRecordzOpt-in data usage record.c                    s&   t t| jt jd |  | _d S )N)r   )superr9   r   r   Pathsopt_in_prompting_cache_pathr#   r.   r   	__class__r   r   r   [   s   
zOptInPromptRecord.__init__c                 C   s   i }| j r
| j |d< |S )Nr/   r-   )r   resr   r   r   r   `   s   
zOptInPromptRecord._ToDictionaryc                 C   sp   |   sd S z"t| j}t|}W d    n1 sw   Y  |dd W S  ty7   t	d Y d S w )Nr/   z?Failed to parse opt-in prompt cache. Using empty cache instead.)
r   r   
FileReaderr   r   loadget	Exceptionr   debug)r   r   datar   r   r   r#   f   s   
z*OptInPromptRecord.ReadPromptRecordFromFile)r1   r2   r3   r4   r   r   r#   __classcell__r   r   r=   r   r9   X   s
    r9   c                   @   s(   e Zd Zejdd Zejdd ZdS )BasePrompterc                 C   r!   r   r   r   r   r   r   Promptv   r"   zBasePrompter.Promptc                 C   r!   r   r   r   r   r   r   ShouldPromptz   r"   zBasePrompter.ShouldPromptN)r1   r2   r3   r5   r6   rH   rI   r   r   r   r   rG   t   s
    
rG   c                   @   s0   e Zd ZdZdZdZdd Zdd Zdd	 Zd
S )OptInPrompterz!Prompter to opt-in in data usage.i O a  To help improve the quality of this product, we collect anonymized usage data and anonymized stacktraces when crashes are encountered; additional information is available at <https://cloud.google.com/sdk/usage-statistics>. This data is handled in accordance with our privacy policy <https://cloud.google.com/terms/cloud-privacy-notice>. You may choose to opt in this collection now (by choosing 'Y' at the below prompt), or at any time in the future by running the following command:

    gcloud config set disable_usage_reporting false
c                 C   s   t  | _d S r   )r9   recordr   r   r   r   r      r)   zOptInPrompter.__init__c                 C   s   t  sdS | j s!| j}d|_W d   n1 sw   Y  |  rVtj| jddddd}|r;t 	t j
jjd | j}t |_W d   dS 1 sOw   Y  dS dS )z'Asks users to opt-in data usage report.Nr   zDo you want to opt-inF)messageprompt_stringdefaultthrow_if_unattendedcancel_on_noFalse)r   IsDefaultUniverserK   r   r/   rI   r	   PromptContinueMESSAGEPersistPropertyVALUEScoredisable_usage_reportingtime)r   pranswerr   r   r   rH      s,   

"zOptInPrompter.Promptc                 C   sB   t j r
t j sdS | jj}t }|r|| | jk rdS dS )z Checks whether to prompt or not.FT)r   outisattyerrrK   r/   rY   PROMPT_INTERVAL)r   r/   nowr   r   r   rI      s   zOptInPrompter.ShouldPromptN)	r1   r2   r3   r4   r_   rT   r   rH   rI   r   r   r   r   rJ      s    rJ   )r4   
__future__r   r   r   r5   r   rY   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.consoler	   googlecloudsdk.core.utilr
   r   sixwith_metaclassABCMetaobjectr   r9   rG   rJ   r   r   r   r   <module>   s$   5