
    H                     X   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 ej&                  ej(                  e            Z G d de      Z G d d ej&                  ej(                  e            Z G d de      Zy)z!This module helps with prompting.    )absolute_import)division)unicode_literalsN)config)log)
properties)yaml)
console_io)filesc                       e Zd ZdZddZd Zd Zej                  d        Z	ej                  d        Z
d Zd	 Zed
        Zed        Zej                   d        Zy)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                      || _         d| _        y )NF)_cache_file_path_dirty)selfcache_file_paths     -lib/googlecloudsdk/core/util/prompt_helper.py__init__zPromptRecordBase.__init__-   s    +DDK    c                 T    t         j                  j                  | j                        S N)ospathisfiler   r   s    r   CacheFileExistsz PromptRecordBase.CacheFileExists1   s    77>>$//00r   c                     | j                   syt        j                  | j                        5 }t	        j
                  | j                         |       ddd       d| _         y# 1 sw Y   d| _         yxY w)z"Serializes data to the cache file.N)streamF)r   
file_utils
FileWriterr   r	   dump_ToDictionary)r   fs     r   SavePromptRecordToFilez'PromptRecordBase.SavePromptRecordToFile4   sR    ;;			t44	5
ii""$Q/ 
6DK 
6DKs   &A##A3c                      y r    r   s    r   r"   zPromptRecordBase._ToDictionary<       r   c                      y r   r&   r   s    r   ReadPromptRecordFromFilez)PromptRecordBase.ReadPromptRecordFromFile@   r'   r   c                     | S r   r&   r   s    r   	__enter__zPromptRecordBase.__enter__D   s    Kr   c                 $    | j                          y r   )r$   )r   exc_typeexc_valexc_tbs       r   __exit__zPromptRecordBase.__exit__G   s    !r   c                     | j                   S r   )r   r   s    r   dirtyzPromptRecordBase.dirtyJ   s    ;;r   c                     | j                   S r   _last_prompt_timer   s    r   last_prompt_timez!PromptRecordBase.last_prompt_timeN   s    !!!r   c                      || _         d| _        y )NT)r5   r   )r   values     r   r6   z!PromptRecordBase.last_prompt_timeR   s    "DDKr   r   )__name__
__module____qualname____doc__r   r   r$   abcabstractmethodr"   r)   r+   r0   propertyr2   r6   setterr&   r   r   r   r   #   s    1 	 	 	 	"   " "  r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )OptInPromptRecordzOpt-in data usage record.c                     t         t        |   t        j                         j
                         | j                         | _        y )N)r   )superrB   r   r   Pathsopt_in_prompting_cache_pathr)   r5   )r   	__class__s    r   r   zOptInPromptRecord.__init__[   s9    	
T+BB , D!::<Dr   c                 @    i }| j                   r| j                   |d<   |S )Nr6   r4   )r   ress     r   r"   zOptInPromptRecord._ToDictionary`   s&    
C $ 6 6c
Jr   c                 $   | j                         sy 	 t        j                  | j                        5 }t	        j
                  |      }d d d        j                  dd       S # 1 sw Y   xY w# t        $ r t        j                  d       Y y w xY w)Nr6   z?Failed to parse opt-in prompt cache. Using empty cache instead.)
r   r   
FileReaderr   r	   loadget	Exceptionr   debug)r   r#   datas      r   r)   z*OptInPromptRecord.ReadPromptRecordFromFilef   s}    !  !6!671yy| 8XX($// 87  	ii - .s(   A. A"A. "A+'A. .BB)r9   r:   r;   r<   r   r"   r)   __classcell__)rG   s   @r   rB   rB   X   s    !=
r   rB   c                   T    e Zd Zej                  d        Zej                  d        Zy)BasePrompterc                      y r   r&   r   s    r   PromptzBasePrompter.Promptv   r'   r   c                      y r   r&   r   s    r   ShouldPromptzBasePrompter.ShouldPromptz   r'   r   N)r9   r:   r;   r=   r>   rU   rW   r&   r   r   rS   rS   t   s4    	 	 	 	r   rS   c                   *    e Zd ZdZdZdZd Zd Zd Zy)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                 "    t               | _        y r   )rB   recordr   s    r   r   zOptInPrompter.__init__   s    #%DKr   c                    t        j                         sy| j                  j                         s| j                  5 }d|_        ddd       | j                         rt        j                  | j                  dddd      }|r8t        j                  t         j                  j                  j                  d       | j                  5 }t        j                         |_        ddd       yy# 1 sw Y   xY w# 1 sw Y   yxY w)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   IsDefaultUniverser[   r   r6   rW   r
   PromptContinueMESSAGEPersistPropertyVALUEScoredisable_usage_reportingtime)r   pranswers      r   rU   zOptInPrompter.Prompt   s    '');;&&(;;"  ((,,/#f 
""""::G	E;;""iik ;  ; ;s   C*C6*C36C?c                     t         j                  j                         rt         j                  j                         sy| j                  j
                  }t        j                         }|r||z
  | j                  k  ryy)z Checks whether to prompt or not.FT)r   outisattyerrr[   r6   rj   PROMPT_INTERVAL)r   r6   nows      r   rW   zOptInPrompter.ShouldPrompt   sX    GGNN!1{{33
))+C		4#7#77r   N)	r9   r:   r;   r<   rq   re   r   rU   rW   r&   r   r   rY   rY      s#    )"/	> 
&*,	r   rY   )r<   
__future__r   r   r   r=   r   rj   googlecloudsdk.corer   r   r   r	   googlecloudsdk.core.consoler
   googlecloudsdk.core.utilr   r   sixwith_metaclassABCMetaobjectr   rB   rS   rY   r&   r   r   <module>r{      s    ( &  ' 
 	  & # * $ 2 8 
2)s))#++v> 2j( 8	%3%%ckk6: 	2L 2r   