
    3&                        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Z G d	 d
e
j                        Z G d de      Z G d de      Z G d de      Z G d d ej"                  ej$                  e            Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)z/This module contains all survey question types.    )absolute_import)division)unicode_literalsN)util)
exceptions)logc                       e Zd ZdZy)Errorz!Base error class for this module.N__name__
__module____qualname____doc__     1lib/googlecloudsdk/command_lib/survey/question.pyr
   r
      s    )r   r
   c                       e Zd ZdZy)AnswerRejectedErrorzRaises when answer is rejected.Nr   r   r   r   r   r   #   s    'r   r   c                       e Zd ZdZy)"RetrieveAnswerOfUnansweredQuestionz:Raises when retrieving answer from an unanswered question.Nr   r   r   r   r   r   (   s    Br   r   c                   "     e Zd ZdZ fdZ xZS )QuestionCreationErrorz>Raises when question cannot be created with the provided data.c                 l    dj                  |      }t        t        |   dj	                  |             y )Nz, zQuestion cannot be created because either some required field is missing or there are redundant fields. Required fields are {}.)joinsuperr   __init__format)selfrequired_fieldsrequired_fields_in_string	__class__s      r   r   zQuestionCreationError.__init__0   s2     $		/ :	
/	 9:<r   )r   r   r   r   r   __classcell__r!   s   @r   r   r   -   s    F< <r   r   c                       e Zd ZdZ	 	 ddZej                  d        Zed        Z	ed        Z
ed        Zd Zej                  d	        Zd
 Zd Zej                  d        Zd Zd Zed        Zd Zd Zd Zy)Questiona  Base class for survey questions.

  Attributes:
    _question: str, the question to ask.
    _instruction: str, instruction on how to answer the question.
    _instruction_on_rejection: str, instruction after the answer is rejected.
    _answer: str, the answer to the question.
  Nc                 <    || _         || _        || _        || _        y N)	_question_instruction_instruction_on_rejection_answer)r   questioninstructioninstruction_on_rejectionanswers        r   r   zQuestion.__init__B   s"    
 DN#D%=D"DLr   c                      y r'   r   )r   contents     r   FromDictionaryzQuestion.FromDictionaryL       r   c                     | j                   S r'   )r(   r   s    r   r,   zQuestion.questionP   s    >>r   c                     | j                   S r'   )r)   r5   s    r   r-   zQuestion.instructionT   s    r   c                     | j                   S r'   )r*   r5   s    r   r.   z!Question.instruction_on_rejectionX   s    )))r   c                 `    | j                          t        j                  j                          y r'   )_PrintQuestionr   outflushr5   s    r   PrintQuestionzQuestion.PrintQuestion\   s    GGMMOr   c                      y r'   r   r5   s    r   r9   zQuestion._PrintQuestion`   r3   r   c                 p    | j                   r*t        j                  j                  | j                          y y r'   )r)   r   errwriter5   s    r   PrintInstructionzQuestion.PrintInstructiond   s&    	ggmmD%%& r   c                 p    | j                   r*t        j                  j                  | j                          y y r'   )r*   r   r?   r@   r5   s    r   PrintInstructionOnRejectionz$Question.PrintInstructionOnRejectionh   s&    %%	ggmmD223 &r   c                      y)<Returns True if answer is accepted, otherwise returns False.Nr   r   r/   s     r   AcceptAnswerzQuestion.AcceptAnswerl   s     	r   c                     | j                   d uS r'   )r+   r5   s    r   
IsAnsweredzQuestion.IsAnsweredq   s    <<t##r   c                 J    | j                  |      r|| _        y t        d      )NzAnswer is invalid.)rG   r+   r   rF   s     r   AnswerQuestionzQuestion.AnswerQuestiont   s#     dl 455r   c                 P    | j                         r| j                  S t        d      )NzNo answer for this question.)rI   r+   r   r5   s    r   r/   zQuestion.answerz   s"    \\./MNNr   c                     t        || j                        rO| j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S yNF)
isinstancer!   r(   r)   r*   r   others     r   __eq__zQuestion.__eq__   s_    %(nn/ P5#5#55P,,0O0OOQ r   c                     | |k(   S r'   r   rP   s     r   __ne__zQuestion.__ne__       u}r   c                 Z    t        | j                  | j                  | j                  f      S r'   )hashr(   r)   r*   r5   s    r   __hash__zQuestion.__hash__   s+    !2!2//1 2 2r   )NN)r   r   r   r   r   abcabstractmethodr2   propertyr,   r-   r.   r<   r9   rA   rC   rG   rI   rK   r/   rR   rT   rX   r   r   r   r%   r%   8   s     )-	 	 	     * * 	 	'4 	 	$6 O O2r   r%   c                   `     e Zd ZdZ	 d fd	Zed        Zd Zd Zd Z	d Z
d Zd	 Zd
 Z xZS )MultiChoiceQuestionzNMulti-choice question.

  Attributes:
    _choices: [str], list of choices.
  c                 B    t         t        |   ||||       || _        y )N)r/   )r   r]   r   _choices)r   r,   r-   r.   choicesr/   r!   s         r   r   zMultiChoiceQuestion.__init__   s,     

t-+7 . HDMr   c                 J    	  | di |S # t         $ r t        g d      w xY w)N)r,   r-   r.   r`   r   r   	TypeErrorr   clsr1   s     r   r2   z"MultiChoiceQuestion.FromDictionary   s4    	^7^ 	! 3 	 		   
 "c                     | j                  t        dt        | j                        dz               }t	        j
                  |       y)z&Prints question and lists all choices.   indexesN)_FormatQuestionrangelenr_   r   Print)r   question_reprs     r   r9   z"MultiChoiceQuestion._PrintQuestion   sB     ((a$--(1,. ) /M IImr   c                 V   t        || j                        D cg c]  \  }}dj                  ||       }}}|D cg c]  }t        j                  |d       }}dj                  |      }t        j                  | j                  d      }dj                  ||g      S c c}}w c c}w )z%Formats question to present to users.z[{}] {}   
ri   )zipr_   r   survey_utilIndentr   r(   )r   rk   indexmsgchoices_reprr1   rp   s          r   rl   z#MultiChoiceQuestion._FormatQuestion   s     gt}}55JE3 	$5   COO,wK&&w2,LO99\*L&&t~~q9M99m\233 Ps
   B B&c                 ~    	 t        |      }d|cxk  xr t        | j                        k  S c S # t        $ r Y yw xY w)rE   ri   F)intrn   r_   
ValueErrorr   r/   
answer_ints      r   rG   z MultiChoiceQuestion.AcceptAnswer   sE    3v;j *2DMM 22222  s   0 	<<c                 &    | j                   |dz
     S )#Gets the choice at the given index.ri   )r_   r   rw   s     r   ChoicezMultiChoiceQuestion.Choice   s     ==##r   c                    t        || j                        rj| j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S yrN   )rO   r!   r(   r)   r*   r_   rP   s     r   rR   zMultiChoiceQuestion.__eq__   ss    %(nn/ 25#5#552,,0O0OO2 --5>>13
 r   c                     t        | j                  | j                  | j                  t	        | j
                        f      S r'   )rW   r(   r)   r*   tupler_   r5   s    r   rX   zMultiChoiceQuestion.__hash__   s9    !2!2//t}}1EG H Hr   c                 ,    t        | j                        S r'   )rn   r_   r5   s    r   __len__zMultiChoiceQuestion.__len__   s    t}}r   r'   )r   r   r   r   r   classmethodr2   r9   rl   rG   r   rR   rX   r   r"   r#   s   @r   r]   r]      sJ      	 		43$
Hr   r]   c                   "    e Zd ZdZd Zd Zd Zy)SatisfactionQuestionzCustomer satisfaction question.c                 T    | j                         rt        | j                        dkD  S y)zFReturns true is user answers "Very satisfied" or "Somewhat satisfied".   N)rI   r{   r/   r5   s    r   IsSatisfiedz SatisfactionQuestion.IsSatisfied   s#    !!r   c                     t        t        | j                        dd      }| j                  |      }t	        j
                  |       y )Nr   rj   )rm   rn   r_   rl   r   ro   )r   choice_indexesrp   s      r   r9   z#SatisfactionQuestion._PrintQuestion   s:     3t}}-q"5N(((@MIImr   c                 L    | j                   t        | j                         |z
     S )r   )r_   rn   r   s     r   r   zSatisfactionQuestion.Choice   s!     ==T]]+e344r   N)r   r   r   r   r   r9   r   r   r   r   r   r      s    '5r   r   c                   T     e Zd ZdZed        Z	 d	 fd	Zd Zd Zd Z	d Z
d Z xZS )
RatingQuestionz"Rating question.

  Attributes:
     min_answer: int, minimum acceptable value for answer.
     max_answer: int, maximum acceptable value for answer.
  c                 J    	  | di |S # t         $ r t        g d      w xY w)N)r,   r-   r.   
min_answer
max_answerrb   r   rc   re   s     r   r2   zRatingQuestion.FromDictionary   s4    	^7^ 	! 3 	 		rg   c                 P    t         t        |   ||||       || _        || _        y )N)r,   r-   r.   r/   )r   r   r   _min_max)r   r,   r-   r.   r   r   r/   r!   s          r   r   zRatingQuestion.__init__  s5     
.$(!9	 ) 
 DIDIr   c                 n    t        j                  | j                  d      }t        j                  |       y Nri   ru   rv   r(   r   ro   r   r,   s     r   r9   zRatingQuestion._PrintQuestion  $    !!$..!4HIIhr   c                     	 t        |      }| j                  |cxk  xr | j                  k  S c S # t        $ r Y yw xY wrN   )r{   r   r   r|   r}   s      r   rG   zRatingQuestion.AcceptAnswer  s@    v;jYY*1		1111 s   +1 1 	==c                 :   t        || j                        r| j                  |j                  k(  xrj | j                  |j                  k(  xrO | j                  |j                  k(  xr4 | j
                  |j
                  k(  xr | j                  |j                  k(  S yrN   )rO   r!   r(   r)   r*   r   r   rP   s     r   rR   zRatingQuestion.__eq__"  s    %(nn/ F5#5#55F,,0O0OOF ))uzz)F /3ii5::.EG
 r   c                     | |k(   S r'   r   rP   s     r   rT   zRatingQuestion.__ne__,  rU   r   c                     t        | j                  | j                  | j                  | j                  | j
                  f      S r'   )rW   r(   r)   r*   r   r   r5   s    r   rX   zRatingQuestion.__hash__/  s:    !2!2//DIIG H Hr   r'   )r   r   r   r   r   r2   r   r9   rG   rR   rT   rX   r"   r#   s   @r   r   r      s@     	 	 Hr   r   c                       e Zd ZdZy)NPSQuestionzNet promoter score question.Nr   r   r   r   r   r   4  s    $r   r   c                   ,    e Zd ZdZd Zd Zed        Zy)FreeTextQuestionzFree text question.c                 n    t        j                  | j                  d      }t        j                  |       y r   r   r   s     r   r9   zFreeTextQuestion._PrintQuestion;  r   r   c                      y)zReturns True if answer is accepted, otherwise returns False.

    Accepts any answer for free text question.

    Args:
      answer: str, the answer to check.

    Returns:
       True
    Tr   rF   s     r   rG   zFreeTextQuestion.AcceptAnswer?  s     r   c                 J    	  | di |S # t         $ r t        ddg      w xY w)Nr,   r-   rb   r   rc   re   s     r   r2   zFreeTextQuestion.FromDictionaryL  s5    O^7^ O!:}2MNNOrg   N)r   r   r   r   r9   rG   r   r2   r   r   r   r   r   8  s%     O Or   r   )r   
__future__r   r   r   rY   !googlecloudsdk.command_lib.surveyr   ru   googlecloudsdk.corer   r   sixr
   r   r   r   with_metaclassABCMetaobjectr%   r]   r   r   r   r   r   r   r   <module>r      s    6 &  ' 
 A * # 
J 
% 
 
<E <W2!s!!#++v6 W2tJ( JZ5. 5.;HX ;H|%. %Ox Or   