ó
Y[c           @   sV  d  Z  d d l Z d d l Z d d l 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 e j e ƒ Z d Z d Z d	 Z d
 Z d Z d d d Z d „  Z e d d „ Z e j j e
 j ƒ d e f d „  ƒ  Yƒ Z d „  Z e j j e
 j ƒ d e f d „  ƒ  Yƒ Z  d „  Z! d „  Z" d S(   s   Certbot display.iÿÿÿÿN(   t	   constants(   t
   interfaces(   t   errors(   t	   completeriH   t   okt   cancelt   helpt   escs   - i'   t   -c      	   C   sX   |  j  ƒ  } g  } x3 | D]+ } | j t j | d d t d t ƒƒ q Wt j j | ƒ S(   sœ   Format lines nicely to 80 chars.

    :param str msg: Original message

    :returns: Formatted message respecting newlines in message
    :rtype: str

    iP   t   break_long_wordst   break_on_hyphens(   t
   splitlinest   appendt   textwrapt   fillt   Falset   ost   linesept   join(   t   msgt   linest   fixed_lt   line(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   _wrap_lines%   s    	g     ”á@c         C   sš   |  r& t  j j |  ƒ t  j j ƒ  n  t j t  j g g  g  | ƒ \ } } } | sn t j d j |  ƒ ƒ ‚ n  | d j	 ƒ  } | s t
 ‚ n  | j d ƒ S(   s  Get user input with a timeout.

    Behaves the same as six.moves.input, however, an error is raised if
    a user doesn't answer after timeout seconds. The default timeout
    value was chosen to place it just under 12 hours for users following
    our advice and running Certbot twice a day.

    :param str prompt: prompt to provide for input
    :param float timeout: maximum number of seconds to wait for input

    :returns: user response
    :rtype: str

    :raises errors.Error if no answer is given before the timeout

    s,   Timed out waiting for answer to prompt '{0}'i    s   
(   t   syst   stdoutt   writet   flusht   selectt   stdinR   t   Errort   formatt   readlinet   EOFErrort   rstrip(   t   promptt   timeoutt   rlistt   _R   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   input_with_timeout;   s    '	t   FileDisplayc           B   s¿   e  Z d  Z d „  Z e e e d „ Z d d d d d e d „ Z d d e d „ Z	 d d d d e d „ Z
 d d e d „ Z d	 „  Z d
 „  Z d d e d „ Z d „  Z d „  Z d „  Z RS(   s   File-based display.c         C   s2   t  t |  ƒ j ƒ  | |  _ | |  _ t |  _ d  S(   N(   t   superR(   t   __init__t   outfilet   force_interactiveR   t   skipped_interaction(   t   selfR+   R,   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR*   d   s    		c         C   sƒ   | r t  | ƒ } n  |  j j d j d t j d t d | ƒ ƒ |  j j ƒ  | r |  j | ƒ ro t	 d ƒ q t
 j d ƒ n  d S(   s­  Displays a notification and waits for user acceptance.

        :param str message: Message to display
        :param bool pause: Whether or not the program should pause for the
            user's confirmation
        :param bool wrap: Whether or not the application should wrap text
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        s+   {line}{frame}{line}{msg}{line}{frame}{line}R   t   frameR   s   Press Enter to Continues!   Not pausing for user confirmationN(   R   R+   R   R   R   R   t
   SIDE_FRAMER   t   _can_interactR'   t   loggert   debug(   R.   t   messaget   pauset   wrapR,   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   notificationj   s    		c	         K   s[   |  j  | | | | ƒ r" t | f S|  j | | ƒ |  j t | ƒ ƒ \ }
 } |
 | d f S(   sC  Display a menu.

        .. todo:: This doesn't enable the help label/button (I wasn't sold on
           any interface I came up with for this). It would be a nice feature

        :param str message: title of menu
        :param choices: Menu lines, len must be > 0
        :type choices: list of tuples (tag, item) or
            list of descriptions (tags will be enumerated)
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        :returns: tuple of (`code`, `index`) where
            `code` - str display exit code
            `index` - int index of the user's selection

        :rtype: tuple

        i   (   t   _return_defaultt   OKt   _print_menut   _get_valid_int_anst   len(   R.   R4   t   choicest   ok_labelt   cancel_labelt
   help_labelt   defaultt   cli_flagR,   t   unused_kwargst   codet	   selection(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   menu‚   s
    
c         K   sr   |  j  | | | | ƒ r" t | f St d | ƒ d } t | ƒ } | d k sZ | d k rd t d f St | f Sd S(   s  Accept input from the user.

        :param str message: message to display to the user
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        :returns: tuple of (`code`, `input`) where
            `code` - str display exit code
            `input` - str of the user's input
        :rtype: tuple

        s   %s (Enter 'c' to cancel):t    t   ct   Cs   -1N(   R8   R9   R   R'   t   CANCEL(   R.   R4   RA   RB   R,   RC   t   ans(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   input¤   s    

t   Yest   Noc   	      K   s  |  j  | | | | ƒ r | St | ƒ } |  j j d j t j d t t j d | ƒƒ |  j j ƒ  x  t	 rt
 d j d t | ƒ d t | ƒ ƒ ƒ } | j | d j ƒ  ƒ sÉ | j | d j ƒ  ƒ rÍ t	 S| j | d j ƒ  ƒ sÿ | j | d j ƒ  ƒ rg t Sqg Wd S(	   sž  Query the user with a yes/no question.

        Yes and No label must begin with different letters, and must contain at
        least one letter each.

        :param str message: question for the user
        :param str yes_label: Label of the "Yes" parameter
        :param str no_label: Label of the "No" parameter
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        :returns: True for "Yes", False for "No"
        :rtype: bool

        s   {0}{frame}{msg}{0}{frame}R/   R   s   {yes}/{no}: t   yest   noi    N(   R8   R   R+   R   R   R   R   R0   R   t   TrueR'   t   _parens_around_chart
   startswitht   lowert   upperR   (	   R.   R4   t	   yes_labelt   no_labelRA   RB   R,   RC   RK   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   yesnoÀ   s      	c         K   s  |  j  | | | | ƒ r" t | f Sxç t r|  j | | ƒ |  j d d t ƒ\ } } | t k rþ t | j ƒ  ƒ d k r© d j d „  t d t | ƒ d ƒ Dƒ ƒ } n  t	 | ƒ }	 |  j
 |	 | ƒ }
 |
 r× | |
 f S|  j j d t j ƒ |  j j ƒ  q% | g  f Sq% Wd S(	   sC  Display a checklist.

        :param str message: Message to display to user
        :param list tags: `str` tags to select, len(tags) > 0
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        :returns: tuple of (`code`, `tags`) where
            `code` - str display exit code
            `tags` - list of selected tags
        :rtype: tuple

        sr   Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shownR,   i    RG   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   str(   t   .0t   x(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pys	   <genexpr>	  s    i   s!   ** Error - Invalid selection **%sN(   R8   R9   RQ   R:   RL   R<   t   stripR   t   ranget   separate_list_inputt   _scrub_checklist_inputR+   R   R   R   R   (   R.   R4   t   tagsRA   RB   R,   RC   RD   RK   t   indicest   selected_tags(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt	   checklistê   s"    
	/
	c         C   sw   |  j  | ƒ r t S| d k r\ d j | ƒ } | rJ | d j | ƒ 7} n  t j | ƒ ‚ n t j d | | ƒ t Sd S(   sÌ  Should we return the default instead of prompting the user?

        :param str prompt: prompt for the user
        :param default: default answer to prompt
        :param str cli_flag: command line option for setting an answer
            to this question
        :param bool force_interactive: if interactivity is forced by the
            IDisplay call

        :returns: True if we should return the default without prompting
        :rtype: bool

        s-   Unable to get an answer for the question:
{0}sA   
You can provide an answer on the command line with the {0} flag.s-   Falling back to default %s for the prompt:
%sN(	   R1   R   t   NoneR   R   R   R2   R3   RQ   (   R.   R#   RA   RB   R,   R   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR8     s    
c         C   s]   |  j  s- | s- t j j ƒ  r1 |  j j ƒ  r1 t S|  j sY t j d t	 j
 ƒ t |  _ n  t S(   sê   Can we safely interact with the user?

        :param bool force_interactive: if interactivity is forced by the
            IDisplay call

        :returns: True if the display can interact with the user
        :rtype: bool

        s   Skipped user interaction because Certbot doesn't appear to be running in a terminal. You should probably include --non-interactive or %s on the command line.(   R,   R   R   t   isattyR+   RQ   R-   R2   t   warningR    t   FORCE_INTERACTIVE_FLAGR   (   R.   R,   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR1   3  s    
	
c      	   K   s-   t  j ƒ   |  j | | | | ƒ SWd QXd S(   s
  Display a directory selection screen.

        :param str message: prompt to give the user
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI
        :param bool force_interactive: True if it's safe to prompt the user
            because it won't cause any workflow regressions

        :returns: tuple of the form (`code`, `string`) where
            `code` - display exit code
            `string` - input entered by the user

        N(   R   t	   CompleterRL   (   R.   R4   RA   RB   R,   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   directory_selectI  s    c         C   sœ   y# g  | D] } t  | ƒ ^ q
 } Wn t k
 r7 g  SXt t | ƒ ƒ } x0 | D]( } | d k  su | t | ƒ k rQ g  SqQ Wg  | D] } | | d ^ q„ S(   sþ   Validate input and transform indices to appropriate tags.

        :param list indices: input
        :param list tags: Original tags of the checklist

        :returns: valid tags the user selected
        :rtype: :class:`list` of :class:`str`

        i   (   t   intt
   ValueErrort   listt   setR<   (   R.   Ra   R`   t   index(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR_   [  s    #c         C   s  | rG t  | d t ƒ rG g  | D] } d | d | d f ^ q  } n  |  j j d j d t j d | ƒ ƒ |  j j t t j ƒ x^ t | d ƒ D]M \ } } d j d | d	 | ƒ } |  j j t	 | ƒ ƒ |  j j t j ƒ q“ W|  j j t t j ƒ |  j j
 ƒ  d
 S(   sã   Print a menu on the screen.

        :param str message: title of menu
        :param choices: Menu lines
        :type choices: list of tuples (tag, item) or
            list of descriptions (tags will be enumerated)

        i    s   %s - %si   s   {new}{msg}{new}t   newR   s   {num}: {desc}t   numt   descN(   t
   isinstancet   tupleR+   R   R   R   R   R0   t	   enumerateR   R   (   R.   R4   R=   RH   t   iRq   R   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR:   v  s    
.	c         C   sò   d } | d k r' d j  d | ƒ } n d } x¸ | d k  rç t | ƒ } | j d ƒ sf | j d ƒ rp t d f Sy7 t | ƒ } | d k  s— | | k r¦ d } t ‚ n  Wq0 t k
 rã |  j j d j  t j	 ƒ ƒ |  j j
 ƒ  q0 Xq0 Wt | f S(	   s5  Get a numerical selection.

        :param int max: The maximum entry (len of choices), must be positive

        :returns: tuple of the form (`code`, `selection`) where
            `code` - str display exit code ('ok' or cancel')
            `selection` - int user's selection
        :rtype: tuple

        iÿÿÿÿi   sM   Select the appropriate number [1-{max_}] then [enter] (press 'c' to cancel): t   max_s@   Press 1 [enter] to confirm the selection (press 'c' to cancel): RH   RI   s   {0}** Invalid input **{0}(   R   R'   RS   RJ   Rj   Rk   R+   R   R   R   R   R9   (   R.   Rv   RE   t	   input_msgRK   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR;   “  s&    	
	N(   t   __name__t
   __module__t   __doc__R*   RQ   R   R7   Rd   RF   RL   RX   Rc   R8   R1   Ri   R_   R:   R;   (    (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR(   ^   s(   	 	)*				c         C   sM   d j  |  ƒ } | r+ | d j  | ƒ 7} n  | d k	 sI | sI t | ƒ ‚ d S(   sN  Verify that provided arguments is a valid IDisplay call.

    :param str prompt: prompt for the user
    :param default: default answer to prompt
    :param str cli_flag: command line option for setting an answer
        to this question
    :param bool force_interactive: if interactivity is forced by the
        IDisplay call

    s*   Invalid IDisplay call for this prompt:
{0}s7   
You can set an answer to this prompt with the {0} flagN(   R   Rd   t   AssertionError(   R#   RA   RB   R,   R   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   assert_valid_call¸  s
    	t   NoninteractiveDisplayc           B   sŒ   e  Z d  Z d „  Z d d „ Z e e d „ Z d
 d
 d
 d
 d
 d „ Z	 d
 d
 d „ Z
 d
 d
 d
 d
 d „ Z d
 d
 d „ Z d
 d
 d	 „ Z RS(   sE   An iDisplay implementation that never asks for interactive user inputc         O   s    t  t |  ƒ j ƒ  | |  _ d  S(   N(   R)   R}   R*   R+   (   R.   R+   t   unused_argsRC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR*   Î  s    t    c         C   sV   d } | | 7} | r' | d | 7} n  | rC | d j  | ƒ 7} n  t j | ƒ ‚ d S(   sB   Error out in case of an attempt to interact in noninteractive modes<   Missing command line flag or config entry for this setting:
s   
s&   

(You can set this with the {0} flag)N(   R   R   t   MissingCommandlineFlag(   R.   R4   RB   t   extraR   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   _interaction_failÒ  s    
c         K   sQ   | r t  | ƒ } n  |  j j d j d t j d t d | ƒ ƒ |  j j ƒ  d S(   s  Displays a notification without waiting for user acceptance.

        :param str message: Message to display to stdout
        :param bool pause: The NoninteractiveDisplay waits for no keyboard
        :param bool wrap: Whether or not the application should wrap text

        s+   {line}{frame}{line}{msg}{line}{frame}{line}R   R/   R   N(   R   R+   R   R   R   R   R0   R   (   R.   R4   R5   R6   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR7   Ü  s    			c   	      K   s6   | d k r, |  j | | d t | ƒ ƒ n  t | f S(   s_  Avoid displaying a menu.

        :param str message: title of menu
        :param choices: Menu lines, len must be > 0
        :type choices: list of tuples (tag, item) or
            list of descriptions (tags will be enumerated)
        :param int default: the default choice
        :param dict kwargs: absorbs various irrelevant labelling arguments

        :returns: tuple of (`code`, `index`) where
            `code` - str display exit code
            `index` - int index of the user's selection
        :rtype: tuple
        :raises errors.MissingCommandlineFlag: if there was no default

        s	   Choices: N(   Rd   R‚   t   reprR9   (	   R.   R4   R=   R>   R?   R@   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRF   ì  s     c         K   s-   | d k r |  j | | ƒ n
 t | f Sd S(   sK  Accept input from the user.

        :param str message: message to display to the user

        :returns: tuple of (`code`, `input`) where
            `code` - str display exit code
            `input` - str of the user's input
        :rtype: tuple
        :raises errors.MissingCommandlineFlag: if there was no default

        N(   Rd   R‚   R9   (   R.   R4   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRL     s    c         K   s'   | d k r |  j | | ƒ n | Sd S(   s+  Decide Yes or No, without asking anybody

        :param str message: question for the user
        :param dict kwargs: absorbs yes_label, no_label

        :raises errors.MissingCommandlineFlag: if there was no default
        :returns: True for "Yes", False for "No"
        :rtype: bool

        N(   Rd   R‚   (   R.   R4   RV   RW   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRX     s    c         K   s9   | d k r+ |  j | | d j | ƒ ƒ n
 t | f Sd S(   sj  Display a checklist.

        :param str message: Message to display to user
        :param list tags: `str` tags to select, len(tags) > 0
        :param dict kwargs: absorbs default_status arg

        :returns: tuple of (`code`, `tags`) where
            `code` - str display exit code
            `tags` - list of selected tags
        :rtype: tuple

        s   ? N(   Rd   R‚   R   R9   (   R.   R4   R`   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRc   '  s    c         K   s   |  j  | | | ƒ S(   s“  Simulate prompting the user for a directory.

        This function returns default if it is not ``None``, otherwise,
        an exception is raised explaining the problem. If cli_flag is
        not ``None``, the error message will include the flag that can
        be used to set this value with the CLI.

        :param str message: prompt to give the user
        :param default: default value to return (if one exists)
        :param str cli_flag: option used to set this value with the CLI

        :returns: tuple of the form (`code`, `string`) where
            `code` - int display exit code
            `string` - input entered by the user

        (   RL   (   R.   R4   RA   RB   RC   (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRi   :  s    N(   Rx   Ry   Rz   R*   R‚   R   RQ   R7   Rd   RF   RL   RX   Rc   Ri   (    (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR}   Ê  s   	
c         C   s5   |  j  d d ƒ } g  | j ƒ  D] } t | ƒ ^ q S(   sƒ   Separate a comma or space separated list.

    :param str input_: input from the user

    :returns: strings
    :rtype: list

    t   ,RG   (   t   replacet   splitRY   (   t   input_t	   no_commast   string(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyR^   O  s    	c         C   s   d j  d |  d d |  d ƒ S(   sn   Place parens around first character of label.

    :param str label: Must contain at least one character

    s   ({first}){rest}t   firsti    t   resti   (   R   (   t   label(    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyRR   ^  s    (#   Rz   t   loggingR   R   R   R   t   zope.interfacet   zopet   certbotR    R   R   t   certbot.displayR   t	   getLoggerRx   R2   t   WIDTHR9   RJ   t   HELPt   ESCR0   R   Rd   R'   t	   interfacet   implementert   IDisplayt   objectR(   R|   R}   R^   RR   (    (    (    s8   /usr/lib/python2.7/dist-packages/certbot/display/util.pyt   <module>   s6   	#ÿ Z	„	